Skip to content
Snippets Groups Projects
Commit fe7a194e authored by Michael Pretty's avatar Michael Pretty
Browse files

adding handling for seperate js and css cdn roots

parent d4722347
No related branches found
No related tags found
No related merge requests found
...@@ -15,13 +15,27 @@ class CDN_Rewrite { ...@@ -15,13 +15,27 @@ class CDN_Rewrite {
const OPTION_GENERAL = 'cdn_general'; const OPTION_GENERAL = 'cdn_general';
private $submenu_general; private $submenu_general;
private $cdn_root_url;
private $file_extensions; private $file_extensions;
private $css_file_extensions;
private $css_cdn_root_url;
private $js_file_extensions;
private $js_cdn_root_url;
private $blog_details; private $blog_details;
private $cdn_root_url;
public function __construct() { public function __construct() {
$this->file_extensions = $this->get_setting('file_extensions');
$this->cdn_root_url = untrailingslashit($this->get_setting('root_url')); $this->cdn_root_url = untrailingslashit($this->get_setting('root_url'));
$this->file_extensions = $this->get_setting('file_extensions');
$this->css_cdn_root_url = untrailingslashit($this->get_setting('css_root_url'));
$this->css_file_extensions = $this->get_setting('css_file_extensions');
$this->js_cdn_root_url = untrailingslashit($this->get_setting('js_root_url'));
$this->js_file_extensions = $this->get_setting('js_file_extensions');
} }
public function initialize() { public function initialize() {
...@@ -50,7 +64,9 @@ class CDN_Rewrite { ...@@ -50,7 +64,9 @@ class CDN_Rewrite {
$settings = get_option(self::OPTION_GENERAL); $settings = get_option(self::OPTION_GENERAL);
if(!$settings || !is_array($settings)) { if(!$settings || !is_array($settings)) {
$settings = array( $settings = array(
'file_extensions' => 'bmp|bz2|css|gif|ico|gz|jpg|jpeg|js|mp3|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|zip' '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); update_option(self::OPTION_GENERAL, $settings);
} }
...@@ -73,6 +89,10 @@ class CDN_Rewrite { ...@@ -73,6 +89,10 @@ class CDN_Rewrite {
$section = $settings->add_section('api', 'CDN Rewrite Settings', $this->submenu_general); $section = $settings->add_section('api', 'CDN Rewrite Settings', $this->submenu_general);
$section->add_field('root_url', 'CDN Root URL (required)', 'field_input', array('description' => 'The base URL of the CDN.')); $section->add_field('root_url', 'CDN Root URL (required)', 'field_input', array('description' => 'The base URL of the CDN.'));
$section->add_field('file_extensions', 'File Extensions (required)', 'field_input'); $section->add_field('file_extensions', 'File Extensions (required)', 'field_input');
$section->add_field('css_root_url', 'CDN Root URL for CSS Files (optional)', 'field_input', array('description' => 'The base URL of the CDN for CSS Files.'));
$section->add_field('css_file_extensions', 'File Extensions for CSS Files (required)', 'field_input');
$section->add_field('js_root_url', 'CDN Root URL for JS Files (optional)', 'field_input', array('description' => 'The base URL of the CDN for JS Files.'));
$section->add_field('js_file_extensions', 'File Extensions for JS Files (optional)', 'field_input');
} }
/** /**
...@@ -111,7 +131,7 @@ class CDN_Rewrite { ...@@ -111,7 +131,7 @@ class CDN_Rewrite {
*/ */
public function filter_urls($content) { public function filter_urls($content) {
$root_url = $this->get_site_root_url(); $root_url = $this->get_site_root_url();
$regex = '#(?<=[(\"\'])'.quotemeta($root_url).'(?:(/[^\"\')]+\.('.$this->file_extensions.')))#'; $regex = '#(?<=[(\"\'])'.quotemeta($root_url).'(?:(/[^\"\')]+\.('.join('|', array($this->file_extensions,$this->css_file_extensions,$this->js_file_extensions)).')))#';
$content = preg_replace_callback($regex, array($this, 'url_rewrite'), $content); $content = preg_replace_callback($regex, array($this, 'url_rewrite'), $content);
...@@ -162,6 +182,12 @@ class CDN_Rewrite { ...@@ -162,6 +182,12 @@ class CDN_Rewrite {
$path = '/'.substr($path, strlen($bloginfo->path)); $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;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment