| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
abstract class autoptimizeBase |
| 5 |
{ |
| 6 |
protected $content = ''; |
| 7 |
|
| 8 |
public function __construct($content) |
| 9 |
{ |
| 10 |
$this->content = $content; |
| 11 |
//Best place to catch errors |
| 12 |
} |
| 13 |
|
| 14 |
//Reads the page and collects tags |
| 15 |
abstract public function read($justhead); |
| 16 |
|
| 17 |
//Joins and optimizes collected things |
| 18 |
abstract public function minify(); |
| 19 |
|
| 20 |
//Caches the things |
| 21 |
abstract public function cache(); |
| 22 |
|
| 23 |
//Returns the content |
| 24 |
abstract public function getcontent(); |
| 25 |
|
| 26 |
//Converts an URL to a full path |
| 27 |
protected function getpath($url) { |
| 28 |
if (strpos($url,'%')!==false) { |
| 29 |
$url=urldecode($url); |
| 30 |
} |
| 31 |
|
| 32 |
// normalize |
| 33 |
if (strpos($url,'//')===0) { |
| 34 |
if (is_ssl()) { |
| 35 |
$url = "https:".$url; |
| 36 |
} else { |
| 37 |
$url = "http:".$url; |
| 38 |
} |
| 39 |
} else if ((strpos($url,'//')===false) && (strpos($url,parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST))===false)) { |
| 40 |
$url = AUTOPTIMIZE_WP_SITE_URL.$url; |
| 41 |
} |
| 42 |
|
| 43 |
// first check; hostname wp site should be hostname of url |
| 44 |
if (parse_url($url,PHP_URL_HOST)!==parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST)) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
// try to remove "wp root url" from url while not minding http<>https |
| 49 |
$tmp_ao_root = preg_replace('/https?/','',AUTOPTIMIZE_WP_ROOT_URL); |
| 50 |
$tmp_url = preg_replace('/https?/','',$url); |
| 51 |
$path = str_replace($tmp_ao_root,'',$tmp_url); |
| 52 |
|
| 53 |
// 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 |
| 54 |
if (preg_match('#^:?//#',$path)) { |
| 55 |
/** External script/css (adsense, etc) */ |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
$path = str_replace('//','/',WP_ROOT_DIR.$path); |
| 60 |
return $path; |
| 61 |
} |
| 62 |
|
| 63 |
// logger |
| 64 |
protected function ao_logger($logmsg,$appendHTML=true) { |
| 65 |
if ($appendHTML) { |
| 66 |
$logmsg="<!--noptimize--><!-- ".$logmsg." --><!--/noptimize-->"; |
| 67 |
$this->content.=$logmsg; |
| 68 |
} else { |
| 69 |
$logfile=WP_CONTENT_DIR.'/ao_log.txt'; |
| 70 |
$logmsg.="\n--\n"; |
| 71 |
file_put_contents($logfile,$logmsg,FILE_APPEND); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// hide everything between noptimize-comment tags |
| 76 |
protected function hide_noptimize($noptimize_in) { |
| 77 |
if ( preg_match( '/<!--\s?noptimize\s?-->/', $noptimize_in ) ) { |
| 78 |
$noptimize_out = preg_replace_callback( |
| 79 |
'#<!--\s?noptimize\s?-->.*?<!--\s?/\s?noptimize\s?-->#is', |
| 80 |
create_function( |
| 81 |
'$matches', |
| 82 |
'return "%%NOPTIMIZE%%".base64_encode($matches[0])."%%NOPTIMIZE%%";' |
| 83 |
), |
| 84 |
$noptimize_in |
| 85 |
); |
| 86 |
} else { |
| 87 |
$noptimize_out = $noptimize_in; |
| 88 |
} |
| 89 |
return $noptimize_out; |
| 90 |
} |
| 91 |
|
| 92 |
// unhide noptimize-tags |
| 93 |
protected function restore_noptimize($noptimize_in) { |
| 94 |
if ( strpos( $noptimize_in, '%%NOPTIMIZE%%' ) !== false ) { |
| 95 |
$noptimize_out = preg_replace_callback( |
| 96 |
'#%%NOPTIMIZE%%(.*?)%%NOPTIMIZE%%#is', |
| 97 |
create_function( |
| 98 |
'$matches', |
| 99 |
'return stripslashes(base64_decode($matches[1]));' |
| 100 |
), |
| 101 |
$noptimize_in |
| 102 |
); |
| 103 |
} else { |
| 104 |
$noptimize_out = $noptimize_in; |
| 105 |
} |
| 106 |
return $noptimize_out; |
| 107 |
} |
| 108 |
|
| 109 |
protected function hide_iehacks($iehacks_in) { |
| 110 |
if ( strpos( $iehacks_in, '<!--[if' ) !== false ) { |
| 111 |
$iehacks_out = preg_replace_callback( |
| 112 |
'#<!--\[if.*?\[endif\]-->#is', |
| 113 |
create_function( |
| 114 |
'$matches', |
| 115 |
'return "%%IEHACK%%".base64_encode($matches[0])."%%IEHACK%%";' |
| 116 |
), |
| 117 |
$iehacks_in |
| 118 |
); |
| 119 |
} else { |
| 120 |
$iehacks_out = $iehacks_in; |
| 121 |
} |
| 122 |
return $iehacks_out; |
| 123 |
} |
| 124 |
|
| 125 |
protected function restore_iehacks($iehacks_in) { |
| 126 |
if ( strpos( $iehacks_in, '%%IEHACK%%' ) !== false ) { |
| 127 |
$iehacks_out = preg_replace_callback( |
| 128 |
'#%%IEHACK%%(.*?)%%IEHACK%%#is', |
| 129 |
create_function( |
| 130 |
'$matches', |
| 131 |
'return stripslashes(base64_decode($matches[1]));' |
| 132 |
), |
| 133 |
$iehacks_in |
| 134 |
); |
| 135 |
} else { |
| 136 |
$iehacks_out=$iehacks_in; |
| 137 |
} |
| 138 |
return $iehacks_out; |
| 139 |
} |
| 140 |
|
| 141 |
protected function hide_comments($comments_in) { |
| 142 |
if ( strpos( $comments_in, '<!--' ) !== false ) { |
| 143 |
$comments_out = preg_replace_callback( |
| 144 |
'#<!--.*?-->#is', |
| 145 |
create_function( |
| 146 |
'$matches', |
| 147 |
'return "%%COMMENTS%%".base64_encode($matches[0])."%%COMMENTS%%";' |
| 148 |
), |
| 149 |
$comments_in |
| 150 |
); |
| 151 |
} else { |
| 152 |
$comments_out = $comments_in; |
| 153 |
} |
| 154 |
return $comments_out; |
| 155 |
} |
| 156 |
|
| 157 |
protected function restore_comments($comments_in) { |
| 158 |
if ( strpos( $comments_in, '%%COMMENTS%%' ) !== false ) { |
| 159 |
$comments_out = preg_replace_callback( |
| 160 |
'#%%COMMENTS%%(.*?)%%COMMENTS%%#is', |
| 161 |
create_function( |
| 162 |
'$matches', |
| 163 |
'return stripslashes(base64_decode($matches[1]));' |
| 164 |
), |
| 165 |
$comments_in |
| 166 |
); |
| 167 |
} else { |
| 168 |
$comments_out=$comments_in; |
| 169 |
} |
| 170 |
return $comments_out; |
| 171 |
} |
| 172 |
|
| 173 |
protected function url_replace_cdn($url) { |
| 174 |
if (!empty($this->cdn_url)) { |
| 175 |
$url=str_replace(AUTOPTIMIZE_WP_SITE_URL,rtrim($this->cdn_url,'/'),$url); |
| 176 |
} |
| 177 |
return $url; |
| 178 |
} |
| 179 |
|
| 180 |
protected function inject_in_html($payload,$replaceTag) { |
| 181 |
if (strpos($this->content,$replaceTag[0])!== false) { |
| 182 |
if ($replaceTag[1]==="after") { |
| 183 |
$replaceBlock=$replaceTag[0].$payload; |
| 184 |
} else if ($replaceTag[1]==="replace"){ |
| 185 |
$replaceBlock=$payload; |
| 186 |
} else { |
| 187 |
$replaceBlock=$payload.$replaceTag[0]; |
| 188 |
} |
| 189 |
$this->content = str_replace($replaceTag[0],$replaceBlock,$this->content); |
| 190 |
} else { |
| 191 |
$this->content .= $payload; |
| 192 |
if (!$tagWarning) { |
| 193 |
$this->content .= "<!--noptimize--><!-- Autoptimize found a problem with the HTML in your Theme, tag ".$replaceTag[0]." missing --><!--/noptimize-->"; |
| 194 |
$tagWarning=true; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|