Better rewrite/clean URL handling

- New roots-utils.php file for base functions
- Defined useful constants for dealing with common paths
- Refactored rewrites and clean URLs to be more flexible for non
  standard setups
- Custom scripts handler now deals with plugin scripts correctly
- Fixes #204, #270, #278
This commit is contained in:
Scott Walkinshaw
2012-02-20 16:13:35 -05:00
parent 47eb95be69
commit db09a64d94
6 changed files with 93 additions and 69 deletions

View File

@@ -3,6 +3,15 @@
if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); } if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); }
require_once locate_template('/inc/roots-config.php'); // config require_once locate_template('/inc/roots-config.php'); // config
require_once locate_template('/inc/roots-utils.php'); // utility functions
define('WP_BASE', wp_base_dir());
define('THEME_NAME', next(explode('/themes/', get_template_directory())));
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('THEME_PATH', RELATIVE_CONTENT_PATH . '/themes/' . THEME_NAME);
require_once locate_template('/inc/roots-activation.php'); // activation require_once locate_template('/inc/roots-activation.php'); // activation
require_once locate_template('/inc/roots-cleanup.php'); // cleanup require_once locate_template('/inc/roots-cleanup.php'); // cleanup
require_once locate_template('/inc/roots-scripts.php'); // modified scripts output require_once locate_template('/inc/roots-scripts.php'); // modified scripts output
@@ -66,4 +75,4 @@ function roots_entry_meta() {
echo '<p class="byline author vcard">'. __('Written by', 'roots') .' <a href="'. get_author_posts_url(get_the_author_meta('id')) .'" rel="author" class="fn">'. get_the_author() .'</a></p>'; echo '<p class="byline author vcard">'. __('Written by', 'roots') .' <a href="'. get_author_posts_url(get_the_author_meta('id')) .'" rel="author" class="fn">'. get_the_author() .'</a></p>';
} }
?> ?>

View File

@@ -1,7 +1,5 @@
<?php <?php
add_action('roots_stylesheets', 'roots_get_stylesheets');
function roots_get_stylesheets() { function roots_get_stylesheets() {
$styles = ''; $styles = '';
$styles .= stylesheet_link_tag('/style.css', 1); $styles .= stylesheet_link_tag('/style.css', 1);
@@ -25,7 +23,7 @@ function stylesheet_link_tag($file, $tabs = 0, $newline = true, $rel = 'styleshe
return $indent . '<link rel="' . $rel .'" href="' . get_template_directory_uri() . '/css' . $file . '">' . ($newline ? "\n" : ""); return $indent . '<link rel="' . $rel .'" href="' . get_template_directory_uri() . '/css' . $file . '">' . ($newline ? "\n" : "");
} }
add_action('roots_footer', 'roots_google_analytics'); add_action('roots_stylesheets', 'roots_get_stylesheets');
function roots_google_analytics() { function roots_google_analytics() {
$roots_google_analytics_id = GOOGLE_ANALYTICS_ID; $roots_google_analytics_id = GOOGLE_ANALYTICS_ID;
@@ -39,4 +37,6 @@ function roots_google_analytics() {
} }
} }
?> add_action('roots_footer', 'roots_google_analytics');
?>

View File

@@ -52,40 +52,31 @@ function roots_root_relative_url($input) {
return $output; return $output;
} }
// Terrible workaround to remove the duplicate subfolder in the src of JS/CSS tags
// Example: /subfolder/subfolder/css/style.css
function roots_fix_duplicate_subfolder_urls($input) {
$output = roots_root_relative_url($input);
preg_match_all('!([^/]+)/([^/]+)!', $output, $matches);
if (isset($matches[1]) && isset($matches[2])) {
if ($matches[1][0] === $matches[2][0]) {
$output = substr($output, strlen($matches[1][0]) + 1);
}
}
return $output;
}
if (!is_admin() && !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) { if (!is_admin() && !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
add_filter('bloginfo_url', 'roots_root_relative_url'); $tags = array(
add_filter('theme_root_uri', 'roots_root_relative_url'); 'bloginfo_url',
add_filter('stylesheet_directory_uri', 'roots_root_relative_url'); 'theme_root_uri',
add_filter('template_directory_uri', 'roots_root_relative_url'); 'stylesheet_directory_uri',
add_filter('script_loader_src', 'roots_fix_duplicate_subfolder_urls'); 'template_directory_uri',
add_filter('style_loader_src', 'roots_fix_duplicate_subfolder_urls'); 'script_loader_src',
add_filter('plugins_url', 'roots_root_relative_url'); 'style_loader_src',
add_filter('the_permalink', 'roots_root_relative_url'); 'plugins_url',
add_filter('wp_list_pages', 'roots_root_relative_url'); 'the_permalink',
add_filter('wp_list_categories', 'roots_root_relative_url'); 'wp_list_pages',
add_filter('wp_nav_menu', 'roots_root_relative_url'); 'wp_list_categories',
add_filter('the_content_more_link', 'roots_root_relative_url'); 'wp_nav_menu',
add_filter('the_tags', 'roots_root_relative_url'); 'the_content_more_link',
add_filter('get_pagenum_link', 'roots_root_relative_url'); 'the_tags',
add_filter('get_comment_link', 'roots_root_relative_url'); 'get_pagenum_link',
add_filter('month_link', 'roots_root_relative_url'); 'get_comment_link',
add_filter('day_link', 'roots_root_relative_url'); 'month_link',
add_filter('year_link', 'roots_root_relative_url'); 'day_link',
add_filter('tag_link', 'roots_root_relative_url'); 'year_link',
add_filter('the_author_posts_link', 'roots_root_relative_url'); 'tag_link',
'the_author_posts_link'
);
add_filters($tags, 'roots_root_relative_url');
} }
// remove root relative URLs on any attachments in the feed // remove root relative URLs on any attachments in the feed
@@ -667,4 +658,4 @@ if (class_exists('RGForms')) {
} }
?> ?>

