| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
abstract class autoptimizeBase |
| 4 |
{ |
| 5 |
protected $content = ''; |
| 6 |
|
| 7 |
public function __construct($content) |
| 8 |
{ |
| 9 |
$this->content = $content; |
| 10 |
//Best place to catch errors |
| 11 |
} |
| 12 |
|
| 13 |
//Reads the page and collects tags |
| 14 |
abstract public function read($justhead); |
| 15 |
|
| 16 |
//Joins and optimizes collected things |
| 17 |
abstract public function minify(); |
| 18 |
|
| 19 |
//Caches the things |
| 20 |
abstract public function cache(); |
| 21 |
|
| 22 |
//Returns the content |
| 23 |
abstract public function getcontent(); |
| 24 |
|
| 25 |
//Converts an URL to a full path |
| 26 |
protected function getpath($url) |
| 27 |
{ |
| 28 |
$siteurl = site_url(); |
| 29 |
if ((strpos($url,'//')===false) && (strpos($url,parse_url($siteurl,PHP_URL_HOST))===false)) { |
| 30 |
$url = $siteurl.$url; |
| 31 |
} |
| 32 |
$path = str_replace(WP_ROOT_URL,'',$url); |
| 33 |
if(preg_match('#^(https?|ftp)://#i',$path)) |
| 34 |
{ |
| 35 |
/** External script/css (adsense, etc) */ |
| 36 |
return false; |
| 37 |
} |
| 38 |
$path = str_replace('//','/',WP_ROOT_DIR.$path); |
| 39 |
return $path; |
| 40 |
} |
| 41 |
// logger |
| 42 |
protected function ao_logger($logmsg) { |
| 43 |
$logfile=WP_CONTENT_DIR.'/ao_log.txt'; |
| 44 |
$logmsg.="\n"; |
| 45 |
file_put_contents($logfile,$logmsg,FILE_APPEND); |
| 46 |
} |
| 47 |
// hide everything between noptimize-comment tags |
| 48 |
protected function hide_noptimize($noptimize_in) { |
| 49 |
if ( preg_match( '/<!--\s?noptimize\s?-->/', $noptimize_in ) ) { |
| 50 |
$noptimize_out = preg_replace_callback( |
| 51 |
'#<!--\s?noptimize\s?-->.*?<!--\s?/\s?noptimize\s?-->#is', |
| 52 |
create_function( |
| 53 |
'$matches', |
| 54 |
'return "%%NOPTIMIZE%%".base64_encode($matches[0])."%%NOPTIMIZE%%";' |
| 55 |
), |
| 56 |
$noptimize_in |
| 57 |
); |
| 58 |
} else { |
| 59 |
$noptimize_out = $noptimize_in; |
| 60 |
} |
| 61 |
return $noptimize_out; |
| 62 |
} |
| 63 |
|
| 64 |
// unhide noptimize-tags |
| 65 |
protected function restore_noptimize($noptimize_in) { |
| 66 |
if ( preg_match( '/%%NOPTIMIZE%%/', $noptimize_in ) ) { |
| 67 |
$noptimize_out = preg_replace_callback( |
| 68 |
'#%%NOPTIMIZE%%(.*?)%%NOPTIMIZE%%#is', |
| 69 |
create_function( |
| 70 |
'$matches', |
| 71 |
'return stripslashes(base64_decode($matches[1]));' |
| 72 |
), |
| 73 |
$noptimize_in |
| 74 |
); |
| 75 |
} else { |
| 76 |
$noptimize_out = $noptimize_in; |
| 77 |
} |
| 78 |
return $noptimize_out; |
| 79 |
} |
| 80 |
} |
| 81 |
|