Completed
Push — master ( 405098...6ca290 )
by frank
02:23
created
classes/autoptimizeScripts.php 2 patches
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -128,27 +128,27 @@  discard block
 block discarded – undo
128 128
                             // should we add flags?
129 129
                             foreach ($excludeJS as $exclTag => $exclFlags) {
130 130
                                 if ( strpos($origTag,$exclTag)!==false && in_array($exclFlags,array("async","defer")) ) {
131
-                                   $newTag = str_replace('<script ','<script '.$exclFlags.' ',$newTag);
131
+                                    $newTag = str_replace('<script ','<script '.$exclFlags.' ',$newTag);
132 132
                                 }
133 133
                             }
134 134
                         }
135 135
                         
136
-   						// should we minify the non-aggregated script?
137
-						if ($path && apply_filters('autoptimize_filter_js_minify_excluded',false)) {
138
-							$_CachedMinifiedUrl = $this->minify_single($path);
136
+                            // should we minify the non-aggregated script?
137
+                        if ($path && apply_filters('autoptimize_filter_js_minify_excluded',false)) {
138
+                            $_CachedMinifiedUrl = $this->minify_single($path);
139 139
 
140
-							// replace orig URL with minified URL from cache if so
141
-							if (!empty($_CachedMinifiedUrl)) {
142
-								$newTag = str_replace($url, $_CachedMinifiedUrl, $newTag);
143
-							}
140
+                            // replace orig URL with minified URL from cache if so
141
+                            if (!empty($_CachedMinifiedUrl)) {
142
+                                $newTag = str_replace($url, $_CachedMinifiedUrl, $newTag);
143
+                            }
144 144
 							
145
-							// remove querystring from URL in newTag
146
-							if ( !empty($explUrl[1]) ) {
147
-								$newTag = str_replace("?".$explUrl[1],"",$newTag);
148
-							}
149
-						}
145
+                            // remove querystring from URL in newTag
146
+                            if ( !empty($explUrl[1]) ) {
147
+                                $newTag = str_replace("?".$explUrl[1],"",$newTag);
148
+                            }
149
+                        }
150 150
 
