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