From 35c13038fa34cfe2eb0471f2222bba06f43f8565 Mon Sep 17 00:00:00 2001 From: Joe Maller Date: Mon, 18 May 2015 12:56:03 -0400 Subject: [PATCH] Refactored root_relative_url() * Switched from `$_SERVER['SERVER_NAME']` to `WP_SITEURL` * Fixed `Undefined Index` warning in `wp-cli export` * Fixed bug where ports aren't correctly matched * Added handling of scheme-less urls * Simplified logic * Improved readability by spreading long conditional across several lines --- lib/utils.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/utils.php b/lib/utils.php index 57ef9157..0d905abe 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -10,10 +10,17 @@ function root_relative_url($input) { if (!isset($url['host']) || !isset($url['path'])) { return $input; } - if (is_multisite()) { - $network_url = parse_url(network_site_url(), PHP_URL_HOST); + $site_url = parse_url(network_site_url()); // falls back to site_url + + if (!isset($url['scheme'])) { + $url['scheme'] = $site_url['scheme']; } - if (($url['host'] === $_SERVER['SERVER_NAME']) || $url['host'] === $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] || (isset($network_url) && $url['host'] === $network_url)) { + $hosts_match = $site_url['host'] === $url['host']; + $schemes_match = $site_url['scheme'] === $url['scheme']; + $ports_exist = isset($site_url['port']) && isset($url['port']); + $ports_match = ($ports_exist) ? $site_url['port'] === $url['port'] : true; + + if ($hosts_match && $schemes_match && $ports_match) { return wp_make_link_relative($input); } return $input;