151
-						// should we move the non-aggregated script?
151
+                        // should we move the non-aggregated script?
152 152
                         if( $this->ismovable($newTag) ) {
153 153
                             // can be moved, flags and all
154 154
                             if( $this->movetolast($newTag) )    {
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
         
262 262
         //$this->jscode has all the uncompressed code now.
263 263
         if ($this->alreadyminified!==true) {
264
-          if (class_exists('JSMin') && apply_filters( 'autoptimize_js_do_minify' , true)) {
264
+            if (class_exists('JSMin') && apply_filters( 'autoptimize_js_do_minify' , true)) {
265 265
             if (@is_callable(array("JSMin","minify"))) {
266 266
                 $tmp_jscode = trim(JSMin::minify($this->jscode));
267 267
                 if (!empty($tmp_jscode)) {
@@ -275,10 +275,10 @@  discard block
 block discarded – undo
275 275
                 $this->jscode = $this->inject_minified($this->jscode);
276 276
                 return false;
277 277
             }
278
-          } else {
279
-              $this->jscode = $this->inject_minified($this->jscode);
278
+            } else {
279
+                $this->jscode = $this->inject_minified($this->jscode);
280 280
             return false;
281
-          }
281
+            }
282 282
         }
283 283
         return true;
284 284
     }
@@ -340,8 +340,8 @@  discard block
 block discarded – undo
340 340
     
341 341
     // Checks against the white- and blacklists
342 342
     private function ismergeable($tag) {
343
-		if (apply_filters('autoptimize_filter_js_dontaggregate',false)) {
344
-			return false;
343
+        if (apply_filters('autoptimize_filter_js_dontaggregate',false)) {
344
+            return false;
345 345
         } else if (!empty($this->whitelist)) {
346 346
             foreach ($this->whitelist as $match) {
347 347
                 if(strpos($tag,$match)!==false) {
Please login to merge, or discard this patch.
Spacing   +112 added lines, -112 removed lines patch added patch discarded remove patch
@@ -1,11 +1,11 @@  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 autoptimizeScripts extends autoptimizeBase {
5 5
     private $scripts = array();
6
-    private $dontmove = array('document.write','html5.js','show_ads.js','google_ad','blogcatalog.com/w','tweetmeme.com/i','mybloglog.com/','histats.com/js','ads.smowtion.com/ad.js','statcounter.com/counter/counter.js','widgets.amung.us','ws.amazon.com/widgets','media.fastclick.net','/ads/','comment-form-quicktags/quicktags.php','edToolbar','intensedebate.com','scripts.chitika.net/','_gaq.push','jotform.com/','admin-bar.min.js','GoogleAnalyticsObject','plupload.full.min.js','syntaxhighlighter','adsbygoogle','gist.github.com','_stq','nonce','post_id','data-noptimize');
7
-    private $domove = array('gaJsHost','load_cmc','jd.gallery.transitions.js','swfobject.embedSWF(','tiny_mce.js','tinyMCEPreInit.go');
8
-    private $domovelast = array('addthis.com','/afsonline/show_afs_search.js','disqus.js','networkedblogs.com/getnetworkwidget','infolinks.com/js/','jd.gallery.js.php','jd.gallery.transitions.js','swfobject.embedSWF(','linkwithin.com/widget.js','tiny_mce.js','tinyMCEPreInit.go');
6
+    private $dontmove = array('document.write', 'html5.js', 'show_ads.js', 'google_ad', 'blogcatalog.com/w', 'tweetmeme.com/i', 'mybloglog.com/', 'histats.com/js', 'ads.smowtion.com/ad.js', 'statcounter.com/counter/counter.js', 'widgets.amung.us', 'ws.amazon.com/widgets', 'media.fastclick.net', '/ads/', 'comment-form-quicktags/quicktags.php', 'edToolbar', 'intensedebate.com', 'scripts.chitika.net/', '_gaq.push', 'jotform.com/', 'admin-bar.min.js', 'GoogleAnalyticsObject', 'plupload.full.min.js', 'syntaxhighlighter', 'adsbygoogle', 'gist.github.com', '_stq', 'nonce', 'post_id', 'data-noptimize');
7
+    private $domove = array('gaJsHost', 'load_cmc', 'jd.gallery.transitions.js', 'swfobject.embedSWF(', 'tiny_mce.js', 'tinyMCEPreInit.go');
8
+    private $domovelast = array('addthis.com', '/afsonline/show_afs_search.js', 'disqus.js', 'networkedblogs.com/getnetworkwidget', 'infolinks.com/js/', 'jd.gallery.js.php', 'jd.gallery.transitions.js', 'swfobject.embedSWF(', 'linkwithin.com/widget.js', 'tiny_mce.js', 'tinyMCEPreInit.go');
9 9
     private $trycatch = false;
10 10
     private $alreadyminified = false;
11 11
     private $forcehead = true;
@@ -21,70 +21,70 @@  discard block
 block discarded – undo
21 21
     
22 22
     //Reads the page and collects script tags
23 23
     public function read($options) {
24
-        $noptimizeJS = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content );
24
+        $noptimizeJS = apply_filters('autoptimize_filter_js_noptimize', false, $this->content);
25 25
         if ($noptimizeJS) return false;
26 26
 
27 27
         // only optimize known good JS?
28
-        $whitelistJS = apply_filters( 'autoptimize_filter_js_whitelist', "" );
28
+        $whitelistJS = apply_filters('autoptimize_filter_js_whitelist', "");
29 29
         if (!empty($whitelistJS)) {
30
-            $this->whitelist = array_filter(array_map('trim',explode(",",$whitelistJS)));
30
+            $this->whitelist = array_filter(array_map('trim', explode(",", $whitelistJS)));
31 31
         }
32 32
 
33 33
         // is there JS we should simply remove
34
-        $removableJS = apply_filters( 'autoptimize_filter_js_removables', '');
34
+        $removableJS = apply_filters('autoptimize_filter_js_removables', '');
35 35
         if (!empty($removableJS)) {
36
-            $this->jsremovables = array_filter(array_map('trim',explode(",",$removableJS)));
36
+            $this->jsremovables = array_filter(array_map('trim', explode(",", $removableJS)));
37 37
         }
38 38
 
39 39
         // only header?
40
-        if( apply_filters('autoptimize_filter_js_justhead',$options['justhead']) == true ) {
41
-            $content = explode('</head>',$this->content,2);
40
+        if (apply_filters('autoptimize_filter_js_justhead', $options['justhead']) == true) {
41
+            $content = explode('</head>', $this->content, 2);
42 42
             $this->content = $content[0].'</head>';
43 43
             $this->restofcontent = $content[1];
44 44
         }
45 45
         
46 46
         // include inline?
47
-        if( apply_filters('autoptimize_js_include_inline',$options['include_inline']) == true ) {
47
+        if (apply_filters('autoptimize_js_include_inline', $options['include_inline']) == true) {
48 48
             $this->include_inline = true;
49 49
         }
50 50
 
51 51
         // filter to "late inject minified JS", default to true for now (it is faster)
52
-        $this->inject_min_late = apply_filters('autoptimize_filter_js_inject_min_late',true);
52
+        $this->inject_min_late = apply_filters('autoptimize_filter_js_inject_min_late', true);
53 53
 
54 54
         // filters to override hardcoded do(nt)move(last) array contents (array in, array out!)
55
-        $this->dontmove = apply_filters( 'autoptimize_filter_js_dontmove', $this->dontmove );        
56
-        $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast );
57
-        $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove );
55
+        $this->dontmove = apply_filters('autoptimize_filter_js_dontmove', $this->dontmove);        
56
+        $this->domovelast = apply_filters('autoptimize_filter_js_movelast', $this->domovelast);
57
+        $this->domove = apply_filters('autoptimize_filter_js_domove', $this->domove);
58 58
 
59 59
         // get extra exclusions settings or filter
60 60
         $excludeJS = $options['js_exclude'];
61
-        $excludeJS = apply_filters( 'autoptimize_filter_js_exclude', $excludeJS );
62
-        if ($excludeJS!=="") {
61
+        $excludeJS = apply_filters('autoptimize_filter_js_exclude', $excludeJS);
62
+        if ($excludeJS !== "") {
63 63
             if (is_array($excludeJS)) {
64
-                if(($removeKeys = array_keys($excludeJS,"remove")) !== false) {
64
+                if (($removeKeys = array_keys($excludeJS, "remove")) !== false) {
65 65
                     foreach ($removeKeys as $removeKey) {
66 66
                         unset($excludeJS[$removeKey]);
67
-                        $this->jsremovables[]=$removeKey;
67
+                        $this->jsremovables[] = $removeKey;
68 68
                     }
69 69
                 }
70 70
                 $exclJSArr = array_keys($excludeJS);
71 71
             } else {
72
-                $exclJSArr = array_filter(array_map('trim',explode(",",$excludeJS)));
72
+                $exclJSArr = array_filter(array_map('trim', explode(",", $excludeJS)));
73 73
             }
74
-            $this->dontmove = array_merge($exclJSArr,$this->dontmove);
74
+            $this->dontmove = array_merge($exclJSArr, $this->dontmove);
75 75
         }
76 76
 
77 77
         //Should we add try-catch?
78
-        if($options['trycatch'] == true)
78
+        if ($options['trycatch'] == true)
79 79
             $this->trycatch = true;
80 80
 
81 81
         // force js in head?    
82
-        if($options['forcehead'] == true) {
82
+        if ($options['forcehead'] == true) {
83 83
             $this->forcehead = true;
84 84
         } else {
85 85
             $this->forcehead = false;
86 86
         }
87
-        $this->forcehead = apply_filters( 'autoptimize_filter_js_forcehead', $this->forcehead );
87
+        $this->forcehead = apply_filters('autoptimize_filter_js_forcehead', $this->forcehead);
88 88
 
89 89
         // get cdn url
90 90
         $this->cdn_url = $options['cdn_url'];
@@ -99,24 +99,24 @@  discard block
 block discarded – undo
99 99
         $this->content = $this->hide_comments($this->content);
100 100
 
101 101
         // Get script files
102
-        if (preg_match_all('#<script.*</script>#Usmi',$this->content,$matches)) {
103
-            foreach($matches[0] as $tag) {
102
+        if (preg_match_all('#<script.*</script>#Usmi', $this->content, $matches)) {
103
+            foreach ($matches[0] as $tag) {
104 104
                 // only consider script aggregation for types whitelisted in should_aggregate-function
105
-                if( !$this->should_aggregate($tag) ) {
106
-                    $tag='';
105
+                if (!$this->should_aggregate($tag)) {
106
+                    $tag = '';
107 107
                     continue;
108 108
                 }
109 109
 
110
-                if (preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi',$tag,$source)) {
110
+                if (preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source)) {
111 111
                     // non-inline script
112
-                    if ($this->isremovable($tag,$this->jsremovables)) {
113
-                        $this->content = str_replace($tag,'',$this->content);
112
+                    if ($this->isremovable($tag, $this->jsremovables)) {
113
+                        $this->content = str_replace($tag, '', $this->content);
114 114
                         continue;
115 115
                     }
116
-                    $explUrl = explode('?',$source[2],2);
116
+                    $explUrl = explode('?', $source[2], 2);
117 117
                     $url = $explUrl[0];
118 118
                     $path = $this->getpath($url);
119
-                    if($path !== false && preg_match('#\.js$#',$path) && $this->ismergeable($tag)) {
119
+                    if ($path !== false && preg_match('#\.js$#', $path) && $this->ismergeable($tag)) {
120 120
                         // ok to optimize, add to array
121 121
                         $this->scripts[] = $path;
122 122
                     } else {
@@ -127,14 +127,14 @@  discard block
 block discarded – undo
127 127
                         if (is_array($excludeJS)) {
128 128
                             // should we add flags?
129 129
                             foreach ($excludeJS as $exclTag => $exclFlags) {
130
-                                if ( strpos($origTag,$exclTag)!==false && in_array($exclFlags,array("async","defer")) ) {
131
-                                   $newTag = str_replace('<script ','<script '.$exclFlags.' ',$newTag);
130
+                                if (strpos($origTag, $exclTag) !== false && in_array($exclFlags, array("async", "defer"))) {
131
+                                   $newTag = str_replace('<script ', '<script '.$exclFlags.' ', $newTag);
132 132
                                 }
133 133
                             }
134 134
                         }
135 135
                         
136 136
    						// should we minify the non-aggregated script?
137
-						if ($path && apply_filters('autoptimize_filter_js_minify_excluded',false)) {
137
+						if ($path && apply_filters('autoptimize_filter_js_minify_excluded', false)) {
138 138
 							$_CachedMinifiedUrl = $this->minify_single($path);
139 139
 
140 140
 							// replace orig URL with minified URL from cache if so
@@ -143,23 +143,23 @@  discard block
 block discarded – undo
143 143
 							}
144 144
 							
145 145
 							// remove querystring from URL in newTag
146
-							if ( !empty($explUrl[1]) ) {
147
-								$newTag = str_replace("?".$explUrl[1],"",$newTag);
146
+							if (!empty($explUrl[1])) {
147
+								$newTag = str_replace("?".$explUrl[1], "", $newTag);
148 148
 							}
149 149
 						}
150 150
 
151 151
 						// should we move the non-aggregated script?
152
-                        if( $this->ismovable($newTag) ) {
152
+                        if ($this->ismovable($newTag)) {
153 153
                             // can be moved, flags and all
154
-                            if( $this->movetolast($newTag) )    {
154
+                            if ($this->movetolast($newTag)) {
155 155
                                 $this->move['last'][] = $newTag;
156 156
                             } else {
157 157
                                 $this->move['first'][] = $newTag;
158 158
                             }
159 159
                         } else {
160 160
                             // cannot be moved, so if flag was added re-inject altered tag immediately
161
-                            if ( $origTag !== $newTag ) {
162
-                                $this->content = str_replace($origTag,$newTag,$this->content);
161
+                            if ($origTag !== $newTag) {
162
+                                $this->content = str_replace($origTag, $newTag, $this->content);
163 163
                                 $origTag = '';
164 164
                             }
165 165
                             // and forget about the $tag (not to be touched any more)
@@ -168,23 +168,23 @@  discard block
 block discarded – undo
168 168
                     }
169 169
                 } else {
170 170
                     // Inline script
171
-                    if ($this->isremovable($tag,$this->jsremovables)) {
172
-                        $this->content = str_replace($tag,'',$this->content);
171
+                    if ($this->isremovable($tag, $this->jsremovables)) {
172
+                        $this->content = str_replace($tag, '', $this->content);
173 173
                         continue;
174 174
                     }
175 175
                     
176 176
                     // unhide comments, as javascript may be wrapped in comment-tags for old times' sake
177 177
                     $tag = $this->restore_comments($tag);
178
-                    if($this->ismergeable($tag) && ( $this->include_inline )) {
179
-                        preg_match('#<script.*>(.*)</script>#Usmi',$tag,$code);
180
-                        $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm','$1',$code[1]);
181
-                        $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/','',$code);
178
+                    if ($this->ismergeable($tag) && ($this->include_inline)) {
179
+                        preg_match('#<script.*>(.*)</script>#Usmi', $tag, $code);
180
+                        $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1]);
181
+                        $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code);
182 182
                         $this->scripts[] = 'INLINE;'.$code;
183 183
                     } else {
184 184
                         // Can we move this?
185
-                        $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag );
186
-                        if( $this->ismovable($tag) || $autoptimize_js_moveable !== '' ) {
187
-                            if( $this->movetolast($tag) || $autoptimize_js_moveable === 'last' ) {
185
+                        $autoptimize_js_moveable = apply_filters('autoptimize_js_moveable', '', $tag);
186
+                        if ($this->ismovable($tag) || $autoptimize_js_moveable !== '') {
187
+                            if ($this->movetolast($tag) || $autoptimize_js_moveable === 'last') {
188 188
                                 $this->move['last'][] = $tag;
189 189
                             } else {
190 190
                                 $this->move['first'][] = $tag;
@@ -199,7 +199,7 @@  discard block
 block discarded – undo
199 199
                 }
200 200
                 
201 201
                 //Remove the original script tag
202
-                $this->content = str_replace($tag,'',$this->content);
202
+                $this->content = str_replace($tag, '', $this->content);
203 203
             }
204 204
             
205 205
             return true;
@@ -211,37 +211,37 @@  discard block
 block discarded – undo
211 211
     
212 212
     //Joins and optimizes JS
213 213
     public function minify() {
214
-        foreach($this->scripts as $script) {
215
-            if(preg_match('#^INLINE;#',$script)) {
214
+        foreach ($this->scripts as $script) {
215
+            if (preg_match('#^INLINE;#', $script)) {
216 216
                 //Inline script
217
-                $script = preg_replace('#^INLINE;#','',$script);
218
-                $script = rtrim( $script, ";\n\t\r" ) . ';';
217
+                $script = preg_replace('#^INLINE;#', '', $script);
218
+                $script = rtrim($script, ";\n\t\r").';';
219 219
                 //Add try-catch?
220
-                if($this->trycatch) {
220
+                if ($this->trycatch) {
221 221
                     $script = 'try{'.$script.'}catch(e){}';
222 222
                 }
223
-                $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, "" );
224
-                if ( has_filter('autoptimize_js_individual_script') && !empty($tmpscript) ) {
225
-                    $script=$tmpscript;
226
-                    $this->alreadyminified=true;
223
+                $tmpscript = apply_filters('autoptimize_js_individual_script', $script, "");
224
+                if (has_filter('autoptimize_js_individual_script') && !empty($tmpscript)) {
225
+                    $script = $tmpscript;
226
+                    $this->alreadyminified = true;
227 227
                 }
228
-                $this->jscode .= "\n" . $script;
228
+                $this->jscode .= "\n".$script;
229 229
             } else {
230 230
                 //External script
231
-                if($script !== false && file_exists($script) && is_readable($script)) {
231
+                if ($script !== false && file_exists($script) && is_readable($script)) {
232 232
                     $scriptsrc = file_get_contents($script);
233
-                    $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$scriptsrc);
234
-                    $scriptsrc = rtrim($scriptsrc,";\n\t\r").';';
233
+                    $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc);
234
+                    $scriptsrc = rtrim($scriptsrc, ";\n\t\r").';';
235 235
                     //Add try-catch?
236
-                    if($this->trycatch) {
236
+                    if ($this->trycatch) {
237 237
                         $scriptsrc = 'try{'.$scriptsrc.'}catch(e){}';
238 238
                     }
239
-                    $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script );
240
-                    if ( has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc) ) {
241
-                        $scriptsrc=$tmpscriptsrc;
242
-                        $this->alreadyminified=true;
243
-                    } else if ((( strpos($script,"min.js") !== false ) || ( strpos($script,"wp-includes/js/jquery/jquery.js") !== false )) && ( $this->inject_min_late === true )) {
244
-                        $scriptsrc="%%INJECTLATER%%".base64_encode($script)."|".md5($scriptsrc)."%%INJECTLATER%%";
239
+                    $tmpscriptsrc = apply_filters('autoptimize_js_individual_script', $scriptsrc, $script);
240
+                    if (has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc)) {
241
+                        $scriptsrc = $tmpscriptsrc;
242
+                        $this->alreadyminified = true;
243
+                    } else if (((strpos($script, "min.js") !== false) || (strpos($script, "wp-includes/js/jquery/jquery.js") !== false)) && ($this->inject_min_late === true)) {
244
+                        $scriptsrc = "%%INJECTLATER%%".base64_encode($script)."|".md5($scriptsrc)."%%INJECTLATER%%";
245 245
                     }
246 246
                     $this->jscode .= "\n".$scriptsrc;
247 247
                 }/*else{
@@ -252,24 +252,24 @@  discard block
 block discarded – undo
252 252
 
253 253
         //Check for already-minified code
254 254
         $this->md5hash = md5($this->jscode);
255
-        $ccheck = new autoptimizeCache($this->md5hash,'js');
256
-        if($ccheck->check()) {
255
+        $ccheck = new autoptimizeCache($this->md5hash, 'js');
256
+        if ($ccheck->check()) {
257 257
             $this->jscode = $ccheck->retrieve();
258 258
             return true;
259 259
         }
260 260
         unset($ccheck);
261 261
         
262 262
         //$this->jscode has all the uncompressed code now.
263
-        if ($this->alreadyminified!==true) {
264
-          if (class_exists('JSMin') && apply_filters( 'autoptimize_js_do_minify' , true)) {
265
-            if (@is_callable(array("JSMin","minify"))) {
263
+        if ($this->alreadyminified !== true) {
264
+          if (class_exists('JSMin') && apply_filters('autoptimize_js_do_minify', true)) {
265
+            if (@is_callable(array("JSMin", "minify"))) {
266 266
                 $tmp_jscode = trim(JSMin::minify($this->jscode));
267 267
                 if (!empty($tmp_jscode)) {
268 268
                     $this->jscode = $tmp_jscode;
269 269
                     unset($tmp_jscode);
270 270
                 }
271 271
                 $this->jscode = $this->inject_minified($this->jscode);
272
-                $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode );
272
+                $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode);
273 273
                 return true;
274 274
             } else {
275 275
                 $this->jscode = $this->inject_minified($this->jscode);
@@ -284,11 +284,11 @@  discard block
 block discarded – undo
284 284
     }
285 285
     
286 286
     //Caches the JS in uncompressed, deflated and gzipped form.
287
-    public function cache()    {
288
-        $cache = new autoptimizeCache($this->md5hash,'js');
289
-        if(!$cache->check()) {
287
+    public function cache() {
288
+        $cache = new autoptimizeCache($this->md5hash, 'js');
289
+        if (!$cache->check()) {
290 290
             //Cache our code
291
-            $cache->cache($this->jscode,'text/javascript');
291
+            $cache->cache($this->jscode, 'text/javascript');
292 292
         }
293 293
         $this->url = AUTOPTIMIZE_CACHE_URL.$cache->getname();
294 294
         $this->url = $this->url_replace_cdn($this->url);
@@ -297,32 +297,32 @@  discard block
 block discarded – undo
297 297
     // Returns the content
298 298
     public function getcontent() {
299 299
         // Restore the full content
300
-        if(!empty($this->restofcontent)) {
300
+        if (!empty($this->restofcontent)) {
301 301
             $this->content .= $this->restofcontent;
302 302
             $this->restofcontent = '';
303 303
         }
304 304
         
305 305
         // Add the scripts taking forcehead/ deferred (default) into account
306
-        if($this->forcehead == true) {
307
-            $replaceTag=array("</head>","before");
308
-            $defer="";
306
+        if ($this->forcehead == true) {
307
+            $replaceTag = array("</head>", "before");
308
+            $defer = "";
309 309
         } else {
310
-            $replaceTag=array("</body>","before");
311
-            $defer="defer ";
310
+            $replaceTag = array("</body>", "before");
311
+            $defer = "defer ";
312 312
         }
313 313
         
314
-        $defer = apply_filters( 'autoptimize_filter_js_defer', $defer );
314
+        $defer = apply_filters('autoptimize_filter_js_defer', $defer);
315 315
         $bodyreplacementpayload = '<script type="text/javascript" '.$defer.'src="'.$this->url.'"></script>';
316
-        $bodyreplacementpayload = apply_filters('autoptimize_filter_js_bodyreplacementpayload',$bodyreplacementpayload);
316
+        $bodyreplacementpayload = apply_filters('autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload);
317 317
 
318
-        $bodyreplacement = implode('',$this->move['first']);
318
+        $bodyreplacement = implode('', $this->move['first']);
319 319
         $bodyreplacement .= $bodyreplacementpayload;
320
-        $bodyreplacement .= implode('',$this->move['last']);
320
+        $bodyreplacement .= implode('', $this->move['last']);
321 321
 
322
-        $replaceTag = apply_filters( 'autoptimize_filter_js_replacetag', $replaceTag );
322
+        $replaceTag = apply_filters('autoptimize_filter_js_replacetag', $replaceTag);
323 323
 
324
-        if (strlen($this->jscode)>0) {
325
-            $this->inject_in_html($bodyreplacement,$replaceTag);
324
+        if (strlen($this->jscode) > 0) {
325
+            $this->inject_in_html($bodyreplacement, $replaceTag);
326 326
         }
327 327
         
328 328
         // restore comments
@@ -340,19 +340,19 @@  discard block
 block discarded – undo
340 340
     
341 341
     // Checks against the white- and blacklists
342 342
     private function ismergeable($tag) {
343
-		if (apply_filters('autoptimize_filter_js_dontaggregate',false)) {
343
+		if (apply_filters('autoptimize_filter_js_dontaggregate', false)) {
344 344
 			return false;
345 345
         } else if (!empty($this->whitelist)) {
346 346
             foreach ($this->whitelist as $match) {
347
-                if(strpos($tag,$match)!==false) {
347
+                if (strpos($tag, $match) !== false) {
348 348
                     return true;
349 349
                 }
350 350
             }
351 351
             // no match with whitelist
352 352
             return false;
353 353
         } else {
354
-            foreach($this->domove as $match) {
355
-                if(strpos($tag,$match)!==false)    {
354
+            foreach ($this->domove as $match) {
355
+                if (strpos($tag, $match) !== false) {
356 356
                     // Matched something
357 357
                     return false;
358 358
                 }
@@ -362,8 +362,8 @@  discard block
 block discarded – undo
362 362
                 return false;
363 363
             }
364 364
             
365
-            foreach($this->dontmove as $match) {
366
-                if(strpos($tag,$match)!==false)    {
365
+            foreach ($this->dontmove as $match) {
366
+                if (strpos($tag, $match) !== false) {
367 367
                     //Matched something
368 368
                     return false;
369 369
                 }
@@ -376,12 +376,12 @@  discard block
 block discarded – undo
376 376
     
377 377
     // Checks againstt the blacklist
378 378
     private function ismovable($tag) {
379
-        if ($this->include_inline !== true || apply_filters('autoptimize_filter_js_unmovable',true)) {
379
+        if ($this->include_inline !== true || apply_filters('autoptimize_filter_js_unmovable', true)) {
380 380
             return false;
381 381
         }
382 382
         
383
-        foreach($this->domove as $match) {
384
-            if(strpos($tag,$match)!==false)    {
383
+        foreach ($this->domove as $match) {
384
+            if (strpos($tag, $match) !== false) {
385 385
                 // Matched something
386 386
                 return true;
387 387
             }
@@ -391,8 +391,8 @@  discard block
 block discarded – undo
391 391
             return true;
392 392
         }
393 393
         
394
-        foreach($this->dontmove as $match) {
395
-            if(strpos($tag,$match)!==false) {
394
+        foreach ($this->dontmove as $match) {
395
+            if (strpos($tag, $match) !== false) {
396 396
                 // Matched something
397 397
                 return false;
398 398
             }
@@ -403,8 +403,8 @@  discard block
 block discarded – undo
403 403
     }
404 404
     
405 405
     private function movetolast($tag) {
406
-        foreach($this->domovelast as $match) {
407
-            if(strpos($tag,$match)!==false)    {
406
+        foreach ($this->domovelast as $match) {
407
+            if (strpos($tag, $match) !== false) {
408 408
                 // Matched, return true
409 409
                 return true;
410 410
             }
@@ -430,10 +430,10 @@  discard block
 block discarded – undo
430 430
      * original function by https://github.com/zytzagoo/ on his AO fork, thanks Tomas!
431 431
      */
432 432
     public function should_aggregate($tag) {
433
-        preg_match('#<(script[^>]*)>#i',$tag,$scripttag);
434
-        if ( strpos($scripttag[1], 'type')===false ) {
433
+        preg_match('#<(script[^>]*)>#i', $tag, $scripttag);
434
+        if (strpos($scripttag[1], 'type') === false) {
435 435
             return true;
436
-        } else if ( preg_match('/type\s*=\s*["\']?(?:text|application)\/(?:javascript|ecmascript)["\']?/i', $scripttag[1]) ) {
436
+        } else if (preg_match('/type\s*=\s*["\']?(?:text|application)\/(?:javascript|ecmascript)["\']?/i', $scripttag[1])) {
437 437
             return true;
438 438
         } else {
439 439
             return false;
Please login to merge, or discard this patch.
classes/autoptimizeStyles.php 2 patches
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -163,32 +163,32 @@  discard block
 block discarded – undo
163 163
                     // Remove the original style tag
164 164
                     $this->content = str_replace($tag,'',$this->content);
165 165
                 } else {
166
-					// excluded CSS, minify if getpath 
167
-					if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) {
168
-						$explUrl = explode('?',$source[2],2);
166
+                    // excluded CSS, minify if getpath 
167
+                    if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) {
168
+                        $explUrl = explode('?',$source[2],2);
169 169
                         $url = $explUrl[0];
170 170
                         $path = $this->getpath($url);
171 171
  					
172
-						if ($path && apply_filters('autoptimize_filter_css_minify_excluded',false)) {
173
-							$_CachedMinifiedUrl = $this->minify_single($path);
174
-
175
-							if (!empty($_CachedMinifiedUrl)) {
176
-								// replace orig URL with URL to cache
177
-								$newTag = str_replace($url, $_CachedMinifiedUrl, $tag);
178
-							} else {
179
-								$newTag = $tag;
180
-							}
172
+                        if ($path && apply_filters('autoptimize_filter_css_minify_excluded',false)) {
173
+                            $_CachedMinifiedUrl = $this->minify_single($path);
174
+
175
+                            if (!empty($_CachedMinifiedUrl)) {
176
+                                // replace orig URL with URL to cache
177
+                                $newTag = str_replace($url, $_CachedMinifiedUrl, $tag);
178
+                            } else {
179
+                                $newTag = $tag;
180
+                            }
181 181
 							
182
-							// remove querystring from URL
183
-							if ( !empty($explUrl[1]) ) {
184
-								$newTag = str_replace("?".$explUrl[1],"",$newTag);
185
-							}
186
-
187
-							// and replace
188
-							$this->content = str_replace($tag,$newTag,$this->content);
189
-						}
190
-					}					
191
-				}
182
+                            // remove querystring from URL
183
+                            if ( !empty($explUrl[1]) ) {
184
+                                $newTag = str_replace("?".$explUrl[1],"",$newTag);
185
+                            }
186
+
187
+                            // and replace
188
+                            $this->content = str_replace($tag,$newTag,$this->content);
189
+                        }
190
+                    }					
191
+                }
192 192
             }
193 193
             return true;
194 194
         }
@@ -661,8 +661,8 @@  discard block
 block discarded – undo
661 661
     }
662 662
     
663 663
     private function ismovable($tag) {
664
-		if (apply_filters('autoptimize_filter_css_dontaggregate',false)) {
665
-			return false;
664
+        if (apply_filters('autoptimize_filter_css_dontaggregate',false)) {
665
+            return false;
666 666
         } else if (!empty($this->whitelist)) {
667 667
             foreach ($this->whitelist as $match) {
668 668
                 if(strpos($tag,$match)!==false) {
Please login to merge, or discard this patch.
Spacing   +196 added lines, -196 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
 
@@ -24,12 +24,12 @@  discard block
 block discarded – undo
24 24
 
25 25
     //Reads the page and collects style tags
26 26
     public function read($options) {
27
-        $noptimizeCSS = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content );
27
+        $noptimizeCSS = apply_filters('autoptimize_filter_css_noptimize', false, $this->content);
28 28
         if ($noptimizeCSS) return false;
29 29
 
30
-        $whitelistCSS = apply_filters( 'autoptimize_filter_css_whitelist', '' );
30
+        $whitelistCSS = apply_filters('autoptimize_filter_css_whitelist', '');
31 31
         if (!empty($whitelistCSS)) {
32
-            $this->whitelist = array_filter(array_map('trim',explode(",",$whitelistCSS)));
32
+            $this->whitelist = array_filter(array_map('trim', explode(",", $whitelistCSS)));
33 33
         }
34 34
         
35 35
         if ($options['nogooglefont'] == true) {
@@ -37,33 +37,33 @@  discard block
 block discarded – undo
37 37
         } else {
38 38
             $removableCSS = "";
39 39
         }
40
-        $removableCSS = apply_filters( 'autoptimize_filter_css_removables', $removableCSS);
40
+        $removableCSS = apply_filters('autoptimize_filter_css_removables', $removableCSS);
41 41
         if (!empty($removableCSS)) {
42
-            $this->cssremovables = array_filter(array_map('trim',explode(",",$removableCSS)));
42
+            $this->cssremovables = array_filter(array_map('trim', explode(",", $removableCSS)));
43 43
         }
44 44
 
45
-        $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize',256);
45
+        $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize', 256);
46 46
 
47 47
         // filter to "late inject minified CSS", default to true for now (it is faster)
48
-        $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late',true);
48
+        $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late', true);
49 49
 
50 50
         // Remove everything that's not the header
51
-        if ( apply_filters('autoptimize_filter_css_justhead',$options['justhead']) == true ) {
52
-            $content = explode('</head>',$this->content,2);
51
+        if (apply_filters('autoptimize_filter_css_justhead', $options['justhead']) == true) {
52
+            $content = explode('</head>', $this->content, 2);
53 53
             $this->content = $content[0].'</head>';
54 54
             $this->restofcontent = $content[1];
55 55
         }
56 56
 
57 57
         // include inline?
58
-        if( apply_filters('autoptimize_css_include_inline',$options['include_inline']) == true ) {
58
+        if (apply_filters('autoptimize_css_include_inline', $options['include_inline']) == true) {
59 59
             $this->include_inline = true;
60 60
         }
61 61
         
62 62
         // what CSS shouldn't be autoptimized
63 63
         $excludeCSS = $options['css_exclude'];
64
-        $excludeCSS = apply_filters( 'autoptimize_filter_css_exclude', $excludeCSS );
65
-        if ($excludeCSS!=="") {
66
-            $this->dontmove = array_filter(array_map('trim',explode(",",$excludeCSS)));
64
+        $excludeCSS = apply_filters('autoptimize_filter_css_exclude', $excludeCSS);
65
+        if ($excludeCSS !== "") {
66
+            $this->dontmove = array_filter(array_map('trim', explode(",", $excludeCSS)));
67 67
         } else {
68 68
             $this->dontmove = "";
69 69
         }
@@ -71,17 +71,17 @@  discard block
 block discarded – undo
71 71
         // should we defer css?
72 72
         // value: true/ false
73 73
         $this->defer = $options['defer'];
74
-        $this->defer = apply_filters( 'autoptimize_filter_css_defer', $this->defer );
74
+        $this->defer = apply_filters('autoptimize_filter_css_defer', $this->defer);
75 75
 
76 76
         // should we inline while deferring?
77 77
         // value: inlined CSS
78 78
         $this->defer_inline = $options['defer_inline'];
79
-        $this->defer_inline = apply_filters( 'autoptimize_filter_css_defer_inline', $this->defer_inline );
79
+        $this->defer_inline = apply_filters('autoptimize_filter_css_defer_inline', $this->defer_inline);
80 80
 
81 81
         // should we inline?
82 82
         // value: true/ false
83 83
         $this->inline = $options['inline'];
84
-        $this->inline = apply_filters( 'autoptimize_filter_css_inline', $this->inline );
84
+        $this->inline = apply_filters('autoptimize_filter_css_inline', $this->inline);
85 85
         
86 86
         // get cdn url
87 87
         $this->cdn_url = $options['cdn_url'];
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
         $this->content = $this->hide_noptimize($this->content);
94 94
         
95 95
         // exclude (no)script, as those may contain CSS which should be left as is
96
-        if ( strpos( $this->content, '<script' ) !== false ) { 
96
+        if (strpos($this->content, '<script') !== false) { 
97 97
             $this->content = preg_replace_callback(
98 98
                 '#<(?:no)?script.*?<\/(?:no)?script>#is',
99 99
                 create_function(
@@ -111,65 +111,65 @@  discard block
 block discarded – undo
111 111
         $this->content = $this->hide_comments($this->content);
112 112
         
113 113
         // Get <style> and <link>
114
-        if(preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi',$this->content,$matches)) {
115
-            foreach($matches[0] as $tag) {
116
-                if ($this->isremovable($tag,$this->cssremovables)) {
117
-                    $this->content = str_replace($tag,'',$this->content);
114
+        if (preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches)) {
115
+            foreach ($matches[0] as $tag) {
116
+                if ($this->isremovable($tag, $this->cssremovables)) {
117
+                    $this->content = str_replace($tag, '', $this->content);
118 118
                 } else if ($this->ismovable($tag)) {
119 119
                     // Get the media
120
-                    if(strpos($tag,'media=')!==false) {
121
-                        preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui',$tag,$medias);
122
-                        $medias = explode(',',$medias[1]);
120
+                    if (strpos($tag, 'media=') !== false) {
121
+                        preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias);
122
+                        $medias = explode(',', $medias[1]);
123 123
                         $media = array();
124
-                        foreach($medias as $elem) {
125
-                            if (empty($elem)) { $elem="all"; }
124
+                        foreach ($medias as $elem) {
125
+                            if (empty($elem)) { $elem = "all"; }
126 126
                             $media[] = $elem;
127 127
                         }
128 128
                     } else {
129 129
                         // No media specified - applies to all
130 130
                         $media = array('all');
131 131
                     }
132
-                    $media = apply_filters( 'autoptimize_filter_css_tagmedia',$media,$tag );
132
+                    $media = apply_filters('autoptimize_filter_css_tagmedia', $media, $tag);
133 133
                 
134
-                    if(preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) {
134
+                    if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) {
135 135
                         // <link>
136
-                        $explUrl = explode('?',$source[2],2);
136
+                        $explUrl = explode('?', $source[2], 2);
137 137
                         $url = $explUrl[0];
138 138
                         $path = $this->getpath($url);
139 139
                         
140
-                        if($path!==false && preg_match('#\.css$#',$path)) {
140
+                        if ($path !== false && preg_match('#\.css$#', $path)) {
141 141
                             // Good link
142
-                            $this->css[] = array($media,$path);
143
-                        }else{
142
+                            $this->css[] = array($media, $path);
143
+                        } else {
144 144
                             // Link is dynamic (.php etc)
145 145
                             $tag = '';
146 146
                         }
147 147
                     } else {
148 148
                         // inline css in style tags can be wrapped in comment tags, so restore comments
149 149
                         $tag = $this->restore_comments($tag);
150
-                        preg_match('#<style.*>(.*)</style>#Usmi',$tag,$code);
150
+                        preg_match('#<style.*>(.*)</style>#Usmi', $tag, $code);
151 151
 
152 152
                         // and re-hide them to be able to to the removal based on tag
153 153
                         $tag = $this->hide_comments($tag);
154 154
 
155
-                        if ( $this->include_inline ) {
156
-                            $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm','$1',$code[1]);
157
-                            $this->css[] = array($media,'INLINE;'.$code);
155
+                        if ($this->include_inline) {
156
+                            $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1]);
157
+                            $this->css[] = array($media, 'INLINE;'.$code);
158 158
                         } else {
159 159
                             $tag = '';
160 160
                         }
161 161
                     }
162 162
                     
163 163
                     // Remove the original style tag
164
-                    $this->content = str_replace($tag,'',$this->content);
164
+                    $this->content = str_replace($tag, '', $this->content);
165 165
                 } else {
166 166
 					// excluded CSS, minify if getpath 
167
-					if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source)) {
168
-						$explUrl = explode('?',$source[2],2);
167
+					if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) {
168
+						$explUrl = explode('?', $source[2], 2);
169 169
                         $url = $explUrl[0];
170 170
                         $path = $this->getpath($url);
171 171
  					
172
-						if ($path && apply_filters('autoptimize_filter_css_minify_excluded',false)) {
172
+						if ($path && apply_filters('autoptimize_filter_css_minify_excluded', false)) {
173 173
 							$_CachedMinifiedUrl = $this->minify_single($path);
174 174
 
175 175
 							if (!empty($_CachedMinifiedUrl)) {
@@ -180,12 +180,12 @@  discard block
 block discarded – undo
180 180
 							}
181 181
 							
182 182
 							// remove querystring from URL
183
-							if ( !empty($explUrl[1]) ) {
184
-								$newTag = str_replace("?".$explUrl[1],"",$newTag);
183
+							if (!empty($explUrl[1])) {
184
+								$newTag = str_replace("?".$explUrl[1], "", $newTag);
185 185
 							}
186 186
 
187 187
 							// and replace
188
-							$this->content = str_replace($tag,$newTag,$this->content);
188
+							$this->content = str_replace($tag, $newTag, $this->content);
189 189
 						}
190 190
 					}					
191 191
 				}
@@ -198,29 +198,29 @@  discard block
 block discarded – undo
198 198
     
199 199
     // Joins and optimizes CSS
200 200
     public function minify() {
201
-        foreach($this->css as $group) {
202
-            list($media,$css) = $group;
203
-            if(preg_match('#^INLINE;#',$css)) {
201
+        foreach ($this->css as $group) {
202
+            list($media, $css) = $group;
203
+            if (preg_match('#^INLINE;#', $css)) {
204 204
                 // <style>
205
-                $css = preg_replace('#^INLINE;#','',$css);
206
-                $css = $this->fixurls(ABSPATH.'/index.php',$css);
207
-                $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, "" );
208
-                if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle) ) {
209
-                    $css=$tmpstyle;
210
-                    $this->alreadyminified=true;
205
+                $css = preg_replace('#^INLINE;#', '', $css);
206
+                $css = $this->fixurls(ABSPATH.'/index.php', $css);
207
+                $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, "");
208
+                if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
209
+                    $css = $tmpstyle;
210
+                    $this->alreadyminified = true;
211 211
                 }
212 212
             } else {
213 213
                 //<link>
214
-                if($css !== false && file_exists($css) && is_readable($css)) {
214
+                if ($css !== false && file_exists($css) && is_readable($css)) {
215 215
                     $cssPath = $css;
216
-                    $css = $this->fixurls($cssPath,file_get_contents($cssPath));
217
-                    $css = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$css);
218
-                    $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $cssPath );
216
+                    $css = $this->fixurls($cssPath, file_get_contents($cssPath));
217
+                    $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css);
218
+                    $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $cssPath);
219 219
                     if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
220
-                        $css=$tmpstyle;
221
-                        $this->alreadyminified=true;
222
-                    } else if ($this->can_inject_late($cssPath,$css)) {
223
-                        $css="%%INJECTLATER%%".base64_encode($cssPath)."|".md5($css)."%%INJECTLATER%%";
220
+                        $css = $tmpstyle;
221
+                        $this->alreadyminified = true;
222
+                    } else if ($this->can_inject_late($cssPath, $css)) {
223
+                        $css = "%%INJECTLATER%%".base64_encode($cssPath)."|".md5($css)."%%INJECTLATER%%";
224 224
                     }
225 225
                 } else {
226 226
                     // Couldn't read CSS. Maybe getpath isn't working?
@@ -228,8 +228,8 @@  discard block
 block discarded – undo
228 228
                 }
229 229
             }
230 230
             
231
-            foreach($media as $elem) {
232
-                if(!isset($this->csscode[$elem]))
231
+            foreach ($media as $elem) {
232
+                if (!isset($this->csscode[$elem]))
233 233
                     $this->csscode[$elem] = '';
234 234
                 $this->csscode[$elem] .= "\n/*FILESTART*/".$css;
235 235
             }
@@ -238,12 +238,12 @@  discard block
 block discarded – undo
238 238
         // Check for duplicate code
239 239
         $md5list = array();
240 240
         $tmpcss = $this->csscode;
241
-        foreach($tmpcss as $media => $code) {
241
+        foreach ($tmpcss as $media => $code) {
242 242
             $md5sum = md5($code);
243 243
             $medianame = $media;
244
-            foreach($md5list as $med => $sum) {
244
+            foreach ($md5list as $med => $sum) {
245 245
                 // If same code
246
-                if($sum === $md5sum) {
246
+                if ($sum === $md5sum) {
247 247
                     //Add the merged code
248 248
                     $medianame = $med.', '.$media;
249 249
                     $this->csscode[$medianame] = $code;
@@ -263,30 +263,30 @@  discard block
 block discarded – undo
263 263
             $external_imports = "";
264 264
 
265 265
             // remove comments to avoid importing commented-out imports
266
-            $thiscss_nocomments = preg_replace('#/\*.*\*/#Us','',$thiscss);
266
+            $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss);
267 267
 
268
-            while(preg_match_all('#@import.*(?:;|$)#Um',$thiscss_nocomments,$matches)) {
269
-                foreach($matches[0] as $import)    {
270
-                    if ($this->isremovable($import,$this->cssremovables)) {
271
-                        $thiscss = str_replace($import,'',$thiscss);
268
+            while (preg_match_all('#@import.*(?:;|$)#Um', $thiscss_nocomments, $matches)) {
269
+                foreach ($matches[0] as $import) {
270
+                    if ($this->isremovable($import, $this->cssremovables)) {
271
+                        $thiscss = str_replace($import, '', $thiscss);
272 272
                         $import_ok = true;
273 273
                     } else {
274
-                        $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#','$1',trim($import))," \t\n\r\0\x0B\"'");
274
+                        $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'");
275 275
                         $path = $this->getpath($url);
276 276
                         $import_ok = false;
277 277
                         if (file_exists($path) && is_readable($path)) {
278
-                            $code = addcslashes($this->fixurls($path,file_get_contents($path)),"\\");
279
-                            $code = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$code);
280
-                            $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, "" );
281
-                            if ( has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
282
-                                $code=$tmpstyle;
283
-                                $this->alreadyminified=true;
284
-                            } else if ($this->can_inject_late($path,$code)) {
285
-                                $code="%%INJECTLATER%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%";
278
+                            $code = addcslashes($this->fixurls($path, file_get_contents($path)), "\\");
279
+                            $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code);
280
+                            $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, "");
281
+                            if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
282
+                                $code = $tmpstyle;
283
+                                $this->alreadyminified = true;
284
+                            } else if ($this->can_inject_late($path, $code)) {
285
+                                $code = "%%INJECTLATER%%".base64_encode($path)."|".md5($code)."%%INJECTLATER%%";
286 286
                             }
287 287
                             
288
-                            if(!empty($code)) {
289
-                                $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import,'#').'#Us','/*FILESTART2*/'.$code.'$1',$thiscss);
288
+                            if (!empty($code)) {
289
+                                $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss);
290 290
                                 if (!empty($tmp_thiscss)) {
291 291
                                     $thiscss = $tmp_thiscss;
292 292
                                     $import_ok = true;
@@ -300,32 +300,32 @@  discard block
 block discarded – undo
300 300
                     if (!$import_ok) {
301 301
                         // external imports and general fall-back
302 302
                         $external_imports .= $import;
303
-                        $thiscss = str_replace($import,'',$thiscss);
303
+                        $thiscss = str_replace($import, '', $thiscss);
304 304
                         $fiximports = true;
305 305
                     }
306 306
                 }
307
-                $thiscss = preg_replace('#/\*FILESTART\*/#','',$thiscss);
308
-                $thiscss = preg_replace('#/\*FILESTART2\*/#','/*FILESTART*/',$thiscss);
307
+                $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss);
308
+                $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss);
309 309
                 
310 310
                 // and update $thiscss_nocomments before going into next iteration in while loop
311
-                $thiscss_nocomments=preg_replace('#/\*.*\*/#Us','',$thiscss);
311
+                $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss);
312 312
             }
313 313
             unset($thiscss_nocomments);
314 314
             
315 315
             // add external imports to top of aggregated CSS
316
-            if($fiximports) {
317
-                $thiscss=$external_imports.$thiscss;
316
+            if ($fiximports) {
317
+                $thiscss = $external_imports.$thiscss;
318 318
             }
319 319
         }
320 320
         unset($thiscss);
321 321
         
322 322
         // $this->csscode has all the uncompressed code now. 
323 323
         $mhtmlcount = 0;
324
-        foreach($this->csscode as &$code) {
324
+        foreach ($this->csscode as &$code) {
325 325
             // Check for already-minified code
326 326
             $hash = md5($code);
327
-            $ccheck = new autoptimizeCache($hash,'css');
328
-            if($ccheck->check()) {
327
+            $ccheck = new autoptimizeCache($hash, 'css');
328
+            if ($ccheck->check()) {
329 329
                 $code = $ccheck->retrieve();
330 330
                 $this->hashmap[md5($code)] = $hash;
331 331
                 continue;
@@ -339,44 +339,44 @@  discard block
 block discarded – undo
339 339
             // this is the old regex that searched for the entire background css rule, but it wouldn't match multiple background image url css rules.
340 340
             // preg_match_all('#(background[^;{}]*url\((?!\s?"?\'?\s?data)(.*)\)[^;}]*)(?:;|$|})#Usm',$code,$matches);
341 341
             // this new regex will be slightly faster too:
342
-            preg_match_all( self::ASSETS_REGEX, $code, $matches );
342
+            preg_match_all(self::ASSETS_REGEX, $code, $matches);
343 343
 
344
-            if(($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches)))    {
345
-                foreach($matches[1] as $count => $quotedurl) {
346
-                    $iurl = trim($quotedurl," \t\n\r\0\x0B\"'");
344
+            if (($this->datauris == true) && (function_exists('base64_encode')) && (is_array($matches))) {
345
+                foreach ($matches[1] as $count => $quotedurl) {
346
+                    $iurl = trim($quotedurl, " \t\n\r\0\x0B\"'");
347 347
 
348 348
                     // if querystring, remove it from url
349
-                    if (strpos($iurl,'?') !== false) { $iurl = strtok($iurl,'?'); }
349
+                    if (strpos($iurl, '?') !== false) { $iurl = strtok($iurl, '?'); }
350 350
                     
351 351
                     $ipath = $this->getpath($iurl);
352 352
 
353 353
                     $datauri_max_size = 4096;
354
-                    $datauri_max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', $datauri_max_size );
355
-                    $datauri_exclude = apply_filters( 'autoptimize_filter_css_datauri_exclude', "");
354
+                    $datauri_max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', $datauri_max_size);
355
+                    $datauri_exclude = apply_filters('autoptimize_filter_css_datauri_exclude', "");
356 356
                     if (!empty($datauri_exclude)) {
357
-                        $no_datauris=array_filter(array_map('trim',explode(",",$datauri_exclude)));
357
+                        $no_datauris = array_filter(array_map('trim', explode(",", $datauri_exclude)));
358 358
                         foreach ($no_datauris as $no_datauri) {
359
-                            if (strpos($iurl,$no_datauri)!==false) {
360
-                                $ipath=false;
359
+                            if (strpos($iurl, $no_datauri) !== false) {
360
+                                $ipath = false;
361 361
                                 break;
362 362
                             }
363 363
                         }
364 364
                     }
365 365
 
366
-                    if($ipath != false && preg_match('#\.(jpe?g|png|gif|bmp)$#i',$ipath) && file_exists($ipath) && is_readable($ipath) && filesize($ipath) <= $datauri_max_size) {
367
-                        $ihash=md5($ipath);
368
-                        $icheck = new autoptimizeCache($ihash,'img');
369
-                        if($icheck->check()) {
366
+                    if ($ipath != false && preg_match('#\.(jpe?g|png|gif|bmp)$#i', $ipath) && file_exists($ipath) && is_readable($ipath) && filesize($ipath) <= $datauri_max_size) {
367
+                        $ihash = md5($ipath);
368
+                        $icheck = new autoptimizeCache($ihash, 'img');
369
+                        if ($icheck->check()) {
370 370
                             // we have the base64 image in cache
371
-                            $headAndData=$icheck->retrieve();
372
-                            $_base64data=explode(";base64,",$headAndData);
373
-                            $base64data=$_base64data[1];
371
+                            $headAndData = $icheck->retrieve();
372
+                            $_base64data = explode(";base64,", $headAndData);
373
+                            $base64data = $_base64data[1];
374 374
                         } else {
375 375
                             // It's an image and we don't have it in cache, get the type
376
-                            $explA=explode('.',$ipath);
377
-                            $type=end($explA);
376
+                            $explA = explode('.', $ipath);
377
+                            $type = end($explA);
378 378
 
379
-                            switch($type) {
379
+                            switch ($type) {
380 380
                                 case 'jpeg':
381 381
                                     $dataurihead = 'data:image/jpeg;base64,';
382 382
                                     break;
@@ -398,15 +398,15 @@  discard block
 block discarded – undo
398 398
                         
399 399
                             // Encode the data
400 400
                             $base64data = base64_encode(file_get_contents($ipath));
401
-                            $headAndData=$dataurihead.$base64data;
401
+                            $headAndData = $dataurihead.$base64data;
402 402
 
403 403
                             // Save in cache
404
-                            $icheck->cache($headAndData,"text/plain");
404
+                            $icheck->cache($headAndData, "text/plain");
405 405
                         }
406 406
                         unset($icheck);
407 407
 
408 408
                         // Add it to the list for replacement
409
-                        $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$headAndData,$matches[0][$count]).";\n*".str_replace($quotedurl,'mhtml:%%MHTML%%!'.$mhtmlcount,$matches[0][$count]).";\n_".$matches[0][$count].';';
409
+                        $imgreplace[$matches[0][$count]] = str_replace($quotedurl, $headAndData, $matches[0][$count]).";\n*".str_replace($quotedurl, 'mhtml:%%MHTML%%!'.$mhtmlcount, $matches[0][$count]).";\n_".$matches[0][$count].';';
410 410
                         
411 411
                         // Store image on the mhtml document
412 412
                         $this->mhtml .= "--_\r\nContent-Location:{$mhtmlcount}\r\nContent-Transfer-Encoding:base64\r\n\r\n{$base64data}\r\n";
@@ -414,52 +414,52 @@  discard block
 block discarded – undo
414 414
                     } else {
415 415
                         // just cdn the URL if applicable
416 416
                         if (!empty($this->cdn_url)) {
417
-                            $url = trim($quotedurl," \t\n\r\0\x0B\"'");
418
-                            $cdn_url=$this->url_replace_cdn($url);
419
-                            $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$cdn_url,$matches[0][$count]);
417
+                            $url = trim($quotedurl, " \t\n\r\0\x0B\"'");
418
+                            $cdn_url = $this->url_replace_cdn($url);
419
+                            $imgreplace[$matches[0][$count]] = str_replace($quotedurl, $cdn_url, $matches[0][$count]);
420 420
                         }
421 421
                     }
422 422
                 }
423 423
             } else if ((is_array($matches)) && (!empty($this->cdn_url))) {
424 424
                 // change background image urls to cdn-url
425
-                foreach($matches[1] as $count => $quotedurl) {
426
-                    $url = trim($quotedurl," \t\n\r\0\x0B\"'");
427
-                    $cdn_url=$this->url_replace_cdn($url);
428
-                    $imgreplace[$matches[0][$count]] = str_replace($quotedurl,$cdn_url,$matches[0][$count]);
425
+                foreach ($matches[1] as $count => $quotedurl) {
426
+                    $url = trim($quotedurl, " \t\n\r\0\x0B\"'");
427
+                    $cdn_url = $this->url_replace_cdn($url);
428
+                    $imgreplace[$matches[0][$count]] = str_replace($quotedurl, $cdn_url, $matches[0][$count]);
429 429
                 }
430 430
             }
431 431
             
432
-            if(!empty($imgreplace)) {
433
-                $code = str_replace(array_keys($imgreplace),array_values($imgreplace),$code);
432
+            if (!empty($imgreplace)) {
433
+                $code = str_replace(array_keys($imgreplace), array_values($imgreplace), $code);
434 434
             }
435 435
             
436 436
             // CDN the fonts!
437
-            if ( (!empty($this->cdn_url)) && (apply_filters('autoptimize_filter_css_fonts_cdn',false)) && (version_compare(PHP_VERSION, '5.3.0') >= 0) ) {
437
+            if ((!empty($this->cdn_url)) && (apply_filters('autoptimize_filter_css_fonts_cdn', false)) && (version_compare(PHP_VERSION, '5.3.0') >= 0)) {
438 438
                 $fontreplace = array();
439 439
                 include_once(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizeFontRegex.php');
440 440
                 
441
-                preg_match_all($fonturl_regex,$code,$matches);
441
+                preg_match_all($fonturl_regex, $code, $matches);
442 442
                 if (is_array($matches)) {
443
-                    foreach($matches[8] as $count => $quotedurl) {
444
-                        $url = trim($quotedurl," \t\n\r\0\x0B\"'");
445
-                        $cdn_url=$this->url_replace_cdn($url);
446
-                        $fontreplace[$matches[8][$count]] = str_replace($quotedurl,$cdn_url,$matches[8][$count]);
443
+                    foreach ($matches[8] as $count => $quotedurl) {
444
+                        $url = trim($quotedurl, " \t\n\r\0\x0B\"'");
445
+                        $cdn_url = $this->url_replace_cdn($url);
446
+                        $fontreplace[$matches[8][$count]] = str_replace($quotedurl, $cdn_url, $matches[8][$count]);
447 447
                     }
448
-                    if(!empty($fontreplace)) {
449
-                        $code = str_replace(array_keys($fontreplace),array_values($fontreplace),$code);
448
+                    if (!empty($fontreplace)) {
449
+                        $code = str_replace(array_keys($fontreplace), array_values($fontreplace), $code);
450 450
                     }
451 451
                 }
452 452
             }
453 453
             
454 454
             // Minify
455
-            if (($this->alreadyminified!==true) && (apply_filters( "autoptimize_css_do_minify", true))) {
455
+            if (($this->alreadyminified !== true) && (apply_filters("autoptimize_css_do_minify", true))) {
456 456
                 if (class_exists('Minify_CSS_Compressor')) {
457 457
                     $tmp_code = trim(Minify_CSS_Compressor::process($code));
458
-                } else if(class_exists('CSSmin')) {
458
+                } else if (class_exists('CSSmin')) {
459 459
                     $cssmin = new CSSmin();
460
-                    if (method_exists($cssmin,"run")) {
460
+                    if (method_exists($cssmin, "run")) {
461 461
                         $tmp_code = trim($cssmin->run($code));
462
-                    } elseif (@is_callable(array($cssmin,"minify"))) {
462
+                    } elseif (@is_callable(array($cssmin, "minify"))) {
463 463
                         $tmp_code = trim(CssMin::minify($code));
464 464
                     }
465 465
                 }
@@ -471,7 +471,7 @@  discard block
 block discarded – undo
471 471
             
472 472
             $code = $this->inject_minified($code);
473 473
             
474
-            $tmp_code = apply_filters( 'autoptimize_css_after_minify',$code );
474
+            $tmp_code = apply_filters('autoptimize_css_after_minify', $code);
475 475
             if (!empty($tmp_code)) {
476 476
                 $code = $tmp_code;
477 477
                 unset($tmp_code);
@@ -485,31 +485,31 @@  discard block
 block discarded – undo
485 485
     
486 486
     //Caches the CSS in uncompressed, deflated and gzipped form.
487 487
     public function cache() {
488
-        if($this->datauris) {
488
+        if ($this->datauris) {
489 489
             // MHTML Preparation
490 490
             $this->mhtml = "/*\r\nContent-Type: multipart/related; boundary=\"_\"\r\n\r\n".$this->mhtml."*/\r\n";
491 491
             $md5 = md5($this->mhtml);
492
-            $cache = new autoptimizeCache($md5,'txt');
493
-            if(!$cache->check()) {
492
+            $cache = new autoptimizeCache($md5, 'txt');
493
+            if (!$cache->check()) {
494 494
                 // Cache our images for IE
495
-                $cache->cache($this->mhtml,'text/plain');
495
+                $cache->cache($this->mhtml, 'text/plain');
496 496
             }
497 497
             $mhtml = AUTOPTIMIZE_CACHE_URL.$cache->getname();
498 498
         }
499 499
         
500 500
         // CSS cache
501
-        foreach($this->csscode as $media => $code) {
501
+        foreach ($this->csscode as $media => $code) {
502 502
             $md5 = $this->hashmap[md5($code)];
503 503
 
504
-            if($this->datauris)    {
504
+            if ($this->datauris) {
505 505
                 // Images for ie! Get the right url
506
-                $code = str_replace('%%MHTML%%',$mhtml,$code);
506
+                $code = str_replace('%%MHTML%%', $mhtml, $code);
507 507
             }
508 508
                 
509
-            $cache = new autoptimizeCache($md5,'css');
510
-            if(!$cache->check()) {
509
+            $cache = new autoptimizeCache($md5, 'css');
510
+            if (!$cache->check()) {
511 511
                 // Cache our code
512
-                $cache->cache($code,'text/css');
512
+                $cache->cache($code, 'text/css');
513 513
             }
514 514
             $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname();
515 515
         }
@@ -524,7 +524,7 @@  discard block
 block discarded – undo
524 524
         $this->content = $this->restore_comments($this->content);
525 525
         
526 526
         // restore (no)script
527
-        if ( strpos( $this->content, '%%SCRIPT%%' ) !== false ) { 
527
+        if (strpos($this->content, '%%SCRIPT%%') !== false) { 
528 528
             $this->content = preg_replace_callback(
529 529
                 '#%%SCRIPT%%(.*?)%%SCRIPT%%#is',
530 530
                 create_function(
@@ -539,74 +539,74 @@  discard block
 block discarded – undo
539 539
         $this->content = $this->restore_noptimize($this->content);
540 540
         
541 541
         //Restore the full content
542
-        if(!empty($this->restofcontent)) {
542
+        if (!empty($this->restofcontent)) {
543 543
             $this->content .= $this->restofcontent;
544 544
             $this->restofcontent = '';
545 545
         }
546 546
         
547 547
         // Inject the new stylesheets
548
-        $replaceTag = array("<title","before");
549
-        $replaceTag = apply_filters( 'autoptimize_filter_css_replacetag', $replaceTag );
548
+        $replaceTag = array("<title", "before");
549
+        $replaceTag = apply_filters('autoptimize_filter_css_replacetag', $replaceTag);
550 550
 
551 551
         if ($this->inline == true) {
552
-            foreach($this->csscode as $media => $code) {
553
-                $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>',$replaceTag);
552
+            foreach ($this->csscode as $media => $code) {
553
+                $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>', $replaceTag);
554 554
             }
555 555
         } else {
556 556
             if ($this->defer == true) {
557 557
                 $preloadCssBlock = "";
558 558
                 $noScriptCssBlock = "<noscript id=\"aonoscrcss\">";
559
-                $defer_inline_code=$this->defer_inline;
560
-                if(!empty($defer_inline_code)){
561
-                    if ( apply_filters( 'autoptimize_filter_css_critcss_minify',true ) ) {
562
-                        $iCssHash=md5($defer_inline_code);
563
-                        $iCssCache = new autoptimizeCache($iCssHash,'css');
564
-                        if($iCssCache->check()) { 
559
+                $defer_inline_code = $this->defer_inline;
560
+                if (!empty($defer_inline_code)) {
561
+                    if (apply_filters('autoptimize_filter_css_critcss_minify', true)) {
562
+                        $iCssHash = md5($defer_inline_code);
563
+                        $iCssCache = new autoptimizeCache($iCssHash, 'css');
564
+                        if ($iCssCache->check()) { 
565 565
                             // we have the optimized inline CSS in cache
566
-                            $defer_inline_code=$iCssCache->retrieve();
566
+                            $defer_inline_code = $iCssCache->retrieve();
567 567
                         } else {
568 568
                             if (class_exists('Minify_CSS_Compressor')) {
569 569
                                 $tmp_code = trim(Minify_CSS_Compressor::process($defer_inline_code));
570
-                            } else if(class_exists('CSSmin')) {
570
+                            } else if (class_exists('CSSmin')) {
571 571
                                 $cssmin = new CSSmin();
572 572
                                 $tmp_code = trim($cssmin->run($defer_inline_code));
573 573
                             }
574 574
                             if (!empty($tmp_code)) {
575 575
                                 $defer_inline_code = $tmp_code;
576
-                                $iCssCache->cache($defer_inline_code,"text/css");
576
+                                $iCssCache->cache($defer_inline_code, "text/css");
577 577
                                 unset($tmp_code);
578 578
                             }
579 579
                         }
580 580
                     }
581
-                    $code_out='<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>';
582
-                    $this->inject_in_html($code_out,$replaceTag);
581
+                    $code_out = '<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>';
582
+                    $this->inject_in_html($code_out, $replaceTag);
583 583
                 }
584 584
             }
585 585
 
586
-            foreach($this->url as $media => $url) {
586
+            foreach ($this->url as $media => $url) {
587 587
                 $url = $this->url_replace_cdn($url);
588 588
                 
589 589
                 //Add the stylesheet either deferred (import at bottom) or normal links in head
590
-                if($this->defer == true) {
590
+                if ($this->defer == true) {
591 591
                     $preloadCssBlock .= '<link rel="preload" as="style" media="'.$media.'" href="'.$url.'" onload="this.rel=\'stylesheet\'" />';
592 592
                     $noScriptCssBlock .= '<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />';
593 593
                 } else {
594 594
                     if (strlen($this->csscode[$media]) > $this->cssinlinesize) {
595
-                        $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />',$replaceTag);
596
-                    } else if (strlen($this->csscode[$media])>0) {
597
-                        $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>',$replaceTag);
595
+                        $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />', $replaceTag);
596
+                    } else if (strlen($this->csscode[$media]) > 0) {
597
+                        $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>', $replaceTag);
598 598
                     }
599 599
                 }
600 600
             }
601 601
             
602
-            if($this->defer == true) {
602
+            if ($this->defer == true) {
603 603
                 $preloadPolyfill = '<script data-cfasync=\'false\'>/*! loadCSS. [c]2017 Filament Group, Inc. MIT License */
604 604
 !function(a){"use strict";var b=function(b,c,d){function e(a){return h.body?a():void setTimeout(function(){e(a)})}function f(){i.addEventListener&&i.removeEventListener("load",f),i.media=d||"all"}var g,h=a.document,i=h.createElement("link");if(c)g=c;else{var j=(h.body||h.getElementsByTagName("head")[0]).childNodes;g=j[j.length-1]}var k=h.styleSheets;i.rel="stylesheet",i.href=b,i.media="only x",e(function(){g.parentNode.insertBefore(i,c?g:g.nextSibling)});var l=function(a){for(var b=i.href,c=k.length;c--;)if(k[c].href===b)return a();setTimeout(function(){l(a)})};return i.addEventListener&&i.addEventListener("load",f),i.onloadcssdefined=l,l(f),i};"undefined"!=typeof exports?exports.loadCSS=b:a.loadCSS=b}("undefined"!=typeof global?global:this);
605 605
 /*! loadCSS rel=preload polyfill. [c]2017 Filament Group, Inc. MIT License */
606 606
 !function(a){if(a.loadCSS){var b=loadCSS.relpreload={};if(b.support=function(){try{return a.document.createElement("link").relList.supports("preload")}catch(b){return!1}},b.poly=function(){for(var b=a.document.getElementsByTagName("link"),c=0;c<b.length;c++){var d=b[c];"preload"===d.rel&&"style"===d.getAttribute("as")&&(a.loadCSS(d.href,d,d.getAttribute("media")),d.rel=null)}},!b.support()){b.poly();var c=a.setInterval(b.poly,300);a.addEventListener&&a.addEventListener("load",function(){b.poly(),a.clearInterval(c)}),a.attachEvent&&a.attachEvent("onload",function(){a.clearInterval(c)})}}}(this);</script>';
607 607
                 $noScriptCssBlock .= "</noscript>";
608
-                $this->inject_in_html($preloadCssBlock.$noScriptCssBlock,$replaceTag);
609
-                $this->inject_in_html($preloadPolyfill,array('</body>','before'));
608
+                $this->inject_in_html($preloadCssBlock.$noScriptCssBlock, $replaceTag);
609
+                $this->inject_in_html($preloadPolyfill, array('</body>', 'before'));
610 610
             }
611 611
         }
612 612
 
@@ -614,38 +614,38 @@  discard block
 block discarded – undo
614 614
         return $this->content;
615 615
     }
616 616
     
617
-    static function fixurls($file,$code) {
618
-        $file = str_replace(WP_ROOT_DIR,'',$file);
619
-        $file = str_replace(AUTOPTIMIZE_WP_CONTENT_NAME,'',$file);
617
+    static function fixurls($file, $code) {
618
+        $file = str_replace(WP_ROOT_DIR, '', $file);
619
+        $file = str_replace(AUTOPTIMIZE_WP_CONTENT_NAME, '', $file);
620 620
         $dir = dirname($file); // Like /themes/expound/css
621 621
 
622 622
         // switch all imports to the url() syntax
623
-        $code=preg_replace('#@import ("|\')(.+?)\.css.*("|\')#','@import url("${2}.css")',$code);
623
+        $code = preg_replace('#@import ("|\')(.+?)\.css.*("|\')#', '@import url("${2}.css")', $code);
624 624
 
625
-        if( preg_match_all( self::ASSETS_REGEX, $code, $matches ) ) {
625
+        if (preg_match_all(self::ASSETS_REGEX, $code, $matches)) {
626 626
             $replace = array();
627
-            foreach($matches[1] as $k => $url) {
627
+            foreach ($matches[1] as $k => $url) {
628 628
                 // Remove quotes
629
-                $url = trim($url," \t\n\r\0\x0B\"'");
630
-                $noQurl = trim($url,"\"'");
629
+                $url = trim($url, " \t\n\r\0\x0B\"'");
630
+                $noQurl = trim($url, "\"'");
631 631
                 
632 632
                 if ($noQurl === '') { continue; }
633 633
                 
634
-                if ($url!==$noQurl) {
635
-                    $removedQuotes=true;
634
+                if ($url !== $noQurl) {
635
+                    $removedQuotes = true;
636 636
                 } else {
637
-                    $removedQuotes=false;
637
+                    $removedQuotes = false;
638 638
                 }
639
-                $url=$noQurl;
640
-                if(substr($url,0,1)=='/' || preg_match('#^(https?://|ftp://|data:)#i',$url)) {
639
+                $url = $noQurl;
640
+                if (substr($url, 0, 1) == '/' || preg_match('#^(https?://|ftp://|data:)#i', $url)) {
641 641
                     //URL is absolute
642 642
                     continue;
643 643
                 } else {
644 644
                     // relative URL
645
-                    $newurl = preg_replace('/https?:/','',str_replace(" ","%20",AUTOPTIMIZE_WP_CONTENT_URL.str_replace('//','/',$dir.'/'.$url)));
645
+                    $newurl = preg_replace('/https?:/', '', str_replace(" ", "%20", AUTOPTIMIZE_WP_CONTENT_URL.str_replace('//', '/', $dir.'/'.$url)));
646 646
 
647 647
                     $hash = md5($url);
648
-                    $code = str_replace($matches[0][$k],$hash,$code);
648
+                    $code = str_replace($matches[0][$k], $hash, $code);
649 649
 
650 650
                     if (!empty($removedQuotes)) {
651 651
                         $replace[$hash] = 'url(\''.$newurl.'\')';
@@ -655,17 +655,17 @@  discard block
 block discarded – undo
655 655
                 }
656 656
             }    
657 657
             //Do the replacing here to avoid breaking URLs
658
-            $code = str_replace(array_keys($replace),array_values($replace),$code);
658
+            $code = str_replace(array_keys($replace), array_values($replace), $code);
659 659
         }    
660 660
         return $code;
661 661
     }
662 662
     
663 663
     private function ismovable($tag) {
664
-		if (apply_filters('autoptimize_filter_css_dontaggregate',false)) {
664
+		if (apply_filters('autoptimize_filter_css_dontaggregate', false)) {
665 665
 			return false;
666 666
         } else if (!empty($this->whitelist)) {
667 667
             foreach ($this->whitelist as $match) {
668
-                if(strpos($tag,$match)!==false) {
668
+                if (strpos($tag, $match) !== false) {
669 669
                     return true;
670 670
                 }
671 671
             }
@@ -673,8 +673,8 @@  discard block
 block discarded – undo
673 673
             return false;
674 674
         } else {
675 675
             if (is_array($this->dontmove)) {
676
-                foreach($this->dontmove as $match) {
677
-                    if(strpos($tag,$match)!==false) {
676
+                foreach ($this->dontmove as $match) {
677
+                    if (strpos($tag, $match) !== false) {
678 678
                         //Matched something
679 679
                         return false;
680 680
                     }
@@ -686,17 +686,17 @@  discard block
 block discarded – undo
686 686
         }
687 687
     }
688 688
     
689
-    private function can_inject_late($cssPath,$css) {
690
-        if ((strpos($cssPath,"min.css")===false) || ($this->inject_min_late!==true)) {
689
+    private function can_inject_late($cssPath, $css) {
690
+        if ((strpos($cssPath, "min.css") === false) || ($this->inject_min_late !== true)) {
691 691
             // late-inject turned off or file not minified based on filename
692 692
             return false;
693
-        } else if (strpos($css,"@import")!==false) {
693
+        } else if (strpos($css, "@import") !== false) {
694 694
             // can't late-inject files with imports as those need to be aggregated 
695 695
             return false;
696
-        } else if ( (strpos($css,"@font-face")!==false ) && ( apply_filters("autoptimize_filter_css_fonts_cdn",false)===true) && (!empty($this->cdn_url)) ) {
696
+        } else if ((strpos($css, "@font-face") !== false) && (apply_filters("autoptimize_filter_css_fonts_cdn", false) === true) && (!empty($this->cdn_url))) {
697 697
             // don't late-inject CSS with font-src's if fonts are set to be CDN'ed
698 698
             return false;
699
-        } else if ( (($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui",$css) ) {
699
+        } else if ((($this->datauris == true) || (!empty($this->cdn_url))) && preg_match("#background[^;}]*url\(#Ui", $css)) {
700 700
             // don't late-inject CSS with images if CDN is set OR is image inlining is on
701 701
             return false;
702 702
         } else {
Please login to merge, or discard this patch.