@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
|
2 | +if (!defined('ABSPATH')) exit; // Exit if accessed directly |
|
3 | 3 | |
4 | 4 | class autoptimizeStyles extends autoptimizeBase { |
5 | 5 | private $css = array(); |
@@ -21,12 +21,12 @@ discard block |
||
21 | 21 | |
22 | 22 | //Reads the page and collects style tags |
23 | 23 | public function read($options) { |
24 | - $noptimizeCSS = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content ); |
|
24 | + $noptimizeCSS = apply_filters('autoptimize_filter_css_noptimize', false, $this->content); |
|
25 | 25 | if ($noptimizeCSS) return false; |
26 | 26 | |
27 | - $whitelistCSS = apply_filters( 'autoptimize_filter_css_whitelist', '' ); |
|
27 | + $whitelistCSS = apply_filters('autoptimize_filter_css_whitelist', ''); |
|
28 | 28 | if (!empty($whitelistCSS)) { |
29 | - $this->whitelist = array_filter(array_map('trim',explode(",",$whitelistCSS))); |
|
29 | + $this->whitelist = array_filter(array_map('trim', explode(",", $whitelistCSS))); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | if ($options['nogooglefont'] == true) { |
@@ -34,33 +34,33 @@ discard block |
||
34 | 34 | } else { |
35 | 35 | $removableCSS = ""; |
36 | 36 | } |
37 | - $removableCSS = apply_filters( 'autoptimize_filter_css_removables', $removableCSS); |
|
37 | + $removableCSS = apply_filters('autoptimize_filter_css_removables', $removableCSS); |
|
38 | 38 | if (!empty($removableCSS)) { |
39 | - $this->cssremovables = array_filter(array_map('trim',explode(",",$removableCSS))); |
|
39 | + $this->cssremovables = array_filter(array_map('trim', explode(",", $removableCSS))); |
|
40 | 40 | } |
41 | 41 | |
42 | - $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize',256); |
|
42 | + $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize', 256); |
|
43 | 43 | |
44 | 44 | // filter to "late inject minified CSS", default to true for now (it is faster) |
45 | - $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late',true); |
|
45 | + $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late', true); |
|
46 | 46 | |
47 | 47 | // Remove everything that's not the header |
48 | - if ( apply_filters('autoptimize_filter_css_justhead',$options['justhead']) == true ) { |
|
49 | - $content = explode('</head>',$this->content,2); |
|
48 | + if (apply_filters('autoptimize_filter_css_justhead', $options['justhead']) == true) { |
|
49 | + $content = explode('</head>', $this->content, 2); |
|
50 | 50 | $this->content = $content[0].'</head>'; |
51 | 51 | $this->restofcontent = $content[1]; |
52 | 52 | } |
53 | 53 | |
54 | 54 | // include inline? |
55 | - if( apply_filters('autoptimize_css_include_inline',$options['include_inline']) == true ) { |
|
55 | + if (apply_filters('autoptimize_css_include_inline', $options['include_inline']) == true) { |
|
56 | 56 | $this->include_inline = true; |
57 | 57 | } |
58 | 58 | |
59 | 59 | // what CSS shouldn't be autoptimized |
60 | 60 | $excludeCSS = $options['css_exclude']; |
61 | - $excludeCSS = apply_filters( 'autoptimize_filter_css_exclude', $excludeCSS ); |
|
62 | - if ($excludeCSS!=="") { |
|
63 | - $this->dontmove = array_filter(array_map('trim',explode(",",$excludeCSS))); |
|
61 | + $excludeCSS = apply_filters('autoptimize_filter_css_exclude', $excludeCSS); |
|
62 | + if ($excludeCSS !== "") { |
|
63 | + $this->dontmove = array_filter(array_map('trim', explode(",", $excludeCSS))); |
|
64 | 64 | } else { |
65 | 65 | $this->dontmove = ""; |
66 | 66 | } |
@@ -68,17 +68,17 @@ discard block |
||
68 | 68 | // should we defer css? |
69 | 69 | // value: true/ false |
70 | 70 | $this->defer = $options['defer']; |
71 | - $this->defer = apply_filters( 'autoptimize_filter_css_defer', $this->defer ); |
|
71 | + $this->defer = apply_filters('autoptimize_filter_css_defer', $this->defer); |
|
72 | 72 | |
73 | 73 | // should we inline while deferring? |
74 | 74 | // value: inlined CSS |
75 | 75 | $this->defer_inline = $options['defer_inline']; |
76 | - $this->defer_inline = apply_filters( 'autoptimize_filter_css_defer_inline', $this->defer_inline ); |
|
76 | + $this->defer_inline = apply_filters('autoptimize_filter_css_defer_inline', $this->defer_inline); |
|
77 | 77 | |
78 | 78 | // should we inline? |
79 | 79 | // value: true/ false |
80 | 80 | $this->inline = $options['inline']; |
81 | - $this->inline = apply_filters( 'autoptimize_filter_css_inline', $this->inline ); |
|
81 | + $this->inline = apply_filters('autoptimize_filter_css_inline', $this->inline); |
|
82 | 82 | |
83 | 83 | // get cdn url |
84 | 84 | $this->cdn_url = $options['cdn_url']; |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | $this->content = $this->hide_noptimize($this->content); |
91 | 91 | |
92 | 92 | // exclude (no)script, as those may contain CSS which should be left as is |
93 | - if ( strpos( $this->content, '<script' ) !== false ) { |
|
93 | + if (strpos($this->content, '<script') !== false) { |
|
94 | 94 | $this->content = preg_replace_callback( |
95 | 95 | '#<(?:no)?script.*?<\/(?:no)?script>#is', |
96 | 96 | create_function( |
@@ -108,56 +108,56 @@ discard block |
||
108 | 108 | $this->content = $this->hide_comments($this->content); |
109 | 109 | |
110 | 110 | // Get <style> and <link> |
111 | - if(preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi',$this->content,$matches)) { |
|
112 | - foreach($matches[0] as $tag) { |
|
113 | - if ($this->isremovable($tag,$this->cssremovables)) { |
|
114 | - $this->content = str_replace($tag,'',$this->content); |
|
111 | + if (preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches)) { |
|
112 | + foreach ($matches[0] as $tag) { |
|
113 | + if ($this->isremovable($tag, $this->cssremovables)) { |
|
114 | + $this->content = str_replace($tag, '', $this->content); |
|
115 | 115 | } else if ($this->ismovable($tag)) { |
116 | 116 | // Get the media |
117 | - if(strpos($tag,'media=')!==false) { |
|
118 | - preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui',$tag,$medias); |
|
119 | - $medias = explode(',',$medias[1]); |
|
117 | + if (strpos($tag, 'media=') !== false) { |
|
118 | + preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias); |
|
119 | + $medias = explode(',', $medias[1]); |
|
120 | 120 | $media = array(); |
121 | - foreach($medias as $elem) { |
|
122 | - if (empty($elem)) { $elem="all"; } |
|
121 | + foreach ($medias as $elem) { |
|
122 | + if (empty($elem)) { $elem = "all"; } |
|
123 | 123 | $media[] = $elem; |
124 | 124 | } |
125 | 125 | } else { |
126 | 126 | // No media specified - applies to all |
127 | 127 | $media = array('all'); |
128 | 128 | } |
129 | - $media = apply_filters( 'autoptimize_filter_css_tagmedia',$media,$tag ); |
|
129 | + $media = apply_filters('autoptimize_filter_css_tagmedia', $media, $tag); |
|
130 | 130 | |
131 | - if(preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) { |
|
131 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
132 | 132 | // <link> |
133 | - $url = current(explode('?',$source[2],2)); |
|
133 | + $url = current(explode('?', $source[2], 2)); |
|
134 | 134 | $path = $this->getpath($url); |
135 | 135 | |
136 | - if($path!==false && preg_match('#\.css$#',$path)) { |
|
136 | + if ($path !== false && preg_match('#\.css$#', $path)) { |
|
137 | 137 | // Good link |
138 | - $this->css[] = array($media,$path); |
|
139 | - }else{ |
|
138 | + $this->css[] = array($media, $path); |
|
139 | + } else { |
|
140 | 140 | // Link is dynamic (.php etc) |
141 | 141 | $tag = ''; |
142 | 142 | } |
143 | 143 | } else { |
144 | 144 | // inline css in style tags can be wrapped in comment tags, so restore comments |
145 | 145 | $tag = $this->restore_comments($tag); |
146 | - preg_match('#<style.*>(.*)</style>#Usmi',$tag,$code); |
|
146 | + preg_match('#<style.*>(.*)</style>#Usmi', $tag, $code); |
|
147 | 147 | |
148 | 148 | // and re-hide them to be able to to the removal based on tag |
149 | 149 | $tag = $this->hide_comments($tag); |
150 | 150 | |
151 | - if ( $this->include_inline ) { |
|
152 | - $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm','$1',$code[1]); |
|
153 | - $this->css[] = array($media,'INLINE;'.$code); |
|
151 | + if ($this->include_inline) { |
|
152 | + $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1]); |
|
153 | + $this->css[] = array($media, 'INLINE;'.$code); |
|
154 | 154 | } else { |
155 | 155 | $tag = ''; |
156 | 156 | } |
157 | 157 | } |
158 | 158 | |
159 | 159 | // Remove the original style tag |
160 | - $this->content = str_replace($tag,'',$this->content); |
|
160 | + $this->content = str_replace($tag, '', $this->content); |
|
161 | 161 | } |
162 | 162 | } |
163 | 163 | return true; |
@@ -168,29 +168,29 @@ discard block |
||
168 | 168 | |
169 | 169 | // Joins and optimizes CSS |
170 | 170 | public function minify() { |
171 | - foreach($this->css as $group) { |
|
172 | - list($media,$css) = $group; |
|
173 | - if(preg_match('#^INLINE;#',$css)) { |
|
171 | + foreach ($this->css as $group) { |
|
172 | + list($media, $css) = $group; |
|
173 | + if (preg_match('#^INLINE;#', $css)) { |
|
174 | 174 | // <style> |
175 | - $css = preg_replace('#^INLINE;#','',$css); |
|
176 | - $css = $this->fixurls(ABSPATH.'/index.php',$css); |
|
177 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, "" ); |
|
178 | - if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle) ) { |
|
179 | - $css=$tmpstyle; |
|
180 | - $this->alreadyminified=true; |
|
175 | + $css = preg_replace('#^INLINE;#', '', $css); |
|
176 | + $css = $this->fixurls(ABSPATH.'/index.php', $css); |
|
177 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, ""); |
|
178 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
179 | + $css = $tmpstyle; |
|
180 | + $this->alreadyminified = true; |
|
181 | 181 | } |
182 | 182 | } else { |
183 | 183 | //<link> |
184 | - if($css !== false && file_exists($css) && is_readable($css)) { |
|
184 | + if ($css !== false && file_exists($css) && is_readable($css)) { |
|
185 | 185 | $cssPath = $css; |
186 | - $css = $this->fixurls($cssPath,file_get_contents($cssPath)); |
|
187 | - $css = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$css); |
|
188 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $cssPath ); |
|
186 | + $css = $this->fixurls($cssPath, file_get_contents($cssPath)); |
|
187 | + $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css); |
|
188 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $cssPath); |
|
189 | 189 | if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
190 | - $css=$tmpstyle; |
|
191 | - $this->alreadyminified=true; |
|
192 | - } else if ($this->can_inject_late($cssPath,$css)) { |
|
193 | - $css="%%INJECTLATER%%".base64_encode($cssPath)."|".md5($css)."%%INJECTLATER%%"; |
|
190 | + $css = $tmpstyle; |
|
191 | + $this->alreadyminified = true; |
|
192 | + } else if ($this->can_inject_late($cssPath, $css)) { |
|
193 | + $css = "%%INJECTLATER%%".base64_encode($cssPath)."|".md5($css)."%%INJECTLATER%%"; |
|
194 | 194 | } |
195 | 195 | } else { |
196 | 196 | // Couldn't read CSS. Maybe getpath isn't working? |
@@ -198,8 +198,8 @@ discard block |
||
198 | 198 | } |
199 | 199 | } |
200 | 200 | |
201 | - foreach($media as $elem) { |
|
202 | - if(!isset($this->csscode[$elem])) |
|
201 | + foreach ($media as $elem) { |
|
202 | + if (!isset($this->csscode[$elem])) |
|
203 | 203 | $this->csscode[$elem] = ''; |
204 | 204 | $this->csscode[$elem] .= "\n/*FILESTART*/".$css; |
205 | 205 | } |
@@ -208,12 +208,12 @@ discard block |
||
208 | 208 | // Check for duplicate code |
209 | 209 | $md5list = array(); |
210 | 210 | $tmpcss = $this->csscode; |
211 | - foreach($tmpcss as $media => $code) { |
|
211 | + foreach ($tmpcss as $media => $code) { |
|
212 | 212 | $md5sum = md5($code); |
213 | 213 | $medianame = $media; |
214 | - foreach($md5list as $med => $sum) { |
|
214 | + foreach ($md5list as $med => $sum) { |
|
215 | 215 | // If same code |
216 | - if($sum === $md5sum) { |
|
216 | + if ($sum === $md5sum) { |
|
217 | 217 | //Add the merged code |
218 | 218 | $medianame = $med.', '.$media; |
219 | 219 | $this->csscode[$medianame] = $code; |
@@ -233,29 +233,29 @@ discard block |
||
233 | 233 | $external_imports = ""; |
234 | 234 | |
235 | 235 | // remove comments to avoid importing commented-out imports |
236 | - $thiscss_nocomments=preg_replace('#/\*.*\*/#Us','',$thiscss); |
|
237 | - while(preg_match_all('#@import.*(?:;|$)#Um',$thiscss_nocomments,$matches)) { |
|
238 | - foreach($matches[0] as $import) { |
|
239 | - if ($this->isremovable($import,$this->cssremovables)) { |
|
240 | - $thiscss = str_replace($import,'',$thiscss); |
|
236 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
237 | + while (preg_match_all('#@import.*(?:;|$)#Um', $thiscss_nocomments, $matches)) { |
|
238 | + foreach ($matches[0] as $import) { |
|
239 | + if ($this->isremovable($import, $this->cssremovables)) { |
|
240 | + $thiscss = str_replace($import, '', $thiscss); |
|
241 | 241 | $import_ok = true; |
242 | 242 | } else { |
243 | - $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#','$1',trim($import))," \t\n\r\0\x0B\"'"); |
|
243 | + $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'"); |
|
244 | 244 | $path = $this->getpath($url); |
245 | 245 | $import_ok = false; |
246 | 246 | if (file_exists($path) && is_readable($path)) { |
247 | - $code = addcslashes($this->fixurls($path,file_get_contents($path)),"\\"); |
|
248 | - $code = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$code); |
|
249 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, "" ); |
|
250 | - if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
251 | - $code=$tmpstyle; |
|
252 | - $this->alreadyminified=true; |
|
253 | - } else if ($this->can_inject_late($path,$code)) { |
|
254 | - $code="%%INJECTLATER%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%"; |
|
247 | + $code = addcslashes($this->fixurls($path, file_get_contents($path)), "\\"); |
|
248 | + $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code); |
|
249 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, ""); |
|
250 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
251 | + $code = $tmpstyle; |
|
252 | + $this->alreadyminified = true; |
|
253 | + } else if ($this->can_inject_late($path, $code)) { |
|
254 | + $code = "%%INJECTLATER%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%"; |
|
255 | 255 | } |
256 | 256 | |
257 | - if(!empty($code)) { |
|
258 | - $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import,'#').'#Us','/*FILESTART2*/'.$code.'$1',$thiscss); |
|
257 | + if (!empty($code)) { |
|
258 | + $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss); |
|
259 | 259 | if (!empty($tmp_thiscss)) { |
260 | 260 | $thiscss = $tmp_thiscss; |
261 | 261 | $import_ok = true; |
@@ -269,32 +269,32 @@ discard block |
||
269 | 269 | if (!$import_ok) { |
270 | 270 | // external imports and general fall-back |
271 | 271 | $external_imports .= $import; |
272 | - $thiscss = str_replace($import,'',$thiscss); |
|
272 | + $thiscss = str_replace($import, '', $thiscss); |
|
273 | 273 | $fiximports = true; |
274 | 274 | } |
275 | 275 | } |
276 | - $thiscss = preg_replace('#/\*FILESTART\*/#','',$thiscss); |
|
277 | - $thiscss = preg_replace('#/\*FILESTART2\*/#','/*FILESTART*/',$thiscss); |
|
276 | + $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss); |
|
277 | + $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss); |
|
278 | 278 | |
279 | 279 | // and update $thiscss_nocomments before going into next iteration in while loop |
280 | - $thiscss_nocomments=preg_replace('#/\*.*\*/#Us','',$thiscss); |
|
280 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
281 | 281 | } |
282 | 282 | unset($thiscss_nocomments); |
283 | 283 | |
284 | 284 | // add external imports to top of aggregated CSS |
285 | - if($fiximports) { |
|
286 | - $thiscss=$external_imports.$thiscss; |
|
285 | + if ($fiximports) { |
|
286 | + $thiscss = $external_imports.$thiscss; |
|
287 | 287 | } |
288 | 288 | } |
289 | 289 | unset($thiscss); |
290 | 290 | |
291 | 291 | // $this->csscode has all the uncompressed code now. |
292 | 292 | $mhtmlcount = 0; |
293 | - foreach($this->csscode as &$code) { |
|
293 | + foreach ($this->csscode as &$code) { |
|
294 | 294 | // Check for already-minified code |
295 | 295 | $hash = md5($code); |
296 | - $ccheck = new autoptimizeCache($hash,'css'); |
|
297 | - if($ccheck->check()) { |
|
296 | + $ccheck = new autoptimizeCache($hash, 'css'); |
|
297 | + if ($ccheck->check()) { |
|
298 | 298 | $code = $ccheck->retrieve(); |
299 | 299 | $this->hashmap[md5($code)] = $hash; |
300 | 300 | continue; |
@@ -308,44 +308,44 @@ discard block |
||
308 | 308 | // this is the old regex that searched for the entire background css rule, but it wouldn't match multiple background image url css rules. |
309 | 309 | // preg_match_all('#(background[^;{}]*url\((?!\s?"?\'?\s?data)(.*)\)[^;}]*)(?:;|$|})#Usm',$code,$matches); |
310 | 310 | // this new regex will be slightly faster too: |
311 | - preg_match_all('#url\((?!\s?"?\'?\s?data)(.*)\)#Usm',$code,$matches); |
|
311 | + preg_match_all('#url\((?!\s?"?\'?\s?data)(.*)\)#Usm', $code, $matches); |
|
312 | 312 | |
313 | - if(($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches))) { |
|
314 | - foreach($matches[1] as $count => $quotedurl) { |
|
315 | - $iurl = trim($quotedurl," \t\n\r\0\x0B\"'"); |
|
313 | + if (($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches))) { |
|
314 | + foreach ($matches[1] as $count => $quotedurl) { |
|
315 | + $iurl = trim($quotedurl, " \t\n\r\0\x0B\"'"); |
|
316 | 316 | |
317 | 317 | // if querystring, remove it from url |
318 | - if (strpos($iurl,'?') !== false) { $iurl = strtok($iurl,'?'); } |
|
318 | + if (strpos($iurl, '?') !== false) { $iurl = strtok($iurl, '?'); } |
|
319 | 319 | |
320 | 320 | $ipath = $this->getpath($iurl); |
321 | 321 | |
322 | 322 | $datauri_max_size = 4096; |
323 | - $datauri_max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', $datauri_max_size ); |
|
324 | - $datauri_exclude = apply_filters( 'autoptimize_filter_css_datauri_exclude', ""); |
|
323 | + $datauri_max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', $datauri_max_size); |
|
324 | + $datauri_exclude = apply_filters('autoptimize_filter_css_datauri_exclude', ""); |
|
325 | 325 | if (!empty($datauri_exclude)) { |
326 | - $no_datauris=array_filter(array_map('trim',explode(",",$datauri_exclude))); |
|
326 | + $no_datauris = array_filter(array_map('trim', explode(",", $datauri_exclude))); |
|
327 | 327 | foreach ($no_datauris as $no_datauri) { |
328 | - if (strpos($iurl,$no_datauri)!==false) { |
|
329 | - $ipath=false; |
|
328 | + if (strpos($iurl, $no_datauri) !== false) { |
|
329 | + $ipath = false; |
|
330 | 330 | break; |
331 | 331 | } |
332 | 332 | } |
333 | 333 | } |
334 | 334 | |
335 | - if($ipath != false && preg_match('#\.(jpe?g|png|gif|bmp)$#i',$ipath) && file_exists($ipath) && is_readable($ipath) && filesize($ipath) <= $datauri_max_size) { |
|
336 | - $ihash=md5($ipath); |
|
337 | - $icheck = new autoptimizeCache($ihash,'img'); |
|
338 | - if($icheck->check()) { |
|
335 | + if ($ipath != false && preg_match('#\.(jpe?g|png|gif|bmp)$#i', $ipath) && file_exists($ipath) && is_readable($ipath) && filesize($ipath) <= $datauri_max_size) { |
|
336 | + $ihash = md5($ipath); |
|
337 | + $icheck = new autoptimizeCache($ihash, 'img'); |
|
338 | + if ($icheck->check()) { |
|
339 | 339 | // we have the base64 image in cache |
340 | - $headAndData=$icheck->retrieve(); |
|
341 | - $_base64data=explode(";base64,",$headAndData); |
|
342 | - $base64data=$_base64data[1]; |
|
340 | + $headAndData = $icheck->retrieve(); |
|
341 | + $_base64data = explode(";base64,", $headAndData); |
|
342 | + $base64data = $_base64data[1]; |
|
343 | 343 | } else { |
344 | 344 | // It's an image and we don't have it in cache, get the type |
345 | - $explA=explode('.',$ipath); |
|
346 | - $type=end($explA); |
|
345 | + $explA = explode('.', $ipath); |
|
346 | + $type = end($explA); |
|
347 | 347 | |
348 | - switch($type) { |
|
348 | + switch ($type) { |
|
349 | 349 | case 'jpeg': |
350 | 350 | $dataurihead = 'data:image/jpeg;base64,'; |
351 | 351 | break; |
@@ -367,15 +367,15 @@ discard block |
||
367 | 367 | |
368 | 368 | // Encode the data |
369 | 369 | $base64data = base64_encode(file_get_contents($ipath)); |
370 | - $headAndData=$dataurihead.$base64data; |
|
370 | + $headAndData = $dataurihead.$base64data; |
|
371 | 371 | |
372 | 372 | // Save in cache |
373 | - $icheck->cache($headAndData,"text/plain"); |
|
373 | + $icheck->cache($headAndData, "text/plain"); |
|
374 | 374 | } |
375 | 375 | unset($icheck); |
376 | 376 | |
377 | 377 | // Add it to the list for replacement |
378 | - $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$headAndData,$matches[0][$count]).";\n*".str_replace($quotedurl,'mhtml:%%MHTML%%!'.$mhtmlcount,$matches[0][$count]).";\n_".$matches[0][$count].';'; |
|
378 | + $imgreplace[$matches[0][$count]] = str_replace($quotedurl, $headAndData, $matches[0][$count]).";\n*".str_replace($quotedurl, 'mhtml:%%MHTML%%!'.$mhtmlcount, $matches[0][$count]).";\n_".$matches[0][$count].';'; |
|
379 | 379 | |
380 | 380 | // Store image on the mhtml document |
381 | 381 | $this->mhtml .= "--_\r\nContent-Location:{$mhtmlcount}\r\nContent-Transfer-Encoding:base64\r\n\r\n{$base64data}\r\n"; |
@@ -383,52 +383,52 @@ discard block |
||
383 | 383 | } else { |
384 | 384 | // just cdn the URL if applicable |
385 | 385 | if (!empty($this->cdn_url)) { |
386 | - $url = trim($quotedurl," \t\n\r\0\x0B\"'"); |
|
387 | - $cdn_url=$this->url_replace_cdn($url); |
|
388 | - $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$cdn_url,$matches[0][$count]); |
|
386 | + $url = trim($quotedurl, " \t\n\r\0\x0B\"'"); |
|
387 | + $cdn_url = $this->url_replace_cdn($url); |
|
388 | + $imgreplace[$matches[0][$count]] = str_replace($quotedurl, $cdn_url, $matches[0][$count]); |
|
389 | 389 | } |
390 | 390 | } |
391 | 391 | } |
392 | 392 | } else if ((is_array($matches)) && (!empty($this->cdn_url))) { |
393 | 393 | // change background image urls to cdn-url |
394 | - foreach($matches[1] as $count => $quotedurl) { |
|
395 | - $url = trim($quotedurl," \t\n\r\0\x0B\"'"); |
|
396 | - $cdn_url=$this->url_replace_cdn($url); |
|
397 | - $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$cdn_url,$matches[0][$count]); |
|
394 | + foreach ($matches[1] as $count => $quotedurl) { |
|
395 | + $url = trim($quotedurl, " \t\n\r\0\x0B\"'"); |
|
396 | + $cdn_url = $this->url_replace_cdn($url); |
|
397 | + $imgreplace[$matches[0][$count]] = str_replace($quotedurl, $cdn_url, $matches[0][$count]); |
|
398 | 398 | } |
399 | 399 | } |
400 | 400 | |
401 | - if(!empty($imgreplace)) { |
|
402 | - $code = str_replace(array_keys($imgreplace),array_values($imgreplace),$code); |
|
401 | + if (!empty($imgreplace)) { |
|
402 | + $code = str_replace(array_keys($imgreplace), array_values($imgreplace), $code); |
|
403 | 403 | } |
404 | 404 | |
405 | 405 | // CDN the fonts! |
406 | - if ( (!empty($this->cdn_url)) && (apply_filters('autoptimize_filter_css_fonts_cdn',false)) && (version_compare(PHP_VERSION, '5.3.0') >= 0) ) { |
|
406 | + if ((!empty($this->cdn_url)) && (apply_filters('autoptimize_filter_css_fonts_cdn', false)) && (version_compare(PHP_VERSION, '5.3.0') >= 0)) { |
|
407 | 407 | $fontreplace = array(); |
408 | 408 | include_once(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizeFontRegex.php'); |
409 | 409 | |
410 | - preg_match_all($fonturl_regex,$code,$matches); |
|
410 | + preg_match_all($fonturl_regex, $code, $matches); |
|
411 | 411 | if (is_array($matches)) { |
412 | - foreach($matches[8] as $count => $quotedurl) { |
|
413 | - $url = trim($quotedurl," \t\n\r\0\x0B\"'"); |
|
414 | - $cdn_url=$this->url_replace_cdn($url); |
|
415 | - $fontreplace[$matches[8][$count]] = str_replace($quotedurl,$cdn_url,$matches[8][$count]); |
|
412 | + foreach ($matches[8] as $count => $quotedurl) { |
|
413 | + $url = trim($quotedurl, " \t\n\r\0\x0B\"'"); |
|
414 | + $cdn_url = $this->url_replace_cdn($url); |
|
415 | + $fontreplace[$matches[8][$count]] = str_replace($quotedurl, $cdn_url, $matches[8][$count]); |
|
416 | 416 | } |
417 | - if(!empty($fontreplace)) { |
|
418 | - $code = str_replace(array_keys($fontreplace),array_values($fontreplace),$code); |
|
417 | + if (!empty($fontreplace)) { |
|
418 | + $code = str_replace(array_keys($fontreplace), array_values($fontreplace), $code); |
|
419 | 419 | } |
420 | 420 | } |
421 | 421 | } |
422 | 422 | |
423 | 423 | // Minify |
424 | - if (($this->alreadyminified!==true) && (apply_filters( "autoptimize_css_do_minify", true))) { |
|
424 | + if (($this->alreadyminified !== true) && (apply_filters("autoptimize_css_do_minify", true))) { |
|
425 | 425 | if (class_exists('Minify_CSS_Compressor')) { |
426 | 426 | $tmp_code = trim(Minify_CSS_Compressor::process($code)); |
427 | - } else if(class_exists('CSSmin')) { |
|
427 | + } else if (class_exists('CSSmin')) { |
|
428 | 428 | $cssmin = new CSSmin(); |
429 | - if (method_exists($cssmin,"run")) { |
|
429 | + if (method_exists($cssmin, "run")) { |
|
430 | 430 | $tmp_code = trim($cssmin->run($code)); |
431 | - } elseif (@is_callable(array($cssmin,"minify"))) { |
|
431 | + } elseif (@is_callable(array($cssmin, "minify"))) { |
|
432 | 432 | $tmp_code = trim(CssMin::minify($code)); |
433 | 433 | } |
434 | 434 | } |
@@ -440,7 +440,7 @@ discard block |
||
440 | 440 | |
441 | 441 | $code = $this->inject_minified($code); |
442 | 442 | |
443 | - $tmp_code = apply_filters( 'autoptimize_css_after_minify',$code ); |
|
443 | + $tmp_code = apply_filters('autoptimize_css_after_minify', $code); |
|
444 | 444 | if (!empty($tmp_code)) { |
445 | 445 | $code = $tmp_code; |
446 | 446 | unset($tmp_code); |
@@ -454,31 +454,31 @@ discard block |
||
454 | 454 | |
455 | 455 | //Caches the CSS in uncompressed, deflated and gzipped form. |
456 | 456 | public function cache() { |
457 | - if($this->datauris) { |
|
457 | + if ($this->datauris) { |
|
458 | 458 | // MHTML Preparation |
459 | 459 | $this->mhtml = "/*\r\nContent-Type: multipart/related; boundary=\"_\"\r\n\r\n".$this->mhtml."*/\r\n"; |
460 | 460 | $md5 = md5($this->mhtml); |
461 | - $cache = new autoptimizeCache($md5,'txt'); |
|
462 | - if(!$cache->check()) { |
|
461 | + $cache = new autoptimizeCache($md5, 'txt'); |
|
462 | + if (!$cache->check()) { |
|
463 | 463 | // Cache our images for IE |
464 | - $cache->cache($this->mhtml,'text/plain'); |
|
464 | + $cache->cache($this->mhtml, 'text/plain'); |
|
465 | 465 | } |
466 | 466 | $mhtml = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
467 | 467 | } |
468 | 468 | |
469 | 469 | // CSS cache |
470 | - foreach($this->csscode as $media => $code) { |
|
470 | + foreach ($this->csscode as $media => $code) { |
|
471 | 471 | $md5 = $this->hashmap[md5($code)]; |
472 | 472 | |
473 | - if($this->datauris) { |
|
473 | + if ($this->datauris) { |
|
474 | 474 | // Images for ie! Get the right url |
475 | - $code = str_replace('%%MHTML%%',$mhtml,$code); |
|
475 | + $code = str_replace('%%MHTML%%', $mhtml, $code); |
|
476 | 476 | } |
477 | 477 | |
478 | - $cache = new autoptimizeCache($md5,'css'); |
|
479 | - if(!$cache->check()) { |
|
478 | + $cache = new autoptimizeCache($md5, 'css'); |
|
479 | + if (!$cache->check()) { |
|
480 | 480 | // Cache our code |
481 | - $cache->cache($code,'text/css'); |
|
481 | + $cache->cache($code, 'text/css'); |
|
482 | 482 | } |
483 | 483 | $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
484 | 484 | } |
@@ -493,7 +493,7 @@ discard block |
||
493 | 493 | $this->content = $this->restore_comments($this->content); |
494 | 494 | |
495 | 495 | // restore (no)script |
496 | - if ( strpos( $this->content, '%%SCRIPT%%' ) !== false ) { |
|
496 | + if (strpos($this->content, '%%SCRIPT%%') !== false) { |
|
497 | 497 | $this->content = preg_replace_callback( |
498 | 498 | '#%%SCRIPT%%(.*?)%%SCRIPT%%#is', |
499 | 499 | create_function( |
@@ -508,71 +508,71 @@ discard block |
||
508 | 508 | $this->content = $this->restore_noptimize($this->content); |
509 | 509 | |
510 | 510 | //Restore the full content |
511 | - if(!empty($this->restofcontent)) { |
|
511 | + if (!empty($this->restofcontent)) { |
|
512 | 512 | $this->content .= $this->restofcontent; |
513 | 513 | $this->restofcontent = ''; |
514 | 514 | } |
515 | 515 | |
516 | 516 | // Inject the new stylesheets |
517 | - $replaceTag = array("<title","before"); |
|
518 | - $replaceTag = apply_filters( 'autoptimize_filter_css_replacetag', $replaceTag ); |
|
517 | + $replaceTag = array("<title", "before"); |
|
518 | + $replaceTag = apply_filters('autoptimize_filter_css_replacetag', $replaceTag); |
|
519 | 519 | |
520 | 520 | if ($this->inline == true) { |
521 | - foreach($this->csscode as $media => $code) { |
|
522 | - $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>',$replaceTag); |
|
521 | + foreach ($this->csscode as $media => $code) { |
|
522 | + $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>', $replaceTag); |
|
523 | 523 | } |
524 | 524 | } else { |
525 | 525 | if ($this->defer == true) { |
526 | 526 | $deferredCssBlock = "<script data-cfasync='false'>function lCss(url,media) {var d=document;var l=d.createElement('link');l.rel='stylesheet';l.type='text/css';l.href=url;l.media=media;aoin=d.getElementById('aonoscrcss');if(window.location.href.indexOf('ao_nodeferredcss=1') === -1){aoin.parentNode.insertBefore(l,aoin.nextSibling);}}function deferredCSS() {"; |
527 | 527 | $noScriptCssBlock = "<noscript id=\"aonoscrcss\">"; |
528 | - $defer_inline_code=$this->defer_inline; |
|
529 | - if(!empty($defer_inline_code)){ |
|
530 | - if ( apply_filters( 'autoptimize_filter_css_critcss_minify',true ) ) { |
|
531 | - $iCssHash=md5($defer_inline_code); |
|
532 | - $iCssCache = new autoptimizeCache($iCssHash,'css'); |
|
533 | - if($iCssCache->check()) { |
|
528 | + $defer_inline_code = $this->defer_inline; |
|
529 | + if (!empty($defer_inline_code)) { |
|
530 | + if (apply_filters('autoptimize_filter_css_critcss_minify', true)) { |
|
531 | + $iCssHash = md5($defer_inline_code); |
|
532 | + $iCssCache = new autoptimizeCache($iCssHash, 'css'); |
|
533 | + if ($iCssCache->check()) { |
|
534 | 534 | // we have the optimized inline CSS in cache |
535 | - $defer_inline_code=$iCssCache->retrieve(); |
|
535 | + $defer_inline_code = $iCssCache->retrieve(); |
|
536 | 536 | } else { |
537 | 537 | if (class_exists('Minify_CSS_Compressor')) { |
538 | 538 | $tmp_code = trim(Minify_CSS_Compressor::process($defer_inline_code)); |
539 | - } else if(class_exists('CSSmin')) { |
|
539 | + } else if (class_exists('CSSmin')) { |
|
540 | 540 | $cssmin = new CSSmin(); |
541 | 541 | $tmp_code = trim($cssmin->run($defer_inline_code)); |
542 | 542 | } |
543 | 543 | if (!empty($tmp_code)) { |
544 | 544 | $defer_inline_code = $tmp_code; |
545 | - $iCssCache->cache($defer_inline_code,"text/css"); |
|
545 | + $iCssCache->cache($defer_inline_code, "text/css"); |
|
546 | 546 | unset($tmp_code); |
547 | 547 | } |
548 | 548 | } |
549 | 549 | } |
550 | - $code_out='<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>'; |
|
551 | - $this->inject_in_html($code_out,$replaceTag); |
|
550 | + $code_out = '<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>'; |
|
551 | + $this->inject_in_html($code_out, $replaceTag); |
|
552 | 552 | } |
553 | 553 | } |
554 | 554 | |
555 | - foreach($this->url as $media => $url) { |
|
555 | + foreach ($this->url as $media => $url) { |
|
556 | 556 | $url = $this->url_replace_cdn($url); |
557 | 557 | |
558 | 558 | //Add the stylesheet either deferred (import at bottom) or normal links in head |
559 | - if($this->defer == true) { |
|
559 | + if ($this->defer == true) { |
|
560 | 560 | $deferredCssBlock .= "lCss('".$url."','".$media."');"; |
561 | 561 | $noScriptCssBlock .= '<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />'; |
562 | 562 | } else { |
563 | 563 | if (strlen($this->csscode[$media]) > $this->cssinlinesize) { |
564 | - $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />',$replaceTag); |
|
565 | - } else if (strlen($this->csscode[$media])>0) { |
|
566 | - $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>',$replaceTag); |
|
564 | + $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />', $replaceTag); |
|
565 | + } else if (strlen($this->csscode[$media]) > 0) { |
|
566 | + $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>', $replaceTag); |
|
567 | 567 | } |
568 | 568 | } |
569 | 569 | } |
570 | 570 | |
571 | - if($this->defer == true) { |
|
571 | + if ($this->defer == true) { |
|
572 | 572 | $deferredCssBlock .= "}if(window.addEventListener){window.addEventListener('DOMContentLoaded',deferredCSS,false);}else{window.onload = deferredCSS;}</script>"; |
573 | 573 | $noScriptCssBlock .= "</noscript>"; |
574 | - $this->inject_in_html($noScriptCssBlock,$replaceTag); |
|
575 | - $this->inject_in_html($deferredCssBlock,array('</body>','before')); |
|
574 | + $this->inject_in_html($noScriptCssBlock, $replaceTag); |
|
575 | + $this->inject_in_html($deferredCssBlock, array('</body>', 'before')); |
|
576 | 576 | } |
577 | 577 | } |
578 | 578 | |
@@ -580,38 +580,38 @@ discard block |
||
580 | 580 | return $this->content; |
581 | 581 | } |
582 | 582 | |
583 | - static function fixurls($file,$code) { |
|
584 | - $file = str_replace(WP_ROOT_DIR,'',$file); |
|
585 | - $file = str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',$file); |
|
583 | + static function fixurls($file, $code) { |
|
584 | + $file = str_replace(WP_ROOT_DIR, '', $file); |
|
585 | + $file = str_replace(AUTOPTIMIZE_WP_CONTENT_NAME, '', $file); |
|
586 | 586 | $dir = dirname($file); // Like /themes/expound/css |
587 | 587 | |
588 | 588 | // quick fix for import-troubles in e.g. arras theme |
589 | - $code=preg_replace('#@import ("|\')(.+?)\.css("|\')#','@import url("${2}.css")',$code); |
|
589 | + $code = preg_replace('#@import ("|\')(.+?)\.css("|\')#', '@import url("${2}.css")', $code); |
|
590 | 590 | |
591 | - if(preg_match_all('#url\((?!data)(?!\#)(?!"\#)(.*)\)#Usi',$code,$matches)) { |
|
591 | + if (preg_match_all('#url\((?!data)(?!\#)(?!"\#)(.*)\)#Usi', $code, $matches)) { |
|
592 | 592 | $replace = array(); |
593 | - foreach($matches[1] as $k => $url) { |
|
593 | + foreach ($matches[1] as $k => $url) { |
|
594 | 594 | // Remove quotes |
595 | - $url = trim($url," \t\n\r\0\x0B\"'"); |
|
596 | - $noQurl = trim($url,"\"'"); |
|
595 | + $url = trim($url, " \t\n\r\0\x0B\"'"); |
|
596 | + $noQurl = trim($url, "\"'"); |
|
597 | 597 | |
598 | 598 | if ($noQurl === '') { continue; } |
599 | 599 | |
600 | - if ($url!==$noQurl) { |
|
601 | - $removedQuotes=true; |
|
600 | + if ($url !== $noQurl) { |
|
601 | + $removedQuotes = true; |
|
602 | 602 | } else { |
603 | - $removedQuotes=false; |
|
603 | + $removedQuotes = false; |
|
604 | 604 | } |
605 | - $url=$noQurl; |
|
606 | - if(substr($url,0,1)=='/' || preg_match('#^(https?://|ftp://|data:)#i',$url)) { |
|
605 | + $url = $noQurl; |
|
606 | + if (substr($url, 0, 1) == '/' || preg_match('#^(https?://|ftp://|data:)#i', $url)) { |
|
607 | 607 | //URL is absolute |
608 | 608 | continue; |
609 | 609 | } else { |
610 | 610 | // relative URL |
611 | - $newurl = preg_replace('/https?:/','',str_replace(" ","%20",AUTOPTIMIZE_WP_CONTENT_URL.str_replace('//','/',$dir.'/'.$url))); |
|
611 | + $newurl = preg_replace('/https?:/', '', str_replace(" ", "%20", AUTOPTIMIZE_WP_CONTENT_URL.str_replace('//', '/', $dir.'/'.$url))); |
|
612 | 612 | |
613 | 613 | $hash = md5($url); |
614 | - $code = str_replace($matches[0][$k],$hash,$code); |
|
614 | + $code = str_replace($matches[0][$k], $hash, $code); |
|
615 | 615 | |
616 | 616 | if (!empty($removedQuotes)) { |
617 | 617 | $replace[$hash] = 'url(\''.$newurl.'\')'; |
@@ -621,7 +621,7 @@ discard block |
||
621 | 621 | } |
622 | 622 | } |
623 | 623 | //Do the replacing here to avoid breaking URLs |
624 | - $code = str_replace(array_keys($replace),array_values($replace),$code); |
|
624 | + $code = str_replace(array_keys($replace), array_values($replace), $code); |
|
625 | 625 | } |
626 | 626 | return $code; |
627 | 627 | } |
@@ -629,7 +629,7 @@ discard block |
||
629 | 629 | private function ismovable($tag) { |
630 | 630 | if (!empty($this->whitelist)) { |
631 | 631 | foreach ($this->whitelist as $match) { |
632 | - if(strpos($tag,$match)!==false) { |
|
632 | + if (strpos($tag, $match) !== false) { |
|
633 | 633 | return true; |
634 | 634 | } |
635 | 635 | } |
@@ -637,8 +637,8 @@ discard block |
||
637 | 637 | return false; |
638 | 638 | } else { |
639 | 639 | if (is_array($this->dontmove)) { |
640 | - foreach($this->dontmove as $match) { |
|
641 | - if(strpos($tag,$match)!==false) { |
|
640 | + foreach ($this->dontmove as $match) { |
|
641 | + if (strpos($tag, $match) !== false) { |
|
642 | 642 | //Matched something |
643 | 643 | return false; |
644 | 644 | } |
@@ -650,17 +650,17 @@ discard block |
||
650 | 650 | } |
651 | 651 | } |
652 | 652 | |
653 | - private function can_inject_late($cssPath,$css) { |
|
654 | - if ((strpos($cssPath,"min.css")===false) || ($this->inject_min_late!==true)) { |
|
653 | + private function can_inject_late($cssPath, $css) { |
|
654 | + if ((strpos($cssPath, "min.css") === false) || ($this->inject_min_late !== true)) { |
|
655 | 655 | // late-inject turned off or file not minified based on filename |
656 | 656 | return false; |
657 | - } else if (strpos($css,"@import")!==false) { |
|
657 | + } else if (strpos($css, "@import") !== false) { |
|
658 | 658 | // can't late-inject files with imports as those need to be aggregated |
659 | 659 | return false; |
660 | - } else if ( (strpos($css,"@font-face")!==false ) && ( apply_filters("autoptimize_filter_css_fonts_cdn",false)===true) && (!empty($this->cdn_url)) ) { |
|
660 | + } else if ((strpos($css, "@font-face") !== false) && (apply_filters("autoptimize_filter_css_fonts_cdn", false) === true) && (!empty($this->cdn_url))) { |
|
661 | 661 | // don't late-inject CSS with font-src's if fonts are set to be CDN'ed |
662 | 662 | return false; |
663 | - } else if ( (($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui",$css) ) { |
|
663 | + } else if ((($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui", $css)) { |
|
664 | 664 | // don't late-inject CSS with images if CDN is set OR is image inlining is on |
665 | 665 | return false; |
666 | 666 | } else { |