Completed
Pull Request — master (#80)
by
unknown
02:17
created
classes/autoptimizeStyles.php 2 patches
Spacing   +179 added lines, -179 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2
+if (!defined('ABSPATH')) exit; // Exit if accessed directly
3 3
 
4 4
 class autoptimizeStyles extends autoptimizeBase {
5 5
     private $css = array();
@@ -21,12 +21,12 @@  discard block
 block discarded – undo
21 21
 
22 22
     //Reads the page and collects style tags
23 23
     public function read($options) {
24
-        $noptimizeCSS = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content );
24
+        $noptimizeCSS = apply_filters('autoptimize_filter_css_noptimize', false, $this->content);
25 25
         if ($noptimizeCSS) return false;
26 26
 
27
-        $whitelistCSS = apply_filters( 'autoptimize_filter_css_whitelist', '' );
27
+        $whitelistCSS = apply_filters('autoptimize_filter_css_whitelist', '');
28 28
         if (!empty($whitelistCSS)) {
29
-            $this->whitelist = array_filter(array_map('trim',explode(",",$whitelistCSS)));
29
+            $this->whitelist = array_filter(array_map('trim', explode(",", $whitelistCSS)));
30 30
         }
31 31
         
32 32
         if ($options['nogooglefont'] == true) {
@@ -34,33 +34,33 @@  discard block
 block discarded – undo
34 34
         } else {
35 35
             $removableCSS = "";
36 36
         }
37
-        $removableCSS = apply_filters( 'autoptimize_filter_css_removables', $removableCSS);
37
+        $removableCSS = apply_filters('autoptimize_filter_css_removables', $removableCSS);
38 38
         if (!empty($removableCSS)) {
39
-            $this->cssremovables = array_filter(array_map('trim',explode(",",$removableCSS)));
39
+            $this->cssremovables = array_filter(array_map('trim', explode(",", $removableCSS)));
40 40
         }
41 41
 
42
-        $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize',256);
42
+        $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize', 256);
43 43
 
44 44
         // filter to "late inject minified CSS", default to true for now (it is faster)
45
-        $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late',true);
45
+        $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late', true);
46 46
 
47 47
         // Remove everything that's not the header
48
-        if ( apply_filters('autoptimize_filter_css_justhead',$options['justhead']) == true ) {
49
-            $content = explode('</head>',$this->content,2);
48
+        if (apply_filters('autoptimize_filter_css_justhead', $options['justhead']) == true) {
49
+            $content = explode('</head>', $this->content, 2);
50 50
             $this->content = $content[0].'</head>';
51 51
             $this->restofcontent = $content[1];
52 52
         }
53 53
 
54 54
         // include inline?
55
-        if( apply_filters('autoptimize_css_include_inline',$options['include_inline']) == true ) {
55
+        if (apply_filters('autoptimize_css_include_inline', $options['include_inline']) == true) {
56 56
             $this->include_inline = true;
57 57
         }
58 58
         
59 59
         // what CSS shouldn't be autoptimized
60 60
         $excludeCSS = $options['css_exclude'];
61
-        $excludeCSS = apply_filters( 'autoptimize_filter_css_exclude', $excludeCSS );
62
-        if ($excludeCSS!=="") {
63
-            $this->dontmove = array_filter(array_map('trim',explode(",",$excludeCSS)));
61
+        $excludeCSS = apply_filters('autoptimize_filter_css_exclude', $excludeCSS);
62
+        if ($excludeCSS !== "") {
63
+            $this->dontmove = array_filter(array_map('trim', explode(",", $excludeCSS)));
64 64
         } else {
65 65
             $this->dontmove = "";
66 66
         }
@@ -68,17 +68,17 @@  discard block
 block discarded – undo
68 68
         // should we defer css?
69 69
         // value: true/ false
70 70
         $this->defer = $options['defer'];
71
-        $this->defer = apply_filters( 'autoptimize_filter_css_defer', $this->defer );
71
+        $this->defer = apply_filters('autoptimize_filter_css_defer', $this->defer);
72 72
 
73 73
         // should we inline while deferring?
74 74
         // value: inlined CSS
75 75
         $this->defer_inline = $options['defer_inline'];
76
-        $this->defer_inline = apply_filters( 'autoptimize_filter_css_defer_inline', $this->defer_inline );
76
+        $this->defer_inline = apply_filters('autoptimize_filter_css_defer_inline', $this->defer_inline);
77 77
 
78 78
         // should we inline?
79 79
         // value: true/ false
80 80
         $this->inline = $options['inline'];
81
-        $this->inline = apply_filters( 'autoptimize_filter_css_inline', $this->inline );
81
+        $this->inline = apply_filters('autoptimize_filter_css_inline', $this->inline);
82 82
         
83 83
         // get cdn url
84 84
         $this->cdn_url = $options['cdn_url'];
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
         $this->content = $this->hide_noptimize($this->content);
91 91
         
92 92
         // exclude (no)script, as those may contain CSS which should be left as is
93
-        if ( strpos( $this->content, '<script' ) !== false ) { 
93
+        if (strpos($this->content, '<script') !== false) { 
94 94
             $this->content = preg_replace_callback(
95 95
                 '#<(?:no)?script.*?<\/(?:no)?script>#is',
96 96
                 create_function(
@@ -108,56 +108,56 @@  discard block
 block discarded – undo
108 108
         $this->content = $this->hide_comments($this->content);
109 109
         
110 110
         // Get <style> and <link>
111
-        if(preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi',$this->content,$matches)) {
112
-            foreach($matches[0] as $tag) {
113
-                if ($this->isremovable($tag,$this->cssremovables)) {
114
-                    $this->content = str_replace($tag,'',$this->content);
111
+        if (preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches)) {
112
+            foreach ($matches[0] as $tag) {
113
+                if ($this->isremovable($tag, $this->cssremovables)) {
114
+                    $this->content = str_replace($tag, '', $this->content);
115 115
                 } else if ($this->ismovable($tag)) {
116 116
                     // Get the media
117
-                    if(strpos($tag,'media=')!==false) {
118
-                        preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui',$tag,$medias);
119
-                        $medias = explode(',',$medias[1]);
117
+                    if (strpos($tag, 'media=') !== false) {
118
+                        preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias);
119
+                        $medias = explode(',', $medias[1]);
120 120
                         $media = array();
121
-                        foreach($medias as $elem) {
122
-                            if (empty($elem)) { $elem="all"; }
121
+                        foreach ($medias as $elem) {
122
+                            if (empty($elem)) { $elem = "all"; }
123 123
                             $media[] = $elem;
124 124
                         }
125 125
                     } else {
126 126
                         // No media specified - applies to all
127 127
                         $media = array('all');
128 128
                     }
129
-                    $media = apply_filters( 'autoptimize_filter_css_tagmedia',$media,$tag );
129
+                    $media = apply_filters('autoptimize_filter_css_tagmedia', $media, $tag);
130 130
                 
131
-                    if(preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) {
131
+                    if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) {
132 132
                         // <link>
133
-                        $url = current(explode('?',$source[2],2));
133
+                        $url = current(explode('?', $source[2], 2));
134 134
                         $path = $this->getpath($url);
135 135
                         
136
-                        if($path!==false && preg_match('#\.css$#',$path)) {
136
+                        if ($path !== false && preg_match('#\.css$#', $path)) {
137 137
                             // Good link
138
-                            $this->css[] = array($media,$path);
139
-                        }else{
138
+                            $this->css[] = array($media, $path);
139
+                        } else {
140 140
                             // Link is dynamic (.php etc)
141 141
                             $tag = '';
142 142
                         }
143 143
                     } else {
144 144
                         // inline css in style tags can be wrapped in comment tags, so restore comments
145 145
                         $tag = $this->restore_comments($tag);
146
-                        preg_match('#<style.*>(.*)</style>#Usmi',$tag,$code);
146
+                        preg_match('#<style.*>(.*)</style>#Usmi', $tag, $code);
147 147
 
148 148
                         // and re-hide them to be able to to the removal based on tag
149 149
                         $tag = $this->hide_comments($tag);
150 150
 
151
-                        if ( $this->include_inline ) {
152
-                            $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm','$1',$code[1]);
153
-                            $this->css[] = array($media,'INLINE;'.$code);
151
+                        if ($this->include_inline) {
152
+                            $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1]);
153
+                            $this->css[] = array($media, 'INLINE;'.$code);
154 154
                         } else {
155 155
                             $tag = '';
156 156
                         }
157 157
                     }
158 158
                     
159 159
                     // Remove the original style tag
160
-                    $this->content = str_replace($tag,'',$this->content);
160
+                    $this->content = str_replace($tag, '', $this->content);
161 161
                 }
162 162
             }
163 163
             return true;
@@ -169,29 +169,29 @@  discard block
 block discarded – undo
169 169
     // Joins and optimizes CSS
170 170
     public function minify() {
171 171
 
172
-        foreach($this->css as $group) {
173
-            list($media,$css) = $group;
174
-            if(preg_match('#^INLINE;#',$css)) {
172
+        foreach ($this->css as $group) {
173
+            list($media, $css) = $group;
174
+            if (preg_match('#^INLINE;#', $css)) {
175 175
                 // <style>
176
-                $css = preg_replace('#^INLINE;#','',$css);
177
-                $css = $this->fixurls(ABSPATH.'/index.php',$css);
178
-                $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, "" );
179
-                if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle) ) {
180
-                    $css=$tmpstyle;
181
-                    $this->alreadyminified=true;
176
+                $css = preg_replace('#^INLINE;#', '', $css);
177
+                $css = $this->fixurls(ABSPATH.'/index.php', $css);
178
+                $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, "");
179
+                if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
180
+                    $css = $tmpstyle;
181
+                    $this->alreadyminified = true;
182 182
                 }
183 183
             } else {
184 184
                 //<link>
185
-                if($css !== false && file_exists($css) && is_readable($css)) {
185
+                if ($css !== false && file_exists($css) && is_readable($css)) {
186 186
                     $cssPath = $css;
187
-                    $css = $this->fixurls($cssPath,file_get_contents($cssPath));
188
-                    $css = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$css);
189
-                    $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $cssPath );
187
+                    $css = $this->fixurls($cssPath, file_get_contents($cssPath));
188
+                    $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css);
189
+                    $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $cssPath);
190 190
                     if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
191
-                        $css=$tmpstyle;
192
-                        $this->alreadyminified=true;
193
-                    } else if ($this->can_inject_late($cssPath,$css)) {
194
-                        $css="%%INJECTLATER%%".base64_encode($cssPath)."|".md5($css)."%%INJECTLATER%%";
191
+                        $css = $tmpstyle;
192
+                        $this->alreadyminified = true;
193
+                    } else if ($this->can_inject_late($cssPath, $css)) {
194
+                        $css = "%%INJECTLATER%%".base64_encode($cssPath)."|".md5($css)."%%INJECTLATER%%";
195 195
                     }
196 196
                 } else {
197 197
                     // Couldn't read CSS. Maybe getpath isn't working?
@@ -199,8 +199,8 @@  discard block
 block discarded – undo
199 199
                 }
200 200
             }
201 201
             
202
-            foreach($media as $elem) {
203
-                if(!isset($this->csscode[$elem]))
202
+            foreach ($media as $elem) {
203
+                if (!isset($this->csscode[$elem]))
204 204
                     $this->csscode[$elem] = '';
205 205
                 $this->csscode[$elem] .= "\n/*FILESTART*/".$css;
206 206
             }
@@ -209,12 +209,12 @@  discard block
 block discarded – undo
209 209
         // Check for duplicate code
210 210
         $md5list = array();
211 211
         $tmpcss = $this->csscode;
212
-        foreach($tmpcss as $media => $code) {
212
+        foreach ($tmpcss as $media => $code) {
213 213
             $md5sum = md5($code);
214 214
             $medianame = $media;
215
-            foreach($md5list as $med => $sum) {
215
+            foreach ($md5list as $med => $sum) {
216 216
                 // If same code
217
-                if($sum === $md5sum) {
217
+                if ($sum === $md5sum) {
218 218
                     //Add the merged code
219 219
                     $medianame = $med.', '.$media;
220 220
                     $this->csscode[$medianame] = $code;
@@ -234,29 +234,29 @@  discard block
 block discarded – undo
234 234
             $external_imports = "";
235 235
 
236 236
             // remove comments to avoid importing commented-out imports
237
-            $thiscss_nocomments=preg_replace('#/\*.*\*/#Us','',$thiscss);
238
-            while(preg_match_all('#@import.*(?:;|$)#Um',$thiscss_nocomments,$matches)) {
239
-                foreach($matches[0] as $import)    {
240
-                    if ($this->isremovable($import,$this->cssremovables)) {
241
-                        $thiscss = str_replace($import,'',$thiscss);
237
+            $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss);
238
+            while (preg_match_all('#@import.*(?:;|$)#Um', $thiscss_nocomments, $matches)) {
239
+                foreach ($matches[0] as $import) {
240
+                    if ($this->isremovable($import, $this->cssremovables)) {
241
+                        $thiscss = str_replace($import, '', $thiscss);
242 242
                         $import_ok = true;
243 243
                     } else {
244
-                        $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#','$1',trim($import))," \t\n\r\0\x0B\"'");
244
+                        $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'");
245 245
                         $path = $this->getpath($url);
246 246
                         $import_ok = false;
247 247
                         if (file_exists($path) && is_readable($path)) {
248
-                            $code = addcslashes($this->fixurls($path,file_get_contents($path)),"\\");
249
-                            $code = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$code);
250
-                            $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, "" );
251
-                            if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
252
-                                $code=$tmpstyle;
253
-                                $this->alreadyminified=true;
254
-                            } else if ($this->can_inject_late($path,$code)) {
255
-                                $code="%%INJECTLATER%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%";
248
+                            $code = addcslashes($this->fixurls($path, file_get_contents($path)), "\\");
249
+                            $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code);
250
+                            $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, "");
251
+                            if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
252
+                                $code = $tmpstyle;
253
+                                $this->alreadyminified = true;
254
+                            } else if ($this->can_inject_late($path, $code)) {
255
+                                $code = "%%INJECTLATER%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%";
256 256
                             }
257 257
                             
258
-                            if(!empty($code)) {
259
-                                $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import,'#').'#Us','/*FILESTART2*/'.$code.'$1',$thiscss);
258
+                            if (!empty($code)) {
259
+                                $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss);
260 260
                                 if (!empty($tmp_thiscss)) {
261 261
                                     $thiscss = $tmp_thiscss;
262 262
                                     $import_ok = true;
@@ -270,32 +270,32 @@  discard block
 block discarded – undo
270 270
                     if (!$import_ok) {
271 271
                         // external imports and general fall-back
272 272
                         $external_imports .= $import;
273
-                        $thiscss = str_replace($import,'',$thiscss);
273
+                        $thiscss = str_replace($import, '', $thiscss);
274 274
                         $fiximports = true;
275 275
                     }
276 276
                 }
277
-                $thiscss = preg_replace('#/\*FILESTART\*/#','',$thiscss);
278
-                $thiscss = preg_replace('#/\*FILESTART2\*/#','/*FILESTART*/',$thiscss);
277
+                $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss);
278
+                $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss);
279 279
                 
280 280
                 // and update $thiscss_nocomments before going into next iteration in while loop
281
-                $thiscss_nocomments=preg_replace('#/\*.*\*/#Us','',$thiscss);
281
+                $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss);
282 282
             }
283 283
             unset($thiscss_nocomments);
284 284
             
285 285
             // add external imports to top of aggregated CSS
286
-            if($fiximports) {
287
-                $thiscss=$external_imports.$thiscss;
286
+            if ($fiximports) {
287
+                $thiscss = $external_imports.$thiscss;
288 288
             }
289 289
         }
290 290
         unset($thiscss);
291 291
         
292 292
         // $this->csscode has all the uncompressed code now. 
293 293
         $mhtmlcount = 0;
294
-        foreach($this->csscode as &$code) {
294
+        foreach ($this->csscode as &$code) {
295 295
             // Check for already-minified code
296 296
             $hash = md5($code);
297
-            $ccheck = new autoptimizeCache($hash,'css');
298
-            if($ccheck->check()) {
297
+            $ccheck = new autoptimizeCache($hash, 'css');
298
+            if ($ccheck->check()) {
299 299
                 $code = $ccheck->retrieve();
300 300
                 $this->hashmap[md5($code)] = $hash;
301 301
                 continue;
@@ -304,7 +304,7 @@  discard block
 block discarded – undo
304 304
 
305 305
             // Do the imaging!
306 306
             $imgreplace = array();
307
-            preg_match_all('#(background[^;{}]*url\((?!\s?"?\'?\s?data)(.*)\)[^;}]*)(?:;|$|})#Usm',$code,$matches);
307
+            preg_match_all('#(background[^;{}]*url\((?!\s?"?\'?\s?data)(.*)\)[^;}]*)(?:;|$|})#Usm', $code, $matches);
308 308
 
309 309
             // Bug fixing by dtbaker. Support for multiple background images on CDN:
310 310
 
@@ -324,20 +324,20 @@  discard block
 block discarded – undo
324 324
             $original_image_string = $matches[0];
325 325
 
326 326
             // put everything into multi dimension arrays, to cover for the multi-image use case:
327
-            foreach( $matches[2] as $index => $url ){
327
+            foreach ($matches[2] as $index => $url) {
328 328
                 // todo: this will fail if someone uses uppercase URL() in their CSS code, oh well. who does that anyway?
329
-                if( substr_count( $matches[0][$index] , 'url(' ) > 1 ) {
329
+                if (substr_count($matches[0][$index], 'url(') > 1) {
330 330
                     // here is where we fire off the second regex.
331
-                    preg_match_all('#url\((?!\s?"?\'?\s?data)(.*)\)#Usm',$matches[0][$index],$multi_matches);
332
-                    if( $multi_matches ){
333
-                        $image_urls_to_replace[ $index ] = $multi_matches[1];
334
-                    }else{
331
+                    preg_match_all('#url\((?!\s?"?\'?\s?data)(.*)\)#Usm', $matches[0][$index], $multi_matches);
332
+                    if ($multi_matches) {
333
+                        $image_urls_to_replace[$index] = $multi_matches[1];
334
+                    } else {
335 335
                         // my regex failed for some reason. default to single url matching like before.
336
-                        $image_urls_to_replace[ $index ] = array($url);
336
+                        $image_urls_to_replace[$index] = array($url);
337 337
                     }
338
-                }else{
338
+                } else {
339 339
                     // just a single image to replace this time:
340
-                    $image_urls_to_replace[ $index ] = array($url);
340
+                    $image_urls_to_replace[$index] = array($url);
341 341
                 }
342 342
             }
343 343
 
@@ -357,9 +357,9 @@  discard block
 block discarded – undo
357 357
              * )
358 358
              */
359 359
 
360
-            if(($this->datauris == true) && (function_exists('base64_encode')) && $image_urls_to_replace) {
360
+            if (($this->datauris == true) && (function_exists('base64_encode')) && $image_urls_to_replace) {
361 361
 
362
-                foreach ($image_urls_to_replace as $count => $image_urls){
362
+                foreach ($image_urls_to_replace as $count => $image_urls) {
363 363
                     foreach ($image_urls as $quotedurl) {
364 364
                         $iurl = trim($quotedurl, " \t\n\r\0\x0B\"'");
365 365
 
@@ -371,7 +371,7 @@  discard block
 block discarded – undo
371 371
                         $ipath = $this->getpath($iurl);
372 372
 
373 373
                         $datauri_max_size = 4096;
374
-                        $datauri_max_size = (int)apply_filters('autoptimize_filter_css_datauri_maxsize', $datauri_max_size);
374
+                        $datauri_max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', $datauri_max_size);
375 375
                         $datauri_exclude = apply_filters('autoptimize_filter_css_datauri_exclude', "");
376 376
                         if (!empty($datauri_exclude)) {
377 377
                             $no_datauris = array_filter(array_map('trim', explode(",", $datauri_exclude)));
@@ -418,7 +418,7 @@  discard block
 block discarded – undo
418 418
 
419 419
                                 // Encode the data
420 420
                                 $base64data = base64_encode(file_get_contents($ipath));
421
-                                $headAndData = $dataurihead . $base64data;
421
+                                $headAndData = $dataurihead.$base64data;
422 422
 
423 423
                                 // Save in cache
424 424
                                 $icheck->cache($headAndData, "text/plain");
@@ -427,7 +427,7 @@  discard block
 block discarded – undo
427 427
 
428 428
                             // Add it to the list for replacement
429 429
                             // replace it in original $matches url in case of multiple image urls
430
-                            $matches[0][$count] = str_replace($quotedurl, $headAndData, $matches[0][$count]) . ";\n*" . str_replace($quotedurl, 'mhtml:%%MHTML%%!' . $mhtmlcount, $matches[0][$count]) . ";\n_" . $matches[0][$count] . ';';
430
+                            $matches[0][$count] = str_replace($quotedurl, $headAndData, $matches[0][$count]).";\n*".str_replace($quotedurl, 'mhtml:%%MHTML%%!'.$mhtmlcount, $matches[0][$count]).";\n_".$matches[0][$count].';';
431 431
                             $imgreplace[$original_image_string[$count]] = $matches[0][$count];
432 432
 
433 433
                             // Store image on the mhtml document
@@ -439,57 +439,57 @@  discard block
 block discarded – undo
439 439
                                 $url = trim($quotedurl, " \t\n\r\0\x0B\"'");
440 440
                                 $cdn_url = $this->url_replace_cdn($url);
441 441
                                 // replace it in original $matches url in case of multiple image urls
442
-                                $matches[0][$count] = str_replace($quotedurl,$cdn_url,$matches[0][$count]);
442
+                                $matches[0][$count] = str_replace($quotedurl, $cdn_url, $matches[0][$count]);
443 443
                                 $imgreplace[$original_image_string[$count]] = $matches[0][$count];
444 444
                             }
445 445
                         }
446 446
                     }
447 447
                 }
448
-            } else if ( $image_urls_to_replace && (!empty($this->cdn_url)) ) {
448
+            } else if ($image_urls_to_replace && (!empty($this->cdn_url))) {
449 449
                 // change background image urls to cdn-url
450 450
 
451 451
                 foreach ($image_urls_to_replace as $count => $image_urls) {
452 452
                     foreach ($image_urls as $quotedurl) {
453
-                        $url = trim($quotedurl," \t\n\r\0\x0B\"'");
454
-                        $cdn_url=$this->url_replace_cdn($url);
453
+                        $url = trim($quotedurl, " \t\n\r\0\x0B\"'");
454
+                        $cdn_url = $this->url_replace_cdn($url);
455 455
                         // replace it in original $matches url in case of multiple image urls
456
-                        $matches[0][$count] = str_replace($quotedurl,$cdn_url,$matches[0][$count]);
456
+                        $matches[0][$count] = str_replace($quotedurl, $cdn_url, $matches[0][$count]);
457 457
                         $imgreplace[$original_image_string[$count]] = $matches[0][$count];
458 458
                     }
459 459
                 }
460 460
             }
461 461
 
462
-            if(!empty($imgreplace)) {
463
-                $code = str_replace(array_keys($imgreplace),array_values($imgreplace),$code);
462
+            if (!empty($imgreplace)) {
463
+                $code = str_replace(array_keys($imgreplace), array_values($imgreplace), $code);
464 464
             }
465 465
 
466 466
             // CDN the fonts!
467
-            if ( (!empty($this->cdn_url)) && (apply_filters('autoptimize_filter_css_fonts_cdn',false)) && (version_compare(PHP_VERSION, '5.3.0') >= 0) ) {
467
+            if ((!empty($this->cdn_url)) && (apply_filters('autoptimize_filter_css_fonts_cdn', false)) && (version_compare(PHP_VERSION, '5.3.0') >= 0)) {
468 468
                 $fontreplace = array();
469 469
                 include_once(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizeFontRegex.php');
470 470
                 
471
-                preg_match_all($fonturl_regex,$code,$matches);
471
+                preg_match_all($fonturl_regex, $code, $matches);
472 472
                 if (is_array($matches)) {
473
-                    foreach($matches[8] as $count => $quotedurl) {
474
-                        $url = trim($quotedurl," \t\n\r\0\x0B\"'");
475
-                        $cdn_url=$this->url_replace_cdn($url);
476
-                        $fontreplace[$matches[8][$count]] = str_replace($quotedurl,$cdn_url,$matches[8][$count]);
473
+                    foreach ($matches[8] as $count => $quotedurl) {
474
+                        $url = trim($quotedurl, " \t\n\r\0\x0B\"'");
475
+                        $cdn_url = $this->url_replace_cdn($url);
476
+                        $fontreplace[$matches[8][$count]] = str_replace($quotedurl, $cdn_url, $matches[8][$count]);
477 477
                     }
478
-                    if(!empty($fontreplace)) {
479
-                        $code = str_replace(array_keys($fontreplace),array_values($fontreplace),$code);
478
+                    if (!empty($fontreplace)) {
479
+                        $code = str_replace(array_keys($fontreplace), array_values($fontreplace), $code);
480 480
                     }
481 481
                 }
482 482
             }
483 483
             
484 484
             // Minify
485
-            if (($this->alreadyminified!==true) && (apply_filters( "autoptimize_css_do_minify", true))) {
485
+            if (($this->alreadyminified !== true) && (apply_filters("autoptimize_css_do_minify", true))) {
486 486
                 if (class_exists('Minify_CSS_Compressor')) {
487 487
                     $tmp_code = trim(Minify_CSS_Compressor::process($code));
488
-                } else if(class_exists('CSSmin')) {
488
+                } else if (class_exists('CSSmin')) {
489 489
                     $cssmin = new CSSmin();
490
-                    if (method_exists($cssmin,"run")) {
490
+                    if (method_exists($cssmin, "run")) {
491 491
                         $tmp_code = trim($cssmin->run($code));
492
-                    } elseif (@is_callable(array($cssmin,"minify"))) {
492
+                    } elseif (@is_callable(array($cssmin, "minify"))) {
493 493
                         $tmp_code = trim(CssMin::minify($code));
494 494
                     }
495 495
                 }
@@ -501,7 +501,7 @@  discard block
 block discarded – undo
501 501
             
502 502
             $code = $this->inject_minified($code);
503 503
             
504
-            $tmp_code = apply_filters( 'autoptimize_css_after_minify',$code );
504
+            $tmp_code = apply_filters('autoptimize_css_after_minify', $code);
505 505
             if (!empty($tmp_code)) {
506 506
                 $code = $tmp_code;
507 507
                 unset($tmp_code);
@@ -515,31 +515,31 @@  discard block
 block discarded – undo
515 515
     
516 516
     //Caches the CSS in uncompressed, deflated and gzipped form.
517 517
     public function cache() {
518
-        if($this->datauris) {
518
+        if ($this->datauris) {
519 519
             // MHTML Preparation
520 520
             $this->mhtml = "/*\r\nContent-Type: multipart/related; boundary=\"_\"\r\n\r\n".$this->mhtml."*/\r\n";
521 521
             $md5 = md5($this->mhtml);
522
-            $cache = new autoptimizeCache($md5,'txt');
523
-            if(!$cache->check()) {
522
+            $cache = new autoptimizeCache($md5, 'txt');
523
+            if (!$cache->check()) {
524 524
                 // Cache our images for IE
525
-                $cache->cache($this->mhtml,'text/plain');
525
+                $cache->cache($this->mhtml, 'text/plain');
526 526
             }
527 527
             $mhtml = AUTOPTIMIZE_CACHE_URL.$cache->getname();
528 528
         }
529 529
         
530 530
         // CSS cache
531
-        foreach($this->csscode as $media => $code) {
531
+        foreach ($this->csscode as $media => $code) {
532 532
             $md5 = $this->hashmap[md5($code)];
533 533
 
534
-            if($this->datauris)    {
534
+            if ($this->datauris) {
535 535
                 // Images for ie! Get the right url
536
-                $code = str_replace('%%MHTML%%',$mhtml,$code);
536
+                $code = str_replace('%%MHTML%%', $mhtml, $code);
537 537
             }
538 538
                 
539
-            $cache = new autoptimizeCache($md5,'css');
540
-            if(!$cache->check()) {
539
+            $cache = new autoptimizeCache($md5, 'css');
540
+            if (!$cache->check()) {
541 541
                 // Cache our code
542
-                $cache->cache($code,'text/css');
542
+                $cache->cache($code, 'text/css');
543 543
             }
544 544
             $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname();
545 545
         }
@@ -554,7 +554,7 @@  discard block
 block discarded – undo
554 554
         $this->content = $this->restore_comments($this->content);
555 555
         
556 556
         // restore (no)script
557
-        if ( strpos( $this->content, '%%SCRIPT%%' ) !== false ) { 
557
+        if (strpos($this->content, '%%SCRIPT%%') !== false) { 
558 558
             $this->content = preg_replace_callback(
559 559
                 '#%%SCRIPT%%(.*?)%%SCRIPT%%#is',
560 560
                 create_function(
@@ -569,71 +569,71 @@  discard block
 block discarded – undo
569 569
         $this->content = $this->restore_noptimize($this->content);
570 570
         
571 571
         //Restore the full content
572
-        if(!empty($this->restofcontent)) {
572
+        if (!empty($this->restofcontent)) {
573 573
             $this->content .= $this->restofcontent;
574 574
             $this->restofcontent = '';
575 575
         }
576 576
         
577 577
         // Inject the new stylesheets
578
-        $replaceTag = array("<title","before");
579
-        $replaceTag = apply_filters( 'autoptimize_filter_css_replacetag', $replaceTag );
578
+        $replaceTag = array("<title", "before");
579
+        $replaceTag = apply_filters('autoptimize_filter_css_replacetag', $replaceTag);
580 580
 
581 581
         if ($this->inline == true) {
582
-            foreach($this->csscode as $media => $code) {
583
-                $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>',$replaceTag);
582
+            foreach ($this->csscode as $media => $code) {
583
+                $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>', $replaceTag);
584 584
             }
585 585
         } else {
586 586
             if ($this->defer == true) {
587 587
                 $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() {";
588 588
                 $noScriptCssBlock = "<noscript id=\"aonoscrcss\">";
589
-                $defer_inline_code=$this->defer_inline;
590
-                if(!empty($defer_inline_code)){
591
-                    if ( apply_filters( 'autoptimize_filter_css_critcss_minify',true ) ) {
592
-                        $iCssHash=md5($defer_inline_code);
593
-                        $iCssCache = new autoptimizeCache($iCssHash,'css');
594
-                        if($iCssCache->check()) { 
589
+                $defer_inline_code = $this->defer_inline;
590
+                if (!empty($defer_inline_code)) {
591
+                    if (apply_filters('autoptimize_filter_css_critcss_minify', true)) {
592
+                        $iCssHash = md5($defer_inline_code);
593
+                        $iCssCache = new autoptimizeCache($iCssHash, 'css');
594
+                        if ($iCssCache->check()) { 
595 595
                             // we have the optimized inline CSS in cache
596
-                            $defer_inline_code=$iCssCache->retrieve();
596
+                            $defer_inline_code = $iCssCache->retrieve();
597 597
                         } else {
598 598
                             if (class_exists('Minify_CSS_Compressor')) {
599 599
                                 $tmp_code = trim(Minify_CSS_Compressor::process($defer_inline_code));
600
-                            } else if(class_exists('CSSmin')) {
600
+                            } else if (class_exists('CSSmin')) {
601 601
                                 $cssmin = new CSSmin();
602 602
                                 $tmp_code = trim($cssmin->run($defer_inline_code));
603 603
                             }
604 604
                             if (!empty($tmp_code)) {
605 605
                                 $defer_inline_code = $tmp_code;
606
-                                $iCssCache->cache($defer_inline_code,"text/css");
606
+                                $iCssCache->cache($defer_inline_code, "text/css");
607 607
                                 unset($tmp_code);
608 608
                             }
609 609
                         }
610 610
                     }
611
-                    $code_out='<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>';
612
-                    $this->inject_in_html($code_out,$replaceTag);
611
+                    $code_out = '<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>';
612
+                    $this->inject_in_html($code_out, $replaceTag);
613 613
                 }
614 614
             }
615 615
 
616
-            foreach($this->url as $media => $url) {
616
+            foreach ($this->url as $media => $url) {
617 617
                 $url = $this->url_replace_cdn($url);
618 618
                 
619 619
                 //Add the stylesheet either deferred (import at bottom) or normal links in head
620
-                if($this->defer == true) {
620
+                if ($this->defer == true) {
621 621
                     $deferredCssBlock .= "lCss('".$url."','".$media."');";
622 622
                     $noScriptCssBlock .= '<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />';
623 623
                 } else {
624 624
                     if (strlen($this->csscode[$media]) > $this->cssinlinesize) {
625
-                        $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />',$replaceTag);
626
-                    } else if (strlen($this->csscode[$media])>0) {
627
-                        $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>',$replaceTag);
625
+                        $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />', $replaceTag);
626
+                    } else if (strlen($this->csscode[$media]) > 0) {
627
+                        $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>', $replaceTag);
628 628
                     }
629 629
                 }
630 630
             }
631 631
             
632
-            if($this->defer == true) {
632
+            if ($this->defer == true) {
633 633
                 $deferredCssBlock .= "}if(window.addEventListener){window.addEventListener('DOMContentLoaded',deferredCSS,false);}else{window.onload = deferredCSS;}</script>";
634 634
                 $noScriptCssBlock .= "</noscript>";
635
-                $this->inject_in_html($noScriptCssBlock,$replaceTag);
636
-                $this->inject_in_html($deferredCssBlock,array('</body>','before'));
635
+                $this->inject_in_html($noScriptCssBlock, $replaceTag);
636
+                $this->inject_in_html($deferredCssBlock, array('</body>', 'before'));
637 637
             }
638 638
         }
639 639
 
@@ -641,38 +641,38 @@  discard block
 block discarded – undo
641 641
         return $this->content;
642 642
     }
643 643
     
644
-    static function fixurls($file,$code) {
645
-        $file = str_replace(WP_ROOT_DIR,'',$file);
646
-        $file = str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',$file);
644
+    static function fixurls($file, $code) {
645
+        $file = str_replace(WP_ROOT_DIR, '', $file);
646
+        $file = str_replace(AUTOPTIMIZE_WP_CONTENT_NAME, '', $file);
647 647
         $dir = dirname($file); // Like /themes/expound/css
648 648
 
649 649
         // quick fix for import-troubles in e.g. arras theme
650
-        $code=preg_replace('#@import ("|\')(.+?)\.css("|\')#','@import url("${2}.css")',$code);
650
+        $code = preg_replace('#@import ("|\')(.+?)\.css("|\')#', '@import url("${2}.css")', $code);
651 651
 
652
-        if(preg_match_all('#url\((?!data)(?!\#)(?!"\#)(.*)\)#Usi',$code,$matches)) {
652
+        if (preg_match_all('#url\((?!data)(?!\#)(?!"\#)(.*)\)#Usi', $code, $matches)) {
653 653
             $replace = array();
654
-            foreach($matches[1] as $k => $url) {
654
+            foreach ($matches[1] as $k => $url) {
655 655
                 // Remove quotes
656
-                $url = trim($url," \t\n\r\0\x0B\"'");
657
-                $noQurl = trim($url,"\"'");
656
+                $url = trim($url, " \t\n\r\0\x0B\"'");
657
+                $noQurl = trim($url, "\"'");
658 658
                 
659 659
                 if ($noQurl === '') { continue; }
660 660
                 
661
-                if ($url!==$noQurl) {
662
-                    $removedQuotes=true;
661
+                if ($url !== $noQurl) {
662
+                    $removedQuotes = true;
663 663
                 } else {
664
-                    $removedQuotes=false;
664
+                    $removedQuotes = false;
665 665
                 }
666
-                $url=$noQurl;
667
-                if(substr($url,0,1)=='/' || preg_match('#^(https?://|ftp://|data:)#i',$url)) {
666
+                $url = $noQurl;
667
+                if (substr($url, 0, 1) == '/' || preg_match('#^(https?://|ftp://|data:)#i', $url)) {
668 668
                     //URL is absolute
669 669
                     continue;
670 670
                 } else {
671 671
                     // relative URL
672
-                    $newurl = preg_replace('/https?:/','',str_replace(" ","%20",AUTOPTIMIZE_WP_CONTENT_URL.str_replace('//','/',$dir.'/'.$url)));
672
+                    $newurl = preg_replace('/https?:/', '', str_replace(" ", "%20", AUTOPTIMIZE_WP_CONTENT_URL.str_replace('//', '/', $dir.'/'.$url)));
673 673
 
674 674
                     $hash = md5($url);
675
-                    $code = str_replace($matches[0][$k],$hash,$code);
675
+                    $code = str_replace($matches[0][$k], $hash, $code);
676 676
 
677 677
                     if (!empty($removedQuotes)) {
678 678
                         $replace[$hash] = 'url(\''.$newurl.'\')';
@@ -682,7 +682,7 @@  discard block
 block discarded – undo
682 682
                 }
683 683
             }    
684 684
             //Do the replacing here to avoid breaking URLs
685
-            $code = str_replace(array_keys($replace),array_values($replace),$code);
685
+            $code = str_replace(array_keys($replace), array_values($replace), $code);
686 686
         }    
687 687
         return $code;
688 688
     }
@@ -690,7 +690,7 @@  discard block
 block discarded – undo
690 690
     private function ismovable($tag) {
691 691
         if (!empty($this->whitelist)) {
692 692
             foreach ($this->whitelist as $match) {
693
-                if(strpos($tag,$match)!==false) {
693
+                if (strpos($tag, $match) !== false) {
694 694
                     return true;
695 695
                 }
696 696
             }
@@ -698,8 +698,8 @@  discard block
 block discarded – undo
698 698
             return false;
699 699
         } else {
700 700
             if (is_array($this->dontmove)) {
701
-                foreach($this->dontmove as $match) {
702
-                    if(strpos($tag,$match)!==false) {
701
+                foreach ($this->dontmove as $match) {
702
+                    if (strpos($tag, $match) !== false) {
703 703
                         //Matched something
704 704
                         return false;
705 705
                     }
@@ -711,17 +711,17 @@  discard block
 block discarded – undo
711 711
         }
712 712
     }
713 713
     
714
-    private function can_inject_late($cssPath,$css) {
715
-        if ((strpos($cssPath,"min.css")===false) || ($this->inject_min_late!==true)) {
714
+    private function can_inject_late($cssPath, $css) {
715
+        if ((strpos($cssPath, "min.css") === false) || ($this->inject_min_late !== true)) {
716 716
             // late-inject turned off or file not minified based on filename
717 717
             return false;
718
-        } else if (strpos($css,"@import")!==false) {
718
+        } else if (strpos($css, "@import") !== false) {
719 719
             // can't late-inject files with imports as those need to be aggregated 
720 720
             return false;
721
-        } else if ( (strpos($css,"@font-face")!==false ) && ( apply_filters("autoptimize_filter_css_fonts_cdn",false)===true) && (!empty($this->cdn_url)) ) {
721
+        } else if ((strpos($css, "@font-face") !== false) && (apply_filters("autoptimize_filter_css_fonts_cdn", false) === true) && (!empty($this->cdn_url))) {
722 722
             // don't late-inject CSS with font-src's if fonts are set to be CDN'ed
723 723
             return false;
724
-        } else if ( (($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui",$css) ) {
724
+        } else if ((($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui", $css)) {
725 725
             // don't late-inject CSS with images if CDN is set OR is image inlining is on
726 726
             return false;
727 727
         } else {
Please login to merge, or discard this patch.
Braces   +13 added lines, -7 removed lines patch added patch discarded remove patch
@@ -1,5 +1,8 @@  discard block
 block discarded – undo
1 1
 <?php
2
-if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2
+if ( ! defined( 'ABSPATH' ) ) {
3
+    exit;
4
+}
5
+// Exit if accessed directly
3 6
 
4 7
 class autoptimizeStyles extends autoptimizeBase {
5 8
     private $css = array();
@@ -22,7 +25,9 @@  discard block
 block discarded – undo
22 25
     //Reads the page and collects style tags
23 26
     public function read($options) {
24 27
         $noptimizeCSS = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content );
25
-        if ($noptimizeCSS) return false;
28
+        if ($noptimizeCSS) {
29
+            return false;
30
+        }
26 31
 
27 32
         $whitelistCSS = apply_filters( 'autoptimize_filter_css_whitelist', '' );
28 33
         if (!empty($whitelistCSS)) {
@@ -136,7 +141,7 @@  discard block
 block discarded – undo
136 141
                         if($path!==false && preg_match('#\.css$#',$path)) {
137 142
                             // Good link
138 143
                             $this->css[] = array($media,$path);
139
-                        }else{
144
+                        } else{
140 145
                             // Link is dynamic (.php etc)
141 146
                             $tag = '';
142 147
                         }
@@ -200,8 +205,9 @@  discard block
 block discarded – undo
200 205
             }
201 206
             
202 207
             foreach($media as $elem) {
203
-                if(!isset($this->csscode[$elem]))
204
-                    $this->csscode[$elem] = '';
208
+                if(!isset($this->csscode[$elem])) {
209
+                                    $this->csscode[$elem] = '';
210
+                }
205 211
                 $this->csscode[$elem] .= "\n/*FILESTART*/".$css;
206 212
             }
207 213
         }
@@ -331,11 +337,11 @@  discard block
 block discarded – undo
331 337
                     preg_match_all('#url\((?!\s?"?\'?\s?data)(.*)\)#Usm',$matches[0][$index],$multi_matches);
332 338
                     if( $multi_matches ){
333 339
                         $image_urls_to_replace[ $index ] = $multi_matches[1];
334
-                    }else{
340
+                    } else{
335 341
                         // my regex failed for some reason. default to single url matching like before.
336 342
                         $image_urls_to_replace[ $index ] = array($url);
337 343
                     }
338
-                }else{
344
+                } else{
339 345
                     // just a single image to replace this time:
340 346
                     $image_urls_to_replace[ $index ] = array($url);
341 347
                 }
Please login to merge, or discard this patch.