Permalink
Browse files

make getPath() more intelligent (on different wp_content URL's + chec…

…k if file exists)
  • Loading branch information...
1 parent 53ffad5 commit 1386e4fe1d390d7c099cab29668bf2f75798fab6 @futtta committed Dec 18, 2016
Showing with 22 additions and 5 deletions.
  1. +18 −2 classes/autoptimizeBase.php
  2. +4 −3 classes/autoptimizeStyles.php
@@ -30,6 +30,7 @@ protected function getpath($url) {
}
$siteHost=parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST);
+ $contentHost=parse_url(AUTOPTIMIZE_WP_ROOT_URL,PHP_URL_HOST);
// normalize
if (strpos($url,'//')===0) {
@@ -46,6 +47,10 @@ protected function getpath($url) {
$url = AUTOPTIMIZE_WP_SITE_URL.str_repeat("/..",$subdir_levels).$url;
}
}
+
+ if ($siteHost !== $contentHost) {
+ $url=str_replace(AUTOPTIMIZE_WP_CONTENT_URL,AUTOPTIMIZE_WP_SITE_URL.AUTOPTIMIZE_WP_CONTENT_NAME,$url);
+ }
// first check; hostname wp site should be hostname of url
$thisHost=@parse_url($url,PHP_URL_HOST);
@@ -82,17 +87,28 @@ protected function getpath($url) {
// try to remove "wp root url" from url while not minding http<>https
$tmp_ao_root = preg_replace('/https?:/','',AUTOPTIMIZE_WP_ROOT_URL);
+ if ($siteHost !== $contentHost) {
+ // as we replaced the content-domain with the site-domain, we should match against that
+ $tmp_ao_root = preg_replace('/https?:/','',AUTOPTIMIZE_WP_SITE_URL);
+ }
$tmp_url = preg_replace('/https?:/','',$url);
$path = str_replace($tmp_ao_root,'',$tmp_url);
- // final check; if path starts with :// or //, this is not a URL in the WP context and we have to assume we can't aggregate
+ // if path starts with :// or //, this is not a URL in the WP context and we have to assume we can't aggregate
if (preg_match('#^:?//#',$path)) {
/** External script/css (adsense, etc) */
return false;
}
+ // prepend with WP_ROOT_DIR to have full path to file
$path = str_replace('//','/',WP_ROOT_DIR.$path);
- return $path;
+
+ // final check: does file exist and is it readable
+ if (file_exists($path) && is_readable($path)) {
+ return $path;
+ } else {
+ return false;
+ }
}
// needed for WPML-filter
@@ -576,8 +576,9 @@ public function getcontent() {
}
static function fixurls($file,$code) {
- $file = str_replace(WP_ROOT_DIR,'/',$file);
- $dir = dirname($file); //Like /wp-content
+ $file = str_replace(WP_ROOT_DIR,'',$file);
+ $file = str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',$file);
+ $dir = dirname($file); // Like /themes/expound/css
// quick fix for import-troubles in e.g. arras theme
$code=preg_replace('#@import ("|\')(.+?)\.css("|\')#','@import url("${2}.css")',$code);
@@ -599,7 +600,7 @@ static function fixurls($file,$code) {
continue;
} else {
// relative URL
- $newurl = preg_replace('/https?:/','',str_replace(" ","%20",AUTOPTIMIZE_WP_ROOT_URL.str_replace('//','/',$dir.'/'.$url)));
+ $newurl = preg_replace('/https?:/','',str_replace(" ","%20",AUTOPTIMIZE_WP_CONTENT_URL.str_replace('//','/',$dir.'/'.$url)));
$hash = md5($url);
$code = str_replace($matches[0][$k],$hash,$code);

0 comments on commit 1386e4f

Please sign in to comment.