| 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,'//')===0) { |
| 29 |
$url = "http:".$url; |
| 30 |
} else if ((strpos($url,'//')===false) && (strpos($url,parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST))===false)) { |
| 31 |
$url = AUTOPTIMIZE_WP_SITE_URL.$url; |
| 32 |
} |
| 33 |
$path = str_replace(AUTOPTIMIZE_WP_ROOT_URL,'',$url); |
| 34 |
if(preg_match('#^((https?|ftp):)?//#i',$path)) { |
| 35 |
/** External script/css (adsense, etc) */ |
| 36 |
return false; |
| 37 |
} |
| 38 |
$path = str_replace('//','/',WP_ROOT_DIR.$path); |
| 39 |
return $path; |
| 40 |
} |
| 41 |
|
| 42 |
// logger |
| 43 |
protected function ao_logger($logmsg) { |
| 44 |
$logfile=WP_CONTENT_DIR.'/ao_log.txt'; |
| 45 |
$logmsg.="\n"; |
| 46 |
file_put_contents($logfile,$logmsg,FILE_APPEND); |
| 47 |
} |
| 48 |
|
| 49 |
// hide everything between noptimize-comment tags |
| 50 |
protected function hide_noptimize($noptimize_in) { |
| 51 |
if ( preg_match( '/<!--\s?noptimize\s?-->/', $noptimize_in ) ) { |
| 52 |
$noptimize_out = preg_replace_callback( |
| 53 |
'#<!--\s?noptimize\s?-->.*?<!--\s?/\s?noptimize\s?-->#is', |
| 54 |
create_function( |
| 55 |
'$matches', |
| 56 |
'return "%%NOPTIMIZE%%".base64_encode($matches[0])."%%NOPTIMIZE%%";' |
| 57 |
), |
| 58 |
$noptimize_in |
| 59 |
); |
| 60 |
} else { |
| 61 |
$noptimize_out = $noptimize_in; |
| 62 |
} |
| 63 |
return $noptimize_out; |
| 64 |
} |
| 65 |
|
| 66 |
// unhide noptimize-tags |
| 67 |
protected function restore_noptimize($noptimize_in) { |
| 68 |
if ( strpos( $noptimize_in, '%%NOPTIMIZE%%' ) !== false ) { |
| 69 |
$noptimize_out = preg_replace_callback( |
| 70 |
'#%%NOPTIMIZE%%(.*?)%%NOPTIMIZE%%#is', |
| 71 |
create_function( |
| 72 |
'$matches', |
| 73 |
'return stripslashes(base64_decode($matches[1]));' |
| 74 |
), |
| 75 |
$noptimize_in |
| 76 |
); |
| 77 |
} else { |
| 78 |
$noptimize_out = $noptimize_in; |
| 79 |
} |
| 80 |
return $noptimize_out; |
| 81 |
} |
| 82 |
|
| 83 |
protected function hide_iehacks($iehacks_in) { |
| 84 |
if ( strpos( $iehacks_in, '<!--[if' ) !== false ) { |
| 85 |
$iehacks_out = preg_replace_callback( |
| 86 |
'#<!--\[if.*?\[endif\]-->#is', |
| 87 |
create_function( |
| 88 |
'$matches', |
| 89 |
'return "%%IEHACK%%".base64_encode($matches[0])."%%IEHACK%%";' |
| 90 |
), |
| 91 |
$iehacks_in |
| 92 |
); |
| 93 |
} else { |
| 94 |
$iehacks_out = $iehacks_in; |
| 95 |
} |
| 96 |
return $iehacks_out; |
| 97 |
} |
| 98 |
|
| 99 |
protected function restore_iehacks($iehacks_in) { |
| 100 |
if ( strpos( $iehacks_in, '%%IEHACK%%' ) !== false ) { |
| 101 |
$iehacks_out = preg_replace_callback( |
| 102 |
'#%%IEHACK%%(.*?)%%IEHACK%%#is', |
| 103 |
create_function( |
| 104 |
'$matches', |
| 105 |
'return stripslashes(base64_decode($matches[1]));' |
| 106 |
), |
| 107 |
$iehacks_in |
| 108 |
); |
| 109 |
} else { |
| 110 |
$iehacks_out=$iehacks_in; |
| 111 |
} |
| 112 |
return $iehacks_out; |
| 113 |
} |
| 114 |
|
| 115 |
protected function hide_comments($comments_in) { |
| 116 |
if ( strpos( $comments_in, '<!--' ) !== false ) { |
| 117 |
$comments_out = preg_replace_callback( |
| 118 |
'#<!--.*?-->#is', |
| 119 |
create_function( |
| 120 |
'$matches', |
| 121 |
'return "%%COMMENTS%%".base64_encode($matches[0])."%%COMMENTS%%";' |
| 122 |
), |
| 123 |
$comments_in |
| 124 |
); |
| 125 |
} else { |
| 126 |
$comments_out = $comments_in; |
| 127 |
} |
| 128 |
return $comments_out; |
| 129 |
} |
| 130 |
|
| 131 |
protected function restore_comments($comments_in) { |
| 132 |
if ( strpos( $comments_in, '%%COMMENTS%%' ) !== false ) { |
| 133 |
$comments_out = preg_replace_callback( |
| 134 |
'#%%COMMENTS%%(.*?)%%COMMENTS%%#is', |
| 135 |
create_function( |
| 136 |
'$matches', |
| 137 |
'return stripslashes(base64_decode($matches[1]));' |
| 138 |
), |
| 139 |
$comments_in |
| 140 |
); |
| 141 |
} else { |
| 142 |
$comments_out=$comments_in; |
| 143 |
} |
| 144 |
return $comments_out; |
| 145 |
} |
| 146 |
|
| 147 |
protected function url_replace_cdn($url) { |
| 148 |
if (!empty($this->cdn_url)) { |
| 149 |
$url=str_replace(AUTOPTIMIZE_WP_SITE_URL,rtrim($this->cdn_url,'/'),$url); |
| 150 |
} |
| 151 |
return $url; |
| 152 |
} |
| 153 |
|
| 154 |
protected function warn_html() { |
| 155 |
$this->content .= "<!--noptimize--><!-- Autoptimize found a problem with the HTML in your Theme, check if the title or body-tags are missing --><!--/noptimize-->"; |
| 156 |
} |
| 157 |
} |
| 158 |
|