@@ -179,32 +179,32 @@ discard block |
||
179 | 179 | // Remove the original style tag |
180 | 180 | $this->content = str_replace($tag,'',$this->content); |
181 | 181 | } else { |
182 | - // excluded CSS, minify if getpath |
|
183 | - if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) { |
|
184 | - $explUrl = explode('?',$source[2],2); |
|
182 | + // excluded CSS, minify if getpath |
|
183 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) { |
|
184 | + $explUrl = explode('?',$source[2],2); |
|
185 | 185 | $url = $explUrl[0]; |
186 | 186 | $path = $this->getpath($url); |
187 | 187 | |
188 | - if ($path && apply_filters('autoptimize_filter_css_minify_excluded',false)) { |
|
189 | - $_CachedMinifiedUrl = $this->minify_single($path); |
|
190 | - |
|
191 | - if (!empty($_CachedMinifiedUrl)) { |
|
192 | - // replace orig URL with URL to cache |
|
193 | - $newTag = str_replace($url, $_CachedMinifiedUrl, $tag); |
|
194 | - } else { |
|
195 | - $newTag = $tag; |
|
196 | - } |
|
188 | + if ($path && apply_filters('autoptimize_filter_css_minify_excluded',false)) { |
|
189 | + $_CachedMinifiedUrl = $this->minify_single($path); |
|
190 | + |
|
191 | + if (!empty($_CachedMinifiedUrl)) { |
|
192 | + // replace orig URL with URL to cache |
|
193 | + $newTag = str_replace($url, $_CachedMinifiedUrl, $tag); |
|
194 | + } else { |
|
195 | + $newTag = $tag; |
|
196 | + } |
|
197 | 197 | |
198 | - // remove querystring from URL |
|
199 | - if ( !empty($explUrl[1]) ) { |
|
200 | - $newTag = str_replace("?".$explUrl[1],"",$newTag); |
|
201 | - } |
|
202 | - |
|
203 | - // and replace |
|
204 | - $this->content = str_replace($tag,$newTag,$this->content); |
|
205 | - } |
|
206 | - } |
|
207 | - } |
|
198 | + // remove querystring from URL |
|
199 | + if ( !empty($explUrl[1]) ) { |
|
200 | + $newTag = str_replace("?".$explUrl[1],"",$newTag); |
|
201 | + } |
|
202 | + |
|
203 | + // and replace |
|
204 | + $this->content = str_replace($tag,$newTag,$this->content); |
|
205 | + } |
|
206 | + } |
|
207 | + } |
|
208 | 208 | } |
209 | 209 | return true; |
210 | 210 | } |
@@ -426,7 +426,7 @@ discard block |
||
426 | 426 | // just cdn the URL if applicable |
427 | 427 | if (!empty($this->cdn_url)) { |
428 | 428 | $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$this->url_replace_cdn($quotedurl),$matches[0][$count]); |
429 | - } |
|
429 | + } |
|
430 | 430 | } |
431 | 431 | } |
432 | 432 | } else if ((is_array($matches)) && (!empty($this->cdn_url))) { |
@@ -647,8 +647,8 @@ discard block |
||
647 | 647 | } |
648 | 648 | |
649 | 649 | private function ismovable($tag) { |
650 | - if ( apply_filters('autoptimize_filter_css_dontaggregate', false) ) { |
|
651 | - return false; |
|
650 | + if ( apply_filters('autoptimize_filter_css_dontaggregate', false) ) { |
|
651 | + return false; |
|
652 | 652 | } else if (!empty($this->whitelist)) { |
653 | 653 | foreach ($this->whitelist as $match) { |
654 | 654 | if(strpos($tag,$match)!==false) { |
@@ -673,13 +673,13 @@ discard block |
||
673 | 673 | } |
674 | 674 | |
675 | 675 | private function can_inject_late($cssPath,$css) { |
676 | - $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false, $cssPath); |
|
676 | + $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false, $cssPath); |
|
677 | 677 | if ( $this->inject_min_late !== true ) { |
678 | 678 | // late-inject turned off |
679 | 679 | return false; |
680 | 680 | } else if ( (strpos($cssPath,"min.css") === false) && ( str_replace($consider_minified_array, '', $cssPath) === $cssPath ) ) { |
681 | - // file not minified based on filename & filter |
|
682 | - return false; |
|
681 | + // file not minified based on filename & filter |
|
682 | + return false; |
|
683 | 683 | } else if ( strpos($css,"@import") !== false ) { |
684 | 684 | // can't late-inject files with imports as those need to be aggregated |
685 | 685 | return false; |
@@ -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 | |
@@ -40,12 +40,12 @@ discard block |
||
40 | 40 | |
41 | 41 | //Reads the page and collects style tags |
42 | 42 | public function read($options) { |
43 | - $noptimizeCSS = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content ); |
|
43 | + $noptimizeCSS = apply_filters('autoptimize_filter_css_noptimize', false, $this->content); |
|
44 | 44 | if ($noptimizeCSS) return false; |
45 | 45 | |
46 | - $whitelistCSS = apply_filters( 'autoptimize_filter_css_whitelist', '', $this->content ); |
|
46 | + $whitelistCSS = apply_filters('autoptimize_filter_css_whitelist', '', $this->content); |
|
47 | 47 | if (!empty($whitelistCSS)) { |
48 | - $this->whitelist = array_filter(array_map('trim',explode(",",$whitelistCSS))); |
|
48 | + $this->whitelist = array_filter(array_map('trim', explode(",", $whitelistCSS))); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | if ($options['nogooglefont'] == true) { |
@@ -53,33 +53,33 @@ discard block |
||
53 | 53 | } else { |
54 | 54 | $removableCSS = ""; |
55 | 55 | } |
56 | - $removableCSS = apply_filters( 'autoptimize_filter_css_removables', $removableCSS); |
|
56 | + $removableCSS = apply_filters('autoptimize_filter_css_removables', $removableCSS); |
|
57 | 57 | if (!empty($removableCSS)) { |
58 | - $this->cssremovables = array_filter(array_map('trim',explode(",",$removableCSS))); |
|
58 | + $this->cssremovables = array_filter(array_map('trim', explode(",", $removableCSS))); |
|
59 | 59 | } |
60 | 60 | |
61 | - $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize',256); |
|
61 | + $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize', 256); |
|
62 | 62 | |
63 | 63 | // filter to "late inject minified CSS", default to true for now (it is faster) |
64 | - $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late',true); |
|
64 | + $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late', true); |
|
65 | 65 | |
66 | 66 | // Remove everything that's not the header |
67 | - if ( apply_filters('autoptimize_filter_css_justhead',$options['justhead']) == true ) { |
|
68 | - $content = explode('</head>',$this->content,2); |
|
67 | + if (apply_filters('autoptimize_filter_css_justhead', $options['justhead']) == true) { |
|
68 | + $content = explode('</head>', $this->content, 2); |
|
69 | 69 | $this->content = $content[0].'</head>'; |
70 | 70 | $this->restofcontent = $content[1]; |
71 | 71 | } |
72 | 72 | |
73 | 73 | // include inline? |
74 | - if( apply_filters('autoptimize_css_include_inline',$options['include_inline']) == true ) { |
|
74 | + if (apply_filters('autoptimize_css_include_inline', $options['include_inline']) == true) { |
|
75 | 75 | $this->include_inline = true; |
76 | 76 | } |
77 | 77 | |
78 | 78 | // what CSS shouldn't be autoptimized |
79 | 79 | $excludeCSS = $options['css_exclude']; |
80 | - $excludeCSS = apply_filters( 'autoptimize_filter_css_exclude', $excludeCSS, $this->content ); |
|
81 | - if ($excludeCSS!=="") { |
|
82 | - $this->dontmove = array_filter(array_map('trim',explode(",",$excludeCSS))); |
|
80 | + $excludeCSS = apply_filters('autoptimize_filter_css_exclude', $excludeCSS, $this->content); |
|
81 | + if ($excludeCSS !== "") { |
|
82 | + $this->dontmove = array_filter(array_map('trim', explode(",", $excludeCSS))); |
|
83 | 83 | } else { |
84 | 84 | $this->dontmove = ""; |
85 | 85 | } |
@@ -87,17 +87,17 @@ discard block |
||
87 | 87 | // should we defer css? |
88 | 88 | // value: true/ false |
89 | 89 | $this->defer = $options['defer']; |
90 | - $this->defer = apply_filters( 'autoptimize_filter_css_defer', $this->defer, $this->content ); |
|
90 | + $this->defer = apply_filters('autoptimize_filter_css_defer', $this->defer, $this->content); |
|
91 | 91 | |
92 | 92 | // should we inline while deferring? |
93 | 93 | // value: inlined CSS |
94 | 94 | $this->defer_inline = $options['defer_inline']; |
95 | - $this->defer_inline = apply_filters( 'autoptimize_filter_css_defer_inline', $this->defer_inline, $this->content ); |
|
95 | + $this->defer_inline = apply_filters('autoptimize_filter_css_defer_inline', $this->defer_inline, $this->content); |
|
96 | 96 | |
97 | 97 | // should we inline? |
98 | 98 | // value: true/ false |
99 | 99 | $this->inline = $options['inline']; |
100 | - $this->inline = apply_filters( 'autoptimize_filter_css_inline', $this->inline, $this->content ); |
|
100 | + $this->inline = apply_filters('autoptimize_filter_css_inline', $this->inline, $this->content); |
|
101 | 101 | |
102 | 102 | // get cdn url |
103 | 103 | $this->cdn_url = $options['cdn_url']; |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | $this->content = $this->hide_noptimize($this->content); |
110 | 110 | |
111 | 111 | // exclude (no)script, as those may contain CSS which should be left as is |
112 | - if ( strpos( $this->content, '<script' ) !== false ) { |
|
112 | + if (strpos($this->content, '<script') !== false) { |
|
113 | 113 | $this->content = preg_replace_callback( |
114 | 114 | '#<(?:no)?script.*?<\/(?:no)?script>#is', |
115 | 115 | create_function( |
@@ -127,65 +127,65 @@ discard block |
||
127 | 127 | $this->content = $this->hide_comments($this->content); |
128 | 128 | |
129 | 129 | // Get <style> and <link> |
130 | - if(preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi',$this->content,$matches)) { |
|
131 | - foreach($matches[0] as $tag) { |
|
132 | - if ($this->isremovable($tag,$this->cssremovables)) { |
|
133 | - $this->content = str_replace($tag,'',$this->content); |
|
130 | + if (preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches)) { |
|
131 | + foreach ($matches[0] as $tag) { |
|
132 | + if ($this->isremovable($tag, $this->cssremovables)) { |
|
133 | + $this->content = str_replace($tag, '', $this->content); |
|
134 | 134 | } else if ($this->ismovable($tag)) { |
135 | 135 | // Get the media |
136 | - if(strpos($tag,'media=')!==false) { |
|
137 | - preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui',$tag,$medias); |
|
138 | - $medias = explode(',',$medias[1]); |
|
136 | + if (strpos($tag, 'media=') !== false) { |
|
137 | + preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias); |
|
138 | + $medias = explode(',', $medias[1]); |
|
139 | 139 | $media = array(); |
140 | - foreach($medias as $elem) { |
|
141 | - if (empty($elem)) { $elem="all"; } |
|
140 | + foreach ($medias as $elem) { |
|
141 | + if (empty($elem)) { $elem = "all"; } |
|
142 | 142 | $media[] = $elem; |
143 | 143 | } |
144 | 144 | } else { |
145 | 145 | // No media specified - applies to all |
146 | 146 | $media = array('all'); |
147 | 147 | } |
148 | - $media = apply_filters( 'autoptimize_filter_css_tagmedia',$media,$tag ); |
|
148 | + $media = apply_filters('autoptimize_filter_css_tagmedia', $media, $tag); |
|
149 | 149 | |
150 | - if(preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) { |
|
150 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
151 | 151 | // <link> |
152 | - $explUrl = explode('?',$source[2],2); |
|
152 | + $explUrl = explode('?', $source[2], 2); |
|
153 | 153 | $url = $explUrl[0]; |
154 | 154 | $path = $this->getpath($url); |
155 | 155 | |
156 | - if($path!==false && preg_match('#\.css$#',$path)) { |
|
156 | + if ($path !== false && preg_match('#\.css$#', $path)) { |
|
157 | 157 | // Good link |
158 | - $this->css[] = array($media,$path); |
|
159 | - }else{ |
|
158 | + $this->css[] = array($media, $path); |
|
159 | + } else { |
|
160 | 160 | // Link is dynamic (.php etc) |
161 | 161 | $tag = ''; |
162 | 162 | } |
163 | 163 | } else { |
164 | 164 | // inline css in style tags can be wrapped in comment tags, so restore comments |
165 | 165 | $tag = $this->restore_comments($tag); |
166 | - preg_match('#<style.*>(.*)</style>#Usmi',$tag,$code); |
|
166 | + preg_match('#<style.*>(.*)</style>#Usmi', $tag, $code); |
|
167 | 167 | |
168 | 168 | // and re-hide them to be able to to the removal based on tag |
169 | 169 | $tag = $this->hide_comments($tag); |
170 | 170 | |
171 | - if ( $this->include_inline ) { |
|
172 | - $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm','$1',$code[1]); |
|
173 | - $this->css[] = array($media,'INLINE;'.$code); |
|
171 | + if ($this->include_inline) { |
|
172 | + $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1]); |
|
173 | + $this->css[] = array($media, 'INLINE;'.$code); |
|
174 | 174 | } else { |
175 | 175 | $tag = ''; |
176 | 176 | } |
177 | 177 | } |
178 | 178 | |
179 | 179 | // Remove the original style tag |
180 | - $this->content = str_replace($tag,'',$this->content); |
|
180 | + $this->content = str_replace($tag, '', $this->content); |
|
181 | 181 | } else { |
182 | 182 | // excluded CSS, minify if getpath |
183 | - if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) { |
|
184 | - $explUrl = explode('?',$source[2],2); |
|
183 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
184 | + $explUrl = explode('?', $source[2], 2); |
|
185 | 185 | $url = $explUrl[0]; |
186 | 186 | $path = $this->getpath($url); |
187 | 187 | |
188 | - if ($path && apply_filters('autoptimize_filter_css_minify_excluded',false)) { |
|
188 | + if ($path && apply_filters('autoptimize_filter_css_minify_excluded', false)) { |
|
189 | 189 | $_CachedMinifiedUrl = $this->minify_single($path); |
190 | 190 | |
191 | 191 | if (!empty($_CachedMinifiedUrl)) { |
@@ -196,12 +196,12 @@ discard block |
||
196 | 196 | } |
197 | 197 | |
198 | 198 | // remove querystring from URL |
199 | - if ( !empty($explUrl[1]) ) { |
|
200 | - $newTag = str_replace("?".$explUrl[1],"",$newTag); |
|
199 | + if (!empty($explUrl[1])) { |
|
200 | + $newTag = str_replace("?".$explUrl[1], "", $newTag); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | // and replace |
204 | - $this->content = str_replace($tag,$newTag,$this->content); |
|
204 | + $this->content = str_replace($tag, $newTag, $this->content); |
|
205 | 205 | } |
206 | 206 | } |
207 | 207 | } |
@@ -214,31 +214,31 @@ discard block |
||
214 | 214 | |
215 | 215 | // Joins and optimizes CSS |
216 | 216 | public function minify() { |
217 | - foreach($this->css as $group) { |
|
218 | - list($media,$css) = $group; |
|
219 | - if(preg_match('#^INLINE;#',$css)) { |
|
217 | + foreach ($this->css as $group) { |
|
218 | + list($media, $css) = $group; |
|
219 | + if (preg_match('#^INLINE;#', $css)) { |
|
220 | 220 | // <style> |
221 | - $css = preg_replace('#^INLINE;#','',$css); |
|
222 | - $css = $this->fixurls(ABSPATH.'/index.php',$css); |
|
223 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, "" ); |
|
224 | - if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle) ) { |
|
225 | - $css=$tmpstyle; |
|
226 | - $this->alreadyminified=true; |
|
221 | + $css = preg_replace('#^INLINE;#', '', $css); |
|
222 | + $css = $this->fixurls(ABSPATH.'/index.php', $css); |
|
223 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, ""); |
|
224 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
225 | + $css = $tmpstyle; |
|
226 | + $this->alreadyminified = true; |
|
227 | 227 | } |
228 | 228 | } else { |
229 | 229 | //<link> |
230 | - if($css !== false && file_exists($css) && is_readable($css)) { |
|
230 | + if ($css !== false && file_exists($css) && is_readable($css)) { |
|
231 | 231 | $cssPath = $css; |
232 | 232 | $cssContents = file_get_contents($cssPath); |
233 | 233 | $cssHash = md5($cssContents); |
234 | - $css = $this->fixurls($cssPath,$cssContents); |
|
235 | - $css = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$css); |
|
236 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $cssPath ); |
|
234 | + $css = $this->fixurls($cssPath, $cssContents); |
|
235 | + $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css); |
|
236 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $cssPath); |
|
237 | 237 | if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
238 | - $css=$tmpstyle; |
|
239 | - $this->alreadyminified=true; |
|
240 | - } else if ($this->can_inject_late($cssPath,$css)) { |
|
241 | - $css="/*!%%INJECTLATER%%".base64_encode($cssPath)."|".$cssHash."%%INJECTLATER%%*/"; |
|
238 | + $css = $tmpstyle; |
|
239 | + $this->alreadyminified = true; |
|
240 | + } else if ($this->can_inject_late($cssPath, $css)) { |
|
241 | + $css = "/*!%%INJECTLATER%%".base64_encode($cssPath)."|".$cssHash."%%INJECTLATER%%*/"; |
|
242 | 242 | } |
243 | 243 | } else { |
244 | 244 | // Couldn't read CSS. Maybe getpath isn't working? |
@@ -246,8 +246,8 @@ discard block |
||
246 | 246 | } |
247 | 247 | } |
248 | 248 | |
249 | - foreach($media as $elem) { |
|
250 | - if(!isset($this->csscode[$elem])) |
|
249 | + foreach ($media as $elem) { |
|
250 | + if (!isset($this->csscode[$elem])) |
|
251 | 251 | $this->csscode[$elem] = ''; |
252 | 252 | $this->csscode[$elem] .= "\n/*FILESTART*/".$css; |
253 | 253 | } |
@@ -256,12 +256,12 @@ discard block |
||
256 | 256 | // Check for duplicate code |
257 | 257 | $md5list = array(); |
258 | 258 | $tmpcss = $this->csscode; |
259 | - foreach($tmpcss as $media => $code) { |
|
259 | + foreach ($tmpcss as $media => $code) { |
|
260 | 260 | $md5sum = md5($code); |
261 | 261 | $medianame = $media; |
262 | - foreach($md5list as $med => $sum) { |
|
262 | + foreach ($md5list as $med => $sum) { |
|
263 | 263 | // If same code |
264 | - if($sum === $md5sum) { |
|
264 | + if ($sum === $md5sum) { |
|
265 | 265 | //Add the merged code |
266 | 266 | $medianame = $med.', '.$media; |
267 | 267 | $this->csscode[$medianame] = $code; |
@@ -281,30 +281,30 @@ discard block |
||
281 | 281 | $external_imports = ""; |
282 | 282 | |
283 | 283 | // remove comments to avoid importing commented-out imports |
284 | - $thiscss_nocomments = preg_replace('#/\*.*\*/#Us','',$thiscss); |
|
284 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
285 | 285 | |
286 | - while(preg_match_all('#@import.*(?:;|$)#Um',$thiscss_nocomments,$matches)) { |
|
287 | - foreach($matches[0] as $import) { |
|
288 | - if ($this->isremovable($import,$this->cssremovables)) { |
|
289 | - $thiscss = str_replace($import,'',$thiscss); |
|
286 | + while (preg_match_all('#@import.*(?:;|$)#Um', $thiscss_nocomments, $matches)) { |
|
287 | + foreach ($matches[0] as $import) { |
|
288 | + if ($this->isremovable($import, $this->cssremovables)) { |
|
289 | + $thiscss = str_replace($import, '', $thiscss); |
|
290 | 290 | $import_ok = true; |
291 | 291 | } else { |
292 | - $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#','$1',trim($import))," \t\n\r\0\x0B\"'"); |
|
292 | + $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'"); |
|
293 | 293 | $path = $this->getpath($url); |
294 | 294 | $import_ok = false; |
295 | 295 | if (file_exists($path) && is_readable($path)) { |
296 | - $code = addcslashes($this->fixurls($path,file_get_contents($path)),"\\"); |
|
297 | - $code = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$code); |
|
298 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, "" ); |
|
299 | - if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
300 | - $code=$tmpstyle; |
|
301 | - $this->alreadyminified=true; |
|
302 | - } else if ($this->can_inject_late($path,$code)) { |
|
303 | - $code="/*!%%INJECTLATER".AUTOPTIMIZE_HASH."%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%*/"; |
|
296 | + $code = addcslashes($this->fixurls($path, file_get_contents($path)), "\\"); |
|
297 | + $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code); |
|
298 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, ""); |
|
299 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
300 | + $code = $tmpstyle; |
|
301 | + $this->alreadyminified = true; |
|
302 | + } else if ($this->can_inject_late($path, $code)) { |
|
303 | + $code = "/*!%%INJECTLATER".AUTOPTIMIZE_HASH."%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%*/"; |
|
304 | 304 | } |
305 | 305 | |
306 | - if(!empty($code)) { |
|
307 | - $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import,'#').'#Us','/*FILESTART2*/'.$code.'$1',$thiscss); |
|
306 | + if (!empty($code)) { |
|
307 | + $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss); |
|
308 | 308 | if (!empty($tmp_thiscss)) { |
309 | 309 | $thiscss = $tmp_thiscss; |
310 | 310 | $import_ok = true; |
@@ -318,31 +318,31 @@ discard block |
||
318 | 318 | if (!$import_ok) { |
319 | 319 | // external imports and general fall-back |
320 | 320 | $external_imports .= $import; |
321 | - $thiscss = str_replace($import,'',$thiscss); |
|
321 | + $thiscss = str_replace($import, '', $thiscss); |
|
322 | 322 | $fiximports = true; |
323 | 323 | } |
324 | 324 | } |
325 | - $thiscss = preg_replace('#/\*FILESTART\*/#','',$thiscss); |
|
326 | - $thiscss = preg_replace('#/\*FILESTART2\*/#','/*FILESTART*/',$thiscss); |
|
325 | + $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss); |
|
326 | + $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss); |
|
327 | 327 | |
328 | 328 | // and update $thiscss_nocomments before going into next iteration in while loop |
329 | - $thiscss_nocomments=preg_replace('#/\*.*\*/#Us','',$thiscss); |
|
329 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
330 | 330 | } |
331 | 331 | unset($thiscss_nocomments); |
332 | 332 | |
333 | 333 | // add external imports to top of aggregated CSS |
334 | - if($fiximports) { |
|
335 | - $thiscss=$external_imports.$thiscss; |
|
334 | + if ($fiximports) { |
|
335 | + $thiscss = $external_imports.$thiscss; |
|
336 | 336 | } |
337 | 337 | } |
338 | 338 | unset($thiscss); |
339 | 339 | |
340 | 340 | // $this->csscode has all the uncompressed code now. |
341 | - foreach($this->csscode as &$code) { |
|
341 | + foreach ($this->csscode as &$code) { |
|
342 | 342 | // Check for already-minified code |
343 | 343 | $hash = md5($code); |
344 | - $ccheck = new autoptimizeCache($hash,'css'); |
|
345 | - if($ccheck->check()) { |
|
344 | + $ccheck = new autoptimizeCache($hash, 'css'); |
|
345 | + if ($ccheck->check()) { |
|
346 | 346 | $code = $ccheck->retrieve(); |
347 | 347 | $this->hashmap[md5($code)] = $hash; |
348 | 348 | continue; |
@@ -354,44 +354,44 @@ discard block |
||
354 | 354 | |
355 | 355 | // Do the imaging! |
356 | 356 | $imgreplace = array(); |
357 | - preg_match_all( self::ASSETS_REGEX, $code, $matches ); |
|
357 | + preg_match_all(self::ASSETS_REGEX, $code, $matches); |
|
358 | 358 | |
359 | - if ( ($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches)) ) { |
|
360 | - foreach($matches[1] as $count => $quotedurl) { |
|
361 | - $iurl = trim($quotedurl," \t\n\r\0\x0B\"'"); |
|
359 | + if (($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches))) { |
|
360 | + foreach ($matches[1] as $count => $quotedurl) { |
|
361 | + $iurl = trim($quotedurl, " \t\n\r\0\x0B\"'"); |
|
362 | 362 | |
363 | 363 | // if querystring, remove it from url |
364 | - if (strpos($iurl,'?') !== false) { $iurl = strtok($iurl,'?'); } |
|
364 | + if (strpos($iurl, '?') !== false) { $iurl = strtok($iurl, '?'); } |
|
365 | 365 | |
366 | 366 | $ipath = $this->getpath($iurl); |
367 | 367 | |
368 | 368 | $datauri_max_size = 4096; |
369 | - $datauri_max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', $datauri_max_size ); |
|
370 | - $datauri_exclude = apply_filters( 'autoptimize_filter_css_datauri_exclude', ""); |
|
369 | + $datauri_max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', $datauri_max_size); |
|
370 | + $datauri_exclude = apply_filters('autoptimize_filter_css_datauri_exclude', ""); |
|
371 | 371 | if (!empty($datauri_exclude)) { |
372 | - $no_datauris=array_filter(array_map('trim',explode(",",$datauri_exclude))); |
|
372 | + $no_datauris = array_filter(array_map('trim', explode(",", $datauri_exclude))); |
|
373 | 373 | foreach ($no_datauris as $no_datauri) { |
374 | - if (strpos($iurl,$no_datauri)!==false) { |
|
375 | - $ipath=false; |
|
374 | + if (strpos($iurl, $no_datauri) !== false) { |
|
375 | + $ipath = false; |
|
376 | 376 | break; |
377 | 377 | } |
378 | 378 | } |
379 | 379 | } |
380 | 380 | |
381 | - if($ipath != false && preg_match('#\.(jpe?g|png|gif|bmp)$#i',$ipath) && file_exists($ipath) && is_readable($ipath) && filesize($ipath) <= $datauri_max_size) { |
|
382 | - $ihash=md5($ipath); |
|
383 | - $icheck = new autoptimizeCache($ihash,'img'); |
|
384 | - if($icheck->check()) { |
|
381 | + if ($ipath != false && preg_match('#\.(jpe?g|png|gif|bmp)$#i', $ipath) && file_exists($ipath) && is_readable($ipath) && filesize($ipath) <= $datauri_max_size) { |
|
382 | + $ihash = md5($ipath); |
|
383 | + $icheck = new autoptimizeCache($ihash, 'img'); |
|
384 | + if ($icheck->check()) { |
|
385 | 385 | // we have the base64 image in cache |
386 | - $headAndData=$icheck->retrieve(); |
|
387 | - $_base64data=explode(";base64,",$headAndData); |
|
388 | - $base64data=$_base64data[1]; |
|
386 | + $headAndData = $icheck->retrieve(); |
|
387 | + $_base64data = explode(";base64,", $headAndData); |
|
388 | + $base64data = $_base64data[1]; |
|
389 | 389 | } else { |
390 | 390 | // It's an image and we don't have it in cache, get the type |
391 | - $explA=explode('.',$ipath); |
|
392 | - $type=end($explA); |
|
391 | + $explA = explode('.', $ipath); |
|
392 | + $type = end($explA); |
|
393 | 393 | |
394 | - switch($type) { |
|
394 | + switch ($type) { |
|
395 | 395 | case 'jpeg': |
396 | 396 | $dataurihead = 'data:image/jpeg;base64,'; |
397 | 397 | break; |
@@ -413,45 +413,45 @@ discard block |
||
413 | 413 | |
414 | 414 | // Encode the data |
415 | 415 | $base64data = base64_encode(file_get_contents($ipath)); |
416 | - $headAndData=$dataurihead.$base64data; |
|
416 | + $headAndData = $dataurihead.$base64data; |
|
417 | 417 | |
418 | 418 | // Save in cache |
419 | - $icheck->cache($headAndData,"text/plain"); |
|
419 | + $icheck->cache($headAndData, "text/plain"); |
|
420 | 420 | } |
421 | 421 | unset($icheck); |
422 | 422 | |
423 | 423 | // Add it to the list for replacement |
424 | - $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$headAndData,$matches[0][$count]); |
|
424 | + $imgreplace[$matches[0][$count]] = str_replace($quotedurl, $headAndData, $matches[0][$count]); |
|
425 | 425 | } else { |
426 | 426 | // just cdn the URL if applicable |
427 | 427 | if (!empty($this->cdn_url)) { |
428 | - $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$this->url_replace_cdn($quotedurl),$matches[0][$count]); |
|
428 | + $imgreplace[$matches[0][$count]] = str_replace($quotedurl, $this->url_replace_cdn($quotedurl), $matches[0][$count]); |
|
429 | 429 | } |
430 | 430 | } |
431 | 431 | } |
432 | 432 | } else if ((is_array($matches)) && (!empty($this->cdn_url))) { |
433 | 433 | // change urls to cdn-url |
434 | - foreach($matches[1] as $count => $quotedurl) { |
|
435 | - $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$this->url_replace_cdn($quotedurl),$matches[0][$count]); |
|
434 | + foreach ($matches[1] as $count => $quotedurl) { |
|
435 | + $imgreplace[$matches[0][$count]] = str_replace($quotedurl, $this->url_replace_cdn($quotedurl), $matches[0][$count]); |
|
436 | 436 | } |
437 | 437 | } |
438 | 438 | |
439 | - if(!empty($imgreplace)) { |
|
440 | - $code = str_replace(array_keys($imgreplace),array_values($imgreplace),$code); |
|
439 | + if (!empty($imgreplace)) { |
|
440 | + $code = str_replace(array_keys($imgreplace), array_values($imgreplace), $code); |
|
441 | 441 | } |
442 | 442 | |
443 | 443 | // Replace back font-face markers with actual font-face declarations |
444 | 444 | $code = $this->restore_fontface($code); |
445 | 445 | |
446 | 446 | // Minify |
447 | - if (($this->alreadyminified!==true) && (apply_filters( "autoptimize_css_do_minify", true))) { |
|
447 | + if (($this->alreadyminified !== true) && (apply_filters("autoptimize_css_do_minify", true))) { |
|
448 | 448 | if (class_exists('Minify_CSS_Compressor')) { |
449 | 449 | $tmp_code = trim(Minify_CSS_Compressor::process($code)); |
450 | - } else if(class_exists('CSSmin')) { |
|
450 | + } else if (class_exists('CSSmin')) { |
|
451 | 451 | $cssmin = new CSSmin(); |
452 | - if (method_exists($cssmin,"run")) { |
|
452 | + if (method_exists($cssmin, "run")) { |
|
453 | 453 | $tmp_code = trim($cssmin->run($code)); |
454 | - } elseif (@is_callable(array($cssmin,"minify"))) { |
|
454 | + } elseif (@is_callable(array($cssmin, "minify"))) { |
|
455 | 455 | $tmp_code = trim(CssMin::minify($code)); |
456 | 456 | } |
457 | 457 | } |
@@ -463,7 +463,7 @@ discard block |
||
463 | 463 | |
464 | 464 | $code = $this->inject_minified($code); |
465 | 465 | |
466 | - $tmp_code = apply_filters( 'autoptimize_css_after_minify', $code ); |
|
466 | + $tmp_code = apply_filters('autoptimize_css_after_minify', $code); |
|
467 | 467 | if (!empty($tmp_code)) { |
468 | 468 | $code = $tmp_code; |
469 | 469 | unset($tmp_code); |
@@ -478,13 +478,13 @@ discard block |
||
478 | 478 | //Caches the CSS in uncompressed, deflated and gzipped form. |
479 | 479 | public function cache() { |
480 | 480 | // CSS cache |
481 | - foreach($this->csscode as $media => $code) { |
|
481 | + foreach ($this->csscode as $media => $code) { |
|
482 | 482 | $md5 = $this->hashmap[md5($code)]; |
483 | 483 | |
484 | - $cache = new autoptimizeCache($md5,'css'); |
|
485 | - if(!$cache->check()) { |
|
484 | + $cache = new autoptimizeCache($md5, 'css'); |
|
485 | + if (!$cache->check()) { |
|
486 | 486 | // Cache our code |
487 | - $cache->cache($code,'text/css'); |
|
487 | + $cache->cache($code, 'text/css'); |
|
488 | 488 | } |
489 | 489 | $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
490 | 490 | } |
@@ -499,7 +499,7 @@ discard block |
||
499 | 499 | $this->content = $this->restore_comments($this->content); |
500 | 500 | |
501 | 501 | // restore (no)script |
502 | - if ( strpos( $this->content, '%%SCRIPT%%' ) !== false ) { |
|
502 | + if (strpos($this->content, '%%SCRIPT%%') !== false) { |
|
503 | 503 | $this->content = preg_replace_callback( |
504 | 504 | '#%%SCRIPT'.AUTOPTIMIZE_HASH.'%%(.*?)%%SCRIPT%%#is', |
505 | 505 | create_function( |
@@ -514,74 +514,74 @@ discard block |
||
514 | 514 | $this->content = $this->restore_noptimize($this->content); |
515 | 515 | |
516 | 516 | //Restore the full content |
517 | - if(!empty($this->restofcontent)) { |
|
517 | + if (!empty($this->restofcontent)) { |
|
518 | 518 | $this->content .= $this->restofcontent; |
519 | 519 | $this->restofcontent = ''; |
520 | 520 | } |
521 | 521 | |
522 | 522 | // Inject the new stylesheets |
523 | - $replaceTag = array("<title","before"); |
|
524 | - $replaceTag = apply_filters( 'autoptimize_filter_css_replacetag', $replaceTag, $this->content ); |
|
523 | + $replaceTag = array("<title", "before"); |
|
524 | + $replaceTag = apply_filters('autoptimize_filter_css_replacetag', $replaceTag, $this->content); |
|
525 | 525 | |
526 | 526 | if ($this->inline == true) { |
527 | - foreach($this->csscode as $media => $code) { |
|
528 | - $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>',$replaceTag); |
|
527 | + foreach ($this->csscode as $media => $code) { |
|
528 | + $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>', $replaceTag); |
|
529 | 529 | } |
530 | 530 | } else { |
531 | 531 | if ($this->defer == true) { |
532 | 532 | $preloadCssBlock = ""; |
533 | 533 | $noScriptCssBlock = "<noscript id=\"aonoscrcss\">"; |
534 | - $defer_inline_code=$this->defer_inline; |
|
535 | - if(!empty($defer_inline_code)){ |
|
536 | - if ( apply_filters( 'autoptimize_filter_css_critcss_minify', true ) ) { |
|
534 | + $defer_inline_code = $this->defer_inline; |
|
535 | + if (!empty($defer_inline_code)) { |
|
536 | + if (apply_filters('autoptimize_filter_css_critcss_minify', true)) { |
|
537 | 537 | $iCssHash = md5($defer_inline_code); |
538 | - $iCssCache = new autoptimizeCache($iCssHash,'css'); |
|
539 | - if($iCssCache->check()) { |
|
538 | + $iCssCache = new autoptimizeCache($iCssHash, 'css'); |
|
539 | + if ($iCssCache->check()) { |
|
540 | 540 | // we have the optimized inline CSS in cache |
541 | - $defer_inline_code=$iCssCache->retrieve(); |
|
541 | + $defer_inline_code = $iCssCache->retrieve(); |
|
542 | 542 | } else { |
543 | 543 | if (class_exists('Minify_CSS_Compressor')) { |
544 | 544 | $tmp_code = trim(Minify_CSS_Compressor::process($defer_inline_code)); |
545 | - } else if(class_exists('CSSmin')) { |
|
545 | + } else if (class_exists('CSSmin')) { |
|
546 | 546 | $cssmin = new CSSmin(); |
547 | 547 | $tmp_code = trim($cssmin->run($defer_inline_code)); |
548 | 548 | } |
549 | 549 | if (!empty($tmp_code)) { |
550 | 550 | $defer_inline_code = $tmp_code; |
551 | - $iCssCache->cache($defer_inline_code,"text/css"); |
|
551 | + $iCssCache->cache($defer_inline_code, "text/css"); |
|
552 | 552 | unset($tmp_code); |
553 | 553 | } |
554 | 554 | } |
555 | 555 | } |
556 | - $code_out='<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>'; |
|
557 | - $this->inject_in_html($code_out,$replaceTag); |
|
556 | + $code_out = '<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>'; |
|
557 | + $this->inject_in_html($code_out, $replaceTag); |
|
558 | 558 | } |
559 | 559 | } |
560 | 560 | |
561 | - foreach($this->url as $media => $url) { |
|
561 | + foreach ($this->url as $media => $url) { |
|
562 | 562 | $url = $this->url_replace_cdn($url); |
563 | 563 | |
564 | 564 | //Add the stylesheet either deferred (import at bottom) or normal links in head |
565 | - if($this->defer == true) { |
|
565 | + if ($this->defer == true) { |
|
566 | 566 | $preloadCssBlock .= '<link rel="preload" as="style" media="'.$media.'" href="'.$url.'" onload="this.rel=\'stylesheet\'" />'; |
567 | 567 | $noScriptCssBlock .= '<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />'; |
568 | 568 | } else { |
569 | 569 | if (strlen($this->csscode[$media]) > $this->cssinlinesize) { |
570 | - $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />',$replaceTag); |
|
571 | - } else if (strlen($this->csscode[$media])>0) { |
|
572 | - $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>',$replaceTag); |
|
570 | + $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />', $replaceTag); |
|
571 | + } else if (strlen($this->csscode[$media]) > 0) { |
|
572 | + $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>', $replaceTag); |
|
573 | 573 | } |
574 | 574 | } |
575 | 575 | } |
576 | 576 | |
577 | - if($this->defer == true) { |
|
577 | + if ($this->defer == true) { |
|
578 | 578 | $preloadPolyfill = '<script data-cfasync=\'false\'>/*! loadCSS. [c]2017 Filament Group, Inc. MIT License */ |
579 | 579 | !function(a){"use strict";var b=function(b,c,d){function e(a){return h.body?a():void setTimeout(function(){e(a)})}function f(){i.addEventListener&&i.removeEventListener("load",f),i.media=d||"all"}var g,h=a.document,i=h.createElement("link");if(c)g=c;else{var j=(h.body||h.getElementsByTagName("head")[0]).childNodes;g=j[j.length-1]}var k=h.styleSheets;i.rel="stylesheet",i.href=b,i.media="only x",e(function(){g.parentNode.insertBefore(i,c?g:g.nextSibling)});var l=function(a){for(var b=i.href,c=k.length;c--;)if(k[c].href===b)return a();setTimeout(function(){l(a)})};return i.addEventListener&&i.addEventListener("load",f),i.onloadcssdefined=l,l(f),i};"undefined"!=typeof exports?exports.loadCSS=b:a.loadCSS=b}("undefined"!=typeof global?global:this); |
580 | 580 | /*! loadCSS rel=preload polyfill. [c]2017 Filament Group, Inc. MIT License */ |
581 | 581 | !function(a){if(a.loadCSS){var b=loadCSS.relpreload={};if(b.support=function(){try{return a.document.createElement("link").relList.supports("preload")}catch(b){return!1}},b.poly=function(){for(var b=a.document.getElementsByTagName("link"),c=0;c<b.length;c++){var d=b[c];"preload"===d.rel&&"style"===d.getAttribute("as")&&(a.loadCSS(d.href,d,d.getAttribute("media")),d.rel=null)}},!b.support()){b.poly();var c=a.setInterval(b.poly,300);a.addEventListener&&a.addEventListener("load",function(){b.poly(),a.clearInterval(c)}),a.attachEvent&&a.attachEvent("onload",function(){a.clearInterval(c)})}}}(this);</script>'; |
582 | 582 | $noScriptCssBlock .= "</noscript>"; |
583 | - $this->inject_in_html($preloadCssBlock.$noScriptCssBlock,$replaceTag); |
|
584 | - $this->inject_in_html($preloadPolyfill,array('</body>','before')); |
|
583 | + $this->inject_in_html($preloadCssBlock.$noScriptCssBlock, $replaceTag); |
|
584 | + $this->inject_in_html($preloadPolyfill, array('</body>', 'before')); |
|
585 | 585 | } |
586 | 586 | } |
587 | 587 | |
@@ -591,51 +591,51 @@ discard block |
||
591 | 591 | |
592 | 592 | static function fixurls($file, $code) { |
593 | 593 | // Switch all imports to the url() syntax |
594 | - $code = preg_replace( '#@import ("|\')(.+?)\.css.*("|\')#', '@import url("${2}.css")', $code ); |
|
594 | + $code = preg_replace('#@import ("|\')(.+?)\.css.*("|\')#', '@import url("${2}.css")', $code); |
|
595 | 595 | |
596 | - if ( preg_match_all( self::ASSETS_REGEX, $code, $matches ) ) { |
|
597 | - $file = str_replace( WP_ROOT_DIR, '/', $file ); |
|
598 | - $dir = dirname( $file ); // Like /themes/expound/css |
|
596 | + if (preg_match_all(self::ASSETS_REGEX, $code, $matches)) { |
|
597 | + $file = str_replace(WP_ROOT_DIR, '/', $file); |
|
598 | + $dir = dirname($file); // Like /themes/expound/css |
|
599 | 599 | |
600 | 600 | // $dir should not contain backslashes, since it's used to replace |
601 | 601 | // urls, but it can contain them when running on Windows because |
602 | 602 | // fixurls() is sometimes called with `ABSPATH . 'index.php'` |
603 | - $dir = str_replace( '\\', '/', $dir ); |
|
604 | - unset( $file ); // not used below at all |
|
603 | + $dir = str_replace('\\', '/', $dir); |
|
604 | + unset($file); // not used below at all |
|
605 | 605 | |
606 | 606 | $replace = array(); |
607 | - foreach ( $matches[1] as $k => $url ) { |
|
607 | + foreach ($matches[1] as $k => $url) { |
|
608 | 608 | // Remove quotes |
609 | - $url = trim( $url," \t\n\r\0\x0B\"'" ); |
|
610 | - $noQurl = trim( $url, "\"'" ); |
|
611 | - if ( $url !== $noQurl ) { |
|
609 | + $url = trim($url, " \t\n\r\0\x0B\"'"); |
|
610 | + $noQurl = trim($url, "\"'"); |
|
611 | + if ($url !== $noQurl) { |
|
612 | 612 | $removedQuotes = true; |
613 | 613 | } else { |
614 | 614 | $removedQuotes = false; |
615 | 615 | } |
616 | 616 | |
617 | - if ( '' === $noQurl ) { |
|
617 | + if ('' === $noQurl) { |
|
618 | 618 | continue; |
619 | 619 | } |
620 | 620 | |
621 | 621 | $url = $noQurl; |
622 | - if ( '/' === $url{0} || preg_match( '#^(https?://|ftp://|data:)#i', $url ) ) { |
|
622 | + if ('/' === $url{0} || preg_match('#^(https?://|ftp://|data:)#i', $url)) { |
|
623 | 623 | // URL is protocol-relative, host-relative or something we don't touch |
624 | 624 | continue; |
625 | 625 | } else { |
626 | 626 | // Relative URL |
627 | - $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_ROOT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) ); |
|
627 | + $newurl = preg_replace('/https?:/', '', str_replace(' ', '%20', AUTOPTIMIZE_WP_ROOT_URL.str_replace('//', '/', $dir.'/'.$url))); |
|
628 | 628 | |
629 | 629 | // Hash the url + whatever was behind potentially for replacement |
630 | 630 | // We must do this, or different css classes referencing the same bg image (but |
631 | 631 | // different parts of it, say, in sprites and such) loose their stuff... |
632 | - $hash = md5( $url . $matches[2][$k] ); |
|
633 | - $code = str_replace( $matches[0][$k], $hash, $code ); |
|
632 | + $hash = md5($url.$matches[2][$k]); |
|
633 | + $code = str_replace($matches[0][$k], $hash, $code); |
|
634 | 634 | |
635 | - if ( $removedQuotes ) { |
|
636 | - $replace[$hash] = "url('" . $newurl . "')" . $matches[2][$k]; |
|
635 | + if ($removedQuotes) { |
|
636 | + $replace[$hash] = "url('".$newurl."')".$matches[2][$k]; |
|
637 | 637 | } else { |
638 | - $replace[$hash] = 'url(' . $newurl . ')' . $matches[2][$k]; |
|
638 | + $replace[$hash] = 'url('.$newurl.')'.$matches[2][$k]; |
|
639 | 639 | } |
640 | 640 | } |
641 | 641 | } |
@@ -647,11 +647,11 @@ discard block |
||
647 | 647 | } |
648 | 648 | |
649 | 649 | private function ismovable($tag) { |
650 | - if ( apply_filters('autoptimize_filter_css_dontaggregate', false) ) { |
|
650 | + if (apply_filters('autoptimize_filter_css_dontaggregate', false)) { |
|
651 | 651 | return false; |
652 | 652 | } else if (!empty($this->whitelist)) { |
653 | 653 | foreach ($this->whitelist as $match) { |
654 | - if(strpos($tag,$match)!==false) { |
|
654 | + if (strpos($tag, $match) !== false) { |
|
655 | 655 | return true; |
656 | 656 | } |
657 | 657 | } |
@@ -659,8 +659,8 @@ discard block |
||
659 | 659 | return false; |
660 | 660 | } else { |
661 | 661 | if (is_array($this->dontmove)) { |
662 | - foreach($this->dontmove as $match) { |
|
663 | - if(strpos($tag,$match)!==false) { |
|
662 | + foreach ($this->dontmove as $match) { |
|
663 | + if (strpos($tag, $match) !== false) { |
|
664 | 664 | //Matched something |
665 | 665 | return false; |
666 | 666 | } |
@@ -672,21 +672,21 @@ discard block |
||
672 | 672 | } |
673 | 673 | } |
674 | 674 | |
675 | - private function can_inject_late($cssPath,$css) { |
|
675 | + private function can_inject_late($cssPath, $css) { |
|
676 | 676 | $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false, $cssPath); |
677 | - if ( $this->inject_min_late !== true ) { |
|
677 | + if ($this->inject_min_late !== true) { |
|
678 | 678 | // late-inject turned off |
679 | 679 | return false; |
680 | - } else if ( (strpos($cssPath,"min.css") === false) && ( str_replace($consider_minified_array, '', $cssPath) === $cssPath ) ) { |
|
680 | + } else if ((strpos($cssPath, "min.css") === false) && (str_replace($consider_minified_array, '', $cssPath) === $cssPath)) { |
|
681 | 681 | // file not minified based on filename & filter |
682 | 682 | return false; |
683 | - } else if ( strpos($css,"@import") !== false ) { |
|
683 | + } else if (strpos($css, "@import") !== false) { |
|
684 | 684 | // can't late-inject files with imports as those need to be aggregated |
685 | 685 | return false; |
686 | - } else if ( (strpos($css,"@font-face")!==false ) && ( apply_filters("autoptimize_filter_css_fonts_cdn",false)===true) && (!empty($this->cdn_url)) ) { |
|
686 | + } else if ((strpos($css, "@font-face") !== false) && (apply_filters("autoptimize_filter_css_fonts_cdn", false) === true) && (!empty($this->cdn_url))) { |
|
687 | 687 | // don't late-inject CSS with font-src's if fonts are set to be CDN'ed |
688 | 688 | return false; |
689 | - } else if ( (($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui",$css) ) { |
|
689 | + } else if ((($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui", $css)) { |
|
690 | 690 | // don't late-inject CSS with images if CDN is set OR is image inlining is on |
691 | 691 | return false; |
692 | 692 | } else { |
@@ -706,12 +706,12 @@ discard block |
||
706 | 706 | */ |
707 | 707 | protected function replace_longest_matches_first($string, $replacements = array()) |
708 | 708 | { |
709 | - if ( ! empty( $replacements ) ) { |
|
709 | + if (!empty($replacements)) { |
|
710 | 710 | // Sort the replacements array by key length in desc order (so that the longest strings are replaced first) |
711 | - $keys = array_map( 'strlen', array_keys( $replacements ) ); |
|
712 | - array_multisort( $keys, SORT_DESC, $replacements ); |
|
711 | + $keys = array_map('strlen', array_keys($replacements)); |
|
712 | + array_multisort($keys, SORT_DESC, $replacements); |
|
713 | 713 | // $this->debug_log($replacements); |
714 | - $string = str_replace( array_keys( $replacements ), array_values( $replacements ), $string ); |
|
714 | + $string = str_replace(array_keys($replacements), array_values($replacements), $string); |
|
715 | 715 | } |
716 | 716 | |
717 | 717 | return $string; |
@@ -728,16 +728,16 @@ discard block |
||
728 | 728 | */ |
729 | 729 | public function replace_urls($code = '') |
730 | 730 | { |
731 | - preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches ); |
|
732 | - if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) { |
|
733 | - foreach ( $url_src_matches[1] as $count => $original_url ) { |
|
731 | + preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches); |
|
732 | + if (is_array($url_src_matches) && !empty($url_src_matches)) { |
|
733 | + foreach ($url_src_matches[1] as $count => $original_url) { |
|
734 | 734 | // Removes quotes and other cruft |
735 | - $url = trim( $original_url, " \t\n\r\0\x0B\"'" ); |
|
735 | + $url = trim($original_url, " \t\n\r\0\x0B\"'"); |
|
736 | 736 | // Do CDN replacement if needed |
737 | - if ( ! empty( $this->cdn_url ) ) { |
|
737 | + if (!empty($this->cdn_url)) { |
|
738 | 738 | $replacement_url = $this->url_replace_cdn($url); |
739 | 739 | // Prepare replacements array |
740 | - $replacements[ $url_src_matches[1][ $count ] ] = str_replace( |
|
740 | + $replacements[$url_src_matches[1][$count]] = str_replace( |
|
741 | 741 | $original_url, $replacement_url, $url_src_matches[1][$count] |
742 | 742 | ); |
743 | 743 | } |
@@ -760,23 +760,23 @@ discard block |
||
760 | 760 | public function hide_fontface_and_maybe_cdn($code) |
761 | 761 | { |
762 | 762 | // Proceed only if @font-face declarations exist within $code |
763 | - preg_match_all( self::FONT_FACE_REGEX, $code, $fontfaces ); |
|
764 | - if ( isset( $fontfaces[0] ) ) { |
|
763 | + preg_match_all(self::FONT_FACE_REGEX, $code, $fontfaces); |
|
764 | + if (isset($fontfaces[0])) { |
|
765 | 765 | // Check if we need to cdn fonts or not |
766 | - $do_font_cdn = apply_filters( 'autoptimize_filter_css_fonts_cdn', false ); |
|
766 | + $do_font_cdn = apply_filters('autoptimize_filter_css_fonts_cdn', false); |
|
767 | 767 | |
768 | - foreach ( $fontfaces[0] as $full_match ) { |
|
768 | + foreach ($fontfaces[0] as $full_match) { |
|
769 | 769 | // Keep original match so we can search/replace it |
770 | 770 | $match_search = $full_match; |
771 | 771 | |
772 | 772 | // Do font cdn if needed |
773 | - if ( $do_font_cdn ) { |
|
773 | + if ($do_font_cdn) { |
|
774 | 774 | $full_match = $this->replace_urls($full_match); |
775 | 775 | } |
776 | 776 | |
777 | 777 | // Replace declaration with its base64 encoded string |
778 | 778 | $replacement = self::build_marker('FONTFACE', $full_match); |
779 | - $code = str_replace( $match_search, $replacement, $code ); |
|
779 | + $code = str_replace($match_search, $replacement, $code); |
|
780 | 780 | } |
781 | 781 | } |
782 | 782 |
@@ -343,43 +343,43 @@ discard block |
||
343 | 343 | } |
344 | 344 | |
345 | 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"; |
|
352 | - $codeMime="text/css"; |
|
353 | - } else { |
|
354 | - return false; |
|
355 | - } |
|
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"; |
|
352 | + $codeMime="text/css"; |
|
353 | + } else { |
|
354 | + return false; |
|
355 | + } |
|
356 | 356 | |
357 | - // if min.js or min.css return false |
|
358 | - if (( $this->str_ends_in($pathIn,"-min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,".min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,"js/jquery/jquery.js") === true ) ) { |
|
359 | - return false; |
|
360 | - } |
|
357 | + // if min.js or min.css return false |
|
358 | + if (( $this->str_ends_in($pathIn,"-min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,".min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,"js/jquery/jquery.js") === true ) ) { |
|
359 | + return false; |
|
360 | + } |
|
361 | 361 | |
362 | - // read file, return false if empty |
|
363 | - $_toMinify = file_get_contents($pathIn); |
|
364 | - if ( empty($_toMinify) ) return false; |
|
362 | + // read file, return false if empty |
|
363 | + $_toMinify = file_get_contents($pathIn); |
|
364 | + if ( empty($_toMinify) ) return false; |
|
365 | 365 | |
366 | - // check cache |
|
367 | - $_md5hash = "single_".md5($_toMinify); |
|
368 | - $_cache = new autoptimizeCache($_md5hash,$codeType); |
|
369 | - if ($_cache->check() ) { |
|
370 | - $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
|
371 | - } else { |
|
372 | - // if not in cache first minify |
|
373 | - $_Minified = $_toMinify; |
|
374 | - if ($codeType === "js") { |
|
375 | - if (class_exists('JSMin') && apply_filters( 'autoptimize_js_do_minify' , true)) { |
|
376 | - if (@is_callable(array("JSMin","minify"))) { |
|
377 | - $tmp_code = trim(JSMin::minify($_toMinify)); |
|
378 | - } |
|
379 | - } |
|
380 | - } else if ($codeType === "css") { |
|
366 | + // check cache |
|
367 | + $_md5hash = "single_".md5($_toMinify); |
|
368 | + $_cache = new autoptimizeCache($_md5hash,$codeType); |
|
369 | + if ($_cache->check() ) { |
|
370 | + $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
|
371 | + } else { |
|
372 | + // if not in cache first minify |
|
373 | + $_Minified = $_toMinify; |
|
374 | + if ($codeType === "js") { |
|
375 | + if (class_exists('JSMin') && apply_filters( 'autoptimize_js_do_minify' , true)) { |
|
376 | + if (@is_callable(array("JSMin","minify"))) { |
|
377 | + $tmp_code = trim(JSMin::minify($_toMinify)); |
|
378 | + } |
|
379 | + } |
|
380 | + } else if ($codeType === "css") { |
|
381 | 381 | if (class_exists('Minify_CSS_Compressor')) { |
382 | - $tmp_code = trim(Minify_CSS_Compressor::process($_toMinify)); |
|
382 | + $tmp_code = trim(Minify_CSS_Compressor::process($_toMinify)); |
|
383 | 383 | } else if(class_exists('CSSmin')) { |
384 | 384 | $cssmin = new CSSmin(); |
385 | 385 | if (method_exists($cssmin,"run")) { |
@@ -388,33 +388,33 @@ discard block |
||
388 | 388 | $tmp_code = trim(CssMin::minify($_toMinify)); |
389 | 389 | } |
390 | 390 | } |
391 | - } |
|
392 | - if (!empty($tmp_code)) { |
|
393 | - $_Minified = $tmp_code; |
|
394 | - unset($tmp_code); |
|
395 | - } |
|
396 | - // and then cache |
|
397 | - $_cache->cache($_Minified,$codeMime); |
|
398 | - $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
|
399 | - } |
|
400 | - unset($_cache); |
|
391 | + } |
|
392 | + if (!empty($tmp_code)) { |
|
393 | + $_Minified = $tmp_code; |
|
394 | + unset($tmp_code); |
|
395 | + } |
|
396 | + // and then cache |
|
397 | + $_cache->cache($_Minified,$codeMime); |
|
398 | + $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
|
399 | + } |
|
400 | + unset($_cache); |
|
401 | 401 | |
402 | - // if CDN, then CDN |
|
403 | - $_CachedMinifiedUrl = $this->url_replace_cdn($_CachedMinifiedUrl); |
|
402 | + // if CDN, then CDN |
|
403 | + $_CachedMinifiedUrl = $this->url_replace_cdn($_CachedMinifiedUrl); |
|
404 | 404 | |
405 | - return $_CachedMinifiedUrl; |
|
406 | - } |
|
405 | + return $_CachedMinifiedUrl; |
|
406 | + } |
|
407 | 407 | |
408 | - protected function str_ends_in($haystack,$needle) { |
|
409 | - $needleLength = strlen($needle); |
|
410 | - $haystackLength = strlen($haystack); |
|
411 | - $lastPos=strrpos($haystack,$needle); |
|
412 | - if ($lastPos === $haystackLength - $needleLength) { |
|
413 | - return true; |
|
414 | - } else { |
|
415 | - return false; |
|
416 | - } |
|
417 | - } |
|
408 | + protected function str_ends_in($haystack,$needle) { |
|
409 | + $needleLength = strlen($needle); |
|
410 | + $haystackLength = strlen($haystack); |
|
411 | + $lastPos=strrpos($haystack,$needle); |
|
412 | + if ($lastPos === $haystackLength - $needleLength) { |
|
413 | + return true; |
|
414 | + } else { |
|
415 | + return false; |
|
416 | + } |
|
417 | + } |
|
418 | 418 | |
419 | 419 | /** |
420 | 420 | * Specialized method to create the INJECTLATER marker. |
@@ -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 | abstract class autoptimizeBase { |
5 | 5 | protected $content = ''; |
@@ -23,37 +23,37 @@ discard block |
||
23 | 23 | |
24 | 24 | //Converts an URL to a full path |
25 | 25 | protected function getpath($url) { |
26 | - $url=apply_filters( 'autoptimize_filter_cssjs_alter_url', $url); |
|
26 | + $url = apply_filters('autoptimize_filter_cssjs_alter_url', $url); |
|
27 | 27 | |
28 | - if (strpos($url,'%')!==false) { |
|
29 | - $url=urldecode($url); |
|
28 | + if (strpos($url, '%') !== false) { |
|
29 | + $url = urldecode($url); |
|
30 | 30 | } |
31 | 31 | |
32 | - $siteHost=parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST); |
|
33 | - $contentHost=parse_url(AUTOPTIMIZE_WP_ROOT_URL,PHP_URL_HOST); |
|
32 | + $siteHost = parse_url(AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST); |
|
33 | + $contentHost = parse_url(AUTOPTIMIZE_WP_ROOT_URL, PHP_URL_HOST); |
|
34 | 34 | |
35 | 35 | // normalize |
36 | - if (strpos($url,'//')===0) { |
|
36 | + if (strpos($url, '//') === 0) { |
|
37 | 37 | if (is_ssl()) { |
38 | 38 | $url = "https:".$url; |
39 | 39 | } else { |
40 | 40 | $url = "http:".$url; |
41 | 41 | } |
42 | - } else if ((strpos($url,'//')===false) && (strpos($url,$siteHost)===false)) { |
|
42 | + } else if ((strpos($url, '//') === false) && (strpos($url, $siteHost) === false)) { |
|
43 | 43 | if (AUTOPTIMIZE_WP_SITE_URL === $siteHost) { |
44 | 44 | $url = AUTOPTIMIZE_WP_SITE_URL.$url; |
45 | 45 | } else { |
46 | - $subdir_levels=substr_count(preg_replace("/https?:\/\//","",AUTOPTIMIZE_WP_SITE_URL),"/"); |
|
47 | - $url = AUTOPTIMIZE_WP_SITE_URL.str_repeat("/..",$subdir_levels).$url; |
|
46 | + $subdir_levels = substr_count(preg_replace("/https?:\/\//", "", AUTOPTIMIZE_WP_SITE_URL), "/"); |
|
47 | + $url = AUTOPTIMIZE_WP_SITE_URL.str_repeat("/..", $subdir_levels).$url; |
|
48 | 48 | } |
49 | 49 | } |
50 | 50 | |
51 | 51 | if ($siteHost !== $contentHost) { |
52 | - $url=str_replace(AUTOPTIMIZE_WP_CONTENT_URL,AUTOPTIMIZE_WP_SITE_URL.AUTOPTIMIZE_WP_CONTENT_NAME,$url); |
|
52 | + $url = str_replace(AUTOPTIMIZE_WP_CONTENT_URL, AUTOPTIMIZE_WP_SITE_URL.AUTOPTIMIZE_WP_CONTENT_NAME, $url); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | // first check; hostname wp site should be hostname of url |
56 | - $thisHost=@parse_url($url,PHP_URL_HOST); |
|
56 | + $thisHost = @parse_url($url, PHP_URL_HOST); |
|
57 | 57 | if ($thisHost !== $siteHost) { |
58 | 58 | /* |
59 | 59 | * first try to get all domains from WPML (if available) |
@@ -65,18 +65,18 @@ discard block |
||
65 | 65 | |
66 | 66 | $multidomainsWPML = apply_filters('wpml_setting', array(), 'language_domains'); |
67 | 67 | if (!empty($multidomainsWPML)) { |
68 | - $multidomains = array_map(array($this,"ao_getDomain"),$multidomainsWPML); |
|
68 | + $multidomains = array_map(array($this, "ao_getDomain"), $multidomainsWPML); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | if (!empty($this->cdn_url)) { |
72 | - $multidomains[]=parse_url($this->cdn_url,PHP_URL_HOST); |
|
72 | + $multidomains[] = parse_url($this->cdn_url, PHP_URL_HOST); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | $multidomains = apply_filters('autoptimize_filter_cssjs_multidomain', $multidomains); |
76 | 76 | |
77 | 77 | if (!empty($multidomains)) { |
78 | - if (in_array($thisHost,$multidomains)) { |
|
79 | - $url=str_replace($thisHost, parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST), $url); |
|
78 | + if (in_array($thisHost, $multidomains)) { |
|
79 | + $url = str_replace($thisHost, parse_url(AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST), $url); |
|
80 | 80 | } else { |
81 | 81 | return false; |
82 | 82 | } |
@@ -86,22 +86,22 @@ discard block |
||
86 | 86 | } |
87 | 87 | |
88 | 88 | // try to remove "wp root url" from url while not minding http<>https |
89 | - $tmp_ao_root = preg_replace('/https?:/','',AUTOPTIMIZE_WP_ROOT_URL); |
|
89 | + $tmp_ao_root = preg_replace('/https?:/', '', AUTOPTIMIZE_WP_ROOT_URL); |
|
90 | 90 | if ($siteHost !== $contentHost) { |
91 | 91 | // as we replaced the content-domain with the site-domain, we should match against that |
92 | - $tmp_ao_root = preg_replace('/https?:/','',AUTOPTIMIZE_WP_SITE_URL); |
|
92 | + $tmp_ao_root = preg_replace('/https?:/', '', AUTOPTIMIZE_WP_SITE_URL); |
|
93 | 93 | } |
94 | - $tmp_url = preg_replace('/https?:/','',$url); |
|
95 | - $path = str_replace($tmp_ao_root,'',$tmp_url); |
|
94 | + $tmp_url = preg_replace('/https?:/', '', $url); |
|
95 | + $path = str_replace($tmp_ao_root, '', $tmp_url); |
|
96 | 96 | |
97 | 97 | // if path starts with :// or //, this is not a URL in the WP context and we have to assume we can't aggregate |
98 | - if (preg_match('#^:?//#',$path)) { |
|
98 | + if (preg_match('#^:?//#', $path)) { |
|
99 | 99 | /** External script/css (adsense, etc) */ |
100 | 100 | return false; |
101 | 101 | } |
102 | 102 | |
103 | 103 | // prepend with WP_ROOT_DIR to have full path to file |
104 | - $path = str_replace('//','/',WP_ROOT_DIR.$path); |
|
104 | + $path = str_replace('//', '/', WP_ROOT_DIR.$path); |
|
105 | 105 | |
106 | 106 | // final check: does file exist and is it readable |
107 | 107 | if (file_exists($path) && is_file($path) && is_readable($path)) { |
@@ -114,25 +114,25 @@ discard block |
||
114 | 114 | // needed for WPML-filter |
115 | 115 | protected function ao_getDomain($in) { |
116 | 116 | // make sure the url starts with something vaguely resembling a protocol |
117 | - if ((strpos($in,"http")!==0) && (strpos($in,"//")!==0)) { |
|
118 | - $in="http://".$in; |
|
117 | + if ((strpos($in, "http") !== 0) && (strpos($in, "//") !== 0)) { |
|
118 | + $in = "http://".$in; |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | // do the actual parse_url |
122 | - $out = parse_url($in,PHP_URL_HOST); |
|
122 | + $out = parse_url($in, PHP_URL_HOST); |
|
123 | 123 | |
124 | 124 | // fallback if parse_url does not understand the url is in fact a url |
125 | - if (empty($out)) $out=$in; |
|
125 | + if (empty($out)) $out = $in; |
|
126 | 126 | |
127 | 127 | return $out; |
128 | 128 | } |
129 | 129 | |
130 | 130 | |
131 | 131 | // logger |
132 | - protected function ao_logger($logmsg,$appendHTML=true) { |
|
132 | + protected function ao_logger($logmsg, $appendHTML = true) { |
|
133 | 133 | if ($appendHTML) { |
134 | - $logmsg="<!--noptimize--><!-- ".$logmsg." --><!--/noptimize-->"; |
|
135 | - $this->content.=$logmsg; |
|
134 | + $logmsg = "<!--noptimize--><!-- ".$logmsg." --><!--/noptimize-->"; |
|
135 | + $this->content .= $logmsg; |
|
136 | 136 | } else { |
137 | 137 | error_log("Autoptimize: ".$logmsg); |
138 | 138 | } |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | |
141 | 141 | // hide everything between noptimize-comment tags |
142 | 142 | protected function hide_noptimize($noptimize_in) { |
143 | - if ( preg_match( '/<!--\s?noptimize\s?-->/', $noptimize_in ) ) { |
|
143 | + if (preg_match('/<!--\s?noptimize\s?-->/', $noptimize_in)) { |
|
144 | 144 | $noptimize_out = preg_replace_callback( |
145 | 145 | '#<!--\s?noptimize\s?-->.*?<!--\s?/\s?noptimize\s?-->#is', |
146 | 146 | create_function( |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | |
158 | 158 | // unhide noptimize-tags |
159 | 159 | protected function restore_noptimize($noptimize_in) { |
160 | - if ( strpos( $noptimize_in, '%%NOPTIMIZE%%' ) !== false ) { |
|
160 | + if (strpos($noptimize_in, '%%NOPTIMIZE%%') !== false) { |
|
161 | 161 | $noptimize_out = preg_replace_callback( |
162 | 162 | '#%%NOPTIMIZE'.AUTOPTIMIZE_HASH.'%%(.*?)%%NOPTIMIZE%%#is', |
163 | 163 | create_function( |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | } |
174 | 174 | |
175 | 175 | protected function hide_iehacks($iehacks_in) { |
176 | - if ( strpos( $iehacks_in, '<!--[if' ) !== false ) { |
|
176 | + if (strpos($iehacks_in, '<!--[if') !== false) { |
|
177 | 177 | $iehacks_out = preg_replace_callback( |
178 | 178 | '#<!--\[if.*?\[endif\]-->#is', |
179 | 179 | create_function( |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | } |
190 | 190 | |
191 | 191 | protected function restore_iehacks($iehacks_in) { |
192 | - if ( strpos( $iehacks_in, '%%IEHACK%%' ) !== false ) { |
|
192 | + if (strpos($iehacks_in, '%%IEHACK%%') !== false) { |
|
193 | 193 | $iehacks_out = preg_replace_callback( |
194 | 194 | '#%%IEHACK'.AUTOPTIMIZE_HASH.'%%(.*?)%%IEHACK%%#is', |
195 | 195 | create_function( |
@@ -199,13 +199,13 @@ discard block |
||
199 | 199 | $iehacks_in |
200 | 200 | ); |
201 | 201 | } else { |
202 | - $iehacks_out=$iehacks_in; |
|
202 | + $iehacks_out = $iehacks_in; |
|
203 | 203 | } |
204 | 204 | return $iehacks_out; |
205 | 205 | } |
206 | 206 | |
207 | 207 | protected function hide_comments($comments_in) { |
208 | - if ( strpos( $comments_in, '<!--' ) !== false ) { |
|
208 | + if (strpos($comments_in, '<!--') !== false) { |
|
209 | 209 | $comments_out = preg_replace_callback( |
210 | 210 | '#<!--.*?-->#is', |
211 | 211 | create_function( |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | } |
222 | 222 | |
223 | 223 | protected function restore_comments($comments_in) { |
224 | - if ( strpos( $comments_in, '%%COMMENTS%%' ) !== false ) { |
|
224 | + if (strpos($comments_in, '%%COMMENTS%%') !== false) { |
|
225 | 225 | $comments_out = preg_replace_callback( |
226 | 226 | '#%%COMMENTS'.AUTOPTIMIZE_HASH.'%%(.*?)%%COMMENTS%%#is', |
227 | 227 | create_function( |
@@ -231,31 +231,31 @@ discard block |
||
231 | 231 | $comments_in |
232 | 232 | ); |
233 | 233 | } else { |
234 | - $comments_out=$comments_in; |
|
234 | + $comments_out = $comments_in; |
|
235 | 235 | } |
236 | 236 | return $comments_out; |
237 | 237 | } |
238 | 238 | |
239 | - protected function url_replace_cdn( $url ) { |
|
239 | + protected function url_replace_cdn($url) { |
|
240 | 240 | // API filter to change base CDN URL |
241 | - $cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', $this->cdn_url ); |
|
241 | + $cdn_url = apply_filters('autoptimize_filter_base_cdnurl', $this->cdn_url); |
|
242 | 242 | |
243 | - if ( !empty($cdn_url) ) { |
|
243 | + if (!empty($cdn_url)) { |
|
244 | 244 | // prepend domain-less absolute URL's |
245 | - if ( ( substr( $url, 0, 1 ) === '/' ) && ( substr( $url, 1, 1 ) !== '/' ) ) { |
|
246 | - $url = rtrim( $cdn_url, '/' ) . $url; |
|
245 | + if ((substr($url, 0, 1) === '/') && (substr($url, 1, 1) !== '/')) { |
|
246 | + $url = rtrim($cdn_url, '/').$url; |
|
247 | 247 | } else { |
248 | 248 | // get wordpress base URL |
249 | - $WPSiteBreakdown = parse_url( AUTOPTIMIZE_WP_SITE_URL ); |
|
250 | - $WPBaseUrl = $WPSiteBreakdown['scheme'] . '://' . $WPSiteBreakdown['host']; |
|
251 | - if ( ! empty( $WPSiteBreakdown['port'] ) ) { |
|
252 | - $WPBaseUrl .= ":" . $WPSiteBreakdown['port']; |
|
249 | + $WPSiteBreakdown = parse_url(AUTOPTIMIZE_WP_SITE_URL); |
|
250 | + $WPBaseUrl = $WPSiteBreakdown['scheme'].'://'.$WPSiteBreakdown['host']; |
|
251 | + if (!empty($WPSiteBreakdown['port'])) { |
|
252 | + $WPBaseUrl .= ":".$WPSiteBreakdown['port']; |
|
253 | 253 | } |
254 | 254 | // replace full url's with scheme |
255 | - $tmp_url = str_replace( $WPBaseUrl, rtrim( $cdn_url, '/' ), $url ); |
|
256 | - if ( $tmp_url === $url ) { |
|
255 | + $tmp_url = str_replace($WPBaseUrl, rtrim($cdn_url, '/'), $url); |
|
256 | + if ($tmp_url === $url) { |
|
257 | 257 | // last attempt; replace scheme-less URL's |
258 | - $url = str_replace( preg_replace( '/https?:/', '', $WPBaseUrl ), rtrim( $cdn_url, '/' ), $url ); |
|
258 | + $url = str_replace(preg_replace('/https?:/', '', $WPBaseUrl), rtrim($cdn_url, '/'), $url); |
|
259 | 259 | } else { |
260 | 260 | $url = $tmp_url; |
261 | 261 | } |
@@ -263,32 +263,32 @@ discard block |
||
263 | 263 | } |
264 | 264 | |
265 | 265 | // allow API filter to alter URL after CDN replacement |
266 | - $url = apply_filters( 'autoptimize_filter_base_replace_cdn', $url ); |
|
266 | + $url = apply_filters('autoptimize_filter_base_replace_cdn', $url); |
|
267 | 267 | return $url; |
268 | 268 | } |
269 | 269 | |
270 | - protected function inject_in_html($payload,$replaceTag) { |
|
271 | - if (strpos($this->content,$replaceTag[0])!== false) { |
|
272 | - if ($replaceTag[1]==="after") { |
|
273 | - $replaceBlock=$replaceTag[0].$payload; |
|
274 | - } else if ($replaceTag[1]==="replace"){ |
|
275 | - $replaceBlock=$payload; |
|
270 | + protected function inject_in_html($payload, $replaceTag) { |
|
271 | + if (strpos($this->content, $replaceTag[0]) !== false) { |
|
272 | + if ($replaceTag[1] === "after") { |
|
273 | + $replaceBlock = $replaceTag[0].$payload; |
|
274 | + } else if ($replaceTag[1] === "replace") { |
|
275 | + $replaceBlock = $payload; |
|
276 | 276 | } else { |
277 | - $replaceBlock=$payload.$replaceTag[0]; |
|
277 | + $replaceBlock = $payload.$replaceTag[0]; |
|
278 | 278 | } |
279 | - $this->content = substr_replace($this->content,$replaceBlock,strpos($this->content,$replaceTag[0]),strlen($replaceTag[0])); |
|
279 | + $this->content = substr_replace($this->content, $replaceBlock, strpos($this->content, $replaceTag[0]), strlen($replaceTag[0])); |
|
280 | 280 | } else { |
281 | 281 | $this->content .= $payload; |
282 | 282 | if (!$this->tagWarning) { |
283 | - $this->content .= "<!--noptimize--><!-- Autoptimize found a problem with the HTML in your Theme, tag \"".str_replace(array("<",">"),"",$replaceTag[0])."\" missing --><!--/noptimize-->"; |
|
284 | - $this->tagWarning=true; |
|
283 | + $this->content .= "<!--noptimize--><!-- Autoptimize found a problem with the HTML in your Theme, tag \"".str_replace(array("<", ">"), "", $replaceTag[0])."\" missing --><!--/noptimize-->"; |
|
284 | + $this->tagWarning = true; |
|
285 | 285 | } |
286 | 286 | } |
287 | 287 | } |
288 | 288 | |
289 | 289 | protected function isremovable($tag, $removables) { |
290 | 290 | foreach ($removables as $match) { |
291 | - if (strpos($tag,$match)!==false) { |
|
291 | + if (strpos($tag, $match) !== false) { |
|
292 | 292 | return true; |
293 | 293 | } |
294 | 294 | } |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | |
298 | 298 | // inject already minified code in optimized JS/CSS |
299 | 299 | protected function inject_minified($in) { |
300 | - if ( strpos( $in, '%%INJECTLATER%%' ) !== false ) { |
|
300 | + if (strpos($in, '%%INJECTLATER%%') !== false) { |
|
301 | 301 | $out = preg_replace_callback( |
302 | 302 | '#\/\*\!%%INJECTLATER'.AUTOPTIMIZE_HASH.'%%(.*?)%%INJECTLATER%%\*\/#is', |
303 | 303 | create_function( |
@@ -344,47 +344,47 @@ discard block |
||
344 | 344 | |
345 | 345 | protected function minify_single($pathIn) { |
346 | 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"; |
|
352 | - $codeMime="text/css"; |
|
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"; |
|
352 | + $codeMime = "text/css"; |
|
353 | 353 | } else { |
354 | 354 | return false; |
355 | 355 | } |
356 | 356 | |
357 | 357 | // if min.js or min.css return false |
358 | - if (( $this->str_ends_in($pathIn,"-min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,".min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,"js/jquery/jquery.js") === true ) ) { |
|
358 | + if (($this->str_ends_in($pathIn, "-min.".$codeType) === true) || ($this->str_ends_in($pathIn, ".min.".$codeType) === true) || ($this->str_ends_in($pathIn, "js/jquery/jquery.js") === true)) { |
|
359 | 359 | return false; |
360 | 360 | } |
361 | 361 | |
362 | 362 | // read file, return false if empty |
363 | 363 | $_toMinify = file_get_contents($pathIn); |
364 | - if ( empty($_toMinify) ) return false; |
|
364 | + if (empty($_toMinify)) return false; |
|
365 | 365 | |
366 | 366 | // check cache |
367 | 367 | $_md5hash = "single_".md5($_toMinify); |
368 | - $_cache = new autoptimizeCache($_md5hash,$codeType); |
|
369 | - if ($_cache->check() ) { |
|
368 | + $_cache = new autoptimizeCache($_md5hash, $codeType); |
|
369 | + if ($_cache->check()) { |
|
370 | 370 | $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
371 | 371 | } else { |
372 | 372 | // if not in cache first minify |
373 | 373 | $_Minified = $_toMinify; |
374 | 374 | if ($codeType === "js") { |
375 | - if (class_exists('JSMin') && apply_filters( 'autoptimize_js_do_minify' , true)) { |
|
376 | - if (@is_callable(array("JSMin","minify"))) { |
|
375 | + if (class_exists('JSMin') && apply_filters('autoptimize_js_do_minify', true)) { |
|
376 | + if (@is_callable(array("JSMin", "minify"))) { |
|
377 | 377 | $tmp_code = trim(JSMin::minify($_toMinify)); |
378 | 378 | } |
379 | 379 | } |
380 | 380 | } else if ($codeType === "css") { |
381 | 381 | if (class_exists('Minify_CSS_Compressor')) { |
382 | 382 | $tmp_code = trim(Minify_CSS_Compressor::process($_toMinify)); |
383 | - } else if(class_exists('CSSmin')) { |
|
383 | + } else if (class_exists('CSSmin')) { |
|
384 | 384 | $cssmin = new CSSmin(); |
385 | - if (method_exists($cssmin,"run")) { |
|
385 | + if (method_exists($cssmin, "run")) { |
|
386 | 386 | $tmp_code = trim($cssmin->run($_toMinify)); |
387 | - } elseif (@is_callable(array($cssmin,"minify"))) { |
|
387 | + } elseif (@is_callable(array($cssmin, "minify"))) { |
|
388 | 388 | $tmp_code = trim(CssMin::minify($_toMinify)); |
389 | 389 | } |
390 | 390 | } |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | unset($tmp_code); |
395 | 395 | } |
396 | 396 | // and then cache |
397 | - $_cache->cache($_Minified,$codeMime); |
|
397 | + $_cache->cache($_Minified, $codeMime); |
|
398 | 398 | $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
399 | 399 | } |
400 | 400 | unset($_cache); |
@@ -405,10 +405,10 @@ discard block |
||
405 | 405 | return $_CachedMinifiedUrl; |
406 | 406 | } |
407 | 407 | |
408 | - protected function str_ends_in($haystack,$needle) { |
|
408 | + protected function str_ends_in($haystack, $needle) { |
|
409 | 409 | $needleLength = strlen($needle); |
410 | 410 | $haystackLength = strlen($haystack); |
411 | - $lastPos=strrpos($haystack,$needle); |
|
411 | + $lastPos = strrpos($haystack, $needle); |
|
412 | 412 | if ($lastPos === $haystackLength - $needleLength) { |
413 | 413 | return true; |
414 | 414 | } else { |
@@ -428,7 +428,7 @@ discard block |
||
428 | 428 | */ |
429 | 429 | public static function build_injectlater_marker($filepath, $hash) |
430 | 430 | { |
431 | - $contents = '/*!' . self::build_marker('INJECTLATER', $filepath, $hash) . '*/'; |
|
431 | + $contents = '/*!'.self::build_marker('INJECTLATER', $filepath, $hash).'*/'; |
|
432 | 432 | return $contents; |
433 | 433 | } |
434 | 434 | |
@@ -449,13 +449,13 @@ discard block |
||
449 | 449 | { |
450 | 450 | // $name = strtoupper($name); |
451 | 451 | // Start the marker, add the data |
452 | - $marker = '%%' . $name . AUTOPTIMIZE_HASH . '%%' . base64_encode( $data ); |
|
452 | + $marker = '%%'.$name.AUTOPTIMIZE_HASH.'%%'.base64_encode($data); |
|
453 | 453 | // Add the hash if provided |
454 | - if ( null !== $hash ) { |
|
455 | - $marker .= '|' . $hash; |
|
454 | + if (null !== $hash) { |
|
455 | + $marker .= '|'.$hash; |
|
456 | 456 | } |
457 | 457 | // Close the marker |
458 | - $marker .= '%%' . $name . '%%'; |
|
458 | + $marker .= '%%'.$name.'%%'; |
|
459 | 459 | return $marker; |
460 | 460 | } |
461 | 461 | |
@@ -468,8 +468,8 @@ discard block |
||
468 | 468 | */ |
469 | 469 | protected function str_is_valid_regex($string) |
470 | 470 | { |
471 | - set_error_handler( function() {}, E_WARNING ); |
|
472 | - $is_regex = ( false !== preg_match( $string, '' ) ); |
|
471 | + set_error_handler(function() {}, E_WARNING); |
|
472 | + $is_regex = (false !== preg_match($string, '')); |
|
473 | 473 | restore_error_handler(); |
474 | 474 | return $is_regex; |
475 | 475 | } |
@@ -493,17 +493,17 @@ discard block |
||
493 | 493 | { |
494 | 494 | $found = false; |
495 | 495 | $is_regex = $this->str_is_valid_regex($search); |
496 | - if ( $is_regex ) { |
|
497 | - $found = preg_match( $search, $content ); |
|
496 | + if ($is_regex) { |
|
497 | + $found = preg_match($search, $content); |
|
498 | 498 | } else { |
499 | - $found = ( false !== strpos( $content, $search ) ); |
|
499 | + $found = (false !== strpos($content, $search)); |
|
500 | 500 | } |
501 | - if ( $found ) { |
|
501 | + if ($found) { |
|
502 | 502 | $content = preg_replace_callback( |
503 | 503 | $re_replace_pattern, |
504 | 504 | create_function( |
505 | 505 | '$matches', |
506 | - 'return autoptimizeBase::build_marker("' . $marker . '", $matches[0]);' |
|
506 | + 'return autoptimizeBase::build_marker("'.$marker.'", $matches[0]);' |
|
507 | 507 | ), |
508 | 508 | $content |
509 | 509 | ); |
@@ -520,9 +520,9 @@ discard block |
||
520 | 520 | */ |
521 | 521 | protected function restore_marked_content($marker, $content) |
522 | 522 | { |
523 | - if ( false !== strpos( $content, $marker ) ) { |
|
523 | + if (false !== strpos($content, $marker)) { |
|
524 | 524 | $content = preg_replace_callback( |
525 | - '#%%' . $marker . AUTOPTIMIZE_HASH . '%%(.*?)%%' . $marker . '%%#is', |
|
525 | + '#%%'.$marker.AUTOPTIMIZE_HASH.'%%(.*?)%%'.$marker.'%%#is', |
|
526 | 526 | create_function( |
527 | 527 | '$matches', |
528 | 528 | 'return base64_decode($matches[1]);' |