diff --git a/functions.php b/functions.php index 6a57839..f810a82 100644 --- a/functions.php +++ b/functions.php @@ -3,6 +3,15 @@ if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); } 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-cleanup.php'); // cleanup require_once locate_template('/inc/roots-scripts.php'); // modified scripts output @@ -66,4 +75,4 @@ function roots_entry_meta() { echo '
'; } -?> \ No newline at end of file +?> diff --git a/inc/roots-actions.php b/inc/roots-actions.php index aeb8a2e..9fd7643 100644 --- a/inc/roots-actions.php +++ b/inc/roots-actions.php @@ -1,7 +1,5 @@ ' . ($newline ? "\n" : ""); } -add_action('roots_footer', 'roots_google_analytics'); +add_action('roots_stylesheets', 'roots_get_stylesheets'); function roots_google_analytics() { $roots_google_analytics_id = GOOGLE_ANALYTICS_ID; @@ -39,4 +37,6 @@ function roots_google_analytics() { } } -?> \ No newline at end of file +add_action('roots_footer', 'roots_google_analytics'); + +?> diff --git a/inc/roots-cleanup.php b/inc/roots-cleanup.php index 91cd4f7..b911ce9 100644 --- a/inc/roots-cleanup.php +++ b/inc/roots-cleanup.php @@ -52,40 +52,31 @@ function roots_root_relative_url($input) { 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'))) { - add_filter('bloginfo_url', 'roots_root_relative_url'); - add_filter('theme_root_uri', 'roots_root_relative_url'); - add_filter('stylesheet_directory_uri', 'roots_root_relative_url'); - add_filter('template_directory_uri', 'roots_root_relative_url'); - add_filter('script_loader_src', 'roots_fix_duplicate_subfolder_urls'); - add_filter('style_loader_src', 'roots_fix_duplicate_subfolder_urls'); - add_filter('plugins_url', 'roots_root_relative_url'); - add_filter('the_permalink', 'roots_root_relative_url'); - add_filter('wp_list_pages', 'roots_root_relative_url'); - add_filter('wp_list_categories', 'roots_root_relative_url'); - add_filter('wp_nav_menu', 'roots_root_relative_url'); - add_filter('the_content_more_link', 'roots_root_relative_url'); - add_filter('the_tags', 'roots_root_relative_url'); - add_filter('get_pagenum_link', 'roots_root_relative_url'); - add_filter('get_comment_link', 'roots_root_relative_url'); - add_filter('month_link', 'roots_root_relative_url'); - add_filter('day_link', 'roots_root_relative_url'); - add_filter('year_link', 'roots_root_relative_url'); - add_filter('tag_link', 'roots_root_relative_url'); - add_filter('the_author_posts_link', 'roots_root_relative_url'); + $tags = array( + 'bloginfo_url', + 'theme_root_uri', + 'stylesheet_directory_uri', + 'template_directory_uri', + 'script_loader_src', + 'style_loader_src', + 'plugins_url', + 'the_permalink', + 'wp_list_pages', + 'wp_list_categories', + 'wp_nav_menu', + 'the_content_more_link', + 'the_tags', + 'get_pagenum_link', + 'get_comment_link', + 'month_link', + 'day_link', + 'year_link', + 'tag_link', + 'the_author_posts_link' + ); + + add_filters($tags, 'roots_root_relative_url'); } // remove root relative URLs on any attachments in the feed @@ -667,4 +658,4 @@ if (class_exists('RGForms')) { } -?> \ No newline at end of file +?> diff --git a/inc/roots-htaccess.php b/inc/roots-htaccess.php index 7bb8ce3..a54e67d 100644 --- a/inc/roots-htaccess.php +++ b/inc/roots-htaccess.php @@ -23,30 +23,22 @@ if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) { function roots_add_rewrites($content) { global $wp_rewrite; - $theme_name = next(explode('/themes/', get_stylesheet_directory())); $roots_new_non_wp_rules = array( - 'css/(.*)' => 'wp-content/themes/'. $theme_name . '/css/$1', - 'js/(.*)' => 'wp-content/themes/'. $theme_name . '/js/$1', - 'img/(.*)' => 'wp-content/themes/'. $theme_name . '/img/$1', - 'plugins/(.*)' => 'wp-content/plugins/$1' + 'css/(.*)' => THEME_PATH . '/css/$1', + 'js/(.*)' => THEME_PATH . '/js/$1', + 'img/(.*)' => THEME_PATH . '/img/$1', + 'plugins/(.*)' => RELATIVE_PLUGIN_PATH . '/$1' ); $wp_rewrite->non_wp_rules = $roots_new_non_wp_rules; return $content; } - function roots_clean_assets($content) { - $theme_name = next(explode('/themes/', $content)); - $current_path = '/wp-content/themes/' . $theme_name; - $new_path = ''; - $content = str_replace($current_path, $new_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; + function roots_clean_urls($content) { + if (strpos($content, FULL_RELATIVE_PLUGIN_PATH) === 0) { + return str_replace(FULL_RELATIVE_PLUGIN_PATH, WP_BASE . '/plugins', $content); + } else { + return str_replace('/' . THEME_PATH, '', $content); + } } // 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_h5bp_htaccess'); if (!is_admin()) { - add_filter('plugins_url', 'roots_clean_plugins'); - add_filter('bloginfo', 'roots_clean_assets'); - add_filter('stylesheet_directory_uri', 'roots_clean_assets'); - add_filter('template_directory_uri', 'roots_clean_assets'); - add_filter('script_loader_src', 'roots_clean_plugins'); - add_filter('style_loader_src', 'roots_clean_plugins'); + $tags = array( + 'plugins_url', + 'bloginfo', + 'stylesheet_directory_uri', + 'template_directory_uri', + 'script_loader_src', + 'style_loader_src' + ); + + add_filters($tags, 'roots_clean_urls'); } } @@ -85,4 +81,4 @@ if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) { } -?> \ No newline at end of file +?> diff --git a/inc/roots-scripts.php b/inc/roots-scripts.php index 7c73013..1522b75 100644 --- a/inc/roots-scripts.php +++ b/inc/roots-scripts.php @@ -1,9 +1,8 @@ to_do as $key => $handle) { $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)) { $locales[] = $locale; @@ -55,4 +55,4 @@ function roots_print_scripts() { return $wp_scripts->done; } -?> \ No newline at end of file +?> diff --git a/inc/roots-utils.php b/inc/roots-utils.php new file mode 100644 index 0000000..f2e3e06 --- /dev/null +++ b/inc/roots-utils.php @@ -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); + } +} + +?>