| @@ 159-173 (lines=15) @@ | ||
| 156 | } |
|
| 157 | ||
| 158 | // unhide noptimize-tags |
|
| 159 | protected function restore_noptimize($noptimize_in) { |
|
| 160 | if ( strpos( $noptimize_in, '%%NOPTIMIZE%%' ) !== false ) { |
|
| 161 | $noptimize_out = preg_replace_callback( |
|
| 162 | '#%%NOPTIMIZE'.AUTOPTIMIZE_HASH.'%%(.*?)%%NOPTIMIZE%%#is', |
|
| 163 | create_function( |
|
| 164 | '$matches', |
|
| 165 | 'return base64_decode($matches[1]);' |
|
| 166 | ), |
|
| 167 | $noptimize_in |
|
| 168 | ); |
|
| 169 | } else { |
|
| 170 | $noptimize_out = $noptimize_in; |
|
| 171 | } |
|
| 172 | return $noptimize_out; |
|
| 173 | } |
|
| 174 | ||
| 175 | protected function hide_iehacks($iehacks_in) { |
|
| 176 | if ( strpos( $iehacks_in, '<!--[if' ) !== false ) { |
|
| @@ 191-205 (lines=15) @@ | ||
| 188 | return $iehacks_out; |
|
| 189 | } |
|
| 190 | ||
| 191 | protected function restore_iehacks($iehacks_in) { |
|
| 192 | if ( strpos( $iehacks_in, '%%IEHACK%%' ) !== false ) { |
|
| 193 | $iehacks_out = preg_replace_callback( |
|
| 194 | '#%%IEHACK'.AUTOPTIMIZE_HASH.'%%(.*?)%%IEHACK%%#is', |
|
| 195 | create_function( |
|
| 196 | '$matches', |
|
| 197 | 'return base64_decode($matches[1]);' |
|
| 198 | ), |
|
| 199 | $iehacks_in |
|
| 200 | ); |
|
| 201 | } else { |
|
| 202 | $iehacks_out=$iehacks_in; |
|
| 203 | } |
|
| 204 | return $iehacks_out; |
|
| 205 | } |
|
| 206 | ||
| 207 | protected function hide_comments($comments_in) { |
|
| 208 | if ( strpos( $comments_in, '<!--' ) !== false ) { |
|
| @@ 223-237 (lines=15) @@ | ||
| 220 | return $comments_out; |
|
| 221 | } |
|
| 222 | ||
| 223 | protected function restore_comments($comments_in) { |
|
| 224 | if ( strpos( $comments_in, '%%COMMENTS%%' ) !== false ) { |
|
| 225 | $comments_out = preg_replace_callback( |
|
| 226 | '#%%COMMENTS'.AUTOPTIMIZE_HASH.'%%(.*?)%%COMMENTS%%#is', |
|
| 227 | create_function( |
|
| 228 | '$matches', |
|
| 229 | 'return base64_decode($matches[1]);' |
|
| 230 | ), |
|
| 231 | $comments_in |
|
| 232 | ); |
|
| 233 | } else { |
|
| 234 | $comments_out=$comments_in; |
|
| 235 | } |
|
| 236 | return $comments_out; |
|
| 237 | } |
|
| 238 | ||
| 239 | protected function url_replace_cdn( $url ) { |
|
| 240 | // API filter to change base CDN URL |
|
| @@ 299-348 (lines=50) @@ | ||
| 296 | } |
|
| 297 | ||
| 298 | // inject already minified code in optimized JS/CSS |
|
| 299 | protected function inject_minified($in) { |
|
| 300 | if ( strpos( $in, '%%INJECTLATER%%' ) !== false ) { |
|
| 301 | $out = preg_replace_callback( |
|
| 302 | '#\/\*\!%%INJECTLATER'.AUTOPTIMIZE_HASH.'%%(.*?)%%INJECTLATER%%\*\/#is', |
|
| 303 | create_function( |
|
| 304 | '$matches', |
|
| 305 | '$filepath=base64_decode(strtok($matches[1],"|")); |
|
| 306 | $filecontent=file_get_contents($filepath); |
|
| 307 | ||
| 308 | // remove BOM |
|
| 309 | $filecontent = preg_replace("#\x{EF}\x{BB}\x{BF}#","",$filecontent); |
|
| 310 | ||
| 311 | // remove comments and blank lines |
|
| 312 | if (substr($filepath,-3,3)===".js") { |
|
| 313 | $filecontent=preg_replace("#^\s*\/\/.*$#Um","",$filecontent); |
|
| 314 | } |
|
| 315 | ||
| 316 | $filecontent=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Um","",$filecontent); |
|
| 317 | $filecontent=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $filecontent); |
|
| 318 | ||
| 319 | // differentiate between JS, CSS and other files |
|
| 320 | if (substr($filepath,-3,3)===".js") { |
|
| 321 | if ((substr($filecontent,-1,1)!==";")&&(substr($filecontent,-1,1)!=="}")) { |
|
| 322 | $filecontent.=";"; |
|
| 323 | } |
|
| 324 | ||
| 325 | if (get_option("autoptimize_js_trycatch")==="on") { |
|
| 326 | $filecontent="try{".$filecontent."}catch(e){}"; |
|
| 327 | } |
|
| 328 | } else if ((substr($filepath,-4,4)===".css")) { |
|
| 329 | $filecontent=autoptimizeStyles::fixurls($filepath,$filecontent); |
|
| 330 | } else { |
|
| 331 | $filecontent=""; |
|
| 332 | } |
|
| 333 | ||
| 334 | // return |
|
| 335 | return "\n".$filecontent;' |
|
| 336 | ), |
|
| 337 | $in |
|
| 338 | ); |
|
| 339 | } else { |
|
| 340 | $out = $in; |
|
| 341 | } |
|
| 342 | return $out; |
|
| 343 | } |
|
| 344 | ||
| 345 | protected function minify_single($pathIn) { |
|
| 346 | // determine JS or CSS and set var (also mimetype), return false if neither |
|
| 347 | if ( $this->str_ends_in($pathIn,".js") === true ) { |
|
| 348 | $codeType="js"; |
|
| 349 | $codeMime="text/javascript"; |
|
| 350 | } else if ( $this->str_ends_in($pathIn,".css") === true ) { |
|
| 351 | $codeType="css"; |
|