Completed
Push — master ( 38e4a5...74ebb1 )
by frank
01:35
created
classes/autoptimizeStyles.php 1 patch
Spacing   +314 added lines, -314 removed lines patch added patch discarded remove patch
@@ -3,7 +3,7 @@  discard block
 block discarded – undo
3 3
  * Class for CSS optimization.
4 4
  */
5 5
 
6
-if ( ! defined( 'ABSPATH' ) ) {
6
+if (!defined('ABSPATH')) {
7 7
     exit;
8 8
 }
9 9
 
@@ -51,53 +51,53 @@  discard block
 block discarded – undo
51 51
     // public $cdn_url; // Used all over the place implicitly, so will have to be either public or protected :/ .
52 52
 
53 53
     // Reads the page and collects style tags.
54
-    public function read( $options )
54
+    public function read($options)
55 55
     {
56
-        $noptimizeCSS = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content );
57
-        if ( $noptimizeCSS ) {
56
+        $noptimizeCSS = apply_filters('autoptimize_filter_css_noptimize', false, $this->content);
57
+        if ($noptimizeCSS) {
58 58
             return false;
59 59
         }
60 60
 
61
-        $whitelistCSS = apply_filters( 'autoptimize_filter_css_whitelist', '', $this->content );
62
-        if ( ! empty( $whitelistCSS ) ) {
63
-            $this->whitelist = array_filter( array_map( 'trim', explode( ',', $whitelistCSS ) ) );
61
+        $whitelistCSS = apply_filters('autoptimize_filter_css_whitelist', '', $this->content);
62
+        if (!empty($whitelistCSS)) {
63
+            $this->whitelist = array_filter(array_map('trim', explode(',', $whitelistCSS)));
64 64
         }
65 65
 
66
-        $removableCSS = apply_filters( 'autoptimize_filter_css_removables', '' );
67
-        if ( ! empty( $removableCSS ) ) {
68
-            $this->cssremovables = array_filter( array_map( 'trim', explode( ',', $removableCSS ) ) );
66
+        $removableCSS = apply_filters('autoptimize_filter_css_removables', '');
67
+        if (!empty($removableCSS)) {
68
+            $this->cssremovables = array_filter(array_map('trim', explode(',', $removableCSS)));
69 69
         }
70 70
 
71
-        $this->cssinlinesize = apply_filters( 'autoptimize_filter_css_inlinesize', 256 );
71
+        $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize', 256);
72 72
 
73 73
         // filter to "late inject minified CSS", default to true for now (it is faster).
74
-        $this->inject_min_late = apply_filters( 'autoptimize_filter_css_inject_min_late', true );
74
+        $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late', true);
75 75
 
76 76
         // Remove everything that's not the header.
77
-        if ( apply_filters( 'autoptimize_filter_css_justhead', $options['justhead'] ) ) {
78
-            $content             = explode( '</head>', $this->content, 2 );
79
-            $this->content       = $content[0] . '</head>';
77
+        if (apply_filters('autoptimize_filter_css_justhead', $options['justhead'])) {
78
+            $content             = explode('</head>', $this->content, 2);
79
+            $this->content       = $content[0].'</head>';
80 80
             $this->restofcontent = $content[1];
81 81
         }
82 82
 
83 83
         // Determine whether we're doing CSS-files aggregation or not.
84
-        if ( isset( $options['aggregate'] ) && ! $options['aggregate'] ) {
84
+        if (isset($options['aggregate']) && !$options['aggregate']) {
85 85
             $this->aggregate = false;
86 86
         }
87 87
         // Returning true for "dontaggregate" turns off aggregation.
88
-        if ( $this->aggregate && apply_filters( 'autoptimize_filter_css_dontaggregate', false ) ) {
88
+        if ($this->aggregate && apply_filters('autoptimize_filter_css_dontaggregate', false)) {
89 89
             $this->aggregate = false;
90 90
         }
91 91
 
92 92
         // include inline?
93
-        if ( apply_filters( 'autoptimize_css_include_inline', $options['include_inline'] ) ) {
93
+        if (apply_filters('autoptimize_css_include_inline', $options['include_inline'])) {
94 94
             $this->include_inline = true;
95 95
         }
96 96
 
97 97
         // List of CSS strings which are excluded from autoptimization.
98
-        $excludeCSS = apply_filters( 'autoptimize_filter_css_exclude', $options['css_exclude'], $this->content );
99
-        if ( '' !== $excludeCSS ) {
100
-            $this->dontmove = array_filter( array_map( 'trim', explode( ',', $excludeCSS ) ) );
98
+        $excludeCSS = apply_filters('autoptimize_filter_css_exclude', $options['css_exclude'], $this->content);
99
+        if ('' !== $excludeCSS) {
100
+            $this->dontmove = array_filter(array_map('trim', explode(',', $excludeCSS)));
101 101
         } else {
102 102
             $this->dontmove = array();
103 103
         }
@@ -108,16 +108,16 @@  discard block
 block discarded – undo
108 108
         // Should we defer css?
109 109
         // value: true / false.
110 110
         $this->defer = $options['defer'];
111
-        $this->defer = apply_filters( 'autoptimize_filter_css_defer', $this->defer, $this->content );
111
+        $this->defer = apply_filters('autoptimize_filter_css_defer', $this->defer, $this->content);
112 112
 
113 113
         // Should we inline while deferring?
114 114
         // value: inlined CSS.
115
-        $this->defer_inline = apply_filters( 'autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content );
115
+        $this->defer_inline = apply_filters('autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content);
116 116
 
117 117
         // Should we inline?
118 118
         // value: true / false.
119 119
         $this->inline = $options['inline'];
120
-        $this->inline = apply_filters( 'autoptimize_filter_css_inline', $this->inline, $this->content );
120
+        $this->inline = apply_filters('autoptimize_filter_css_inline', $this->inline, $this->content);
121 121
 
122 122
         // Store cdn url.
123 123
         $this->cdn_url = $options['cdn_url'];
@@ -126,12 +126,12 @@  discard block
 block discarded – undo
126 126
         $this->datauris = $options['datauris'];
127 127
 
128 128
         // Determine whether excluded files should be minified if not yet so.
129
-        if ( ! $options['minify_excluded'] && $options['aggregate'] ) {
129
+        if (!$options['minify_excluded'] && $options['aggregate']) {
130 130
             $this->minify_excluded = false;
131 131
         }
132 132
 
133 133
         // noptimize me.
134
-        $this->content = $this->hide_noptimize( $this->content );
134
+        $this->content = $this->hide_noptimize($this->content);
135 135
 
136 136
         // Exclude (no)script, as those may contain CSS which should be left as is.
137 137
         $this->content = $this->replace_contents_with_marker_if_exists(
@@ -142,26 +142,26 @@  discard block
 block discarded – undo
142 142
         );
143 143
 
144 144
         // Save IE hacks.
145
-        $this->content = $this->hide_iehacks( $this->content );
145
+        $this->content = $this->hide_iehacks($this->content);
146 146
 
147 147
         // Hide HTML comments.
148
-        $this->content = $this->hide_comments( $this->content );
148
+        $this->content = $this->hide_comments($this->content);
149 149
 
150 150
         // Get <style> and <link>.
151
-        if ( preg_match_all( '#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches ) ) {
151
+        if (preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches)) {
152 152
 
153
-            foreach ( $matches[0] as $tag ) {
154
-                if ( $this->isremovable( $tag, $this->cssremovables ) ) {
155
-                    $this->content = str_replace( $tag, '', $this->content );
156
-                } elseif ( $this->ismovable( $tag ) ) {
153
+            foreach ($matches[0] as $tag) {
154
+                if ($this->isremovable($tag, $this->cssremovables)) {
155
+                    $this->content = str_replace($tag, '', $this->content);
156
+                } elseif ($this->ismovable($tag)) {
157 157
                     // Get the media.
158
-                    if ( false !== strpos( $tag, 'media=' ) ) {
159
-                        preg_match( '#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias );
160
-                        $medias = explode( ',', $medias[1] );
158
+                    if (false !== strpos($tag, 'media=')) {
159
+                        preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias);
160
+                        $medias = explode(',', $medias[1]);
161 161
                         $media = array();
162
-                        foreach ( $medias as $elem ) {
162
+                        foreach ($medias as $elem) {
163 163
                             /* $media[] = current(explode(' ',trim($elem),2)); */
164
-                            if ( empty( $elem ) ) {
164
+                            if (empty($elem)) {
165 165
                                 $elem = 'all';
166 166
                             }
167 167
 
@@ -169,72 +169,72 @@  discard block
 block discarded – undo
169 169
                         }
170 170
                     } else {
171 171
                         // No media specified - applies to all.
172
-                        $media = array( 'all' );
172
+                        $media = array('all');
173 173
                     }
174 174
 
175
-                    $media = apply_filters( 'autoptimize_filter_css_tagmedia', $media, $tag );
175
+                    $media = apply_filters('autoptimize_filter_css_tagmedia', $media, $tag);
176 176
 
177
-                    if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) {
177
+                    if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) {
178 178
                         // <link>.
179
-                        $url  = current( explode( '?', $source[2], 2 ) );
180
-                        $path = $this->getpath( $url );
179
+                        $url  = current(explode('?', $source[2], 2));
180
+                        $path = $this->getpath($url);
181 181
 
182
-                        if ( false !== $path && preg_match( '#\.css$#', $path ) ) {
182
+                        if (false !== $path && preg_match('#\.css$#', $path)) {
183 183
                             // Good link.
184
-                            $this->css[] = array( $media, $path );
184
+                            $this->css[] = array($media, $path);
185 185
                         } else {
186 186
                             // Link is dynamic (.php etc).
187
-                            $new_tag = $this->optionally_defer_excluded( $tag, 'none' );
188
-                            if ( $new_tag !== '' && $new_tag !== $tag ) {
189
-                                $this->content = str_replace( $tag, $new_tag, $this->content );
187
+                            $new_tag = $this->optionally_defer_excluded($tag, 'none');
188
+                            if ($new_tag !== '' && $new_tag !== $tag) {
189
+                                $this->content = str_replace($tag, $new_tag, $this->content);
190 190
                             }
191 191
                             $tag = '';
192 192
                         }
193 193
                     } else {
194 194
                         // Inline css in style tags can be wrapped in comment tags, so restore comments.
195
-                        $tag = $this->restore_comments( $tag );
196
-                        preg_match( '#<style.*>(.*)</style>#Usmi', $tag, $code );
195
+                        $tag = $this->restore_comments($tag);
196
+                        preg_match('#<style.*>(.*)</style>#Usmi', $tag, $code);
197 197
 
198 198
                         // And re-hide them to be able to to the removal based on tag.
199
-                        $tag = $this->hide_comments( $tag );
199
+                        $tag = $this->hide_comments($tag);
200 200
 
201
-                        if ( $this->include_inline ) {
202
-                            $code = preg_replace( '#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1] );
203
-                            $this->css[] = array( $media, 'INLINE;' . $code );
201
+                        if ($this->include_inline) {
202
+                            $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1]);
203
+                            $this->css[] = array($media, 'INLINE;'.$code);
204 204
                         } else {
205 205
                             $tag = '';
206 206
                         }
207 207
                     }
208 208
 
209 209
                     // Remove the original style tag.
210
-                    $this->content = str_replace( $tag, '', $this->content );
210
+                    $this->content = str_replace($tag, '', $this->content);
211 211
                 } else {
212
-                    if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) {
213
-                        $exploded_url = explode( '?', $source[2], 2 );
212
+                    if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) {
213
+                        $exploded_url = explode('?', $source[2], 2);
214 214
                         $url          = $exploded_url[0];
215
-                        $path         = $this->getpath( $url );
215
+                        $path         = $this->getpath($url);
216 216
                         $new_tag      = $tag;
217 217
 
218 218
                         // Excluded CSS, minify that file:
219 219
                         // -> if aggregate is on and exclude minify is on
220 220
                         // -> if aggregate is off and the file is not in dontmove.
221
-                        if ( $path && ( $this->minify_excluded || apply_filters( 'autoptimize_filter_css_minify_excluded', false, $url ) ) ) {
222
-                            $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false );
223
-                            if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) {
224
-                                $minified_url = $this->minify_single( $path );
225
-                                if ( ! empty( $minified_url ) ) {
221
+                        if ($path && ($this->minify_excluded || apply_filters('autoptimize_filter_css_minify_excluded', false, $url))) {
222
+                            $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false);
223
+                            if ((false === $this->aggregate && str_replace($this->dontmove, '', $path) === $path) || (true === $this->aggregate && (false === $consider_minified_array || str_replace($consider_minified_array, '', $path) === $path))) {
224
+                                $minified_url = $this->minify_single($path);
225
+                                if (!empty($minified_url)) {
226 226
                                     // Replace orig URL with cached minified URL.
227
-                                    $new_tag = str_replace( $url, $minified_url, $tag );
227
+                                    $new_tag = str_replace($url, $minified_url, $tag);
228 228
                                 }
229 229
                             }
230 230
                         }
231 231
 
232 232
                         // Optionally defer (preload) non-aggregated CSS.
233
-                        $new_tag = $this->optionally_defer_excluded( $new_tag, $url );
233
+                        $new_tag = $this->optionally_defer_excluded($new_tag, $url);
234 234
 
235 235
                         // And replace!
236
-                        if ( $new_tag !== '' && $new_tag !== $tag ) {
237
-                            $this->content = str_replace( $tag, $new_tag, $this->content );
236
+                        if ($new_tag !== '' && $new_tag !== $tag) {
237
+                            $this->content = str_replace($tag, $new_tag, $this->content);
238 238
                         }
239 239
                     }
240 240
                 }
@@ -255,10 +255,10 @@  discard block
 block discarded – undo
255 255
      *
256 256
      * @return string $new_tag
257 257
      */
258
-    private function optionally_defer_excluded( $tag, $url = '' )
258
+    private function optionally_defer_excluded($tag, $url = '')
259 259
     {
260 260
         // Defer single CSS if "inline & defer" is ON and there is inline CSS.
261
-        if ( $this->defer && ! empty( $this->defer_inline ) ) {
261
+        if ($this->defer && !empty($this->defer_inline)) {
262 262
             // Get/ set (via filter) the JS to be triggers onload of the preloaded CSS.
263 263
             $_preload_onload = apply_filters(
264 264
                 'autoptimize_filter_css_preload_onload',
@@ -266,12 +266,12 @@  discard block
 block discarded – undo
266 266
                 $url
267 267
             );
268 268
             // Adapt original <link> element for CSS to be preloaded and add <noscript>-version for fallback.
269
-            $new_tag = '<noscript>' . $tag . '</noscript>' . str_replace(
269
+            $new_tag = '<noscript>'.$tag.'</noscript>'.str_replace(
270 270
                 array(
271 271
                     "rel='stylesheet'",
272 272
                     'rel="stylesheet"',
273 273
                 ),
274
-                "rel='preload' as='style' onload=\"" . $_preload_onload . "\"",
274
+                "rel='preload' as='style' onload=\"".$_preload_onload."\"",
275 275
                 $tag
276 276
             );
277 277
         } else {
@@ -288,22 +288,22 @@  discard block
 block discarded – undo
288 288
      * @param string $path
289 289
      * @return boolean
290 290
      */
291
-    private function is_datauri_candidate( $path )
291
+    private function is_datauri_candidate($path)
292 292
     {
293 293
         // Call only once since it's called from a loop.
294 294
         static $max_size = null;
295
-        if ( null === $max_size ) {
295
+        if (null === $max_size) {
296 296
             $max_size = $this->get_datauri_maxsize();
297 297
         }
298 298
 
299
-        if ( $path && preg_match( '#\.(jpe?g|png|gif|webp|bmp)$#i', $path ) &&
300
-            file_exists( $path ) && is_readable( $path ) && filesize( $path ) <= $max_size ) {
299
+        if ($path && preg_match('#\.(jpe?g|png|gif|webp|bmp)$#i', $path) &&
300
+            file_exists($path) && is_readable($path) && filesize($path) <= $max_size) {
301 301
 
302 302
             // Seems we have a candidate.
303 303
             $is_candidate = true;
304 304
         } else {
305 305
             // Filter allows overriding default decision (which checks for local file existence).
306
-            $is_candidate = apply_filters( 'autoptimize_filter_css_is_datauri_candidate', false, $path );
306
+            $is_candidate = apply_filters('autoptimize_filter_css_is_datauri_candidate', false, $path);
307 307
         }
308 308
 
309 309
         return $is_candidate;
@@ -328,29 +328,29 @@  discard block
 block discarded – undo
328 328
          * of thing you're probably better of building assets completely
329 329
          * outside of WordPress anyway.
330 330
          */
331
-        if ( null === $max_size ) {
332
-            $max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', 4096 );
331
+        if (null === $max_size) {
332
+            $max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', 4096);
333 333
         }
334 334
 
335 335
         return $max_size;
336 336
     }
337 337
 
338
-    private function check_datauri_exclude_list( $url )
338
+    private function check_datauri_exclude_list($url)
339 339
     {
340 340
         static $exclude_list = null;
341 341
         $no_datauris         = array();
342 342
 
343 343
         // Again, skip doing certain stuff repeatedly when loop-called.
344
-        if ( null === $exclude_list ) {
345
-            $exclude_list = apply_filters( 'autoptimize_filter_css_datauri_exclude', '' );
346
-            $no_datauris = array_filter( array_map( 'trim', explode( ',', $exclude_list ) ) );
344
+        if (null === $exclude_list) {
345
+            $exclude_list = apply_filters('autoptimize_filter_css_datauri_exclude', '');
346
+            $no_datauris = array_filter(array_map('trim', explode(',', $exclude_list)));
347 347
         }
348 348
 
349 349
         $matched = false;
350 350
 
351
-        if ( ! empty( $exclude_list ) ) {
352
-            foreach ( $no_datauris as $no_datauri ) {
353
-                if ( false !== strpos( $url, $no_datauri ) ) {
351
+        if (!empty($exclude_list)) {
352
+            foreach ($no_datauris as $no_datauri) {
353
+                if (false !== strpos($url, $no_datauri)) {
354 354
                     $matched = true;
355 355
                     break;
356 356
                 }
@@ -360,7 +360,7 @@  discard block
 block discarded – undo
360 360
         return $matched;
361 361
     }
362 362
 
363
-    private function build_or_get_datauri_image( $path )
363
+    private function build_or_get_datauri_image($path)
364 364
     {
365 365
         /**
366 366
          * TODO/FIXME: document the required return array format, or better yet,
@@ -369,27 +369,27 @@  discard block
 block discarded – undo
369 369
          */
370 370
 
371 371
         // Allows short-circuiting datauri generation for an image.
372
-        $result = apply_filters( 'autoptimize_filter_css_datauri_image', array(), $path );
373
-        if ( ! empty( $result ) ) {
374
-            if ( is_array( $result ) && isset( $result['full'] ) && isset( $result['base64data'] ) ) {
372
+        $result = apply_filters('autoptimize_filter_css_datauri_image', array(), $path);
373
+        if (!empty($result)) {
374
+            if (is_array($result) && isset($result['full']) && isset($result['base64data'])) {
375 375
                 return $result;
376 376
             }
377 377
         }
378 378
 
379
-        $hash = md5( $path );
380
-        $check = new autoptimizeCache( $hash, 'img' );
381
-        if ( $check->check() ) {
379
+        $hash = md5($path);
380
+        $check = new autoptimizeCache($hash, 'img');
381
+        if ($check->check()) {
382 382
             // we have the base64 image in cache.
383 383
             $headAndData = $check->retrieve();
384
-            $_base64data = explode( ';base64,', $headAndData );
384
+            $_base64data = explode(';base64,', $headAndData);
385 385
             $base64data  = $_base64data[1];
386
-            unset( $_base64data );
386
+            unset($_base64data);
387 387
         } else {
388 388
             // It's an image and we don't have it in cache, get the type by extension.
389
-            $exploded_path = explode( '.', $path );
390
-            $type = end( $exploded_path );
389
+            $exploded_path = explode('.', $path);
390
+            $type = end($exploded_path);
391 391
 
392
-            switch ( $type ) {
392
+            switch ($type) {
393 393
                 case 'jpg':
394 394
                 case 'jpeg':
395 395
                     $dataurihead = 'data:image/jpeg;base64,';
@@ -411,15 +411,15 @@  discard block
 block discarded – undo
411 411
             }
412 412
 
413 413
             // Encode the data.
414
-            $base64data  = base64_encode( file_get_contents( $path ) );
415
-            $headAndData = $dataurihead . $base64data;
414
+            $base64data  = base64_encode(file_get_contents($path));
415
+            $headAndData = $dataurihead.$base64data;
416 416
 
417 417
             // Save in cache.
418
-            $check->cache( $headAndData, 'text/plain' );
418
+            $check->cache($headAndData, 'text/plain');
419 419
         }
420
-        unset( $check );
420
+        unset($check);
421 421
 
422
-        return array( 'full' => $headAndData, 'base64data' => $base64data );
422
+        return array('full' => $headAndData, 'base64data' => $base64data);
423 423
     }
424 424
 
425 425
     /**
@@ -431,13 +431,13 @@  discard block
 block discarded – undo
431 431
      *
432 432
      * @return string
433 433
      */
434
-    protected static function replace_longest_matches_first( $string, $replacements = array() )
434
+    protected static function replace_longest_matches_first($string, $replacements = array())
435 435
     {
436
-        if ( ! empty( $replacements ) ) {
436
+        if (!empty($replacements)) {
437 437
             // Sort the replacements array by key length in desc order (so that the longest strings are replaced first).
438
-            $keys = array_map( 'strlen', array_keys( $replacements ) );
439
-            array_multisort( $keys, SORT_DESC, $replacements );
440
-            $string = str_replace( array_keys( $replacements ), array_values( $replacements ), $string );
438
+            $keys = array_map('strlen', array_keys($replacements));
439
+            array_multisort($keys, SORT_DESC, $replacements);
440
+            $string = str_replace(array_keys($replacements), array_values($replacements), $string);
441 441
         }
442 442
 
443 443
         return $string;
@@ -453,15 +453,15 @@  discard block
 block discarded – undo
453 453
      *
454 454
      * @return string
455 455
      */
456
-    public function replace_urls( $code = '' )
456
+    public function replace_urls($code = '')
457 457
     {
458 458
         $replacements = array();
459 459
 
460
-        preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches );
461
-        if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) {
462
-            foreach ( $url_src_matches[1] as $count => $original_url ) {
460
+        preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches);
461
+        if (is_array($url_src_matches) && !empty($url_src_matches)) {
462
+            foreach ($url_src_matches[1] as $count => $original_url) {
463 463
                 // Removes quotes and other cruft.
464
-                $url = trim( $original_url, " \t\n\r\0\x0B\"'" );
464
+                $url = trim($original_url, " \t\n\r\0\x0B\"'");
465 465
 
466 466
                 /**
467 467
                  * TODO/FIXME: Add a way for other code / callable to be called here
@@ -479,17 +479,17 @@  discard block
 block discarded – undo
479 479
                  */
480 480
 
481 481
                 // Do CDN replacement if needed.
482
-                if ( ! empty( $this->cdn_url ) ) {
483
-                    $replacement_url = $this->url_replace_cdn( $url );
482
+                if (!empty($this->cdn_url)) {
483
+                    $replacement_url = $this->url_replace_cdn($url);
484 484
                     // Prepare replacements array.
485
-                    $replacements[ $url_src_matches[1][ $count ] ] = str_replace(
485
+                    $replacements[$url_src_matches[1][$count]] = str_replace(
486 486
                         $original_url, $replacement_url, $url_src_matches[1][$count]
487 487
                     );
488 488
                 }
489 489
             }
490 490
         }
491 491
 
492
-        $code = self::replace_longest_matches_first( $code, $replacements );
492
+        $code = self::replace_longest_matches_first($code, $replacements);
493 493
 
494 494
         return $code;
495 495
     }
@@ -502,26 +502,26 @@  discard block
 block discarded – undo
502 502
      * @param string $code
503 503
      * @return string
504 504
      */
505
-    public function hide_fontface_and_maybe_cdn( $code )
505
+    public function hide_fontface_and_maybe_cdn($code)
506 506
     {
507 507
         // Proceed only if @font-face declarations exist within $code.
508
-        preg_match_all( self::FONT_FACE_REGEX, $code, $fontfaces );
509
-        if ( isset( $fontfaces[0] ) ) {
508
+        preg_match_all(self::FONT_FACE_REGEX, $code, $fontfaces);
509
+        if (isset($fontfaces[0])) {
510 510
             // Check if we need to cdn fonts or not.
511
-            $do_font_cdn = apply_filters( 'autoptimize_filter_css_fonts_cdn', false );
511
+            $do_font_cdn = apply_filters('autoptimize_filter_css_fonts_cdn', false);
512 512
 
513
-            foreach ( $fontfaces[0] as $full_match ) {
513
+            foreach ($fontfaces[0] as $full_match) {
514 514
                 // Keep original match so we can search/replace it.
515 515
                 $match_search = $full_match;
516 516
 
517 517
                 // Do font cdn if needed.
518
-                if ( $do_font_cdn ) {
519
-                    $full_match = $this->replace_urls( $full_match );
518
+                if ($do_font_cdn) {
519
+                    $full_match = $this->replace_urls($full_match);
520 520
                 }
521 521
 
522 522
                 // Replace declaration with its base64 encoded string.
523
-                $replacement = self::build_marker( 'FONTFACE', $full_match );
524
-                $code = str_replace( $match_search, $replacement, $code );
523
+                $replacement = self::build_marker('FONTFACE', $full_match);
524
+                $code = str_replace($match_search, $replacement, $code);
525 525
             }
526 526
         }
527 527
 
@@ -535,16 +535,16 @@  discard block
 block discarded – undo
535 535
      * @param string $code
536 536
      * @return string
537 537
      */
538
-    public function restore_fontface( $code )
538
+    public function restore_fontface($code)
539 539
     {
540
-        return $this->restore_marked_content( 'FONTFACE', $code );
540
+        return $this->restore_marked_content('FONTFACE', $code);
541 541
     }
542 542
 
543 543
     // Re-write (and/or inline) referenced assets.
544
-    public function rewrite_assets( $code )
544
+    public function rewrite_assets($code)
545 545
     {
546 546
         // Handle @font-face rules by hiding and processing them separately.
547
-        $code = $this->hide_fontface_and_maybe_cdn( $code );
547
+        $code = $this->hide_fontface_and_maybe_cdn($code);
548 548
 
549 549
         /**
550 550
          * TODO/FIXME:
@@ -560,30 +560,30 @@  discard block
 block discarded – undo
560 560
         $url_src_matches = array();
561 561
         $imgreplace = array();
562 562
         // Matches and captures anything specified within the literal `url()` and excludes those containing data: URIs.
563
-        preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches );
564
-        if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) {
565
-            foreach ( $url_src_matches[1] as $count => $original_url ) {
563
+        preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches);
564
+        if (is_array($url_src_matches) && !empty($url_src_matches)) {
565
+            foreach ($url_src_matches[1] as $count => $original_url) {
566 566
                 // Removes quotes and other cruft.
567
-                $url = trim( $original_url, " \t\n\r\0\x0B\"'" );
567
+                $url = trim($original_url, " \t\n\r\0\x0B\"'");
568 568
 
569 569
                 // If datauri inlining is turned on, do it.
570 570
                 $inlined = false;
571
-                if ( $this->datauris ) {
571
+                if ($this->datauris) {
572 572
                     $iurl = $url;
573
-                    if ( false !== strpos( $iurl, '?' ) ) {
574
-                        $iurl = strtok( $iurl, '?' );
573
+                    if (false !== strpos($iurl, '?')) {
574
+                        $iurl = strtok($iurl, '?');
575 575
                     }
576 576
 
577
-                    $ipath = $this->getpath( $iurl );
577
+                    $ipath = $this->getpath($iurl);
578 578
 
579
-                    $excluded = $this->check_datauri_exclude_list( $ipath );
580
-                    if ( ! $excluded ) {
581
-                        $is_datauri_candidate = $this->is_datauri_candidate( $ipath );
582
-                        if ( $is_datauri_candidate ) {
583
-                            $datauri     = $this->build_or_get_datauri_image( $ipath );
579
+                    $excluded = $this->check_datauri_exclude_list($ipath);
580
+                    if (!$excluded) {
581
+                        $is_datauri_candidate = $this->is_datauri_candidate($ipath);
582
+                        if ($is_datauri_candidate) {
583
+                            $datauri     = $this->build_or_get_datauri_image($ipath);
584 584
                             $base64data  = $datauri['base64data'];
585 585
                             // Add it to the list for replacement.
586
-                            $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace(
586
+                            $imgreplace[$url_src_matches[1][$count]] = str_replace(
587 587
                                     $original_url,
588 588
                                     $datauri['full'],
589 589
                                     $url_src_matches[1][$count]
@@ -599,20 +599,20 @@  discard block
 block discarded – undo
599 599
                  * inlining isn't turned on, or if a resource is skipped from
600 600
                  * being inlined for whatever reason above.
601 601
                  */
602
-                if ( ! $inlined && ( ! empty( $this->cdn_url ) || has_filter( 'autoptimize_filter_base_replace_cdn' ) ) ) {
602
+                if (!$inlined && (!empty($this->cdn_url) || has_filter('autoptimize_filter_base_replace_cdn'))) {
603 603
                     // Just do the "simple" CDN replacement.
604
-                    $replacement_url = $this->url_replace_cdn( $url );
605
-                    $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace(
604
+                    $replacement_url = $this->url_replace_cdn($url);
605
+                    $imgreplace[$url_src_matches[1][$count]] = str_replace(
606 606
                         $original_url, $replacement_url, $url_src_matches[1][$count]
607 607
                     );
608 608
                 }
609 609
             }
610 610
         }
611 611
 
612
-        $code = self::replace_longest_matches_first( $code, $imgreplace );
612
+        $code = self::replace_longest_matches_first($code, $imgreplace);
613 613
 
614 614
         // Replace back font-face markers with actual font-face declarations.
615
-        $code = $this->restore_fontface( $code );
615
+        $code = $this->restore_fontface($code);
616 616
 
617 617
         return $code;
618 618
     }
@@ -620,29 +620,29 @@  discard block
 block discarded – undo
620 620
     // Joins and optimizes CSS.
621 621
     public function minify()
622 622
     {
623
-        foreach ( $this->css as $group ) {
624
-            list( $media, $css ) = $group;
625
-            if ( preg_match( '#^INLINE;#', $css ) ) {
623
+        foreach ($this->css as $group) {
624
+            list($media, $css) = $group;
625
+            if (preg_match('#^INLINE;#', $css)) {
626 626
                 // <style>.
627
-                $css = preg_replace( '#^INLINE;#', '', $css );
628
-                $css = self::fixurls( ABSPATH . 'index.php', $css ); // ABSPATH already contains a trailing slash.
629
-                $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, '' );
630
-                if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) {
627
+                $css = preg_replace('#^INLINE;#', '', $css);
628
+                $css = self::fixurls(ABSPATH.'index.php', $css); // ABSPATH already contains a trailing slash.
629
+                $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, '');
630
+                if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
631 631
                     $css = $tmpstyle;
632 632
                     $this->alreadyminified = true;
633 633
                 }
634 634
             } else {
635 635
                 // <link>
636
-                if ( false !== $css && file_exists( $css ) && is_readable( $css ) ) {
636
+                if (false !== $css && file_exists($css) && is_readable($css)) {
637 637
                     $cssPath = $css;
638
-                    $css = self::fixurls( $cssPath, file_get_contents( $cssPath ) );
639
-                    $css = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $css );
640
-                    $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $cssPath );
641
-                    if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) {
638
+                    $css = self::fixurls($cssPath, file_get_contents($cssPath));
639
+                    $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css);
640
+                    $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $cssPath);
641
+                    if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
642 642
                         $css = $tmpstyle;
643 643
                         $this->alreadyminified = true;
644
-                    } elseif ( $this->can_inject_late( $cssPath, $css ) ) {
645
-                        $css = self::build_injectlater_marker( $cssPath, md5( $css ) );
644
+                    } elseif ($this->can_inject_late($cssPath, $css)) {
645
+                        $css = self::build_injectlater_marker($cssPath, md5($css));
646 646
                     }
647 647
                 } else {
648 648
                     // Couldn't read CSS. Maybe getpath isn't working?
@@ -650,12 +650,12 @@  discard block
 block discarded – undo
650 650
                 }
651 651
             }
652 652
 
653
-            foreach ( $media as $elem ) {
654
-                if ( ! empty( $css ) ) {
655
-                    if ( ! isset( $this->csscode[$elem] ) ) {
653
+            foreach ($media as $elem) {
654
+                if (!empty($css)) {
655
+                    if (!isset($this->csscode[$elem])) {
656 656
                         $this->csscode[$elem] = '';
657 657
                     }
658
-                    $this->csscode[$elem] .= "\n/*FILESTART*/" . $css;
658
+                    $this->csscode[$elem] .= "\n/*FILESTART*/".$css;
659 659
                 }
660 660
             }
661 661
         }
@@ -663,133 +663,133 @@  discard block
 block discarded – undo
663 663
         // Check for duplicate code.
664 664
         $md5list = array();
665 665
         $tmpcss  = $this->csscode;
666
-        foreach ( $tmpcss as $media => $code ) {
667
-            $md5sum    = md5( $code );
666
+        foreach ($tmpcss as $media => $code) {
667
+            $md5sum    = md5($code);
668 668
             $medianame = $media;
669
-            foreach ( $md5list as $med => $sum ) {
669
+            foreach ($md5list as $med => $sum) {
670 670
                 // If same code.
671
-                if ( $sum === $md5sum ) {
671
+                if ($sum === $md5sum) {
672 672
                     // Add the merged code.
673
-                    $medianame                 = $med . ', ' . $media;
673
+                    $medianame                 = $med.', '.$media;
674 674
                     $this->csscode[$medianame] = $code;
675 675
                     $md5list[$medianame]       = $md5list[$med];
676
-                    unset( $this->csscode[$med], $this->csscode[$media], $md5list[$med] );
676
+                    unset($this->csscode[$med], $this->csscode[$media], $md5list[$med]);
677 677
                 }
678 678
             }
679 679
             $md5list[$medianame] = $md5sum;
680 680
         }
681
-        unset( $tmpcss );
681
+        unset($tmpcss);
682 682
 
683 683
         // Manage @imports, while is for recursive import management.
684
-        foreach ( $this->csscode as &$thiscss ) {
684
+        foreach ($this->csscode as &$thiscss) {
685 685
             // Flag to trigger import reconstitution and var to hold external imports.
686 686
             $fiximports       = false;
687 687
             $external_imports = '';
688 688
 
689 689
             // remove comments to avoid importing commented-out imports.
690
-            $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss );
691
-            while ( preg_match_all( '#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches ) ) {
692
-                foreach ( $matches[0] as $import ) {
693
-                    if ( $this->isremovable( $import, $this->cssremovables ) ) {
694
-                        $thiscss = str_replace( $import, '', $thiscss );
690
+            $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss);
691
+            while (preg_match_all('#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches)) {
692
+                foreach ($matches[0] as $import) {
693
+                    if ($this->isremovable($import, $this->cssremovables)) {
694
+                        $thiscss = str_replace($import, '', $thiscss);
695 695
                         $import_ok = true;
696 696
                     } else {
697
-                        $url = trim( preg_replace( '#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim( $import ) ), " \t\n\r\0\x0B\"'" );
698
-                        $path = $this->getpath( $url );
697
+                        $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'");
698
+                        $path = $this->getpath($url);
699 699
                         $import_ok = false;
700
-                        if ( file_exists( $path ) && is_readable( $path ) ) {
701
-                            $code = addcslashes( self::fixurls( $path, file_get_contents( $path ) ), "\\" );
702
-                            $code = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $code );
703
-                            $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, '' );
704
-                            if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) {
700
+                        if (file_exists($path) && is_readable($path)) {
701
+                            $code = addcslashes(self::fixurls($path, file_get_contents($path)), "\\");
702
+                            $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code);
703
+                            $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, '');
704
+                            if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) {
705 705
                                 $code = $tmpstyle;
706 706
                                 $this->alreadyminified = true;
707
-                            } elseif ( $this->can_inject_late( $path, $code ) ) {
708
-                                $code = self::build_injectlater_marker( $path, md5( $code ) );
707
+                            } elseif ($this->can_inject_late($path, $code)) {
708
+                                $code = self::build_injectlater_marker($path, md5($code));
709 709
                             }
710 710
 
711
-                            if ( ! empty( $code ) ) {
712
-                                $tmp_thiscss = preg_replace( '#(/\*FILESTART\*/.*)' . preg_quote( $import, '#' ) . '#Us', '/*FILESTART2*/' . $code . '$1', $thiscss );
713
-                                if ( ! empty( $tmp_thiscss ) ) {
711
+                            if (!empty($code)) {
712
+                                $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss);
713
+                                if (!empty($tmp_thiscss)) {
714 714
                                     $thiscss = $tmp_thiscss;
715 715
                                     $import_ok = true;
716
-                                    unset( $tmp_thiscss );
716
+                                    unset($tmp_thiscss);
717 717
                                 }
718 718
                             }
719
-                            unset( $code );
719
+                            unset($code);
720 720
                         }
721 721
                     }
722
-                    if ( ! $import_ok ) {
722
+                    if (!$import_ok) {
723 723
                         // External imports and general fall-back.
724 724
                         $external_imports .= $import;
725 725
 
726
-                        $thiscss    = str_replace( $import, '', $thiscss );
726
+                        $thiscss    = str_replace($import, '', $thiscss);
727 727
                         $fiximports = true;
728 728
                     }
729 729
                 }
730
-                $thiscss = preg_replace( '#/\*FILESTART\*/#', '', $thiscss );
731
-                $thiscss = preg_replace( '#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss );
730
+                $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss);
731
+                $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss);
732 732
 
733 733
                 // and update $thiscss_nocomments before going into next iteration in while loop.
734
-                $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss );
734
+                $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss);
735 735
             }
736
-            unset( $thiscss_nocomments );
736
+            unset($thiscss_nocomments);
737 737
 
738 738
             // Add external imports to top of aggregated CSS.
739
-            if ( $fiximports ) {
740
-                $thiscss = $external_imports . $thiscss;
739
+            if ($fiximports) {
740
+                $thiscss = $external_imports.$thiscss;
741 741
             }
742 742
         }
743
-        unset( $thiscss );
743
+        unset($thiscss);
744 744
 
745 745
         // $this->csscode has all the uncompressed code now.
746
-        foreach ( $this->csscode as &$code ) {
746
+        foreach ($this->csscode as &$code) {
747 747
             // Check for already-minified code.
748
-            $hash = md5( $code );
749
-            do_action( 'autoptimize_action_css_hash', $hash );
750
-            $ccheck = new autoptimizeCache( $hash, 'css' );
751
-            if ( $ccheck->check() ) {
748
+            $hash = md5($code);
749
+            do_action('autoptimize_action_css_hash', $hash);
750
+            $ccheck = new autoptimizeCache($hash, 'css');
751
+            if ($ccheck->check()) {
752 752
                 $code = $ccheck->retrieve();
753
-                $this->hashmap[md5( $code )] = $hash;
753
+                $this->hashmap[md5($code)] = $hash;
754 754
                 continue;
755 755
             }
756
-            unset( $ccheck );
756
+            unset($ccheck);
757 757
 
758 758
             // Rewrite and/or inline referenced assets.
759
-            $code = $this->rewrite_assets( $code );
759
+            $code = $this->rewrite_assets($code);
760 760
 
761 761
             // Minify.
762
-            $code = $this->run_minifier_on( $code );
762
+            $code = $this->run_minifier_on($code);
763 763
 
764 764
             // Bring back INJECTLATER stuff.
765
-            $code = $this->inject_minified( $code );
765
+            $code = $this->inject_minified($code);
766 766
 
767 767
             // Filter results.
768
-            $tmp_code = apply_filters( 'autoptimize_css_after_minify', $code );
769
-            if ( ! empty( $tmp_code ) ) {
768
+            $tmp_code = apply_filters('autoptimize_css_after_minify', $code);
769
+            if (!empty($tmp_code)) {
770 770
                 $code = $tmp_code;
771
-                unset( $tmp_code );
771
+                unset($tmp_code);
772 772
             }
773 773
 
774
-            $this->hashmap[md5( $code )] = $hash;
774
+            $this->hashmap[md5($code)] = $hash;
775 775
         }
776 776
 
777
-        unset( $code );
777
+        unset($code);
778 778
         return true;
779 779
     }
780 780
 
781
-    public function run_minifier_on( $code )
781
+    public function run_minifier_on($code)
782 782
     {
783
-        if ( ! $this->alreadyminified ) {
784
-            $do_minify = apply_filters( 'autoptimize_css_do_minify', true );
783
+        if (!$this->alreadyminified) {
784
+            $do_minify = apply_filters('autoptimize_css_do_minify', true);
785 785
 
786
-            if ( $do_minify ) {
786
+            if ($do_minify) {
787 787
                 $cssmin   = new autoptimizeCSSmin();
788
-                $tmp_code = trim( $cssmin->run( $code ) );
788
+                $tmp_code = trim($cssmin->run($code));
789 789
 
790
-                if ( ! empty( $tmp_code ) ) {
790
+                if (!empty($tmp_code)) {
791 791
                     $code = $tmp_code;
792
-                    unset( $tmp_code );
792
+                    unset($tmp_code);
793 793
                 }
794 794
             }
795 795
         }
@@ -801,14 +801,14 @@  discard block
 block discarded – undo
801 801
     public function cache()
802 802
     {
803 803
         // CSS cache.
804
-        foreach ( $this->csscode as $media => $code ) {
805
-            $md5 = $this->hashmap[md5( $code )];
806
-            $cache = new autoptimizeCache( $md5, 'css' );
807
-            if ( ! $cache->check() ) {
804
+        foreach ($this->csscode as $media => $code) {
805
+            $md5 = $this->hashmap[md5($code)];
806
+            $cache = new autoptimizeCache($md5, 'css');
807
+            if (!$cache->check()) {
808 808
                 // Cache our code.
809
-                $cache->cache( $code, 'text/css' );
809
+                $cache->cache($code, 'text/css');
810 810
             }
811
-            $this->url[$media] = AUTOPTIMIZE_CACHE_URL . $cache->getname();
811
+            $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname();
812 812
         }
813 813
     }
814 814
 
@@ -816,136 +816,136 @@  discard block
 block discarded – undo
816 816
     public function getcontent()
817 817
     {
818 818
         // Restore the full content (only applies when "autoptimize_filter_css_justhead" filter is true).
819
-        if ( ! empty( $this->restofcontent ) ) {
819
+        if (!empty($this->restofcontent)) {
820 820
             $this->content .= $this->restofcontent;
821 821
             $this->restofcontent = '';
822 822
         }
823 823
 
824 824
         // Inject the new stylesheets.
825
-        $replaceTag = array( '<title', 'before' );
826
-        $replaceTag = apply_filters( 'autoptimize_filter_css_replacetag', $replaceTag, $this->content );
825
+        $replaceTag = array('<title', 'before');
826
+        $replaceTag = apply_filters('autoptimize_filter_css_replacetag', $replaceTag, $this->content);
827 827
 
828
-        if ( $this->inline ) {
829
-            foreach ( $this->csscode as $media => $code ) {
830
-                $this->inject_in_html( '<style type="text/css" media="' . $media . '">' . $code . '</style>', $replaceTag );
828
+        if ($this->inline) {
829
+            foreach ($this->csscode as $media => $code) {
830
+                $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$code.'</style>', $replaceTag);
831 831
             }
832 832
         } else {
833
-            if ( $this->defer ) {
833
+            if ($this->defer) {
834 834
                 $preloadCssBlock = '';
835 835
                 $noScriptCssBlock = "<noscript id=\"aonoscrcss\">";
836 836
 
837 837
                 $defer_inline_code = $this->defer_inline;
838
-                if ( ! empty( $defer_inline_code ) ) {
839
-                    if ( apply_filters( 'autoptimize_filter_css_critcss_minify', true ) ) {
840
-                        $iCssHash = md5( $defer_inline_code );
841
-                        $iCssCache = new autoptimizeCache( $iCssHash, 'css' );
842
-                        if ( $iCssCache->check() ) {
838
+                if (!empty($defer_inline_code)) {
839
+                    if (apply_filters('autoptimize_filter_css_critcss_minify', true)) {
840
+                        $iCssHash = md5($defer_inline_code);
841
+                        $iCssCache = new autoptimizeCache($iCssHash, 'css');
842
+                        if ($iCssCache->check()) {
843 843
                             // we have the optimized inline CSS in cache.
844 844
                             $defer_inline_code = $iCssCache->retrieve();
845 845
                         } else {
846 846
                             $cssmin   = new autoptimizeCSSmin();
847
-                            $tmp_code = trim( $cssmin->run( $defer_inline_code ) );
847
+                            $tmp_code = trim($cssmin->run($defer_inline_code));
848 848
 
849
-                            if ( ! empty( $tmp_code ) ) {
849
+                            if (!empty($tmp_code)) {
850 850
                                 $defer_inline_code = $tmp_code;
851
-                                $iCssCache->cache( $defer_inline_code, 'text/css' );
852
-                                unset( $tmp_code );
851
+                                $iCssCache->cache($defer_inline_code, 'text/css');
852
+                                unset($tmp_code);
853 853
                             }
854 854
                         }
855 855
                     }
856 856
                     // inlined critical css set here, but injected when full CSS is injected
857 857
                     // to avoid CSS containing SVG with <title tag receiving the full CSS link.
858
-                    $inlined_ccss_block = '<style type="text/css" id="aoatfcss" media="all">' . $defer_inline_code . '</style>';
858
+                    $inlined_ccss_block = '<style type="text/css" id="aoatfcss" media="all">'.$defer_inline_code.'</style>';
859 859
                 }
860 860
             }
861 861
 
862
-            foreach ( $this->url as $media => $url ) {
863
-                $url = $this->url_replace_cdn( $url );
862
+            foreach ($this->url as $media => $url) {
863
+                $url = $this->url_replace_cdn($url);
864 864
 
865 865
                 // Add the stylesheet either deferred (import at bottom) or normal links in head.
866
-                if ( $this->defer ) {
866
+                if ($this->defer) {
867 867
                     $preloadOnLoad = autoptimizeConfig::get_ao_css_preload_onload();
868 868
 
869
-                    $preloadCssBlock .= '<link rel="preload" as="style" media="' . $media . '" href="' . $url . '" onload="' . $preloadOnLoad . '" />';
870
-                    $noScriptCssBlock .= '<link type="text/css" media="' . $media . '" href="' . $url . '" rel="stylesheet" />';
869
+                    $preloadCssBlock .= '<link rel="preload" as="style" media="'.$media.'" href="'.$url.'" onload="'.$preloadOnLoad.'" />';
870
+                    $noScriptCssBlock .= '<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />';
871 871
                 } else {
872 872
                     // $this->inject_in_html('<link type="text/css" media="' . $media . '" href="' . $url . '" rel="stylesheet" />', $replaceTag);
873
-                    if ( strlen( $this->csscode[$media] ) > $this->cssinlinesize ) {
874
-                        $this->inject_in_html( '<link type="text/css" media="' . $media . '" href="' . $url . '" rel="stylesheet" />', $replaceTag );
875
-                    } elseif ( strlen( $this->csscode[$media] ) > 0 ) {
876
-                        $this->inject_in_html( '<style type="text/css" media="' . $media . '">' . $this->csscode[$media] . '</style>', $replaceTag );
873
+                    if (strlen($this->csscode[$media]) > $this->cssinlinesize) {
874
+                        $this->inject_in_html('<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" />', $replaceTag);
875
+                    } elseif (strlen($this->csscode[$media]) > 0) {
876
+                        $this->inject_in_html('<style type="text/css" media="'.$media.'">'.$this->csscode[$media].'</style>', $replaceTag);
877 877
                     }
878 878
                 }
879 879
             }
880 880
 
881
-            if ( $this->defer ) {
881
+            if ($this->defer) {
882 882
                 $preload_polyfill = autoptimizeConfig::get_ao_css_preload_polyfill();
883 883
                 $noScriptCssBlock .= '</noscript>';
884 884
                 // Inject inline critical CSS, the preloaded full CSS and the noscript-CSS.
885
-                $this->inject_in_html( $inlined_ccss_block . $preloadCssBlock . $noScriptCssBlock, $replaceTag );
885
+                $this->inject_in_html($inlined_ccss_block.$preloadCssBlock.$noScriptCssBlock, $replaceTag);
886 886
 
887 887
                 // Adds preload polyfill at end of body tag.
888 888
                 $this->inject_in_html(
889
-                    apply_filters( 'autoptimize_css_preload_polyfill', $preload_polyfill ),
890
-                    apply_filters( 'autoptimize_css_preload_polyfill_injectat', array( '</body>', 'before' ) )
889
+                    apply_filters('autoptimize_css_preload_polyfill', $preload_polyfill),
890
+                    apply_filters('autoptimize_css_preload_polyfill_injectat', array('</body>', 'before'))
891 891
                 );
892 892
             }
893 893
         }
894 894
 
895 895
         // restore comments.
896
-        $this->content = $this->restore_comments( $this->content );
896
+        $this->content = $this->restore_comments($this->content);
897 897
 
898 898
         // restore IE hacks.
899
-        $this->content = $this->restore_iehacks( $this->content );
899
+        $this->content = $this->restore_iehacks($this->content);
900 900
 
901 901
         // restore (no)script.
902
-        $this->content = $this->restore_marked_content( 'SCRIPT', $this->content );
902
+        $this->content = $this->restore_marked_content('SCRIPT', $this->content);
903 903
 
904 904
         // Restore noptimize.
905
-        $this->content = $this->restore_noptimize( $this->content );
905
+        $this->content = $this->restore_noptimize($this->content);
906 906
 
907 907
         // Return the modified stylesheet.
908 908
         return $this->content;
909 909
     }
910 910
 
911
-    static function fixurls( $file, $code )
911
+    static function fixurls($file, $code)
912 912
     {
913 913
         // Switch all imports to the url() syntax.
914
-        $code = preg_replace( '#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code );
914
+        $code = preg_replace('#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code);
915 915
 
916
-        if ( preg_match_all( self::ASSETS_REGEX, $code, $matches ) ) {
917
-            $file = str_replace( WP_ROOT_DIR, '/', $file );
916
+        if (preg_match_all(self::ASSETS_REGEX, $code, $matches)) {
917
+            $file = str_replace(WP_ROOT_DIR, '/', $file);
918 918
             /**
919 919
              * rollback as per https://github.com/futtta/autoptimize/issues/94
920 920
              * $file = str_replace( AUTOPTIMIZE_WP_CONTENT_NAME, '', $file );
921 921
              */
922
-            $dir = dirname( $file ); // Like /themes/expound/css.
922
+            $dir = dirname($file); // Like /themes/expound/css.
923 923
 
924 924
             /**
925 925
              * $dir should not contain backslashes, since it's used to replace
926 926
              * urls, but it can contain them when running on Windows because
927 927
              * fixurls() is sometimes called with `ABSPATH . 'index.php'`
928 928
              */
929
-            $dir = str_replace( '\\', '/', $dir );
930
-            unset( $file ); // not used below at all.
929
+            $dir = str_replace('\\', '/', $dir);
930
+            unset($file); // not used below at all.
931 931
 
932 932
             $replace = array();
933
-            foreach ( $matches[1] as $k => $url ) {
933
+            foreach ($matches[1] as $k => $url) {
934 934
                 // Remove quotes.
935
-                $url    = trim( $url, " \t\n\r\0\x0B\"'" );
936
-                $noQurl = trim( $url, "\"'" );
937
-                if ( $url !== $noQurl ) {
935
+                $url    = trim($url, " \t\n\r\0\x0B\"'");
936
+                $noQurl = trim($url, "\"'");
937
+                if ($url !== $noQurl) {
938 938
                     $removedQuotes = true;
939 939
                 } else {
940 940
                     $removedQuotes = false;
941 941
                 }
942 942
 
943
-                if ( '' === $noQurl ) {
943
+                if ('' === $noQurl) {
944 944
                     continue;
945 945
                 }
946 946
 
947 947
                 $url = $noQurl;
948
-                if ( '/' === $url{0} || preg_match( '#^(https?://|ftp://|data:)#i', $url ) ) {
948
+                if ('/' === $url{0} || preg_match('#^(https?://|ftp://|data:)#i', $url)) {
949 949
                     // URL is protocol-relative, host-relative or something we don't touch.
950 950
                     continue;
951 951
                 } else {
@@ -954,49 +954,49 @@  discard block
 block discarded – undo
954 954
                      * rollback as per https://github.com/futtta/autoptimize/issues/94
955 955
                      * $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_CONTENT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) );
956 956
                      */
957
-                    $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_ROOT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) );
958
-                    $newurl = apply_filters( 'autoptimize_filter_css_fixurl_newurl', $newurl );
957
+                    $newurl = preg_replace('/https?:/', '', str_replace(' ', '%20', AUTOPTIMIZE_WP_ROOT_URL.str_replace('//', '/', $dir.'/'.$url)));
958
+                    $newurl = apply_filters('autoptimize_filter_css_fixurl_newurl', $newurl);
959 959
 
960 960
                     /**
961 961
                      * Hash the url + whatever was behind potentially for replacement
962 962
                      * We must do this, or different css classes referencing the same bg image (but
963 963
                      * different parts of it, say, in sprites and such) loose their stuff...
964 964
                      */
965
-                    $hash = md5( $url . $matches[2][$k] );
966
-                    $code = str_replace( $matches[0][$k], $hash, $code );
965
+                    $hash = md5($url.$matches[2][$k]);
966
+                    $code = str_replace($matches[0][$k], $hash, $code);
967 967
 
968
-                    if ( $removedQuotes ) {
969
-                        $replace[$hash] = "url('" . $newurl . "')" . $matches[2][$k];
968
+                    if ($removedQuotes) {
969
+                        $replace[$hash] = "url('".$newurl."')".$matches[2][$k];
970 970
                     } else {
971
-                        $replace[$hash] = 'url(' . $newurl . ')' . $matches[2][$k];
971
+                        $replace[$hash] = 'url('.$newurl.')'.$matches[2][$k];
972 972
                     }
973 973
                 }
974 974
             }
975 975
 
976
-            $code = self::replace_longest_matches_first( $code, $replace );
976
+            $code = self::replace_longest_matches_first($code, $replace);
977 977
         }
978 978
 
979 979
         return $code;
980 980
     }
981 981
 
982
-    private function ismovable( $tag )
982
+    private function ismovable($tag)
983 983
     {
984
-        if ( ! $this->aggregate ) {
984
+        if (!$this->aggregate) {
985 985
             return false;
986 986
         }
987 987
 
988
-        if ( ! empty( $this->whitelist ) ) {
989
-            foreach ( $this->whitelist as $match ) {
990
-                if ( false !== strpos( $tag, $match ) ) {
988
+        if (!empty($this->whitelist)) {
989
+            foreach ($this->whitelist as $match) {
990
+                if (false !== strpos($tag, $match)) {
991 991
                     return true;
992 992
                 }
993 993
             }
994 994
             // no match with whitelist.
995 995
             return false;
996 996
         } else {
997
-            if ( is_array( $this->dontmove ) && ! empty( $this->dontmove ) ) {
998
-                foreach ( $this->dontmove as $match ) {
999
-                    if ( false !== strpos( $tag, $match ) ) {
997
+            if (is_array($this->dontmove) && !empty($this->dontmove)) {
998
+                foreach ($this->dontmove as $match) {
999
+                    if (false !== strpos($tag, $match)) {
1000 1000
                         // Matched something.
1001 1001
                         return false;
1002 1002
                     }
@@ -1008,22 +1008,22 @@  discard block
 block discarded – undo
1008 1008
         }
1009 1009
     }
1010 1010
 
1011
-    private function can_inject_late( $cssPath, $css )
1011
+    private function can_inject_late($cssPath, $css)
1012 1012
     {
1013
-        $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false, $cssPath );
1014
-        if ( true !== $this->inject_min_late ) {
1013
+        $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false, $cssPath);
1014
+        if (true !== $this->inject_min_late) {
1015 1015
             // late-inject turned off.
1016 1016
             return false;
1017
-        } elseif ( ( false === strpos( $cssPath, 'min.css' ) ) && ( str_replace( $consider_minified_array, '', $cssPath ) === $cssPath ) ) {
1017
+        } elseif ((false === strpos($cssPath, 'min.css')) && (str_replace($consider_minified_array, '', $cssPath) === $cssPath)) {
1018 1018
             // file not minified based on filename & filter.
1019 1019
             return false;
1020
-        } elseif ( false !== strpos( $css, '@import' ) ) {
1020
+        } elseif (false !== strpos($css, '@import')) {
1021 1021
             // can't late-inject files with imports as those need to be aggregated.
1022 1022
             return false;
1023
-        } elseif ( ( false !== strpos( $css, '@font-face' ) ) && ( apply_filters( 'autoptimize_filter_css_fonts_cdn', false ) === true ) && ( ! empty( $this->cdn_url ) ) ) {
1023
+        } elseif ((false !== strpos($css, '@font-face')) && (apply_filters('autoptimize_filter_css_fonts_cdn', false) === true) && (!empty($this->cdn_url))) {
1024 1024
             // don't late-inject CSS with font-src's if fonts are set to be CDN'ed.
1025 1025
             return false;
1026
-        } elseif ( ( ( $this->datauris == true ) || ( ! empty( $this->cdn_url ) ) ) && preg_match( '#background[^;}]*url\(#Ui', $css ) ) {
1026
+        } elseif ((($this->datauris == true) || (!empty($this->cdn_url))) && preg_match('#background[^;}]*url\(#Ui', $css)) {
1027 1027
             // don't late-inject CSS with images if CDN is set OR if image inlining is on.
1028 1028
             return false;
1029 1029
         } else {
@@ -1041,32 +1041,32 @@  discard block
 block discarded – undo
1041 1041
      *
1042 1042
      * @return bool|string Url pointing to the minified css file or false.
1043 1043
      */
1044
-    public function minify_single( $filepath, $cache_miss = false )
1044
+    public function minify_single($filepath, $cache_miss = false)
1045 1045
     {
1046
-        $contents = $this->prepare_minify_single( $filepath );
1046
+        $contents = $this->prepare_minify_single($filepath);
1047 1047
 
1048
-        if ( empty( $contents ) ) {
1048
+        if (empty($contents)) {
1049 1049
             return false;
1050 1050
         }
1051 1051
 
1052 1052
         // Check cache.
1053
-        $hash  = 'single_' . md5( $contents );
1054
-        $cache = new autoptimizeCache( $hash, 'css' );
1053
+        $hash  = 'single_'.md5($contents);
1054
+        $cache = new autoptimizeCache($hash, 'css');
1055 1055
 
1056 1056
         // If not in cache already, minify...
1057
-        if ( ! $cache->check() || $cache_miss ) {
1057
+        if (!$cache->check() || $cache_miss) {
1058 1058
             // Fixurls...
1059
-            $contents = self::fixurls( $filepath, $contents );
1059
+            $contents = self::fixurls($filepath, $contents);
1060 1060
             // CDN-replace any referenced assets if needed...
1061
-            $contents = $this->replace_urls( $contents );
1061
+            $contents = $this->replace_urls($contents);
1062 1062
             // Now minify...
1063 1063
             $cssmin   = new autoptimizeCSSmin();
1064
-            $contents = trim( $cssmin->run( $contents ) );
1064
+            $contents = trim($cssmin->run($contents));
1065 1065
             // Store in cache.
1066
-            $cache->cache( $contents, 'text/css' );
1066
+            $cache->cache($contents, 'text/css');
1067 1067
         }
1068 1068
 
1069
-        $url = $this->build_minify_single_url( $cache );
1069
+        $url = $this->build_minify_single_url($cache);
1070 1070
 
1071 1071
         return $url;
1072 1072
     }
@@ -1086,18 +1086,18 @@  discard block
 block discarded – undo
1086 1086
         return $this->options;
1087 1087
     }
1088 1088
 
1089
-    public function replaceOptions( $options )
1089
+    public function replaceOptions($options)
1090 1090
     {
1091 1091
         $this->options = $options;
1092 1092
     }
1093 1093
 
1094
-    public function setOption( $name, $value )
1094
+    public function setOption($name, $value)
1095 1095
     {
1096 1096
         $this->options[$name] = $value;
1097 1097
         $this->$name = $value;
1098 1098
     }
1099 1099
 
1100
-    public function getOption( $name )
1100
+    public function getOption($name)
1101 1101
     {
1102 1102
         return $this->options[$name];
1103 1103
     }
Please login to merge, or discard this patch.