Completed
Push — master ( 129fd9...49ad0c )
by frank
02:18
created
classes/autoptimizeStyles.php 1 patch
Spacing   +189 added lines, -189 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;
@@ -168,29 +168,29 @@  discard block
 block discarded – undo
168 168
     
169 169
     // Joins and optimizes CSS
170 170
     public function minify() {
171
-        foreach($this->css as $group) {
172
-            list($media,$css) = $group;
173
-            if(preg_match('#^INLINE;#',$css)) {
171
+        foreach ($this->css as $group) {
172
+            list($media, $css) = $group;
173
+            if (preg_match('#^INLINE;#', $css)) {
174 174
                 // <style>
175
-                $css = preg_replace('#^INLINE;#','',$css);
176
-                $css = $this->fixurls(ABSPATH.'/index.php',$css);
177
-                $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, "" );
178
-                if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle) ) {
179
-                    $css=$tmpstyle;
180
-                    $this->alreadyminified=true;
175
+                $css = preg_replace('#^INLINE;#', '', $css);
176
+                $css = $this->fixurls(ABSPATH.'/index.php', $css);
177
+                $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, "");
178
+                if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
179
+                    $css = $tmpstyle;
180
+                    $this->alreadyminified = true;
181 181
                 }
182 182
             } else {
183 183
                 //<link>
184
-                if($css !== false && file_exists($css) && is_readable($css)) {
184
+                if ($css !== false && file_exists($css) && is_readable($css)) {
185 185
                     $cssPath = $css;
186
-                    $css = $this->fixurls($cssPath,file_get_contents($cssPath));
187
-                    $css = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$css);
188
-                    $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $cssPath );
186
+                    $css = $this->fixurls($cssPath, file_get_contents($cssPath));
187
+                    $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css);
188
+                    $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $cssPath);
189 189
                     if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
190
-                        $css=$tmpstyle;
191
-                        $this->alreadyminified=true;
192
-                    } else if ($this->can_inject_late($cssPath,$css)) {
193
-                        $css="%%INJECTLATER%%".base64_encode($cssPath)."|".md5($css)."%%INJECTLATER%%";
190
+                        $css = $tmpstyle;
191
+                        $this->alreadyminified = true;
192
+                    } else if ($this->can_inject_late($cssPath, $css)) {
193
+                        $css = "%%INJECTLATER%%".base64_encode($cssPath)."|".md5($css)."%%INJECTLATER%%";
194 194
                     }
195 195
                 } else {
196 196
                     // Couldn't read CSS. Maybe getpath isn't working?
@@ -198,8 +198,8 @@  discard block
 block discarded – undo
198 198
                 }
199 199
             }
200 200
             
