diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5657f6ea7d574e423dc2c297e2e19a9dbd7a7170 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index ae37baf1e419a9f8505e2c0409a7196e257a0ecc..0000000000000000000000000000000000000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "voce-settings-api"] - path = voce-settings-api - url = git@github.com:voceconnect/voce-settings-api.git diff --git a/README.md b/README.md index ce1b758887b68e5e6ea8f781063a2c1190b8f340..64f0fe243197e870757a5f3e76b816a36913d1b5 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ CDN Rewrite =========== -Contributors: voceplatforms +Contributors: voceplatforms, chrisscott, prettyboymp Tags: cdn, rewrite Requires at least: 3.3 -Tested up to: 3.5 +Tested up to: 3.7.1 Stable tag: 0.1.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html ## Description -This plugin allows you to rewrite the root url of assets, css, and JavaScript files. This allows you to load these resources from an external URL improving the page load time by taking advantage of parallel browser requests. +This plugin allows you to rewrite the root url of assets, css, and js files. This allows you to load these resources from an external URL improving the page load time by taking advantage of parallel browser requests. ## Installation diff --git a/cdn-rewrite.php b/cdn-rewrite.php index c14cf529ca27fd059affe8ba9669b3c376c70f6a..1e682e6b182a4bc63cbd45d15a856d90646c3fb1 100644 --- a/cdn-rewrite.php +++ b/cdn-rewrite.php @@ -8,216 +8,219 @@ Author: Chris Scott, Michael Pretty Author URI: http://voceconnect.com/ */ -require_once('voce-settings-api/voce-settings-api.php'); +if(file_exists( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php' ) ) + include_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php' ); -class CDN_Rewrite { +if( !class_exists( 'CDN_Rewrite' ) ){ - const OPTION_GENERAL = 'cdn_general'; + class CDN_Rewrite { - private $cdn_root_url; - private $file_extensions; + const OPTION_GENERAL = 'cdn_general'; - private $css_file_extensions; - private $css_cdn_root_url; + private $cdn_root_url; + private $file_extensions; - private $js_file_extensions; - private $js_cdn_root_url; + private $css_file_extensions; + private $css_cdn_root_url; - private $blog_details; + private $js_file_extensions; + private $js_cdn_root_url; - public function __construct() { - $this->cdn_root_url = $this->css_cdn_root_url = $this->js_cdn_root_url = untrailingslashit($this->get_setting('root_url')); + private $blog_details; - if ($css_url = trim($this->get_setting('css_root_url'))) { - $this->css_cdn_root_url = untrailingslashit($css_url); - } + public function __construct() { + $this->cdn_root_url = $this->css_cdn_root_url = $this->js_cdn_root_url = untrailingslashit($this->get_setting('root_url')); - if ($js_url = trim($this->get_setting('js_root_url'))) { - $this->js_cdn_root_url = untrailingslashit($js_url); - } + if ($css_url = trim($this->get_setting('css_root_url'))) { + $this->css_cdn_root_url = untrailingslashit($css_url); + } - $this->file_extensions = $this->get_setting('file_extensions'); - $this->css_file_extensions = $this->get_setting('css_file_extensions'); - $this->js_file_extensions = $this->get_setting('js_file_extensions'); - } + if ($js_url = trim($this->get_setting('js_root_url'))) { + $this->js_cdn_root_url = untrailingslashit($js_url); + } - public function initialize() { - if (!class_exists('Voce_Settings_API')) { - return; + $this->file_extensions = $this->get_setting('file_extensions'); + $this->css_file_extensions = $this->get_setting('css_file_extensions'); + $this->js_file_extensions = $this->get_setting('js_file_extensions'); } - $this->add_options_page(); - if ('' == $this->file_extensions || '' == $this->cdn_root_url) { - add_action('admin_notices', array($this, 'settings_warning')); - return; - } + public function initialize() { + if( !class_exists( 'Voce_Settings_API' ) ) + _doing_it_wrong( __CLASS__, 'The Voce Settings API plugin must be active for the CDN Rewrite plugin to work' ); - if('/' != $this->cdn_root_url) { - $action = (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) ? 'xmlrpc_call' : 'template_redirect'; - add_action($action, array($this, 'start_buffer'), 1); - } - } + $this->add_options_page(); + if ('' == $this->file_extensions || '' == $this->cdn_root_url) { + add_action('admin_notices', array($this, 'settings_warning')); + return; + } - /** - * get general setting - * - * @param string $setting setting name - * @return mixed setting value or false if not set - */ - private function get_setting($setting) { - $settings = get_option(self::OPTION_GENERAL); - if(!$settings || !is_array($settings)) { - $settings = array( - 'file_extensions' => 'bmp|bz2|gif|ico|gz|jpg|jpeg|mp3|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|zip', - 'css_file_extensions' => 'css', - 'js_file_extensions' => 'js' - ); - update_option(self::OPTION_GENERAL, $settings); + if('/' != $this->cdn_root_url) { + $action = (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) ? 'xmlrpc_call' : 'template_redirect'; + add_action($action, array($this, 'start_buffer'), 1); + } } - return (isset($settings[$setting])) ? $settings[$setting] : false; - } - public function settings_warning() { - echo "<div class='update-nag'>The CDN Rewrite plugin is missing some required settings.</div>"; - } + /** + * get general setting + * + * @param string $setting setting name + * @return mixed setting value or false if not set + */ + private function get_setting($setting) { + $settings = get_option(self::OPTION_GENERAL); + if(!$settings || !is_array($settings)) { + $settings = array( + 'file_extensions' => 'bmp|bz2|gif|ico|gz|jpg|jpeg|mp3|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|zip', + 'css_file_extensions' => 'css', + 'js_file_extensions' => 'js' + ); + update_option(self::OPTION_GENERAL, $settings); + } + return (isset($settings[$setting])) ? $settings[$setting] : false; + } - /** - * adds the options page - * - * @return void - */ - public function add_options_page() { - Voce_Settings_API::GetInstance()->add_page('CDN Rewrite', 'CDN Rewrite', self::OPTION_GENERAL, 'manage_options', '', 'options-general.php' ) - ->add_group( 'CDN Rewrite Settings', 'cdn_general' ) - ->add_setting( 'CDN Root URL (required)', 'root_url', array( 'description' => 'The base URL of the CDN.' ) )->group - ->add_setting( 'File Extensions (required)', 'file_extensions' )->group - ->add_setting( 'CDN Root URL for CSS Files (optional)', 'css_root_url', array( 'description' => 'The base URL of the CDN for CSS Files.' ) )->group - ->add_setting( 'File Extensions for CSS Files (optional)', 'css_file_extensions' )->group - ->add_setting( 'CDN Root URL for JS Files (optional, defaults to Root URL)', 'js_root_url', array( 'description' => 'The base URL of the CDN for JS Files.' ) )->group - ->add_setting( 'File Extensions for JS Files (optional, defaults to Root URL)', 'js_file_extensions' ); - } + public function settings_warning() { + echo "<div class='update-nag'>The CDN Rewrite plugin is missing some required settings.</div>"; + } - /** - * start output buffering. - * - */ - public function start_buffer() { - ob_start(array($this, 'filter_urls')); - } + /** + * adds the options page + * + * @return void + */ + public function add_options_page() { + Voce_Settings_API::GetInstance()->add_page('CDN Rewrite', 'CDN Rewrite', self::OPTION_GENERAL, 'manage_options', '', 'options-general.php' ) + ->add_group( 'CDN Rewrite Settings', 'cdn_general' ) + ->add_setting( 'CDN Root URL (required)', 'root_url', array( 'description' => 'The base URL of the CDN.' ) )->group + ->add_setting( 'File Extensions (required)', 'file_extensions' )->group + ->add_setting( 'CDN Root URL for CSS Files (optional)', 'css_root_url', array( 'description' => 'The base URL of the CDN for CSS Files.' ) )->group + ->add_setting( 'File Extensions for CSS Files (optional)', 'css_file_extensions' )->group + ->add_setting( 'CDN Root URL for JS Files (optional, defaults to Root URL)', 'js_root_url', array( 'description' => 'The base URL of the CDN for JS Files.' ) )->group + ->add_setting( 'File Extensions for JS Files (optional, defaults to Root URL)', 'js_file_extensions' ); + } - /** - * Callback for output buffering. Search content for urls to replace - * - * @param string $content - * @return string - */ - public function filter_urls($content) { - - $root_url = $this->get_site_root_url(); - $xml_begin = $xml_end = ''; - if (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) { - $xml_begin = '>'; - $xml_end = '<'; + /** + * start output buffering. + * + */ + public function start_buffer() { + ob_start(array($this, 'filter_urls')); } - $extensions = join('|', array_filter(array($this->file_extensions, $this->css_file_extensions, $this->js_file_extensions))); - $regex = '#(?<=[(\"\''.$xml_begin.'])'.quotemeta($root_url).'(?:(/[^\"\''.$xml_end.')]+\.('.$extensions.')))#'; - $content = preg_replace_callback($regex, array($this, 'url_rewrite'), $content); - return $content; - } + /** + * Callback for output buffering. Search content for urls to replace + * + * @param string $content + * @return string + */ + public function filter_urls($content) { + + $root_url = $this->get_site_root_url(); + $xml_begin = $xml_end = ''; + if (defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) { + $xml_begin = '>'; + $xml_end = '<'; + } + $extensions = join('|', array_filter(array($this->file_extensions, $this->css_file_extensions, $this->js_file_extensions))); + $regex = '#(?<=[(\"\''.$xml_begin.'])'.quotemeta($root_url).'(?:(/[^\"\''.$xml_end.')]+\.('.$extensions.')))#'; + $content = preg_replace_callback($regex, array($this, 'url_rewrite'), $content); - /** - * Returns the root url of the current site - * - * @return string - */ - public function get_site_root_url() { - if(is_multisite() && !is_subdomain_install()) { - $root_blog = get_blog_details(1); - $root_url = $root_blog->siteurl; - } else { - $root_url = site_url(); + return $content; } - return $root_url; - } - /** - * Returns the details for the current blog - * - * @return object - */ - public function get_this_blog_details() { - if(!isset($this->blog_details)) { - global $blog_id; - $this->blog_details = get_blog_details($blog_id); + /** + * Returns the root url of the current site + * + * @return string + */ + public function get_site_root_url() { + if(is_multisite() && !is_subdomain_install()) { + $root_blog = get_blog_details(1); + $root_url = $root_blog->siteurl; + } else { + $root_url = site_url(); + } + return $root_url; } - return $this->blog_details; - } - /** - * Callback for url preg_replace_callback. Returns corrected URL - * - * @param array $match - * @return string - */ - public function url_rewrite($match) { - global $blog_id; - $path = $match[1]; - //if is subfolder install and isn't root blog and path starts with site_url and isnt uploads dir - if(is_multisite() && !is_subdomain_install() && $blog_id !== 1) { - $bloginfo = $this->get_this_blog_details(); - if((0 === strpos($path, $bloginfo->path)) && (0 !== strpos($path, $bloginfo->path.'files/'))) { - $path = '/'.substr($path, strlen($bloginfo->path)); + /** + * Returns the details for the current blog + * + * @return object + */ + public function get_this_blog_details() { + if(!isset($this->blog_details)) { + global $blog_id; + $this->blog_details = get_blog_details($blog_id); } + return $this->blog_details; } - if('/' !== $this->css_cdn_root_url && preg_match("/^.*\.(".$this->css_file_extensions.")$/i", $path) ) { - return $this->css_cdn_root_url . $path; - } - if('/' !== $this->js_cdn_root_url && preg_match("/^.*\.(".$this->js_file_extensions.")$/i", $path) ) { - return $this->js_cdn_root_url . $path; + + /** + * Callback for url preg_replace_callback. Returns corrected URL + * + * @param array $match + * @return string + */ + public function url_rewrite($match) { + global $blog_id; + $path = $match[1]; + //if is subfolder install and isn't root blog and path starts with site_url and isnt uploads dir + if(is_multisite() && !is_subdomain_install() && $blog_id !== 1) { + $bloginfo = $this->get_this_blog_details(); + if((0 === strpos($path, $bloginfo->path)) && (0 !== strpos($path, $bloginfo->path.'files/'))) { + $path = '/'.substr($path, strlen($bloginfo->path)); + } + } + if('/' !== $this->css_cdn_root_url && preg_match("/^.*\.(".$this->css_file_extensions.")$/i", $path) ) { + return $this->css_cdn_root_url . $path; + } + if('/' !== $this->js_cdn_root_url && preg_match("/^.*\.(".$this->js_file_extensions.")$/i", $path) ) { + return $this->js_cdn_root_url . $path; + } + return $this->cdn_root_url . $path; } - return $this->cdn_root_url . $path; - } -} -add_action('init', array(new CDN_Rewrite(), 'initialize')); + } + add_action('init', array(new CDN_Rewrite(), 'initialize')); -class CDN_VersionAssets { + class CDN_VersionAssets { - private $default_version = ''; - private $root_url; + private $default_version = ''; + private $root_url; - public function __construct() { - $this->root_url = site_url(); - } + public function __construct() { + $this->root_url = site_url(); + } - public function initialize() { - add_filter('style_loader_src', array($this, 'replace_version'), 10); - add_filter('script_loader_src', array($this, 'replace_version'), 10); - add_filter('stylesheet_uri', array($this, 'replace_version'), 10); - } + public function initialize() { + add_filter('style_loader_src', array($this, 'replace_version'), 10); + add_filter('script_loader_src', array($this, 'replace_version'), 10); + add_filter('stylesheet_uri', array($this, 'replace_version'), 10); + } - public function on_template_redirect() { - $this->default_version = @filemtime(get_stylesheet_directory().'/style.css'); - } + public function on_template_redirect() { + $this->default_version = @filemtime(get_stylesheet_directory().'/style.css'); + } - private function get_version($url) { - if(0 === strpos($url, $this->root_url)) { - $parts = parse_url($url); - $file_path = str_replace(site_url('/'), ABSPATH, $parts['scheme'].'://'.$parts['host'].$parts['path']); - if( !($version = @filemtime($file_path)) ) { - $version = $this->default_version; + private function get_version($url) { + if(0 === strpos($url, $this->root_url)) { + $parts = parse_url($url); + $file_path = str_replace(site_url('/'), ABSPATH, $parts['scheme'].'://'.$parts['host'].$parts['path']); + if( !($version = @filemtime($file_path)) ) { + $version = $this->default_version; + } + return $version; } - return $version; + return false; } - return false; - } - public function replace_version($src) { - if( $new_version = $this->get_version($src) ) { - return add_query_arg('ver', $new_version, $src); + public function replace_version($src) { + if( $new_version = $this->get_version($src) ) { + return add_query_arg('ver', $new_version, $src); + } + return $src; } - return $src; } -} -add_action('init', array(new CDN_VersionAssets(), 'initialize')); \ No newline at end of file + add_action('init', array(new CDN_VersionAssets(), 'initialize')); +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000000000000000000000000000000000000..11a063456582b2dd9b5c63890f7e0438a99b3036 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "voceconnect/cdn-rewrite", + "description": "This plugin allows you to rewrite the root url of assets, css, and js files. This allows you to load these resources from an external URL improving the page load time by taking advantage of parallel browser requests.", + "license": "GPLv2+", + "authors": [ + { + "name": "Michael Pretty", + "email": "mpretty@voceconnect.com" + }, + { + "name": "Chris Scott", + "email": "cscott@voceconnect.com" + } + ], + "require": { + "voceconnect/voce-settings-api": "~0.4" + }, + "autoload": { + "files": ["cdn-rewrite.php"] + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000000000000000000000000000000000000..0525905450be55b25cb4d485b0588b84f5c4d542 --- /dev/null +++ b/composer.lock @@ -0,0 +1,58 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" + ], + "hash": "7c1bd0d8afe567da719dcc87e85b56f3", + "packages": [ + { + "name": "voceconnect/voce-settings-api", + "version": "v0.4.2", + "source": { + "type": "git", + "url": "https://github.com/voceconnect/voce-settings-api.git", + "reference": "9c8066b1a26eb59a36a0ce8da0d4d0b2c581811f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/voceconnect/voce-settings-api/zipball/9c8066b1a26eb59a36a0ce8da0d4d0b2c581811f", + "reference": "9c8066b1a26eb59a36a0ce8da0d4d0b2c581811f", + "shasum": "" + }, + "type": "library", + "autoload": { + "files": [ + "voce-settings-api.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPLv2+" + ], + "authors": [ + { + "name": "Michael Pretty", + "email": "mpretty@voceconnect.com" + } + ], + "description": "A simplification of the core WordPress settings API", + "time": "2013-11-27 17:19:08" + } + ], + "packages-dev": [ + + ], + "aliases": [ + + ], + "minimum-stability": "stable", + "stability-flags": [ + + ], + "platform": [ + + ], + "platform-dev": [ + + ] +} diff --git a/readme.txt b/readme.txt index 090093ef74ef09cc12c2435e3df4054766283ac6..9626775b5f03a525d7aaeda6bca0749027ad2fe3 100644 --- a/readme.txt +++ b/readme.txt @@ -1,20 +1,20 @@ === CDN Rewrite === -Contributors: voceplatforms +Contributors: voceplatforms, chrisscott, prettyboymp Tags: cdn, rewrite Requires at least: 3.3 -Tested up to: 3.5 +Tested up to: 3.7.1 Stable tag: 0.1.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html -Rewrite the root url of assets, css, and JavaScript files. +Rewrite the root url of assets, css, and js files. == Description == -This plugin allows you to rewrite the root url of assets, css, and JavaScript files. This allows you to load these resources from an external URL improving the page load time by taking advantage of parallel browser requests. +This plugin allows you to rewrite the root url of assets, css, and js files. This allows you to load these resources from an external URL improving the page load time by taking advantage of parallel browser requests. == Installation == 1. Upload `cdn-rewrite` to the `/wp-content/plugins/` directory -1. Activate the plugin through the 'Plugins' menu in WordPress -1. Once the plugin has been activated, navigate to the CDN Rewrite settings page and set the appropriate settings. \ No newline at end of file +2. Activate the plugin through the 'Plugins' menu in WordPress +3. Once the plugin has been activated, navigate to the CDN Rewrite settings page and set the appropriate settings. \ No newline at end of file diff --git a/voce-settings-api b/voce-settings-api deleted file mode 160000 index 4ef6990f9cbe89ac10be1d17678fbadb3c045bd1..0000000000000000000000000000000000000000 --- a/voce-settings-api +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4ef6990f9cbe89ac10be1d17678fbadb3c045bd1