| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
abstract class autoptimizeBase { |
| 5 |
protected $content = ''; |
| 6 |
protected $tagWarning = false; |
| 7 |
|
| 8 |
public function __construct($content) { |
| 9 |
$this->content = $content; |
| 10 |
} |
| 11 |
|
| 12 |
//Reads the page and collects tags |
| 13 |
abstract public function read($justhead); |
| 14 |
|
| 15 |
//Joins and optimizes collected things |
| 16 |
abstract public function minify(); |
| 17 |
|
| 18 |
//Caches the things |
| 19 |
abstract public function cache(); |
| 20 |
|
| 21 |
//Returns the content |
| 22 |
abstract public function getcontent(); |
| 23 |
|
| 24 |
//Converts an URL to a full path |
| 25 |
protected function getpath($url) { |
| 26 |
$url=apply_filters( 'autoptimize_filter_cssjs_alter_url', $url); |
| 27 |
|
| 28 |
if (strpos($url,'%')!==false) { |
| 29 |
$url=urldecode($url); |
| 30 |
} |
| 31 |
|
| 32 |
$siteHost=parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST); |
| 33 |
|
| 34 |
// normalize |
| 35 |
if (strpos($url,'//')===0) { |
| 36 |
if (is_ssl()) { |
| 37 |
$url = "https:".$url; |
| 38 |
} else { |
| 39 |
$url = "http:".$url; |
| 40 |
} |
| 41 |
} else if ((strpos($url,'//')===false) && (strpos($url,$siteHost)===false)) { |
| 42 |
if (AUTOPTIMIZE_WP_SITE_URL === $siteHost) { |
| 43 |
$url = AUTOPTIMIZE_WP_SITE_URL.$url; |
| 44 |
} else { |
| 45 |
$subdir_levels=substr_count(preg_replace("/https?:\/\//","",AUTOPTIMIZE_WP_SITE_URL),"/"); |
| 46 |
$url = AUTOPTIMIZE_WP_SITE_URL.str_repeat("/..",$subdir_levels).$url; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// first check; hostname wp site should be hostname of url |
| 51 |
$thisHost=@parse_url($url,PHP_URL_HOST); |
| 52 |
if ($thisHost !== $siteHost) { |
| 53 |
/* |
| 54 |
* first try to get all domains from WPML (if available) |
| 55 |
* then explicitely declare $this->cdn_url as OK as well |
| 56 |
* then apply own filter autoptimize_filter_cssjs_multidomain takes an array of hostnames |
| 57 |
* each item in that array will be considered part of the same WP multisite installation |
| 58 |
*/ |
| 59 |
$multidomains = array(); |
| 60 |
|
| 61 |
$multidomainsWPML = apply_filters('wpml_setting', array(), 'language_domains'); |
| 62 |
if (!empty($multidomainsWPML)) { |
| 63 |
$multidomains = array_map(array($this,"ao_getDomain"),$multidomainsWPML); |
| 64 |
} |
| 65 |
|
| 66 |
if (!empty($this->cdn_url)) { |
| 67 |
$multidomains[]=parse_url($this->cdn_url,PHP_URL_HOST); |
| 68 |
} |
| 69 |
|
| 70 |
$multidomains = apply_filters('autoptimize_filter_cssjs_multidomain', $multidomains); |
| 71 |
|
| 72 |
if (!empty($multidomains)) { |
| 73 |
if (in_array($thisHost,$multidomains)) { |
| 74 |
$url=str_replace($thisHost, parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST), $url); |
| 75 |
} else { |
| 76 |
return false; |
| 77 |
} |
| 78 |
} else { |
| 79 |
return false; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// try to remove "wp root url" from url while not minding http<>https |
| 84 |
$tmp_ao_root = preg_replace('/https?:/','',AUTOPTIMIZE_WP_ROOT_URL); |
| 85 |
$tmp_url = preg_replace('/https?:/','',$url); |
| 86 |
$path = str_replace($tmp_ao_root,'',$tmp_url); |
| 87 |
|
| 88 |
// 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 |
| 89 |
if (preg_match('#^:?//#',$path)) { |
| 90 |
/** External script/css (adsense, etc) */ |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
$path = str_replace('//','/',WP_ROOT_DIR.$path); |
| 95 |
return $path; |
| 96 |
} |
| 97 |
|
| 98 |
// needed for WPML-filter |
| 99 |
protected function ao_getDomain($in) { |
| 100 |
// make sure the url starts with something vaguely resembling a protocol |
| 101 |
if ((strpos($in,"http")!==0) && (strpos($in,"//")!==0)) { |
| 102 |
$in="http://".$in; |
| 103 |
} |
| 104 |
|
| 105 |
// do the actual parse_url |
| 106 |
$out = parse_url($in,PHP_URL_HOST); |
| 107 |
|
| 108 |
// fallback if parse_url does not understand the url is in fact a url |
| 109 |
if (empty($out)) $out=$in; |
| 110 |
|
| 111 |
return $out; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
// logger |
| 116 |
protected function ao_logger($logmsg,$appendHTML=true) { |
| 117 |
if ($appendHTML) { |
| 118 |
$logmsg="<!--noptimize--><!-- ".$logmsg." --><!--/noptimize-->"; |
| 119 |
$this->content.=$logmsg; |
| 120 |
} else { |
| 121 |
error_log("Autoptimize: ".$logmsg); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// hide everything between noptimize-comment tags |
| 126 |
protected function hide_noptimize($noptimize_in) { |
| 127 |
if ( preg_match( '/<!--\s?noptimize\s?-->/', $noptimize_in ) ) { |
| 128 |
$noptimize_out = preg_replace_callback( |
| 129 |
'#<!--\s?noptimize\s?-->.*?<!--\s?/\s?noptimize\s?-->#is', |
| 130 |
create_function( |
| 131 |
'$matches', |
| 132 |
'return "%%NOPTIMIZE".AUTOPTIMIZE_HASH."%%".base64_encode($matches[0])."%%NOPTIMIZE%%";' |
| 133 |
), |
| 134 |
$noptimize_in |
| 135 |
); |
| 136 |
} else { |
| 137 |
$noptimize_out = $noptimize_in; |
| 138 |
} |
| 139 |
return $noptimize_out; |
| 140 |
} |
| 141 |
|
| 142 |
// unhide noptimize-tags |
| 143 |
protected function restore_noptimize($noptimize_in) { |
| 144 |
if ( strpos( $noptimize_in, '%%NOPTIMIZE%%' ) !== false ) { |
| 145 |
$noptimize_out = preg_replace_callback( |
| 146 |
'#%%NOPTIMIZE'.AUTOPTIMIZE_HASH.'%%(.*?)%%NOPTIMIZE%%#is', |
| 147 |
create_function( |
| 148 |
'$matches', |
| 149 |
'return base64_decode($matches[1]);' |
| 150 |
), |
| 151 |
$noptimize_in |
| 152 |
); |
| 153 |
} else { |
| 154 |
$noptimize_out = $noptimize_in; |
| 155 |
} |
| 156 |
return $noptimize_out; |
| 157 |
} |
| 158 |
|
| 159 |
protected function hide_iehacks($iehacks_in) { |
| 160 |
if ( strpos( $iehacks_in, '<!--[if' ) !== false ) { |
| 161 |
$iehacks_out = preg_replace_callback( |
| 162 |
'#<!--\[if.*?\[endif\]-->#is', |
| 163 |
create_function( |
| 164 |
'$matches', |
| 165 |
'return "%%IEHACK".AUTOPTIMIZE_HASH."%%".base64_encode($matches[0])."%%IEHACK%%";' |
| 166 |
), |
| 167 |
$iehacks_in |
| 168 |
); |
| 169 |
} else { |
| 170 |
$iehacks_out = $iehacks_in; |
| 171 |
} |
| 172 |
return $iehacks_out; |
| 173 |
} |
| 174 |
|
| 175 |
protected function restore_iehacks($iehacks_in) { |
| 176 |
if ( strpos( $iehacks_in, '%%IEHACK%%' ) !== false ) { |
| 177 |
$iehacks_out = preg_replace_callback( |
| 178 |
'#%%IEHACK'.AUTOPTIMIZE_HASH.'%%(.*?)%%IEHACK%%#is', |
| 179 |
create_function( |
| 180 |
'$matches', |
| 181 |
'return base64_decode($matches[1]);' |
| 182 |
), |
| 183 |
$iehacks_in |
| 184 |
); |
| 185 |
} else { |
| 186 |
$iehacks_out=$iehacks_in; |
| 187 |
} |
| 188 |
return $iehacks_out; |
| 189 |
} |
| 190 |
|
| 191 |
protected function hide_comments($comments_in) { |
| 192 |
if ( strpos( $comments_in, '<!--' ) !== false ) { |
| 193 |
$comments_out = preg_replace_callback( |
| 194 |
'#<!--.*?-->#is', |
| 195 |
create_function( |
| 196 |
'$matches', |
| 197 |
'return "%%COMMENTS".AUTOPTIMIZE_HASH."%%".base64_encode($matches[0])."%%COMMENTS%%";' |
| 198 |
), |
| 199 |
$comments_in |
| 200 |
); |
| 201 |
} else { |
| 202 |
$comments_out = $comments_in; |
| 203 |
} |
| 204 |
return $comments_out; |
| 205 |
} |
| 206 |
|
| 207 |
protected function restore_comments($comments_in) { |
| 208 |
if ( strpos( $comments_in, '%%COMMENTS%%' ) !== false ) { |
| 209 |
$comments_out = preg_replace_callback( |
| 210 |
'#%%COMMENTS'.AUTOPTIMIZE_HASH.'%%(.*?)%%COMMENTS%%#is', |
| 211 |
create_function( |
| 212 |
'$matches', |
| 213 |
'return base64_decode($matches[1]);' |
| 214 |
), |
| 215 |
$comments_in |
| 216 |
); |
| 217 |
} else { |
| 218 |
$comments_out=$comments_in; |
| 219 |
} |
| 220 |
return $comments_out; |
| 221 |
} |
| 222 |
|
| 223 |
protected function url_replace_cdn( $url ) { |
| 224 |
// API filter to change base CDN URL |
| 225 |
$cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', $this->cdn_url ); |
| 226 |
|
| 227 |
if ( !empty($cdn_url) ) { |
| 228 |
// prepend domain-less absolute URL's |
| 229 |
if ( ( substr( $url, 0, 1 ) === '/' ) && ( substr( $url, 1, 1 ) !== '/' ) ) { |
| 230 |
$url = rtrim( $cdn_url, '/' ) . $url; |
| 231 |
} else { |
| 232 |
// get wordpress base URL |
| 233 |
$WPSiteBreakdown = parse_url( AUTOPTIMIZE_WP_SITE_URL ); |
| 234 |
$WPBaseUrl = $WPSiteBreakdown['scheme'] . '://' . $WPSiteBreakdown['host']; |
| 235 |
if ( ! empty( $WPSiteBreakdown['port'] ) ) { |
| 236 |
$WPBaseUrl .= ":" . $WPSiteBreakdown['port']; |
| 237 |
} |
| 238 |
// replace full url's with scheme |
| 239 |
$tmp_url = str_replace( $WPBaseUrl, rtrim( $cdn_url, '/' ), $url ); |
| 240 |
if ( $tmp_url === $url ) { |
| 241 |
// last attempt; replace scheme-less URL's |
| 242 |
$url = str_replace( preg_replace( '/https?:/', '', $WPBaseUrl ), rtrim( $cdn_url, '/' ), $url ); |
| 243 |
} else { |
| 244 |
$url = $tmp_url; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// allow API filter to alter URL after CDN replacement |
| 250 |
$url = apply_filters( 'autoptimize_filter_base_replace_cdn', $url ); |
| 251 |
return $url; |
| 252 |
} |
| 253 |
|
| 254 |
protected function inject_in_html($payload,$replaceTag) { |
| 255 |
if (strpos($this->content,$replaceTag[0])!== false) { |
| 256 |
if ($replaceTag[1]==="after") { |
| 257 |
$replaceBlock=$replaceTag[0].$payload; |
| 258 |
} else if ($replaceTag[1]==="replace"){ |
| 259 |
$replaceBlock=$payload; |
| 260 |
} else { |
| 261 |
$replaceBlock=$payload.$replaceTag[0]; |
| 262 |
} |
| 263 |
$this->content = substr_replace($this->content,$replaceBlock,strpos($this->content,$replaceTag[0]),strlen($replaceTag[0])); |
| 264 |
} else { |
| 265 |
$this->content .= $payload; |
| 266 |
if (!$this->tagWarning) { |
| 267 |
$this->content .= "<!--noptimize--><!-- Autoptimize found a problem with the HTML in your Theme, tag \"".str_replace(array("<",">"),"",$replaceTag[0])."\" missing --><!--/noptimize-->"; |
| 268 |
$this->tagWarning=true; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
protected function isremovable($tag, $removables) { |
| 274 |
foreach ($removables as $match) { |
| 275 |
if (strpos($tag,$match)!==false) { |
| 276 |
return true; |
| 277 |
} |
| 278 |
} |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
// inject already minified code in optimized JS/CSS |
| 283 |
protected function inject_minified($in) { |
| 284 |
if ( strpos( $in, '%%INJECTLATER%%' ) !== false ) { |
| 285 |
$out = preg_replace_callback( |
| 286 |
'#%%INJECTLATER'.AUTOPTIMIZE_HASH.'%%(.*?)%%INJECTLATER%%#is', |
| 287 |
create_function( |
| 288 |
'$matches', |
| 289 |
'$filepath=base64_decode(strtok($matches[1],"|")); |
| 290 |
$filecontent=file_get_contents($filepath); |
| 291 |
|
| 292 |
// remove BOM |
| 293 |
$filecontent = preg_replace("#\x{EF}\x{BB}\x{BF}#","",$filecontent); |
| 294 |
|
| 295 |
// remove comments and blank lines |
| 296 |
if (substr($filepath,-3,3)===".js") { |
| 297 |
$filecontent=preg_replace("#^\s*\/\/.*$#Um","",$filecontent); |
| 298 |
} |
| 299 |
|
| 300 |
$filecontent=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Um","",$filecontent); |
| 301 |
$filecontent=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $filecontent); |
| 302 |
|
| 303 |
// specific stuff for JS-files |
| 304 |
if (substr($filepath,-3,3)===".js") { |
| 305 |
if ((substr($filecontent,-1,1)!==";")&&(substr($filecontent,-1,1)!=="}")) { |
| 306 |
$filecontent.=";"; |
| 307 |
} |
| 308 |
|
| 309 |
if (get_option("autoptimize_js_trycatch")==="on") { |
| 310 |
$filecontent="try{".$filecontent."}catch(e){}"; |
| 311 |
} |
| 312 |
} else if ((substr($filepath,-4,4)===".css")) { |
| 313 |
$filecontent=autoptimizeStyles::fixurls($filepath,$filecontent); |
| 314 |
} else { |
| 315 |
$filecontent=""; |
| 316 |
} |
| 317 |
|
| 318 |
// return |
| 319 |
return "\n".$filecontent;' |
| 320 |
), |
| 321 |
$in |
| 322 |
); |
| 323 |
} else { |
| 324 |
$out = $in; |
| 325 |
} |
| 326 |
return $out; |
| 327 |
} |
| 328 |
} |
| 329 |
|