201
-            foreach($media as $elem) {
202
-                if(!isset($this->csscode[$elem]))
201
+            foreach ($media as $elem) {
202
+                if (!isset($this->csscode[$elem]))
203 203
                     $this->csscode[$elem] = '';
204 204
                 $this->csscode[$elem] .= "\n/*FILESTART*/".$css;
205 205
             }
@@ -208,12 +208,12 @@  discard block
 block discarded – undo
208 208
         // Check for duplicate code
209 209
         $md5list = array();
210 210
         $tmpcss = $this->csscode;
211
-        foreach($tmpcss as $media => $code) {
211
+        foreach ($tmpcss as $media => $code) {
212 212
             $md5sum = md5($code);
213 213
             $medianame = $media;
214
-            foreach($md5list as $med => $sum) {
214
+            foreach ($md5list as $med => $sum) {
215 215
                 // If same code
216
-                if($sum === $md5sum) {
216
+                if ($sum === $md5sum) {
217 217
                     //Add the merged code
218 218
                     $medianame = $med.', '.$media;
219 219
                     $this->csscode[$medianame] = $code;
@@ -233,29 +233,29 @@  discard block
 block discarded – undo
233 233
             $external_imports = "";
234 234
 
235 235
             // remove comments to avoid importing commented-out imports
236
-            $thiscss_nocomments=preg_replace('#/\*.*\*/#Us','',$thiscss);
237
-            while(preg_match_all('#@import.*(?:;|$)#Um',$thiscss_nocomments,$matches)) {
238
-                foreach($matches[0] as $import)    {
239
-                    if ($this->isremovable($import,$this->cssremovables)) {
240
-                        $thiscss = str_replace($import,'',$thiscss);
236
+            $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss);
237
+            while (preg_match_all('#@import.*(?:;|$)#Um', $thiscss_nocomments, $matches)) {
238
+                foreach ($matches[0] as $import) {
239
+                    if ($this->isremovable($import, $this->cssremovables)) {
240
+                        $thiscss = str_replace($import, '', $thiscss);
241 241
                         $import_ok = true;
242 242
                     } else {
243
-                        $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#','$1',trim($import))," \t\n\r\0\x0B\"'");
243
+                        $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'");
244 244
                         $path = $this->getpath($url);
245 245
                         $import_ok = false;
246 246
                         if (file_exists($path) && is_readable($path)) {
247
-                            $code = addcslashes($this->fixurls($path,file_get_contents($path)),"\\");
248
-                            $code = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$code);
249
-                            $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, "" );
250
-                            if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
251
-                                $code=$tmpstyle;
252
-                                $this->alreadyminified=true;
253
-                            } else if ($this->can_inject_late($path,$code)) {
254
-                                $code="%%INJECTLATER%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%";
247
+                            $code = addcslashes($this->fixurls($path, file_get_contents($path)), "\\");
248
+                            $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code);
249
+                            $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, "");
250
+                            if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
251
+                                $code = $tmpstyle;
252
+                                $this->alreadyminified = true;
253
+                            } else if ($this->can_inject_late($path, $code)) {
254
+                                $code = "%%INJECTLATER%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%";
255 255
                             }
256 256
                             
257
-                            if(!empty($code)) {
258
-                                $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import,'#').'#Us','/*FILESTART2*/'.$code.'$1',$thiscss);
257
+                            if (!empty($code)) {
258
+                                $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss);
259 259
                                 if (!empty($tmp_thiscss)) {
260 260
                                     $thiscss = $tmp_thiscss;
261 261
                                     $import_ok = true;
@@ -269,32 +269,32 @@  discard block
 block discarded – undo
269 269
                     if (!$import_ok) {
270 270
                         // external imports and general fall-back
271 271
                         $external_imports .= $import;
272
-                        $thiscss = str_replace($import,'',$thiscss);
272
+                        $thiscss = str_replace($import, '', $thiscss);
273 273
                         $fiximports = true;
274 274
                     }
275 275
                 }
276
-                $thiscss = preg_replace('#/\*FILESTART\*/#','',$thiscss);
277
-                $thiscss = preg_replace('#/\*FILESTART2\*/#','/*FILESTART*/',$thiscss);
276
+                $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss);
277
+                $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss);
278 278
                 
279 279
                 // and update $thiscss_nocomments before going into next iteration in while loop
280
-                $thiscss_nocomments=preg_replace('#/\*.*\*/#Us','',$thiscss);
280
+                $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss);
281 281
             }
282 282
             unset($thiscss_nocomments);
283 283
             
284 284
             // add external imports to top of aggregated CSS
285
-            if($fiximports) {
286
-                $thiscss=$external_imports.$thiscss;
285
+            if ($fiximports) {
286
+                $thiscss = $external_imports.$thiscss;
287 287
             }
288 288
         }
289 289
         unset($thiscss);
290 290
         
291 291
         // $this->csscode has all the uncompressed code now. 
292 292
         $mhtmlcount = 0;
293
-        foreach($this->csscode as &$code) {
293
+        foreach ($this->csscode as &$code) {
294 294
             // Check for already-minified code
295 295
             $hash = md5($code);
296
-            $ccheck = new autoptimizeCache($hash,'css');
297
-            if($ccheck->check()) {
296
+            $ccheck = new autoptimizeCache($hash, 'css');
297
+            if ($ccheck->check()) {
298 298
                 $code = $ccheck->retrieve();
299 299
                 $this->hashmap[md5($code)] = $hash;
300 300
                 continue;
@@ -303,44 +303,44 @@  discard block
 block discarded – undo
303 303
 
304 304
             // Do the imaging!
305 305
             $imgreplace = array();
306
-            preg_match_all('#(background[^;{}]*url\((?!\s?"?\'?\s?data)(.*)\)[^;}]*)(?:;|$|})#Usm',$code,$matches);
306
+            preg_match_all('#(background[^;{}]*url\((?!\s?"?\'?\s?data)(.*)\)[^;}]*)(?:;|$|})#Usm', $code, $matches);
307 307
             
308
-            if(($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches)))    {
309
-                foreach($matches[2] as $count => $quotedurl) {
310
-                    $iurl = trim($quotedurl," \t\n\r\0\x0B\"'");
308
+            if (($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches))) {
309
+                foreach ($matches[2] as $count => $quotedurl) {
310
+                    $iurl = trim($quotedurl, " \t\n\r\0\x0B\"'");
311 311
 
312 312
                     // if querystring, remove it from url
313
-                    if (strpos($iurl,'?') !== false) { $iurl = strtok($iurl,'?'); }
313
+                    if (strpos($iurl, '?') !== false) { $iurl = strtok($iurl, '?'); }
314 314
                     
315 315
                     $ipath = $this->getpath($iurl);
316 316
 
317 317
                     $datauri_max_size = 4096;
318
-                    $datauri_max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', $datauri_max_size );
319
-                    $datauri_exclude = apply_filters( 'autoptimize_filter_css_datauri_exclude', "");
318
+                    $datauri_max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', $datauri_max_size);
319
+                    $datauri_exclude = apply_filters('autoptimize_filter_css_datauri_exclude', "");
320 320
                     if (!empty($datauri_exclude)) {
321
-                        $no_datauris=array_filter(array_map('trim',explode(",",$datauri_exclude)));
321
+                        $no_datauris = array_filter(array_map('trim', explode(",", $datauri_exclude)));
322 322
                         foreach ($no_datauris as $no_datauri) {
323
-                            if (strpos($iurl,$no_datauri)!==false) {
324
-                                $ipath=false;
323
+                            if (strpos($iurl, $no_datauri) !== false) {
324
+                                $ipath = false;
325 325
                                 break;
326 326
                             }
327 327
                         }
328 328
                     }
329 329
 
330
-                    if($ipath != false && preg_match('#\.(jpe?g|png|gif|bmp)$#i',$ipath) && file_exists($ipath) && is_readable($ipath) && filesize($ipath) <= $datauri_max_size) {
331
-                        $ihash=md5($ipath);
332
-                        $icheck = new autoptimizeCache($ihash,'img');
333
-                        if($icheck->check()) {
330
+                    if ($ipath != false && preg_match('#\.(jpe?g|png|gif|bmp)$#i', $ipath) && file_exists($ipath) && is_readable($ipath) && filesize($ipath) <= $datauri_max_size) {
331
+                        $ihash = md5($ipath);
332
+                        $icheck = new autoptimizeCache($ihash, 'img');
333
+                        if ($icheck->check()) {
334 334
                             // we have the base64 image in cache
335
-                            $headAndData=$icheck->retrieve();
336
-                            $_base64data=explode(";base64,",$headAndData);
337
-                            $base64data=$_base64data[1];
335
+                            $headAndData = $icheck->retrieve();
336
+                            $_base64data = explode(";base64,", $headAndData);
337
+                            $base64data = $_base64data[1];
338 338
                         } else {
339 339
                             // It's an image and we don't have it in cache, get the type
340
-                            $explA=explode('.',$ipath);
341
-                            $type=end($explA);
340
+                            $explA = explode('.', $ipath);
341
+                            $type = end($explA);
342 342
 
343
-                            switch($type) {
343
+                            switch ($type) {
344 344
                                 case 'jpeg':
345 345
                                     $dataurihead = 'data:image/jpeg;base64,';
346 346
                                     break;
@@ -362,15 +362,15 @@  discard block
 block discarded – undo
362 362
                         
363 363
                             // Encode the data
364 364
                             $base64data = base64_encode(file_get_contents($ipath));
365
-                            $headAndData=$dataurihead.$base64data;
365
+                            $headAndData = $dataurihead.$base64data;
366 366
 
367 367
                             // Save in cache
368
-                            $icheck->cache($headAndData,"text/plain");
368
+                            $icheck->cache($headAndData, "text/plain");
369 369
                         }
370 370
                         unset($icheck);
371 371
 
372 372
                         // Add it to the list for replacement
373
-                        $imgreplace[$matches[1][$count]] = str_replace($quotedurl,$headAndData,$matches[1][$count]).";\n*".str_replace($quotedurl,'mhtml:%%MHTML%%!'.$mhtmlcount,$matches[1][$count]).";\n_".$matches[1][$count].';';
373
+                        $imgreplace[$matches[1][$count]] = str_replace($quotedurl, $headAndData, $matches[1][$count]).";\n*".str_replace($quotedurl, 'mhtml:%%MHTML%%!'.$mhtmlcount, $matches[1][$count]).";\n_".$matches[1][$count].';';
374 374
                         
375 375
                         // Store image on the mhtml document
376 376
                         $this->mhtml .= "--_\r\nContent-Location:{$mhtmlcount}\r\nContent-Transfer-Encoding:base64\r\n\r\n{$base64data}\r\n";
@@ -378,52 +378,52 @@  discard block
 block discarded – undo
378 378
                     } else {
379 379
                         // just cdn the URL if applicable
380 380
                         if (!empty($this->cdn_url)) {
381
-                            $url = trim($quotedurl," \t\n\r\0\x0B\"'");
382
-                            $cdn_url=$this->url_replace_cdn($url);
383
-                            $imgreplace[$matches[1][$count]] = str_replace($quotedurl,$cdn_url,$matches[1][$count]);
381
+                            $url = trim($quotedurl, " \t\n\r\0\x0B\"'");
382
+                            $cdn_url = $this->url_replace_cdn($url);
383
+                            $imgreplace[$matches[1][$count]] = str_replace($quotedurl, $cdn_url, $matches[1][$count]);
384 384
                         }
385 385
                     }
386 386
                 }
387 387
             } else if ((is_array($matches)) && (!empty($this->cdn_url))) {
388 388
                 // change background image urls to cdn-url
389
-                foreach($matches[2] as $count => $quotedurl) {
390
-                    $url = trim($quotedurl," \t\n\r\0\x0B\"'");
391
-                    $cdn_url=$this->url_replace_cdn($url);
392
-                    $imgreplace[$matches[1][$count]] = str_replace($quotedurl,$cdn_url,$matches[1][$count]);
389
+                foreach ($matches[2] as $count => $quotedurl) {
390
+                    $url = trim($quotedurl, " \t\n\r\0\x0B\"'");
391
+                    $cdn_url = $this->url_replace_cdn($url);
392
+                    $imgreplace[$matches[1][$count]] = str_replace($quotedurl, $cdn_url, $matches[1][$count]);
393 393
                 }
394 394
             }
395 395
             
396
-            if(!empty($imgreplace)) {
397
-                $code = str_replace(array_keys($imgreplace),array_values($imgreplace),$code);
396
+            if (!empty($imgreplace)) {
397
+                $code = str_replace(array_keys($imgreplace), array_values($imgreplace), $code);
398 398
             }
399 399
             
400 400
             // CDN the fonts!
401
-            if ( (!empty($this->cdn_url)) && (apply_filters('autoptimize_filter_css_fonts_cdn',false)) && (version_compare(PHP_VERSION, '5.3.0') >= 0) ) {
401
+            if ((!empty($this->cdn_url)) && (apply_filters('autoptimize_filter_css_fonts_cdn', false)) && (version_compare(PHP_VERSION, '5.3.0') >= 0)) {
402 402
                 $fontreplace = array();
403 403
                 include_once(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizeFontRegex.php');
404 404
                 
405
-                preg_match_all($fonturl_regex,$code,$matches);
405
+                preg_match_all($fonturl_regex, $code, $matches);
406 406
                 if (is_array($matches)) {
407
-                    foreach($matches[8] as $count => $quotedurl) {
408
-                        $url = trim($quotedurl," \t\n\r\0\x0B\"'");
409
-                        $cdn_url=$this->url_replace_cdn($url);
410
-                        $fontreplace[$matches[8][$count]] = str_replace($quotedurl,$cdn_url,$matches[8][$count]);
407
+                    foreach ($matches[8] as $count => $quotedurl) {
408
+                        $url = trim($quotedurl, " \t\n\r\0\x0B\"'");
409
+                        $cdn_url = $this->url_replace_cdn($url);
410
+                        $fontreplace[$matches[8][$count]] = str_replace($quotedurl, $cdn_url, $matches[8][$count]);
411 411
                     }
412
-                    if(!empty($fontreplace)) {
413
-                        $code = str_replace(array_keys($fontreplace),array_values($fontreplace),$code);
412
+                    if (!empty($fontreplace)) {
413
+                        $code = str_replace(array_keys($fontreplace), array_values($fontreplace), $code);
414 414
                     }
415 415
                 }
416 416
             }
417 417
             
418 418
             // Minify
419
-            if (($this->alreadyminified!==true) && (apply_filters( "autoptimize_css_do_minify", true))) {
419
+            if (($this->alreadyminified !== true) && (apply_filters("autoptimize_css_do_minify", true))) {
420 420
                 if (class_exists('Minify_CSS_Compressor')) {
421 421
                     $tmp_code = trim(Minify_CSS_Compressor::process($code));
422
-                } else if(class_exists('CSSmin')) {
422
+                } else if (class_exists('CSSmin')) {
423 423
                     $cssmin = new CSSmin();
424
-                    if (method_exists($cssmin,"run")) {
424
+                    if (method_exists($cssmin, "run")) {
425 425
                         $tmp_code = trim($cssmin->run($code));
426
-                    } elseif (@is_callable(array($cssmin,"minify"))) {
426
+                    } elseif (@is_callable(array($cssmin, "minify"))) {
427 427
                         $tmp_code = trim(CssMin::minify($code));
428 428
                     }
429 429
                 }
@@ -435,7 +435,7 @@  discard block
 block discarded – undo
435 435
             
436 436
             $code = $this->inject_minified($code);
437 437
             
438
-            $tmp_code = apply_filters( 'autoptimize_css_after_minify',$code );
438
+            $tmp_code = apply_filters('autoptimize_css_after_minify', $code);
439 439
             if (!empty($tmp_code)) {
440 440
                 $code = $tmp_code;
441 441
                 unset($tmp_code);
@@ -449,31 +449,31 @@  discard block
 block discarded – undo
449 449
     
450 450
     //Caches the CSS in uncompressed, deflated and gzipped form.
451 451
     public function cache() {
452
-        if($this->datauris) {
452
+        if ($this->datauris) {
453 453
             // MHTML Preparation
454 454
             $this->mhtml = "/*\r\nContent-Type: multipart/related; boundary=\"_\"\r\n\r\n".$this->mhtml."*/\r\n";
455 455
             $md5 = md5($this->mhtml);
456
-            $cache = new autoptimizeCache($md5,'txt');
457
-            if(!$cache->check()) {
456
+            $cache = new autoptimizeCache($md5, 'txt');
457
+            if (!$cache->check()) {
458 458
                 // Cache our images for IE
459
-                $cache->cache($this->mhtml,'text/plain');
459
+                $cache->cache($this->mhtml, 'text/plain');
460 460
             }
461 461
             $mhtml = AUTOPTIMIZE_CACHE_URL.$cache->getname();
462 462
         }
463 463
         
464 464
         // CSS cache
465
-        foreach($this->csscode as $media => $code) {
465
+        foreach ($this->csscode as $media => $code) {
466 466
             $md5 = $this->hashmap[md5($code)];
467 467
 
468
-            if($this->datauris)    {
468
+            if ($this->datauris) {
469 469
                 // Images for ie! Get the right url
470
-                $code = str_replace('%%MHTML%%',$mhtml,$code);
470
+                $code = str_replace('%%MHTML%%', $mhtml, $code);
471 471
             }
472 472
                 
473
-            $cache = new autoptimizeCache($md5,'css');
474
-            if(!$cache->check()) {
473
+            $cache = new autoptimizeCache($md5, 'css');
474
+            if (!$cache->check()) {
475 475
                 // Cache our code
476
-                $cache->cache($code,'text/css');
476
+                $cache->cache($code, 'text/css');
477 477
             }
478 478
             $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname();
479 479
         }
@@ -488,7 +488,7 @@  discard block
 block discarded – undo
488 488
         $this->content = $this->restore_comments($this->content);
489 489
         
490 490
         // restore (no)script
491
-        if ( strpos( $this->content, '%%SCRIPT%%' ) !== false ) { 
491
+        if (strpos($this->content, '%%SCRIPT%%') !== false) { 
492 492
             $this->content = preg_replace_callback(
493 493
                 '#%%SCRIPT%%(.*?)%%SCRIPT%%#is',
494 494
                 create_function(
@@ -503,71 +503,71 @@  discard block
 block discarded – undo
503 503
         $this->content = $this->restore_noptimize($this->content);
504 504
         
505 505
         //Restore the full content
506
-        if(!empty($this->restofcontent)) {
506
+        if (!empty($this->restofcontent)) {
507 507
             $this->content .= $this->restofcontent;
508 508
             $this->restofcontent = '';
509 509
         }
510 510
         
511 511
         // Inject the new stylesheets
512
-        $replaceTag = array("<title","before");
513
-        $replaceTag = apply_filters( 'autoptimize_filter_css_replacetag', $replaceTag );
512
+        $replaceTag = array("<title", "before");
513
+        $replaceTag = apply_filters('autoptimize_filter_css_replacetag', $replaceTag);
514 514
 
515 515
         if ($this->inline == true) {
516
-            foreach($this->csscode as $media => $code) {
517
-                $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>',$replaceTag);
516
+            foreach ($this->csscode as $media => $code) {
517
+                $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>', $replaceTag);
518 518
             }
519 519
         } else {
520 520
             if ($this->defer == true) {
521 521
                 $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');aoin.parentNode.insertBefore(l,aoin.nextSibling);}function deferredCSS() {";
522 522
                 $noScriptCssBlock = "<noscript id=\"aonoscrcss\">";
523
-                $defer_inline_code=$this->defer_inline;
524
-                if(!empty($defer_inline_code)){
525
-                    if ( apply_filters( 'autoptimize_filter_css_critcss_minify',true ) ) {
526
-                        $iCssHash=md5($defer_inline_code);
527
-                        $iCssCache = new autoptimizeCache($iCssHash,'css');
528
-                        if($iCssCache->check()) { 
523
+                $defer_inline_code = $this->defer_inline;
524
+                if (!empty($defer_inline_code)) {
525
+                    if (apply_filters('autoptimize_filter_css_critcss_minify', true)) {
526
+                        $iCssHash = md5($defer_inline_code);
527
+                        $iCssCache = new autoptimizeCache($iCssHash, 'css');
528
+                        if ($iCssCache->check()) { 
529 529
                             // we have the optimized inline CSS in cache
530
-                            $defer_inline_code=$iCssCache->retrieve();
530
+                            $defer_inline_code = $iCssCache->retrieve();
531 531
                         } else {
532 532
                             if (class_exists('Minify_CSS_Compressor')) {
533 533
                                 $tmp_code = trim(Minify_CSS_Compressor::process($defer_inline_code));
534
-                            } else if(class_exists('CSSmin')) {
534
+                            } else if (class_exists('CSSmin')) {
535 535
                                 $cssmin = new CSSmin();
536 536
                                 $tmp_code = trim($cssmin->run($defer_inline_code));
537 537
                             }
538 538
                             if (!empty($tmp_code)) {
539 539
                                 $defer_inline_code = $tmp_code;
540
-                                $iCssCache->cache($defer_inline_code,"text/css");
540
+                                $iCssCache->cache($defer_inline_code, "text/css");
541 541
                                 unset($tmp_code);
542 542
                             }
543 543
                         }
544 544
                     }
545
-                    $code_out='<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>';
546
-                    $this->inject_in_html($code_out,$replaceTag);
545
+                    $code_out = '<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>';
546
+                    $this->inject_in_html($code_out, $replaceTag);
547 547
                 }
548 548
             }
549 549
 
550
-            foreach($this->url as $media => $url) {
550
+            foreach ($this->url as $media => $url) {
551 551
                 $url = $this->url_replace_cdn($url);
552 552
                 
553 553
                 //Add the stylesheet either deferred (import at bottom) or normal links in head
554
-                if($this->defer == true) {
554
+                if ($this->defer == true) {
555 555
                     $deferredCssBlock .= "lCss('".$url."','".$media."');";
556 556
                     $noScriptCssBlock .= '<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />';
557 557
                 } else {
558 558
                     if (strlen($this->csscode[$media]) > $this->cssinlinesize) {
559
-                        $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />',$replaceTag);
560
-                    } else if (strlen($this->csscode[$media])>0) {
561
-                        $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>',$replaceTag);
559
+                        $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />', $replaceTag);
560
+                    } else if (strlen($this->csscode[$media]) > 0) {
561
+                        $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>', $replaceTag);
562 562
                     }
563 563
                 }
564 564
             }
565 565
             
566
-            if($this->defer == true) {
566
+            if ($this->defer == true) {
567 567
                 $deferredCssBlock .= "}if(window.addEventListener){window.addEventListener('DOMContentLoaded',deferredCSS,false);}else{window.onload = deferredCSS;}</script>";
568 568
                 $noScriptCssBlock .= "</noscript>";
569
-                $this->inject_in_html($noScriptCssBlock,$replaceTag);
570
-                $this->inject_in_html($deferredCssBlock,array('</body>','before'));
569
+                $this->inject_in_html($noScriptCssBlock, $replaceTag);
570
+                $this->inject_in_html($deferredCssBlock, array('</body>', 'before'));
571 571
             }
572 572
         }
573 573
 
@@ -575,35 +575,35 @@  discard block
 block discarded – undo
575 575
         return $this->content;
576 576
     }
577 577
     
578
-    static function fixurls($file,$code) {
579
-        $file = str_replace(WP_ROOT_DIR,'',$file);
580
-        $file = str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',$file);
578
+    static function fixurls($file, $code) {
579
+        $file = str_replace(WP_ROOT_DIR, '', $file);
580
+        $file = str_replace(AUTOPTIMIZE_WP_CONTENT_NAME, '', $file);
581 581
         $dir = dirname($file); // Like /themes/expound/css
582 582
 
583 583
         // quick fix for import-troubles in e.g. arras theme
584
-        $code=preg_replace('#@import ("|\')(.+?)\.css("|\')#','@import url("${2}.css")',$code);
584
+        $code = preg_replace('#@import ("|\')(.+?)\.css("|\')#', '@import url("${2}.css")', $code);
585 585
 
586
-        if(preg_match_all('#url\((?!data)(?!\#)(?!"\#)(.*)\)#Usi',$code,$matches)) {
586
+        if (preg_match_all('#url\((?!data)(?!\#)(?!"\#)(.*)\)#Usi', $code, $matches)) {
587 587
             $replace = array();
588
-            foreach($matches[1] as $k => $url) {
588
+            foreach ($matches[1] as $k => $url) {
589 589
                 // Remove quotes
590
-                $url = trim($url," \t\n\r\0\x0B\"'");
591
-                $noQurl = trim($url,"\"'");
592
-                if ($url!==$noQurl) {
593
-                    $removedQuotes=true;
590
+                $url = trim($url, " \t\n\r\0\x0B\"'");
591
+                $noQurl = trim($url, "\"'");
592
+                if ($url !== $noQurl) {
593
+                    $removedQuotes = true;
594 594
                 } else {
595
-                    $removedQuotes=false;
595
+                    $removedQuotes = false;
596 596
                 }
597
-                $url=$noQurl;
598
-                if(substr($url,0,1)=='/' || preg_match('#^(https?://|ftp://|data:)#i',$url)) {
597
+                $url = $noQurl;
598
+                if (substr($url, 0, 1) == '/' || preg_match('#^(https?://|ftp://|data:)#i', $url)) {
599 599
                     //URL is absolute
600 600
                     continue;
601 601
                 } else {
602 602
                     // relative URL
603
-                    $newurl = preg_replace('/https?:/','',str_replace(" ","%20",AUTOPTIMIZE_WP_CONTENT_URL.str_replace('//','/',$dir.'/'.$url)));
603
+                    $newurl = preg_replace('/https?:/', '', str_replace(" ", "%20", AUTOPTIMIZE_WP_CONTENT_URL.str_replace('//', '/', $dir.'/'.$url)));
604 604
 
605 605
                     $hash = md5($url);
606
-                    $code = str_replace($matches[0][$k],$hash,$code);
606
+                    $code = str_replace($matches[0][$k], $hash, $code);
607 607
 
608 608
                     if (!empty($removedQuotes)) {
609 609
                         $replace[$hash] = 'url(\''.$newurl.'\')';
@@ -613,7 +613,7 @@  discard block
 block discarded – undo
613 613
                 }
614 614
             }    
615 615
             //Do the replacing here to avoid breaking URLs
616
-            $code = str_replace(array_keys($replace),array_values($replace),$code);
616
+            $code = str_replace(array_keys($replace), array_values($replace), $code);
617 617
         }    
618 618
         return $code;
619 619
     }
@@ -621,7 +621,7 @@  discard block
 block discarded – undo
621 621
     private function ismovable($tag) {
622 622
         if (!empty($this->whitelist)) {
623 623
             foreach ($this->whitelist as $match) {
624
-                if(strpos($tag,$match)!==false) {
624
+                if (strpos($tag, $match) !== false) {
625 625
                     return true;
626 626
                 }
627 627
             }
@@ -629,8 +629,8 @@  discard block
 block discarded – undo
629 629
             return false;
630 630
         } else {
631 631
             if (is_array($this->dontmove)) {
632
-                foreach($this->dontmove as $match) {
633
-                    if(strpos($tag,$match)!==false) {
632
+                foreach ($this->dontmove as $match) {
633
+                    if (strpos($tag, $match) !== false) {
634 634
                         //Matched something
635 635
                         return false;
636 636
                     }
@@ -642,17 +642,17 @@  discard block
 block discarded – undo
642 642
         }
643 643
     }
644 644
     
645
-    private function can_inject_late($cssPath,$css) {
646
-        if ((strpos($cssPath,"min.css")===false) || ($this->inject_min_late!==true)) {
645
+    private function can_inject_late($cssPath, $css) {
646
+        if ((strpos($cssPath, "min.css") === false) || ($this->inject_min_late !== true)) {
647 647
             // late-inject turned off or file not minified based on filename
648 648
             return false;
649
-        } else if (strpos($css,"@import")!==false) {
649
+        } else if (strpos($css, "@import") !== false) {
650 650
             // can't late-inject files with imports as those need to be aggregated 
651 651
             return false;
652
-        } else if ( (strpos($css,"@font-face")!==false ) && ( apply_filters("autoptimize_filter_css_fonts_cdn",false)===true) && (!empty($this->cdn_url)) ) {
652
+        } else if ((strpos($css, "@font-face") !== false) && (apply_filters("autoptimize_filter_css_fonts_cdn", false) === true) && (!empty($this->cdn_url))) {
653 653
             // don't late-inject CSS with font-src's if fonts are set to be CDN'ed
654 654
             return false;
655
-        } else if ( (($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui",$css) ) {
655
+        } else if ((($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui", $css)) {
656 656
             // don't late-inject CSS with images if CDN is set OR is image inlining is on
657 657
             return false;
658 658
         } else {
Please login to merge, or discard this patch.
autoptimize.php 1 patch
Spacing   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -12,15 +12,15 @@  discard block
 block discarded – undo
12 12
 http://www.gnu.org/licenses/gpl.txt
13 13
 */
14 14
 
15
-if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
15
+if (!defined('ABSPATH')) exit; // Exit if accessed directly
16 16
 
17
-define('AUTOPTIMIZE_PLUGIN_DIR',plugin_dir_path(__FILE__));
17
+define('AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path(__FILE__));
18 18
 
19 19
 // Load config class
20 20
 include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeConfig.php');
21 21
 
22 22
 // Load toolbar class
23
-include( AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeToolbar.php' );
23
+include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeToolbar.php');
24 24
 
25 25
 // Load partners tab if admin
26 26
 if (is_admin()) {
@@ -28,25 +28,25 @@  discard block
 block discarded – undo
28 28
 }
29 29
 
30 30
 // Do we gzip when caching (needed early to load autoptimizeCache.php)
31
-define('AUTOPTIMIZE_CACHE_NOGZIP',(bool) get_option('autoptimize_cache_nogzip'));
31
+define('AUTOPTIMIZE_CACHE_NOGZIP', (bool) get_option('autoptimize_cache_nogzip'));
32 32
 
33 33
 // Load cache class
34 34
 include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeCache.php');
35 35
 
36 36
 // wp-content dir name (automagically set, should not be needed), dirname of AO cache dir and AO-prefix can be overridden in wp-config.php
37
-if (!defined('AUTOPTIMIZE_WP_CONTENT_NAME')) { define('AUTOPTIMIZE_WP_CONTENT_NAME','/'.wp_basename( WP_CONTENT_DIR )); }
38
-if (!defined('AUTOPTIMIZE_CACHE_CHILD_DIR')) { define('AUTOPTIMIZE_CACHE_CHILD_DIR','/cache/autoptimize/'); }
37
+if (!defined('AUTOPTIMIZE_WP_CONTENT_NAME')) { define('AUTOPTIMIZE_WP_CONTENT_NAME', '/'.wp_basename(WP_CONTENT_DIR)); }
38
+if (!defined('AUTOPTIMIZE_CACHE_CHILD_DIR')) { define('AUTOPTIMIZE_CACHE_CHILD_DIR', '/cache/autoptimize/'); }
39 39
 if (!defined('AUTOPTIMIZE_CACHEFILE_PREFIX')) { define('AUTOPTIMIZE_CACHEFILE_PREFIX', 'autoptimize_'); }
40 40
 
41 41
 // Plugin dir constants (plugin url's defined later to accomodate domain mapped sites)
42
-if (is_multisite() && apply_filters( 'autoptimize_separate_blog_caches' , true )) {
42
+if (is_multisite() && apply_filters('autoptimize_separate_blog_caches', true)) {
43 43
     $blog_id = get_current_blog_id();
44
-    define('AUTOPTIMIZE_CACHE_DIR', WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR.$blog_id.'/' );
44
+    define('AUTOPTIMIZE_CACHE_DIR', WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR.$blog_id.'/');
45 45
 } else {
46 46
     define('AUTOPTIMIZE_CACHE_DIR', WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR);
47 47
 }
48
-define('AUTOPTIMIZE_CACHE_DELAY',true);
49
-define('WP_ROOT_DIR',str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',WP_CONTENT_DIR));
48
+define('AUTOPTIMIZE_CACHE_DELAY', true);
49
+define('WP_ROOT_DIR', str_replace(AUTOPTIMIZE_WP_CONTENT_NAME, '', WP_CONTENT_DIR));
50 50
 
51 51
 // Initialize the cache at least once
52 52
 $conf = autoptimizeConfig::instance();
@@ -54,59 +54,59 @@  discard block
 block discarded – undo
54 54
 /* Check if we're updating, in which case we might need to do stuff and flush the cache
55 55
 to avoid old versions of aggregated files lingering around */
56 56
 
57
-$autoptimize_version="2.1.0";
58
-$autoptimize_db_version=get_option('autoptimize_version','none');
57
+$autoptimize_version = "2.1.0";
58
+$autoptimize_db_version = get_option('autoptimize_version', 'none');
59 59
 
60 60
 if ($autoptimize_db_version !== $autoptimize_version) {
61
-    if ($autoptimize_db_version==="none") {
61
+    if ($autoptimize_db_version === "none") {
62 62
         add_action('admin_notices', 'autoptimize_install_config_notice');
63 63
     } else {
64 64
         // updating, include the update-code
65 65
         include(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizeUpdateCode.php');
66 66
     }
67 67
 
68
-    update_option('autoptimize_version',$autoptimize_version);
69
-    $autoptimize_db_version=$autoptimize_version;
68
+    update_option('autoptimize_version', $autoptimize_version);
69
+    $autoptimize_db_version = $autoptimize_version;
70 70
 }
71 71
 
72 72
 // Load translations
73 73
 function autoptimize_load_plugin_textdomain() {
74
-    load_plugin_textdomain('autoptimize',false,plugin_basename(dirname( __FILE__ )).'/localization');
74
+    load_plugin_textdomain('autoptimize', false, plugin_basename(dirname(__FILE__)).'/localization');
75 75
 }
76
-add_action( 'init', 'autoptimize_load_plugin_textdomain' );
76
+add_action('init', 'autoptimize_load_plugin_textdomain');
77 77
 
78
-function autoptimize_uninstall(){
78
+function autoptimize_uninstall() {
79 79
     autoptimizeCache::clearall();
80 80
 
81
-    $delete_options=array("autoptimize_cache_clean", "autoptimize_cache_nogzip", "autoptimize_css", "autoptimize_css_datauris", "autoptimize_css_justhead", "autoptimize_css_defer", "autoptimize_css_defer_inline", "autoptimize_css_inline", "autoptimize_css_exclude", "autoptimize_html", "autoptimize_html_keepcomments", "autoptimize_js", "autoptimize_js_exclude", "autoptimize_js_forcehead", "autoptimize_js_justhead", "autoptimize_js_trycatch", "autoptimize_version", "autoptimize_show_adv", "autoptimize_cdn_url", "autoptimize_cachesize_notice","autoptimize_css_include_inline","autoptimize_js_include_inline","autoptimize_css_nogooglefont");
81
+    $delete_options = array("autoptimize_cache_clean", "autoptimize_cache_nogzip", "autoptimize_css", "autoptimize_css_datauris", "autoptimize_css_justhead", "autoptimize_css_defer", "autoptimize_css_defer_inline", "autoptimize_css_inline", "autoptimize_css_exclude", "autoptimize_html", "autoptimize_html_keepcomments", "autoptimize_js", "autoptimize_js_exclude", "autoptimize_js_forcehead", "autoptimize_js_justhead", "autoptimize_js_trycatch", "autoptimize_version", "autoptimize_show_adv", "autoptimize_cdn_url", "autoptimize_cachesize_notice", "autoptimize_css_include_inline", "autoptimize_js_include_inline", "autoptimize_css_nogooglefont");
82 82
 
83
-    if ( !is_multisite() ) {
84
-        foreach ($delete_options as $del_opt) {    delete_option( $del_opt ); }
83
+    if (!is_multisite()) {
84
+        foreach ($delete_options as $del_opt) {    delete_option($del_opt); }
85 85
     } else {
86 86
         global $wpdb;
87
-        $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
87
+        $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
88 88
         $original_blog_id = get_current_blog_id();
89
-        foreach ( $blog_ids as $blog_id ) {
90
-            switch_to_blog( $blog_id );
91
-            foreach ($delete_options as $del_opt) {    delete_option( $del_opt ); }
89
+        foreach ($blog_ids as $blog_id) {
90
+            switch_to_blog($blog_id);
91
+            foreach ($delete_options as $del_opt) {    delete_option($del_opt); }
92 92
         }
93
-        switch_to_blog( $original_blog_id );
93
+        switch_to_blog($original_blog_id);
94 94
     }
95 95
 
96
-    if ( wp_get_schedule( 'ao_cachechecker' ) ) {
97
-        wp_clear_scheduled_hook( 'ao_cachechecker' );
96
+    if (wp_get_schedule('ao_cachechecker')) {
97
+        wp_clear_scheduled_hook('ao_cachechecker');
98 98
     }
99 99
 }
100 100
 
101 101
 function autoptimize_install_config_notice() {
102 102
     echo '<div class="updated"><p>';
103
-    _e('Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' );
103
+    _e('Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize');
104 104
     echo '</p></div>';
105 105
 }
106 106
 
107 107
 function autoptimize_update_config_notice() {
108 108
     echo '<div class="updated"><p>';
109
-    _e('Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize' );
109
+    _e('Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize');
110 110
     echo '</p></div>';
111 111
 }
112 112
 
@@ -126,21 +126,21 @@  discard block
 block discarded – undo
126 126
     $ao_noptimize = false;
127 127
 
128 128
     // noptimize in qs to get non-optimized page for debugging
129
-    if (array_key_exists("ao_noptimize",$_GET)) {
130
-        if ( ($_GET["ao_noptimize"]==="1") && (apply_filters('autoptimize_filter_honor_qs_noptimize',true)) ) {
129
+    if (array_key_exists("ao_noptimize", $_GET)) {
130
+        if (($_GET["ao_noptimize"] === "1") && (apply_filters('autoptimize_filter_honor_qs_noptimize', true))) {
131 131
             $ao_noptimize = true;
132 132
         }
133 133
     }
134 134
 
135 135
     // check for DONOTMINIFY constant as used by e.g. WooCommerce POS
136
-    if (defined('DONOTMINIFY') && (constant('DONOTMINIFY')===true || constant('DONOTMINIFY')==="true")) {
136
+    if (defined('DONOTMINIFY') && (constant('DONOTMINIFY') === true || constant('DONOTMINIFY') === "true")) {
137 137
         $ao_noptimize = true;
138 138
     }
139 139
 
140 140
     // filter you can use to block autoptimization on your own terms
141
-    $ao_noptimize = (bool) apply_filters( 'autoptimize_filter_noptimize', $ao_noptimize );
141
+    $ao_noptimize = (bool) apply_filters('autoptimize_filter_noptimize', $ao_noptimize);
142 142
 
143
-    if (!is_feed() && !$ao_noptimize && !is_admin() && ( !function_exists('is_customize_preview') || !is_customize_preview() ) ) {
143
+    if (!is_feed() && !$ao_noptimize && !is_admin() && (!function_exists('is_customize_preview') || !is_customize_preview())) {
144 144
         // Config element
145 145
         $conf = autoptimizeConfig::instance();
146 146
 
@@ -148,7 +148,7 @@  discard block
 block discarded – undo
148 148
         include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeBase.php');
149 149
 
150 150
         // Load extra classes and set some vars
151
-        if($conf->get('autoptimize_html')) {
151
+        if ($conf->get('autoptimize_html')) {
152 152
             include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeHTML.php');
153 153
             // BUG: new minify-html does not support keeping HTML comments, skipping for now
154 154
             // if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) {
@@ -158,7 +158,7 @@  discard block
 block discarded – undo
158 158
             // }
159 159
         }
160 160
 
161
-        if($conf->get('autoptimize_js')) {
161
+        if ($conf->get('autoptimize_js')) {
162 162
             include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeScripts.php');
163 163
             if (!class_exists('JSMin')) {
164 164
                 if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) {
@@ -167,15 +167,15 @@  discard block
 block discarded – undo
167 167
                     @include(AUTOPTIMIZE_PLUGIN_DIR.'classes/external/php/minify-2.3.1-jsmin.php');
168 168
                 }
169 169
             }
170
-            if ( ! defined( 'CONCATENATE_SCRIPTS' )) {
171
-                define('CONCATENATE_SCRIPTS',false);
170
+            if (!defined('CONCATENATE_SCRIPTS')) {
171
+                define('CONCATENATE_SCRIPTS', false);
172 172
             }
173
-            if ( ! defined( 'COMPRESS_SCRIPTS' )) {
174
-                define('COMPRESS_SCRIPTS',false);
173
+            if (!defined('COMPRESS_SCRIPTS')) {
174
+                define('COMPRESS_SCRIPTS', false);
175 175
             }
176 176
         }
177 177
 
178
-        if($conf->get('autoptimize_css')) {
178
+        if ($conf->get('autoptimize_css')) {
179 179
             include(AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeStyles.php');
180 180
             if (defined('AUTOPTIMIZE_LEGACY_MINIFIERS')) {
181 181
                 if (!class_exists('Minify_CSS_Compressor')) {
@@ -186,13 +186,13 @@  discard block
 block discarded – undo
186 186
                     @include(AUTOPTIMIZE_PLUGIN_DIR.'classes/external/php/yui-php-cssmin-2.4.8-4_fgo.php');
187 187
                 }
188 188
             }
189
-            if ( ! defined( 'COMPRESS_CSS' )) {
190
-                define('COMPRESS_CSS',false);
189
+            if (!defined('COMPRESS_CSS')) {
190
+                define('COMPRESS_CSS', false);
191 191
             }
192 192
         }
193 193
 
194 194
         // filter to be used with care, kills all output buffers when true. use with extreme caution. you have been warned!
195
-        if (apply_filters('autoptimize_filter_obkiller',false)) {
195
+        if (apply_filters('autoptimize_filter_obkiller', false)) {
196 196
             while (ob_get_level() > 0) {
197 197
                 ob_end_clean();
198 198
             }
@@ -205,35 +205,35 @@  discard block
 block discarded – undo
205 205
 
206 206
 // Action on end, this is where the magic happens
207 207
 function autoptimize_end_buffering($content) {
208
-    if ( ((stripos($content,"<html") === false) && (stripos($content,"<!DOCTYPE html") === false)) || preg_match('/<html[^>]*(?:amp|⚡)/',$content) === 1 || stripos($content,"<xsl:stylesheet") !== false ) { return $content; }
208
+    if (((stripos($content, "<html") === false) && (stripos($content, "<!DOCTYPE html") === false)) || preg_match('/<html[^>]*(?:amp|⚡)/', $content) === 1 || stripos($content, "<xsl:stylesheet") !== false) { return $content; }
209 209
     
210 210
     // load URL constants as late as possible to allow domain mapper to kick in
211 211
     if (function_exists("domain_mapping_siteurl")) {
212
-        define('AUTOPTIMIZE_WP_SITE_URL',domain_mapping_siteurl(get_current_blog_id()));
213
-        define('AUTOPTIMIZE_WP_CONTENT_URL',str_replace(get_original_url(AUTOPTIMIZE_WP_SITE_URL),AUTOPTIMIZE_WP_SITE_URL,content_url()));
212
+        define('AUTOPTIMIZE_WP_SITE_URL', domain_mapping_siteurl(get_current_blog_id()));
213
+        define('AUTOPTIMIZE_WP_CONTENT_URL', str_replace(get_original_url(AUTOPTIMIZE_WP_SITE_URL), AUTOPTIMIZE_WP_SITE_URL, content_url()));
214 214
     } else {
215
-        define('AUTOPTIMIZE_WP_SITE_URL',site_url());
216
-        define('AUTOPTIMIZE_WP_CONTENT_URL',content_url());
215
+        define('AUTOPTIMIZE_WP_SITE_URL', site_url());
216
+        define('AUTOPTIMIZE_WP_CONTENT_URL', content_url());
217 217
     }
218 218
 
219
-    if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches' , true ) ) {
219
+    if (is_multisite() && apply_filters('autoptimize_separate_blog_caches', true)) {
220 220
         $blog_id = get_current_blog_id();
221
-        define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR.$blog_id.'/' );
221
+        define('AUTOPTIMIZE_CACHE_URL', AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR.$blog_id.'/');
222 222
     } else {
223
-        define('AUTOPTIMIZE_CACHE_URL',AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR);
223
+        define('AUTOPTIMIZE_CACHE_URL', AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR);
224 224
     }
225
-    define('AUTOPTIMIZE_WP_ROOT_URL',str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',AUTOPTIMIZE_WP_CONTENT_URL));
225
+    define('AUTOPTIMIZE_WP_ROOT_URL', str_replace(AUTOPTIMIZE_WP_CONTENT_NAME, '', AUTOPTIMIZE_WP_CONTENT_URL));
226 226
 
227 227
     // Config element
228 228
     $conf = autoptimizeConfig::instance();
229 229
 
230 230
     // Choose the classes
231 231
     $classes = array();
232
-    if($conf->get('autoptimize_js'))
232
+    if ($conf->get('autoptimize_js'))
233 233
         $classes[] = 'autoptimizeScripts';
234
-    if($conf->get('autoptimize_css'))
234
+    if ($conf->get('autoptimize_css'))
235 235
         $classes[] = 'autoptimizeStyles';
236
-    if($conf->get('autoptimize_html'))
236
+    if ($conf->get('autoptimize_html'))
237 237
         $classes[] = 'autoptimizeHTML';
238 238
 
239 239
     // Set some options
@@ -262,12 +262,12 @@  discard block
 block discarded – undo
262 262
         )
263 263
     );
264 264
 
265
-    $content = apply_filters( 'autoptimize_filter_html_before_minify', $content );
265
+    $content = apply_filters('autoptimize_filter_html_before_minify', $content);
266 266
 
267 267
     // Run the classes
268
-    foreach($classes as $name) {
268
+    foreach ($classes as $name) {
269 269
         $instance = new $name($content);
270
-        if($instance->read($classoptions[$name])) {
270
+        if ($instance->read($classoptions[$name])) {
271 271
             $instance->minify();
272 272
             $instance->cache();
273 273
             $content = $instance->getcontent();
@@ -275,19 +275,19 @@  discard block
 block discarded – undo
275 275
         unset($instance);
276 276
     }
277 277
     
278
-    $content = apply_filters( 'autoptimize_html_after_minify', $content );
278
+    $content = apply_filters('autoptimize_html_after_minify', $content);
279 279
     return $content;
280 280
 }
281 281
 
282
-if ( autoptimizeCache::cacheavail() ) {
282
+if (autoptimizeCache::cacheavail()) {
283 283
     $conf = autoptimizeConfig::instance();
284
-    if( $conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') ) {
284
+    if ($conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css')) {
285 285
         // Hook to wordpress
286 286
         if (defined('AUTOPTIMIZE_INIT_EARLIER')) {
287
-            add_action('init','autoptimize_start_buffering',-1);
287
+            add_action('init', 'autoptimize_start_buffering', -1);
288 288
         } else {
289 289
             if (!defined('AUTOPTIMIZE_HOOK_INTO')) { define('AUTOPTIMIZE_HOOK_INTO', 'template_redirect'); }
290
-            add_action(constant("AUTOPTIMIZE_HOOK_INTO"),'autoptimize_start_buffering',2);
290
+            add_action(constant("AUTOPTIMIZE_HOOK_INTO"), 'autoptimize_start_buffering', 2);
291 291
         }
292 292
     }
293 293
 } else {
@@ -295,9 +295,9 @@  discard block
 block discarded – undo
295 295
 }
296 296
 
297 297
 function autoptimize_activate() {
298
-    register_uninstall_hook( __FILE__, 'autoptimize_uninstall' );
298
+    register_uninstall_hook(__FILE__, 'autoptimize_uninstall');
299 299
 }
300
-register_activation_hook( __FILE__, 'autoptimize_activate' );
300
+register_activation_hook(__FILE__, 'autoptimize_activate');
301 301
 
302 302
 include_once('classlesses/autoptimizeCacheChecker.php');
303 303
 
Please login to merge, or discard this patch.