From a8c543753ae4f2e9b39a9ece4f023d54f95c6588 Mon Sep 17 00:00:00 2001 From: Ben Word Date: Wed, 1 May 2013 15:51:59 -0500 Subject: [PATCH] Fix clean URL issues with non-standard setups --- CHANGELOG.md | 1 + lib/cleanup.php | 31 +------------------------------ lib/init.php | 6 ++---- lib/rewrites.php | 4 ++-- lib/utils.php | 23 ----------------------- 5 files changed, 6 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8977ad..c945c12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ### HEAD +* Fix issues with root relative URLs and rewrites on non-standard setups * Make sure rewrites are added to .htaccess immediately after activation * Move HTML5 Boilerplate's .htaccess to a [plugin](https://github.com/retlehs/wp-h5bp-htaccess) * Rename page-custom.php to template-custom.php diff --git a/lib/cleanup.php b/lib/cleanup.php index 069b896..1b0d369 100644 --- a/lib/cleanup.php +++ b/lib/cleanup.php @@ -138,32 +138,7 @@ add_filter('body_class', 'roots_body_class'); * @author Scott Walkinshaw */ function roots_root_relative_url($input) { - // Fix for site_url() != home_url() - if (!is_admin() && site_url() != home_url() && stristr($input, 'wp-includes') === false) { - $input = str_replace(site_url(), '', $input); - } - - $output = preg_replace_callback( - '!(https?://[^/|"]+)([^"]+)?!', - create_function( - '$matches', - // If full URL is home_url("/") and this isn't a subdir install, return a slash for relative root - 'if (isset($matches[0]) && $matches[0] === home_url("/") && str_replace("http://", "", home_url("/", "http"))==$_SERVER["HTTP_HOST"]) { return "/";' . - // If domain is equal to home_url("/"), then make URL relative - '} elseif (isset($matches[0]) && strpos($matches[0], home_url("/")) !== false) { return $matches[2];' . - // If domain is not equal to home_url("/"), do not make external link relative - '} else { return $matches[0]; };' - ), - $input - ); - - // detect and correct for subdir installs - if ($subdir = parse_url(home_url(), PHP_URL_PATH)) { - if (substr($output, 0, strlen($subdir)) == (substr($output, strlen($subdir), strlen($subdir)))) { - $output = substr($output, strlen($subdir)); - } - } - + $output = wp_make_link_relative($input); return $output; } @@ -174,10 +149,6 @@ function roots_enable_root_relative_urls() { if (roots_enable_root_relative_urls()) { $root_rel_filters = array( 'bloginfo_url', - 'theme_root_uri', - 'stylesheet_directory_uri', - 'template_directory_uri', - 'plugins_url', 'the_permalink', 'wp_list_pages', 'wp_list_categories', diff --git a/lib/init.php b/lib/init.php index 9fa9954..7084ac9 100644 --- a/lib/init.php +++ b/lib/init.php @@ -30,9 +30,7 @@ if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); } // Define helper constants $get_theme_name = explode('/themes/', get_template_directory()); -define('WP_BASE', wp_base_dir()); define('THEME_NAME', next($get_theme_name)); -define('RELATIVE_PLUGIN_PATH', str_replace(site_url() . '/', '', plugins_url())); -define('FULL_RELATIVE_PLUGIN_PATH', WP_BASE . '/' . RELATIVE_PLUGIN_PATH); -define('RELATIVE_CONTENT_PATH', str_replace(site_url() . '/', '', content_url())); +define('RELATIVE_PLUGIN_PATH', str_replace(home_url() . '/', '', plugins_url())); +define('RELATIVE_CONTENT_PATH', str_replace(home_url() . '/', '', content_url())); define('THEME_PATH', RELATIVE_CONTENT_PATH . '/themes/' . THEME_NAME); diff --git a/lib/rewrites.php b/lib/rewrites.php index f13a2b0..add141b 100644 --- a/lib/rewrites.php +++ b/lib/rewrites.php @@ -28,8 +28,8 @@ function roots_add_rewrites($content) { } function roots_clean_urls($content) { - if (strpos($content, FULL_RELATIVE_PLUGIN_PATH) === 0) { - return str_replace(FULL_RELATIVE_PLUGIN_PATH, WP_BASE . '/plugins', $content); + if (strpos($content, RELATIVE_PLUGIN_PATH) > 0) { + return str_replace('/' . RELATIVE_PLUGIN_PATH, '/plugins', $content); } else { return str_replace('/' . THEME_PATH, '', $content); } diff --git a/lib/utils.php b/lib/utils.php index 395c93c..9a86123 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -85,29 +85,6 @@ function roots_title() { } } -/** - * Return WordPress subdirectory if applicable - */ -function wp_base_dir() { - preg_match('!(https?://[^/|"]+)([^"]+)?!', site_url(), $matches); - if (count($matches) === 3) { - return end($matches); - } else { - return ''; - } -} - -/** - * Opposite of built in WP functions for trailing slashes - */ -function leadingslashit($string) { - return '/' . unleadingslashit($string); -} - -function unleadingslashit($string) { - return ltrim($string, '/'); -} - function add_filters($tags, $function) { foreach($tags as $tag) { add_filter($tag, $function);