View File

@@ -23,30 +23,22 @@ if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) {
function roots_add_rewrites($content) { function roots_add_rewrites($content) {
global $wp_rewrite; global $wp_rewrite;
$theme_name = next(explode('/themes/', get_stylesheet_directory()));
$roots_new_non_wp_rules = array( $roots_new_non_wp_rules = array(
'css/(.*)' => 'wp-content/themes/'. $theme_name . '/css/$1', 'css/(.*)' => THEME_PATH . '/css/$1',
'js/(.*)' => 'wp-content/themes/'. $theme_name . '/js/$1', 'js/(.*)' => THEME_PATH . '/js/$1',
'img/(.*)' => 'wp-content/themes/'. $theme_name . '/img/$1', 'img/(.*)' => THEME_PATH . '/img/$1',
'plugins/(.*)' => 'wp-content/plugins/$1' 'plugins/(.*)' => RELATIVE_PLUGIN_PATH . '/$1'
); );
$wp_rewrite->non_wp_rules = $roots_new_non_wp_rules; $wp_rewrite->non_wp_rules = $roots_new_non_wp_rules;
return $content; return $content;
} }
function roots_clean_assets($content) { function roots_clean_urls($content) {
$theme_name = next(explode('/themes/', $content)); if (strpos($content, FULL_RELATIVE_PLUGIN_PATH) === 0) {
$current_path = '/wp-content/themes/' . $theme_name; return str_replace(FULL_RELATIVE_PLUGIN_PATH, WP_BASE . '/plugins', $content);
$new_path = ''; } else {
$content = str_replace($current_path, $new_path, $content); return str_replace('/' . THEME_PATH, '', $content);
return $content; }
}
function roots_clean_plugins($content) {
$current_path = '/wp-content/plugins';
$new_path = '/plugins';
$content = str_replace($current_path, $new_path, $content);
return $content;
} }
// only use clean urls if the theme isn't a child or an MU (Network) install // only use clean urls if the theme isn't a child or an MU (Network) install
@@ -54,12 +46,16 @@ if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) {
add_action('generate_rewrite_rules', 'roots_add_rewrites'); add_action('generate_rewrite_rules', 'roots_add_rewrites');
add_action('generate_rewrite_rules', 'roots_add_h5bp_htaccess'); add_action('generate_rewrite_rules', 'roots_add_h5bp_htaccess');
if (!is_admin()) { if (!is_admin()) {
add_filter('plugins_url', 'roots_clean_plugins'); $tags = array(
add_filter('bloginfo', 'roots_clean_assets'); 'plugins_url',
add_filter('stylesheet_directory_uri', 'roots_clean_assets'); 'bloginfo',
add_filter('template_directory_uri', 'roots_clean_assets'); 'stylesheet_directory_uri',
add_filter('script_loader_src', 'roots_clean_plugins'); 'template_directory_uri',
add_filter('style_loader_src', 'roots_clean_plugins'); 'script_loader_src',
'style_loader_src'
);
add_filters($tags, 'roots_clean_urls');
} }
} }
@@ -85,4 +81,4 @@ if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) {
} }
?> ?>

View File

@@ -1,9 +1,8 @@
<?php <?php
function roots_scripts() { function roots_scripts() {
$template_uri = get_template_directory_uri(); wp_register_script('roots_plugins', THEME_PATH . '/js/plugins.js', false, null, false);
wp_register_script('roots_plugins', ''.$template_uri.'/js/plugins.js', false, null, false); wp_register_script('roots_script', THEME_PATH . '/js/script.js', false, null, false);
wp_register_script('roots_script', ''.$template_uri.'/js/script.js', false, null, false);
wp_enqueue_script('roots_plugins'); wp_enqueue_script('roots_plugins');
wp_enqueue_script('roots_script'); wp_enqueue_script('roots_script');
} }
@@ -25,7 +24,8 @@ function roots_print_scripts() {
foreach ($wp_scripts->to_do as $key => $handle) { foreach ($wp_scripts->to_do as $key => $handle) {
$skip_scripts = array('jquery', 'roots_script', 'roots_plugins'); $skip_scripts = array('jquery', 'roots_script', 'roots_plugins');
$src = $wp_scripts->registered[$handle]->src; $src = WP_BASE . leadingslashit($wp_scripts->registered[$handle]->src);
$src = apply_filters('script_loader_src', $src);
if ($locale = $wp_scripts->print_extra_script($handle, false)) { if ($locale = $wp_scripts->print_extra_script($handle, false)) {
$locales[] = $locale; $locales[] = $locale;
@@ -55,4 +55,4 @@ function roots_print_scripts() {
return $wp_scripts->done; return $wp_scripts->done;
} }
?> ?>

28
inc/roots-utils.php Normal file
View File

@@ -0,0 +1,28 @@
<?
// returns 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);
}
}
?>