@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Handles disk-cache-related operations. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -38,22 +38,22 @@ discard block |
||
38 | 38 | * @param string $md5 Hash. |
39 | 39 | * @param string $ext Extension. |
40 | 40 | */ |
41 | - public function __construct( $md5, $ext = 'php' ) |
|
41 | + public function __construct($md5, $ext = 'php') |
|
42 | 42 | { |
43 | 43 | $_min_ext = ''; |
44 | - if ( apply_filters( 'autoptimize_filter_cache_url_add_min_ext', false ) ) { |
|
44 | + if (apply_filters('autoptimize_filter_cache_url_add_min_ext', false)) { |
|
45 | 45 | $_min_ext = '.min'; |
46 | 46 | } |
47 | 47 | |
48 | 48 | $this->cachedir = AUTOPTIMIZE_CACHE_DIR; |
49 | 49 | $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP; |
50 | - if ( ! $this->nogzip ) { |
|
51 | - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.php'; |
|
50 | + if (!$this->nogzip) { |
|
51 | + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.$_min_ext.'.php'; |
|
52 | 52 | } else { |
53 | - if ( in_array( $ext, array( 'js', 'css' ) ) ) { |
|
54 | - $this->filename = $ext . '/' . AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext; |
|
53 | + if (in_array($ext, array('js', 'css'))) { |
|
54 | + $this->filename = $ext.'/'.AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.$_min_ext.'.'.$ext; |
|
55 | 55 | } else { |
56 | - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext; |
|
56 | + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.$_min_ext.'.'.$ext; |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function check() |
67 | 67 | { |
68 | - return file_exists( $this->cachedir . $this->filename ); |
|
68 | + return file_exists($this->cachedir.$this->filename); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -75,11 +75,11 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function retrieve() |
77 | 77 | { |
78 | - if ( $this->check() ) { |
|
79 | - if ( false == $this->nogzip ) { |
|
80 | - return file_get_contents( $this->cachedir . $this->filename . '.none' ); |
|
78 | + if ($this->check()) { |
|
79 | + if (false == $this->nogzip) { |
|
80 | + return file_get_contents($this->cachedir.$this->filename.'.none'); |
|
81 | 81 | } else { |
82 | - return file_get_contents( $this->cachedir . $this->filename ); |
|
82 | + return file_get_contents($this->cachedir.$this->filename); |
|
83 | 83 | } |
84 | 84 | } |
85 | 85 | return false; |
@@ -93,50 +93,50 @@ discard block |
||
93 | 93 | * |
94 | 94 | * @return void |
95 | 95 | */ |
96 | - public function cache( $data, $mime ) |
|
96 | + public function cache($data, $mime) |
|
97 | 97 | { |
98 | 98 | // off by default; check if cachedirs exist every time before caching |
99 | 99 | // |
100 | 100 | // to be activated for users that experience these ugly errors; |
101 | 101 | // PHP Warning: file_put_contents failed to open stream: No such file or directory. |
102 | - if ( apply_filters( 'autoptimize_filter_cache_checkdirs_on_write', false ) ) { |
|
102 | + if (apply_filters('autoptimize_filter_cache_checkdirs_on_write', false)) { |
|
103 | 103 | $this->check_and_create_dirs(); |
104 | 104 | } |
105 | 105 | |
106 | - if ( false === $this->nogzip ) { |
|
106 | + if (false === $this->nogzip) { |
|
107 | 107 | // We handle gzipping ourselves. |
108 | 108 | $file = 'default.php'; |
109 | - $phpcode = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $file ); |
|
110 | - $phpcode = str_replace( array( '%%CONTENT%%', 'exit;' ), array( $mime, '' ), $phpcode ); |
|
109 | + $phpcode = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'config/'.$file); |
|
110 | + $phpcode = str_replace(array('%%CONTENT%%', 'exit;'), array($mime, ''), $phpcode); |
|
111 | 111 | |
112 | - file_put_contents( $this->cachedir . $this->filename, $phpcode ); |
|
113 | - file_put_contents( $this->cachedir . $this->filename . '.none', $data ); |
|
112 | + file_put_contents($this->cachedir.$this->filename, $phpcode); |
|
113 | + file_put_contents($this->cachedir.$this->filename.'.none', $data); |
|
114 | 114 | } else { |
115 | 115 | // Write code to cache without doing anything else. |
116 | - file_put_contents( $this->cachedir . $this->filename, $data ); |
|
116 | + file_put_contents($this->cachedir.$this->filename, $data); |
|
117 | 117 | |
118 | 118 | // save fallback .js or .css file if filter true (to be false by default) but not if snippet or single. |
119 | - if ( self::do_fallback() && strpos( $this->filename, '_snippet_' ) === false && strpos( $this->filename, '_single_' ) === false ) { |
|
120 | - $_extension = pathinfo( $this->filename, PATHINFO_EXTENSION ); |
|
121 | - $_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX . 'fallback.' . $_extension; |
|
122 | - if ( ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) { |
|
123 | - file_put_contents( $this->cachedir . $_extension . '/' . $_fallback_file, $data ); |
|
119 | + if (self::do_fallback() && strpos($this->filename, '_snippet_') === false && strpos($this->filename, '_single_') === false) { |
|
120 | + $_extension = pathinfo($this->filename, PATHINFO_EXTENSION); |
|
121 | + $_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX.'fallback.'.$_extension; |
|
122 | + if (!file_exists($this->cachedir.$_extension.'/'.$_fallback_file)) { |
|
123 | + file_put_contents($this->cachedir.$_extension.'/'.$_fallback_file, $data); |
|
124 | 124 | } |
125 | 125 | } |
126 | 126 | |
127 | - if ( apply_filters( 'autoptimize_filter_cache_create_static_gzip', false ) ) { |
|
127 | + if (apply_filters('autoptimize_filter_cache_create_static_gzip', false)) { |
|
128 | 128 | // Create an additional cached gzip file. |
129 | - file_put_contents( $this->cachedir . $this->filename . '.gz', gzencode( $data, 9, FORCE_GZIP ) ); |
|
129 | + file_put_contents($this->cachedir.$this->filename.'.gz', gzencode($data, 9, FORCE_GZIP)); |
|
130 | 130 | // If PHP Brotli extension is installed, create an additional cached Brotli file. |
131 | - if ( function_exists( 'brotli_compress' ) ) { |
|
132 | - file_put_contents( $this->cachedir . $this->filename . '.br', brotli_compress( $data, 11, BROTLI_GENERIC ) ); |
|
131 | + if (function_exists('brotli_compress')) { |
|
132 | + file_put_contents($this->cachedir.$this->filename.'.br', brotli_compress($data, 11, BROTLI_GENERIC)); |
|
133 | 133 | } |
134 | 134 | } |
135 | 135 | } |
136 | 136 | |
137 | 137 | // Provide 3rd party action hook for every cache file that is created. |
138 | 138 | // This hook can for example be used to inject a copy of the created cache file to a other domain. |
139 | - do_action( 'autoptimize_action_cache_file_created', $this->cachedir . $this->filename ); |
|
139 | + do_action('autoptimize_action_cache_file_created', $this->cachedir.$this->filename); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | // The original idea here was to provide 3rd party code a hook so that |
152 | 152 | // it can "listen" to all the complete autoptimized-urls that the page |
153 | 153 | // will emit... Or something to that effect I think? |
154 | - apply_filters( 'autoptimize_filter_cache_getname', AUTOPTIMIZE_CACHE_URL . $this->filename ); |
|
154 | + apply_filters('autoptimize_filter_cache_getname', AUTOPTIMIZE_CACHE_URL.$this->filename); |
|
155 | 155 | |
156 | 156 | return $this->filename; |
157 | 157 | } |
@@ -164,11 +164,11 @@ discard block |
||
164 | 164 | * @param string $file Filename. |
165 | 165 | * @return bool |
166 | 166 | */ |
167 | - protected static function is_valid_cache_file( $dir, $file ) |
|
167 | + protected static function is_valid_cache_file($dir, $file) |
|
168 | 168 | { |
169 | - if ( '.' !== $file && '..' !== $file && |
|
170 | - false !== strpos( $file, AUTOPTIMIZE_CACHEFILE_PREFIX ) && |
|
171 | - is_file( $dir . $file ) ) { |
|
169 | + if ('.' !== $file && '..' !== $file && |
|
170 | + false !== strpos($file, AUTOPTIMIZE_CACHEFILE_PREFIX) && |
|
171 | + is_file($dir.$file)) { |
|
172 | 172 | |
173 | 173 | // It's a valid file! |
174 | 174 | return true; |
@@ -186,16 +186,16 @@ discard block |
||
186 | 186 | protected static function clear_cache_classic() |
187 | 187 | { |
188 | 188 | $contents = self::get_cache_contents(); |
189 | - foreach ( $contents as $name => $files ) { |
|
190 | - $dir = rtrim( AUTOPTIMIZE_CACHE_DIR . $name, '/' ) . '/'; |
|
191 | - foreach ( $files as $file ) { |
|
192 | - if ( self::is_valid_cache_file( $dir, $file ) ) { |
|
193 | - @unlink( $dir . $file ); // @codingStandardsIgnoreLine |
|
189 | + foreach ($contents as $name => $files) { |
|
190 | + $dir = rtrim(AUTOPTIMIZE_CACHE_DIR.$name, '/').'/'; |
|
191 | + foreach ($files as $file) { |
|
192 | + if (self::is_valid_cache_file($dir, $file)) { |
|
193 | + @unlink($dir.$file); // @codingStandardsIgnoreLine |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | } |
197 | 197 | |
198 | - @unlink( AUTOPTIMIZE_CACHE_DIR . '/.htaccess' ); // @codingStandardsIgnoreLine |
|
198 | + @unlink(AUTOPTIMIZE_CACHE_DIR.'/.htaccess'); // @codingStandardsIgnoreLine |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
@@ -206,19 +206,19 @@ discard block |
||
206 | 206 | * |
207 | 207 | * @return bool |
208 | 208 | */ |
209 | - protected static function rmdir( $pathname ) |
|
209 | + protected static function rmdir($pathname) |
|
210 | 210 | { |
211 | - $files = self::get_dir_contents( $pathname ); |
|
212 | - foreach ( $files as $file ) { |
|
213 | - $path = $pathname . '/' . $file; |
|
214 | - if ( is_dir( $path ) ) { |
|
215 | - self::rmdir( $path ); |
|
211 | + $files = self::get_dir_contents($pathname); |
|
212 | + foreach ($files as $file) { |
|
213 | + $path = $pathname.'/'.$file; |
|
214 | + if (is_dir($path)) { |
|
215 | + self::rmdir($path); |
|
216 | 216 | } else { |
217 | - unlink( $path ); |
|
217 | + unlink($path); |
|
218 | 218 | } |
219 | 219 | } |
220 | 220 | |
221 | - return rmdir( $pathname ); |
|
221 | + return rmdir($pathname); |
|
222 | 222 | } |
223 | 223 | |
224 | 224 | /** |
@@ -239,12 +239,12 @@ discard block |
||
239 | 239 | $new_name = self::get_unique_name(); |
240 | 240 | |
241 | 241 | // Makes sure the new pathname is on the same level... |
242 | - $new_pathname = dirname( $dir ) . '/' . $new_name; |
|
243 | - $renamed = @rename( $dir, $new_pathname ); // @codingStandardsIgnoreLine |
|
242 | + $new_pathname = dirname($dir).'/'.$new_name; |
|
243 | + $renamed = @rename($dir, $new_pathname); // @codingStandardsIgnoreLine |
|
244 | 244 | |
245 | 245 | // When renamed, re-create the default cache directory back so it's |
246 | 246 | // available again... |
247 | - if ( $renamed ) { |
|
247 | + if ($renamed) { |
|
248 | 248 | $ok = self::cacheavail(); |
249 | 249 | } |
250 | 250 | |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | */ |
259 | 259 | public static function advanced_cache_clear_enabled() |
260 | 260 | { |
261 | - return apply_filters( 'autoptimize_filter_cache_clear_advanced', false ); |
|
261 | + return apply_filters('autoptimize_filter_cache_clear_advanced', false); |
|
262 | 262 | } |
263 | 263 | |
264 | 264 | /** |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | protected static function get_unique_name() |
270 | 270 | { |
271 | 271 | $prefix = self::get_advanced_cache_clear_prefix(); |
272 | - $new_name = uniqid( $prefix, true ); |
|
272 | + $new_name = uniqid($prefix, true); |
|
273 | 273 | |
274 | 274 | return $new_name; |
275 | 275 | } |
@@ -282,8 +282,8 @@ discard block |
||
282 | 282 | protected static function get_advanced_cache_clear_prefix() |
283 | 283 | { |
284 | 284 | $pathname = self::get_pathname_base(); |
285 | - $basename = basename( $pathname ); |
|
286 | - $prefix = $basename . '-artifact-'; |
|
285 | + $basename = basename($pathname); |
|
286 | + $prefix = $basename.'-artifact-'; |
|
287 | 287 | |
288 | 288 | return $prefix; |
289 | 289 | } |
@@ -296,9 +296,9 @@ discard block |
||
296 | 296 | * |
297 | 297 | * @return array |
298 | 298 | */ |
299 | - protected static function get_dir_contents( $pathname ) |
|
299 | + protected static function get_dir_contents($pathname) |
|
300 | 300 | { |
301 | - return array_slice( scandir( $pathname ), 2 ); |
|
301 | + return array_slice(scandir($pathname), 2); |
|
302 | 302 | } |
303 | 303 | |
304 | 304 | /** |
@@ -311,24 +311,24 @@ discard block |
||
311 | 311 | public static function delete_advanced_cache_clear_artifacts() |
312 | 312 | { |
313 | 313 | // Don't go through these motions (called from the cachechecker) if advanced cache clear isn't even active. |
314 | - if ( ! self::advanced_cache_clear_enabled() ) { |
|
314 | + if (!self::advanced_cache_clear_enabled()) { |
|
315 | 315 | return false; |
316 | 316 | } |
317 | 317 | |
318 | 318 | $dir = self::get_pathname_base(); |
319 | 319 | $prefix = self::get_advanced_cache_clear_prefix(); |
320 | - $parent = dirname( $dir ); |
|
320 | + $parent = dirname($dir); |
|
321 | 321 | $ok = false; |
322 | 322 | |
323 | 323 | // Returns the list of files without '.' and '..' elements. |
324 | - $files = self::get_dir_contents( $parent ); |
|
325 | - if ( is_array( $files ) && ! empty( $files ) ) { |
|
326 | - foreach ( $files as $file ) { |
|
327 | - $path = $parent . '/' . $file; |
|
328 | - $prefixed = ( false !== strpos( $path, $prefix ) ); |
|
324 | + $files = self::get_dir_contents($parent); |
|
325 | + if (is_array($files) && !empty($files)) { |
|
326 | + foreach ($files as $file) { |
|
327 | + $path = $parent.'/'.$file; |
|
328 | + $prefixed = (false !== strpos($path, $prefix)); |
|
329 | 329 | // Removing only our own (prefixed) directories... |
330 | - if ( is_dir( $path ) && $prefixed ) { |
|
331 | - $ok = self::rmdir( $path ); |
|
330 | + if (is_dir($path) && $prefixed) { |
|
331 | + $ok = self::rmdir($path); |
|
332 | 332 | } |
333 | 333 | } |
334 | 334 | } |
@@ -348,9 +348,9 @@ discard block |
||
348 | 348 | { |
349 | 349 | $pathname = self::get_pathname_base(); |
350 | 350 | |
351 | - if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches', true ) ) { |
|
351 | + if (is_multisite() && apply_filters('autoptimize_separate_blog_caches', true)) { |
|
352 | 352 | $blog_id = get_current_blog_id(); |
353 | - $pathname .= $blog_id . '/'; |
|
353 | + $pathname .= $blog_id.'/'; |
|
354 | 354 | } |
355 | 355 | |
356 | 356 | return $pathname; |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | */ |
364 | 364 | protected static function get_pathname_base() |
365 | 365 | { |
366 | - $pathname = WP_CONTENT_DIR . AUTOPTIMIZE_CACHE_CHILD_DIR; |
|
366 | + $pathname = WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR; |
|
367 | 367 | |
368 | 368 | return $pathname; |
369 | 369 | } |
@@ -375,46 +375,46 @@ discard block |
||
375 | 375 | * |
376 | 376 | * @return bool |
377 | 377 | */ |
378 | - public static function clearall( $propagate = true ) |
|
378 | + public static function clearall($propagate = true) |
|
379 | 379 | { |
380 | - if ( ! self::cacheavail() ) { |
|
380 | + if (!self::cacheavail()) { |
|
381 | 381 | return false; |
382 | 382 | } |
383 | 383 | |
384 | 384 | // TODO/FIXME: If cache is big, switch to advanced/new cache clearing automatically? |
385 | - if ( self::advanced_cache_clear_enabled() ) { |
|
385 | + if (self::advanced_cache_clear_enabled()) { |
|
386 | 386 | self::clear_cache_via_rename(); |
387 | 387 | } else { |
388 | 388 | self::clear_cache_classic(); |
389 | 389 | } |
390 | 390 | |
391 | 391 | // Remove 404 handler if required. |
392 | - if ( self::do_fallback() ) { |
|
393 | - $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . 'autoptimize_404_handler.php'; |
|
394 | - @unlink( $_fallback_php ); // @codingStandardsIgnoreLine |
|
392 | + if (self::do_fallback()) { |
|
393 | + $_fallback_php = trailingslashit(WP_CONTENT_DIR).'autoptimize_404_handler.php'; |
|
394 | + @unlink($_fallback_php); // @codingStandardsIgnoreLine |
|
395 | 395 | } |
396 | 396 | |
397 | 397 | // Remove the transient so it gets regenerated... |
398 | - delete_transient( 'autoptimize_stats' ); |
|
398 | + delete_transient('autoptimize_stats'); |
|
399 | 399 | |
400 | 400 | // Cache was just purged, clear page cache and allow others to hook into our purging... |
401 | - if ( true === $propagate ) { |
|
402 | - if ( ! function_exists( 'autoptimize_do_cachepurged_action' ) ) { |
|
401 | + if (true === $propagate) { |
|
402 | + if (!function_exists('autoptimize_do_cachepurged_action')) { |
|
403 | 403 | function autoptimize_do_cachepurged_action() { |
404 | - do_action( 'autoptimize_action_cachepurged' ); |
|
404 | + do_action('autoptimize_action_cachepurged'); |
|
405 | 405 | } |
406 | 406 | } |
407 | - add_action( 'shutdown', 'autoptimize_do_cachepurged_action', 11 ); |
|
408 | - add_action( 'autoptimize_action_cachepurged', array( 'autoptimizeCache', 'flushPageCache' ), 10, 0 ); |
|
407 | + add_action('shutdown', 'autoptimize_do_cachepurged_action', 11); |
|
408 | + add_action('autoptimize_action_cachepurged', array('autoptimizeCache', 'flushPageCache'), 10, 0); |
|
409 | 409 | } |
410 | 410 | |
411 | 411 | // Warm cache (part of speedupper)! |
412 | - if ( apply_filters( 'autoptimize_filter_speedupper', true ) && false == get_transient( 'autoptimize_cache_warmer_protector' ) ) { |
|
413 | - set_transient( 'autoptimize_cache_warmer_protector', 'I shall not warm cache for another 10 minutes.', 60 * 10 ); |
|
414 | - $url = site_url() . '/?ao_speedup_cachebuster=' . rand( 1, 100000 ); |
|
415 | - $url = apply_filters( 'autoptimize_filter_cache_warmer_url', $url ); |
|
416 | - $cache = @wp_remote_get( $url ); // @codingStandardsIgnoreLine |
|
417 | - unset( $cache ); |
|
412 | + if (apply_filters('autoptimize_filter_speedupper', true) && false == get_transient('autoptimize_cache_warmer_protector')) { |
|
413 | + set_transient('autoptimize_cache_warmer_protector', 'I shall not warm cache for another 10 minutes.', 60*10); |
|
414 | + $url = site_url().'/?ao_speedup_cachebuster='.rand(1, 100000); |
|
415 | + $url = apply_filters('autoptimize_filter_cache_warmer_url', $url); |
|
416 | + $cache = @wp_remote_get($url); // @codingStandardsIgnoreLine |
|
417 | + unset($cache); |
|
418 | 418 | } |
419 | 419 | |
420 | 420 | return true; |
@@ -429,7 +429,7 @@ discard block |
||
429 | 429 | */ |
430 | 430 | public static function clearall_actionless() |
431 | 431 | { |
432 | - return self::clearall( false ); |
|
432 | + return self::clearall(false); |
|
433 | 433 | } |
434 | 434 | |
435 | 435 | /** |
@@ -441,8 +441,8 @@ discard block |
||
441 | 441 | { |
442 | 442 | $contents = array(); |
443 | 443 | |
444 | - foreach ( array( '', 'js', 'css' ) as $dir ) { |
|
445 | - $contents[ $dir ] = scandir( AUTOPTIMIZE_CACHE_DIR . $dir ); |
|
444 | + foreach (array('', 'js', 'css') as $dir) { |
|
445 | + $contents[$dir] = scandir(AUTOPTIMIZE_CACHE_DIR.$dir); |
|
446 | 446 | } |
447 | 447 | |
448 | 448 | return $contents; |
@@ -455,21 +455,21 @@ discard block |
||
455 | 455 | */ |
456 | 456 | public static function stats() |
457 | 457 | { |
458 | - $stats = get_transient( 'autoptimize_stats' ); |
|
458 | + $stats = get_transient('autoptimize_stats'); |
|
459 | 459 | |
460 | 460 | // If no transient, do the actual scan! |
461 | - if ( ! is_array( $stats ) ) { |
|
462 | - if ( ! self::cacheavail() ) { |
|
461 | + if (!is_array($stats)) { |
|
462 | + if (!self::cacheavail()) { |
|
463 | 463 | return 0; |
464 | 464 | } |
465 | 465 | $stats = self::stats_scan(); |
466 | 466 | $count = $stats[0]; |
467 | - if ( $count > 100 ) { |
|
467 | + if ($count > 100) { |
|
468 | 468 | // Store results in transient. |
469 | 469 | set_transient( |
470 | 470 | 'autoptimize_stats', |
471 | 471 | $stats, |
472 | - apply_filters( 'autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS ) |
|
472 | + apply_filters('autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS) |
|
473 | 473 | ); |
474 | 474 | } |
475 | 475 | } |
@@ -492,30 +492,30 @@ discard block |
||
492 | 492 | $size = 0; |
493 | 493 | |
494 | 494 | // Scan everything in our cache directories. |
495 | - foreach ( self::get_cache_contents() as $name => $files ) { |
|
496 | - $dir = rtrim( AUTOPTIMIZE_CACHE_DIR . $name, '/' ) . '/'; |
|
497 | - foreach ( $files as $file ) { |
|
498 | - if ( self::is_valid_cache_file( $dir, $file ) ) { |
|
499 | - if ( AUTOPTIMIZE_CACHE_NOGZIP && |
|
495 | + foreach (self::get_cache_contents() as $name => $files) { |
|
496 | + $dir = rtrim(AUTOPTIMIZE_CACHE_DIR.$name, '/').'/'; |
|
497 | + foreach ($files as $file) { |
|
498 | + if (self::is_valid_cache_file($dir, $file)) { |
|
499 | + if (AUTOPTIMIZE_CACHE_NOGZIP && |
|
500 | 500 | ( |
501 | - false !== strpos( $file, '.js' ) || |
|
502 | - false !== strpos( $file, '.css' ) || |
|
503 | - false !== strpos( $file, '.img' ) || |
|
504 | - false !== strpos( $file, '.txt' ) |
|
501 | + false !== strpos($file, '.js') || |
|
502 | + false !== strpos($file, '.css') || |
|
503 | + false !== strpos($file, '.img') || |
|
504 | + false !== strpos($file, '.txt') |
|
505 | 505 | ) |
506 | 506 | ) { |
507 | 507 | // Web server is gzipping, we count .js|.css|.img|.txt files. |
508 | 508 | $count++; |
509 | - } elseif ( ! AUTOPTIMIZE_CACHE_NOGZIP && false !== strpos( $file, '.none' ) ) { |
|
509 | + } elseif (!AUTOPTIMIZE_CACHE_NOGZIP && false !== strpos($file, '.none')) { |
|
510 | 510 | // We are gzipping ourselves via php, counting only .none files. |
511 | 511 | $count++; |
512 | 512 | } |
513 | - $size += filesize( $dir . $file ); |
|
513 | + $size += filesize($dir.$file); |
|
514 | 514 | } |
515 | 515 | } |
516 | 516 | } |
517 | 517 | |
518 | - $stats = array( $count, $size, time() ); |
|
518 | + $stats = array($count, $size, time()); |
|
519 | 519 | |
520 | 520 | return $stats; |
521 | 521 | } |
@@ -529,22 +529,22 @@ discard block |
||
529 | 529 | */ |
530 | 530 | public static function cacheavail() |
531 | 531 | { |
532 | - if ( false === autoptimizeCache::check_and_create_dirs() ) { |
|
532 | + if (false === autoptimizeCache::check_and_create_dirs()) { |
|
533 | 533 | return false; |
534 | 534 | } |
535 | 535 | |
536 | 536 | // Using .htaccess inside our cache folder to overrule wp-super-cache. |
537 | - $htaccess = AUTOPTIMIZE_CACHE_DIR . '/.htaccess'; |
|
538 | - if ( ! is_file( $htaccess ) ) { |
|
537 | + $htaccess = AUTOPTIMIZE_CACHE_DIR.'/.htaccess'; |
|
538 | + if (!is_file($htaccess)) { |
|
539 | 539 | /** |
540 | 540 | * Create `wp-content/AO_htaccess_tmpl` file with |
541 | 541 | * whatever htaccess rules you might need |
542 | 542 | * if you want to override default AO htaccess |
543 | 543 | */ |
544 | - $htaccess_tmpl = WP_CONTENT_DIR . '/AO_htaccess_tmpl'; |
|
545 | - if ( is_file( $htaccess_tmpl ) ) { |
|
546 | - $content = file_get_contents( $htaccess_tmpl ); |
|
547 | - } elseif ( is_multisite() || ! AUTOPTIMIZE_CACHE_NOGZIP ) { |
|
544 | + $htaccess_tmpl = WP_CONTENT_DIR.'/AO_htaccess_tmpl'; |
|
545 | + if (is_file($htaccess_tmpl)) { |
|
546 | + $content = file_get_contents($htaccess_tmpl); |
|
547 | + } elseif (is_multisite() || !AUTOPTIMIZE_CACHE_NOGZIP) { |
|
548 | 548 | $content = '<IfModule mod_expires.c> |
549 | 549 | ExpiresActive On |
550 | 550 | ExpiresByType text/css A30672000 |
@@ -598,13 +598,13 @@ discard block |
||
598 | 598 | </IfModule>'; |
599 | 599 | } |
600 | 600 | |
601 | - if ( self::do_fallback() === true ) { |
|
602 | - $content .= "\nErrorDocument 404 " . trailingslashit( parse_url( content_url(), PHP_URL_PATH ) ) . 'autoptimize_404_handler.php'; |
|
601 | + if (self::do_fallback() === true) { |
|
602 | + $content .= "\nErrorDocument 404 ".trailingslashit(parse_url(content_url(), PHP_URL_PATH)).'autoptimize_404_handler.php'; |
|
603 | 603 | } |
604 | - @file_put_contents( $htaccess, $content ); // @codingStandardsIgnoreLine |
|
604 | + @file_put_contents($htaccess, $content); // @codingStandardsIgnoreLine |
|
605 | 605 | } |
606 | 606 | |
607 | - if ( self::do_fallback() ) { |
|
607 | + if (self::do_fallback()) { |
|
608 | 608 | self::check_fallback_php(); |
609 | 609 | } |
610 | 610 | |
@@ -619,17 +619,17 @@ discard block |
||
619 | 619 | */ |
620 | 620 | public static function check_fallback_php() { |
621 | 621 | $_fallback_filename = 'autoptimize_404_handler.php'; |
622 | - $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . $_fallback_filename; |
|
622 | + $_fallback_php = trailingslashit(WP_CONTENT_DIR).$_fallback_filename; |
|
623 | 623 | $_fallback_status = true; |
624 | 624 | |
625 | - if ( ! file_exists( $_fallback_php ) ) { |
|
626 | - $_fallback_php_contents = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $_fallback_filename ); |
|
627 | - $_fallback_php_contents = str_replace( '<?php exit;', '<?php', $_fallback_php_contents ); |
|
628 | - $_fallback_php_contents = str_replace( '<!--ao-cache-dir-->', AUTOPTIMIZE_CACHE_DIR, $_fallback_php_contents ); |
|
629 | - if ( apply_filters( 'autoptimize_filter_cache_fallback_log_errors', false ) ) { |
|
630 | - $_fallback_php_contents = str_replace( '// error_log', 'error_log', $_fallback_php_contents ); |
|
625 | + if (!file_exists($_fallback_php)) { |
|
626 | + $_fallback_php_contents = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'config/'.$_fallback_filename); |
|
627 | + $_fallback_php_contents = str_replace('<?php exit;', '<?php', $_fallback_php_contents); |
|
628 | + $_fallback_php_contents = str_replace('<!--ao-cache-dir-->', AUTOPTIMIZE_CACHE_DIR, $_fallback_php_contents); |
|
629 | + if (apply_filters('autoptimize_filter_cache_fallback_log_errors', false)) { |
|
630 | + $_fallback_php_contents = str_replace('// error_log', 'error_log', $_fallback_php_contents); |
|
631 | 631 | } |
632 | - $_fallback_status = file_put_contents( $_fallback_php, $_fallback_php_contents ); |
|
632 | + $_fallback_status = file_put_contents($_fallback_php, $_fallback_php_contents); |
|
633 | 633 | } |
634 | 634 | |
635 | 635 | return $_fallback_status; |
@@ -646,8 +646,8 @@ discard block |
||
646 | 646 | public static function do_fallback() { |
647 | 647 | static $_do_fallback = null; |
648 | 648 | |
649 | - if ( null === $_do_fallback ) { |
|
650 | - $_do_fallback = (bool) apply_filters( 'autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option( 'autoptimize_cache_fallback', '' ) ); |
|
649 | + if (null === $_do_fallback) { |
|
650 | + $_do_fallback = (bool) apply_filters('autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option('autoptimize_cache_fallback', '')); |
|
651 | 651 | } |
652 | 652 | |
653 | 653 | return $_do_fallback; |
@@ -659,26 +659,26 @@ discard block |
||
659 | 659 | * and 410'ing ("Gone") if fallback not available. |
660 | 660 | */ |
661 | 661 | public static function wordpress_notfound_fallback() { |
662 | - $original_request = strtok( $_SERVER['REQUEST_URI'], '?' ); |
|
663 | - if ( strpos( $original_request, wp_basename( WP_CONTENT_DIR ) . AUTOPTIMIZE_CACHE_CHILD_DIR ) !== false && is_404() ) { |
|
662 | + $original_request = strtok($_SERVER['REQUEST_URI'], '?'); |
|
663 | + if (strpos($original_request, wp_basename(WP_CONTENT_DIR).AUTOPTIMIZE_CACHE_CHILD_DIR) !== false && is_404()) { |
|
664 | 664 | // make sure this is not considered a 404. |
665 | 665 | global $wp_query; |
666 | 666 | $wp_query->is_404 = false; |
667 | 667 | |
668 | 668 | // set fallback path. |
669 | - $js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION ); |
|
670 | - $fallback_path = AUTOPTIMIZE_CACHE_DIR . $js_or_css . '/autoptimize_fallback.' . $js_or_css; |
|
669 | + $js_or_css = pathinfo($original_request, PATHINFO_EXTENSION); |
|
670 | + $fallback_path = AUTOPTIMIZE_CACHE_DIR.$js_or_css.'/autoptimize_fallback.'.$js_or_css; |
|
671 | 671 | |
672 | 672 | // set fallback URL. |
673 | - $fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request ); |
|
673 | + $fallback_target = preg_replace('/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request); |
|
674 | 674 | |
675 | 675 | // redirect to fallback if possible. |
676 | - if ( $original_request !== $fallback_target && file_exists( $fallback_path ) ) { |
|
676 | + if ($original_request !== $fallback_target && file_exists($fallback_path)) { |
|
677 | 677 | // redirect to fallback. |
678 | - wp_redirect( $fallback_target, 302 ); |
|
678 | + wp_redirect($fallback_target, 302); |
|
679 | 679 | } else { |
680 | 680 | // return HTTP 410 (gone) reponse. |
681 | - status_header( 410 ); |
|
681 | + status_header(410); |
|
682 | 682 | } |
683 | 683 | } |
684 | 684 | } |
@@ -690,13 +690,13 @@ discard block |
||
690 | 690 | * @return bool |
691 | 691 | */ |
692 | 692 | public static function check_and_create_dirs() { |
693 | - if ( ! defined( 'AUTOPTIMIZE_CACHE_DIR' ) ) { |
|
693 | + if (!defined('AUTOPTIMIZE_CACHE_DIR')) { |
|
694 | 694 | // We didn't set a cache. |
695 | 695 | return false; |
696 | 696 | } |
697 | 697 | |
698 | - foreach ( array( '', 'js', 'css' ) as $dir ) { |
|
699 | - if ( ! self::check_cache_dir( AUTOPTIMIZE_CACHE_DIR . $dir ) ) { |
|
698 | + foreach (array('', 'js', 'css') as $dir) { |
|
699 | + if (!self::check_cache_dir(AUTOPTIMIZE_CACHE_DIR.$dir)) { |
|
700 | 700 | return false; |
701 | 701 | } |
702 | 702 | } |
@@ -711,25 +711,25 @@ discard block |
||
711 | 711 | * |
712 | 712 | * @return bool |
713 | 713 | */ |
714 | - protected static function check_cache_dir( $dir ) |
|
714 | + protected static function check_cache_dir($dir) |
|
715 | 715 | { |
716 | 716 | // Try creating the dir if it doesn't exist. |
717 | - if ( ! file_exists( $dir ) ) { |
|
718 | - @mkdir( $dir, 0775, true ); // @codingStandardsIgnoreLine |
|
719 | - if ( ! file_exists( $dir ) ) { |
|
717 | + if (!file_exists($dir)) { |
|
718 | + @mkdir($dir, 0775, true); // @codingStandardsIgnoreLine |
|
719 | + if (!file_exists($dir)) { |
|
720 | 720 | return false; |
721 | 721 | } |
722 | 722 | } |
723 | 723 | |
724 | 724 | // If we still cannot write, bail. |
725 | - if ( ! is_writable( $dir ) ) { |
|
725 | + if (!is_writable($dir)) { |
|
726 | 726 | return false; |
727 | 727 | } |
728 | 728 | |
729 | 729 | // Create an index.html in there to avoid prying eyes! |
730 | - $idx_file = rtrim( $dir, '/\\' ) . '/index.html'; |
|
731 | - if ( ! is_file( $idx_file ) ) { |
|
732 | - @file_put_contents( $idx_file, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>' ); // @codingStandardsIgnoreLine |
|
730 | + $idx_file = rtrim($dir, '/\\').'/index.html'; |
|
731 | + if (!is_file($idx_file)) { |
|
732 | + @file_put_contents($idx_file, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>'); // @codingStandardsIgnoreLine |
|
733 | 733 | } |
734 | 734 | |
735 | 735 | return true; |
@@ -743,50 +743,50 @@ discard block |
||
743 | 743 | // @codingStandardsIgnoreStart |
744 | 744 | public static function flushPageCache() |
745 | 745 | { |
746 | - if ( function_exists( 'wp_cache_clear_cache' ) ) { |
|
747 | - if ( is_multisite() ) { |
|
746 | + if (function_exists('wp_cache_clear_cache')) { |
|
747 | + if (is_multisite()) { |
|
748 | 748 | $blog_id = get_current_blog_id(); |
749 | - wp_cache_clear_cache( $blog_id ); |
|
749 | + wp_cache_clear_cache($blog_id); |
|
750 | 750 | } else { |
751 | 751 | wp_cache_clear_cache(); |
752 | 752 | } |
753 | - } elseif ( has_action( 'cachify_flush_cache' ) ) { |
|
754 | - do_action( 'cachify_flush_cache' ); |
|
755 | - } elseif ( function_exists( 'w3tc_pgcache_flush' ) ) { |
|
753 | + } elseif (has_action('cachify_flush_cache')) { |
|
754 | + do_action('cachify_flush_cache'); |
|
755 | + } elseif (function_exists('w3tc_pgcache_flush')) { |
|
756 | 756 | w3tc_pgcache_flush(); |
757 | - } elseif ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) { |
|
757 | + } elseif (function_exists('wp_fast_cache_bulk_delete_all')) { |
|
758 | 758 | wp_fast_cache_bulk_delete_all(); |
759 | - } elseif ( class_exists( 'WpFastestCache' ) ) { |
|
759 | + } elseif (class_exists('WpFastestCache')) { |
|
760 | 760 | $wpfc = new WpFastestCache(); |
761 | 761 | $wpfc->deleteCache(); |
762 | - } elseif ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) { |
|
762 | + } elseif (class_exists('c_ws_plugin__qcache_purging_routines')) { |
|
763 | 763 | c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache |
764 | - } elseif ( class_exists( 'zencache' ) ) { |
|
764 | + } elseif (class_exists('zencache')) { |
|
765 | 765 | zencache::clear(); |
766 | - } elseif ( class_exists( 'comet_cache' ) ) { |
|
766 | + } elseif (class_exists('comet_cache')) { |
|
767 | 767 | comet_cache::clear(); |
768 | - } elseif ( class_exists( 'WpeCommon' ) ) { |
|
768 | + } elseif (class_exists('WpeCommon')) { |
|
769 | 769 | // WPEngine cache purge/flush methods to call by default |
770 | 770 | $wpe_methods = array( |
771 | 771 | 'purge_varnish_cache', |
772 | 772 | ); |
773 | 773 | |
774 | 774 | // More agressive clear/flush/purge behind a filter |
775 | - if ( apply_filters( 'autoptimize_flush_wpengine_aggressive', false ) ) { |
|
776 | - $wpe_methods = array_merge( $wpe_methods, array( 'purge_memcached', 'clear_maxcdn_cache' ) ); |
|
775 | + if (apply_filters('autoptimize_flush_wpengine_aggressive', false)) { |
|
776 | + $wpe_methods = array_merge($wpe_methods, array('purge_memcached', 'clear_maxcdn_cache')); |
|
777 | 777 | } |
778 | 778 | |
779 | 779 | // Filtering the entire list of WpeCommon methods to be called (for advanced usage + easier testing) |
780 | - $wpe_methods = apply_filters( 'autoptimize_flush_wpengine_methods', $wpe_methods ); |
|
780 | + $wpe_methods = apply_filters('autoptimize_flush_wpengine_methods', $wpe_methods); |
|
781 | 781 | |
782 | - foreach ( $wpe_methods as $wpe_method ) { |
|
783 | - if ( method_exists( 'WpeCommon', $wpe_method ) ) { |
|
782 | + foreach ($wpe_methods as $wpe_method) { |
|
783 | + if (method_exists('WpeCommon', $wpe_method)) { |
|
784 | 784 | WpeCommon::$wpe_method(); |
785 | 785 | } |
786 | 786 | } |
787 | - } elseif ( function_exists( 'sg_cachepress_purge_cache' ) ) { |
|
787 | + } elseif (function_exists('sg_cachepress_purge_cache')) { |
|
788 | 788 | sg_cachepress_purge_cache(); |
789 | - } elseif ( array_key_exists( 'KINSTA_CACHE_ZONE', $_SERVER ) ) { |
|
789 | + } elseif (array_key_exists('KINSTA_CACHE_ZONE', $_SERVER)) { |
|
790 | 790 | $_kinsta_clear_cache_url = 'https://localhost/kinsta-clear-cache-all'; |
791 | 791 | $_kinsta_response = wp_remote_get( |
792 | 792 | $_kinsta_clear_cache_url, |
@@ -795,18 +795,18 @@ discard block |
||
795 | 795 | 'timeout' => 5, |
796 | 796 | ) |
797 | 797 | ); |
798 | - } elseif ( defined('NGINX_HELPER_BASENAME') ) { |
|
799 | - do_action( 'rt_nginx_helper_purge_all' ); |
|
800 | - } elseif ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) { |
|
798 | + } elseif (defined('NGINX_HELPER_BASENAME')) { |
|
799 | + do_action('rt_nginx_helper_purge_all'); |
|
800 | + } elseif (file_exists(WP_CONTENT_DIR.'/wp-cache-config.php') && function_exists('prune_super_cache')) { |
|
801 | 801 | // fallback for WP-Super-Cache |
802 | 802 | global $cache_path; |
803 | - if ( is_multisite() ) { |
|
803 | + if (is_multisite()) { |
|
804 | 804 | $blog_id = get_current_blog_id(); |
805 | - prune_super_cache( get_supercache_dir( $blog_id ), true ); |
|
806 | - prune_super_cache( $cache_path . 'blogs/', true ); |
|
805 | + prune_super_cache(get_supercache_dir($blog_id), true); |
|
806 | + prune_super_cache($cache_path.'blogs/', true); |
|
807 | 807 | } else { |
808 | - prune_super_cache( $cache_path . 'supercache/', true ); |
|
809 | - prune_super_cache( $cache_path, true ); |
|
808 | + prune_super_cache($cache_path.'supercache/', true); |
|
809 | + prune_super_cache($cache_path, true); |
|
810 | 810 | } |
811 | 811 | } |
812 | 812 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * processes the queue, submitting jobs to criticalcss.com and retrieving generated CSS from criticalcss.com and saving rules. |
5 | 5 | */ |
6 | 6 | |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -13,23 +13,23 @@ discard block |
||
13 | 13 | { |
14 | 14 | // fetch all options at once and populate them individually explicitely as globals. |
15 | 15 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
16 | - foreach ( $all_options as $_option => $_value ) { |
|
16 | + foreach ($all_options as $_option => $_value) { |
|
17 | 17 | global ${$_option}; |
18 | 18 | ${$_option} = $_value; |
19 | 19 | } |
20 | 20 | |
21 | 21 | // Add queue control to a registered event. |
22 | - add_action( 'ao_ccss_queue', array( $this, 'ao_ccss_queue_control' ) ); |
|
22 | + add_action('ao_ccss_queue', array($this, 'ao_ccss_queue_control')); |
|
23 | 23 | // Add cleaning job to a registered event. |
24 | - add_action( 'ao_ccss_maintenance', array( $this, 'ao_ccss_cleaning' ) ); |
|
24 | + add_action('ao_ccss_maintenance', array($this, 'ao_ccss_cleaning')); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | public function ao_ccss_queue_control() { |
28 | 28 | // The queue execution backend. |
29 | 29 | global $ao_ccss_key; |
30 | - if ( ! isset( $ao_ccss_key ) || empty( $ao_ccss_key ) ) { |
|
30 | + if (!isset($ao_ccss_key) || empty($ao_ccss_key)) { |
|
31 | 31 | // no key set, not processing the queue! |
32 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No key set, so not processing queue.', 3 ); |
|
32 | + autoptimizeCriticalCSSCore::ao_ccss_log('No key set, so not processing queue.', 3); |
|
33 | 33 | return; |
34 | 34 | } |
35 | 35 | |
@@ -50,35 +50,35 @@ discard block |
||
50 | 50 | * When properly set, queue will always finish a job with the declared settings above regardless of the real API responses. |
51 | 51 | */ |
52 | 52 | $queue_debug = false; |
53 | - if ( file_exists( AO_CCSS_DEBUG ) ) { |
|
54 | - $qdobj_raw = file_get_contents( AO_CCSS_DEBUG ); |
|
55 | - $qdobj = json_decode( $qdobj_raw, true ); |
|
56 | - if ( $qdobj ) { |
|
57 | - if ( 1 === $qdobj['enable'] ) { |
|
53 | + if (file_exists(AO_CCSS_DEBUG)) { |
|
54 | + $qdobj_raw = file_get_contents(AO_CCSS_DEBUG); |
|
55 | + $qdobj = json_decode($qdobj_raw, true); |
|
56 | + if ($qdobj) { |
|
57 | + if (1 === $qdobj['enable']) { |
|
58 | 58 | $queue_debug = true; |
59 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue operating in debug mode with the following settings: <' . $qdobj_raw . '>', 3 ); |
|
59 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue operating in debug mode with the following settings: <'.$qdobj_raw.'>', 3); |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | 64 | // Set some default values for $qdobj to avoid function call warnings. |
65 | - if ( ! $queue_debug ) { |
|
65 | + if (!$queue_debug) { |
|
66 | 66 | $qdobj['htcode'] = false; |
67 | 67 | } |
68 | 68 | |
69 | 69 | // Check if queue is already running. |
70 | 70 | $queue_lock = false; |
71 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
71 | + if (file_exists(AO_CCSS_LOCK)) { |
|
72 | 72 | $queue_lock = true; |
73 | 73 | } |
74 | 74 | |
75 | 75 | // Proceed with the queue if it's not already running. |
76 | - if ( ! $queue_lock ) { |
|
76 | + if (!$queue_lock) { |
|
77 | 77 | |
78 | 78 | // Log queue start and create the lock file. |
79 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control started', 3 ); |
|
80 | - if ( touch( AO_CCSS_LOCK ) ) { |
|
81 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control locked', 3 ); |
|
79 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control started', 3); |
|
80 | + if (touch(AO_CCSS_LOCK)) { |
|
81 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control locked', 3); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | // Attach required variables. |
@@ -88,218 +88,218 @@ discard block |
||
88 | 88 | // Initialize job counters. |
89 | 89 | $jc = 1; |
90 | 90 | $jr = 1; |
91 | - $jt = count( $ao_ccss_queue ); |
|
91 | + $jt = count($ao_ccss_queue); |
|
92 | 92 | |
93 | 93 | // Sort queue by ascending job status (e.g. ERROR, JOB_ONGOING, JOB_QUEUED, NEW...). |
94 | - array_multisort( array_column( $ao_ccss_queue, 'jqstat' ), $ao_ccss_queue ); // @codingStandardsIgnoreLine |
|
94 | + array_multisort(array_column($ao_ccss_queue, 'jqstat'), $ao_ccss_queue); // @codingStandardsIgnoreLine |
|
95 | 95 | |
96 | 96 | // Iterates over the entire queue. |
97 | - foreach ( $ao_ccss_queue as $path => $jprops ) { |
|
97 | + foreach ($ao_ccss_queue as $path => $jprops) { |
|
98 | 98 | // Prepare flags and target rule. |
99 | 99 | $update = false; |
100 | 100 | $deljob = false; |
101 | 101 | $rule_update = false; |
102 | 102 | $oldccssfile = false; |
103 | - $trule = explode( '|', $jprops['rtarget'] ); |
|
103 | + $trule = explode('|', $jprops['rtarget']); |
|
104 | 104 | |
105 | 105 | // Log job count. |
106 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Processing job ' . $jc . ' of ' . $jt . ' with id <' . $jprops['ljid'] . '> and status <' . $jprops['jqstat'] . '>', 3 ); |
|
106 | + autoptimizeCriticalCSSCore::ao_ccss_log('Processing job '.$jc.' of '.$jt.' with id <'.$jprops['ljid'].'> and status <'.$jprops['jqstat'].'>', 3); |
|
107 | 107 | |
108 | 108 | // Process NEW jobs. |
109 | - if ( 'NEW' == $jprops['jqstat'] ) { |
|
109 | + if ('NEW' == $jprops['jqstat']) { |
|
110 | 110 | |
111 | 111 | // Log the new job. |
112 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found NEW job with local ID <' . $jprops['ljid'] . '>, starting its queue processing', 3 ); |
|
112 | + autoptimizeCriticalCSSCore::ao_ccss_log('Found NEW job with local ID <'.$jprops['ljid'].'>, starting its queue processing', 3); |
|
113 | 113 | |
114 | 114 | // Compare job and rule hashes (if any). |
115 | - $hash = $this->ao_ccss_diff_hashes( $jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget'] ); |
|
115 | + $hash = $this->ao_ccss_diff_hashes($jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget']); |
|
116 | 116 | |
117 | 117 | // If job hash is new or different of a previous one. |
118 | - if ( $hash ) { |
|
118 | + if ($hash) { |
|
119 | 119 | // Set job hash. |
120 | 120 | $jprops['hash'] = $hash; |
121 | 121 | |
122 | 122 | // If this is not the first job, wait 15 seconds before process next job due criticalcss.com API limits. |
123 | - if ( $jr > 1 ) { |
|
124 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting 15 seconds due to criticalcss.com API limits', 3 ); |
|
125 | - sleep( 15 ); |
|
123 | + if ($jr > 1) { |
|
124 | + autoptimizeCriticalCSSCore::ao_ccss_log('Waiting 15 seconds due to criticalcss.com API limits', 3); |
|
125 | + sleep(15); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | // Dispatch the job generate request and increment request count. |
129 | - $apireq = $this->ao_ccss_api_generate( $path, $queue_debug, $qdobj['htcode'] ); |
|
129 | + $apireq = $this->ao_ccss_api_generate($path, $queue_debug, $qdobj['htcode']); |
|
130 | 130 | $jr++; |
131 | 131 | |
132 | 132 | // NOTE: All the following conditions maps to the ones in admin_settings_queue.js.php. |
133 | - if ( 'JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status'] ) { |
|
133 | + if ('JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status']) { |
|
134 | 134 | // SUCCESS: request has a valid result. |
135 | 135 | // Update job properties. |
136 | 136 | $jprops['jid'] = $apireq['job']['id']; |
137 | 137 | $jprops['jqstat'] = $apireq['job']['status']; |
138 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request successful, remote id <' . $jprops['jid'] . '>, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
139 | - } elseif ( 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
138 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> generate request successful, remote id <'.$jprops['jid'].'>, status now is <'.$jprops['jqstat'].'>', 3); |
|
139 | + } elseif ('STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
140 | 140 | // ERROR: concurrent requests |
141 | 141 | // Update job properties. |
142 | 142 | $jprops['jid'] = $apireq['job']['id']; |
143 | 143 | $jprops['jqstat'] = $apireq['job']['status']; |
144 | 144 | $jprops['jrstat'] = $apireq['error']; |
145 | 145 | $jprops['jvstat'] = 'NONE'; |
146 | - $jprops['jftime'] = microtime( true ); |
|
147 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Concurrent requests when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
|
148 | - } elseif ( 'INVALID_JWT_TOKEN' == $apireq['errorCode'] ) { |
|
146 | + $jprops['jftime'] = microtime(true); |
|
147 | + autoptimizeCriticalCSSCore::ao_ccss_log('Concurrent requests when processing job id <'.$jprops['ljid'].'>, job status is now <'.$jprops['jqstat'].'>', 3); |
|
148 | + } elseif ('INVALID_JWT_TOKEN' == $apireq['errorCode']) { |
|
149 | 149 | // ERROR: key validation |
150 | 150 | // Update job properties. |
151 | 151 | $jprops['jqstat'] = $apireq['errorCode']; |
152 | 152 | $jprops['jrstat'] = $apireq['error']; |
153 | 153 | $jprops['jvstat'] = 'NONE'; |
154 | - $jprops['jftime'] = microtime( true ); |
|
155 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'API key validation error when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
|
156 | - } elseif ( empty( $apireq ) ) { |
|
154 | + $jprops['jftime'] = microtime(true); |
|
155 | + autoptimizeCriticalCSSCore::ao_ccss_log('API key validation error when processing job id <'.$jprops['ljid'].'>, job status is now <'.$jprops['jqstat'].'>', 3); |
|
156 | + } elseif (empty($apireq)) { |
|
157 | 157 | // ERROR: no response |
158 | 158 | // Update job properties. |
159 | 159 | $jprops['jqstat'] = 'NO_RESPONSE'; |
160 | 160 | $jprops['jrstat'] = 'NONE'; |
161 | 161 | $jprops['jvstat'] = 'NONE'; |
162 | - $jprops['jftime'] = microtime( true ); |
|
163 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
162 | + $jprops['jftime'] = microtime(true); |
|
163 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> request has no response, status now is <'.$jprops['jqstat'].'>', 3); |
|
164 | 164 | } else { |
165 | 165 | // UNKNOWN: unhandled generate exception |
166 | 166 | // Update job properties. |
167 | 167 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
168 | 168 | $jprops['jrstat'] = 'NONE'; |
169 | 169 | $jprops['jvstat'] = 'NONE'; |
170 | - $jprops['jftime'] = microtime( true ); |
|
171 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
|
172 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
170 | + $jprops['jftime'] = microtime(true); |
|
171 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> generate request has an UNKNOWN condition, status now is <'.$jprops['jqstat'].'>, check log messages above for more information', 2); |
|
172 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
173 | 173 | } |
174 | 174 | } else { |
175 | 175 | // SUCCESS: Job hash is equal to a previous one, so it's done |
176 | 176 | // Update job status and finish time. |
177 | 177 | $jprops['jqstat'] = 'JOB_DONE'; |
178 | - $jprops['jftime'] = microtime( true ); |
|
179 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> requires no further processing, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
178 | + $jprops['jftime'] = microtime(true); |
|
179 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> requires no further processing, status now is <'.$jprops['jqstat'].'>', 3); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | // Set queue update flag. |
183 | 183 | $update = true; |
184 | 184 | |
185 | - } elseif ( 'JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat'] ) { |
|
185 | + } elseif ('JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat']) { |
|
186 | 186 | // Process QUEUED and ONGOING jobs |
187 | 187 | // Log the pending job. |
188 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found PENDING job with local ID <' . $jprops['ljid'] . '>, continuing its queue processing', 3 ); |
|
188 | + autoptimizeCriticalCSSCore::ao_ccss_log('Found PENDING job with local ID <'.$jprops['ljid'].'>, continuing its queue processing', 3); |
|
189 | 189 | |
190 | 190 | // If this is not the first job, wait 15 seconds before process next job due criticalcss.com API limits. |
191 | - if ( $jr > 1 ) { |
|
192 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting 15 seconds due to criticalcss.com API limits', 3 ); |
|
193 | - sleep( 15 ); |
|
191 | + if ($jr > 1) { |
|
192 | + autoptimizeCriticalCSSCore::ao_ccss_log('Waiting 15 seconds due to criticalcss.com API limits', 3); |
|
193 | + sleep(15); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | // Dispatch the job result request and increment request count. |
197 | - $apireq = $this->ao_ccss_api_results( $jprops['jid'], $queue_debug, $qdobj['htcode'] ); |
|
197 | + $apireq = $this->ao_ccss_api_results($jprops['jid'], $queue_debug, $qdobj['htcode']); |
|
198 | 198 | $jr++; |
199 | 199 | |
200 | 200 | // NOTE: All the following condigitons maps to the ones in admin_settings_queue.js.php |
201 | 201 | // Replace API response values if queue debugging is enabled and some value is set. |
202 | - if ( $queue_debug ) { |
|
203 | - if ( $qdobj['status'] ) { |
|
202 | + if ($queue_debug) { |
|
203 | + if ($qdobj['status']) { |
|
204 | 204 | $apireq['status'] = $qdobj['status']; |
205 | 205 | } |
206 | - if ( $qdobj['resultStatus'] ) { |
|
206 | + if ($qdobj['resultStatus']) { |
|
207 | 207 | $apireq['resultStatus'] = $qdobj['resultStatus']; |
208 | 208 | } |
209 | - if ( $qdobj['validationStatus'] ) { |
|
209 | + if ($qdobj['validationStatus']) { |
|
210 | 210 | $apireq['validationStatus'] = $qdobj['validationStatus']; |
211 | 211 | } |
212 | 212 | } |
213 | 213 | |
214 | - if ( 'JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status'] ) { |
|
214 | + if ('JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status']) { |
|
215 | 215 | // SUCCESS: request has a valid result |
216 | 216 | // Process a PENDING job |
217 | 217 | // Update job properties. |
218 | 218 | $jprops['jqstat'] = $apireq['status']; |
219 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '> unchanged', 3 ); |
|
220 | - } elseif ( 'JOB_DONE' == $apireq['status'] ) { |
|
219 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].'> unchanged', 3); |
|
220 | + } elseif ('JOB_DONE' == $apireq['status']) { |
|
221 | 221 | // Process a DONE job |
222 | 222 | // New resultStatus from ccss.com "HTML_404", consider as "GOOD" for now. |
223 | - if ( 'HTML_404' == $apireq['resultStatus'] ) { |
|
223 | + if ('HTML_404' == $apireq['resultStatus']) { |
|
224 | 224 | $apireq['resultStatus'] = 'GOOD'; |
225 | 225 | } |
226 | 226 | |
227 | - if ( 'GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus'] ) { |
|
227 | + if ('GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus']) { |
|
228 | 228 | // SUCCESS: GOOD job with GOOD validation |
229 | 229 | // Update job properties. |
230 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, false ); |
|
230 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, false); |
|
231 | 231 | $jprops['jqstat'] = $apireq['status']; |
232 | 232 | $jprops['jrstat'] = $apireq['resultStatus']; |
233 | 233 | $jprops['jvstat'] = $apireq['validationStatus']; |
234 | - $jprops['jftime'] = microtime( true ); |
|
234 | + $jprops['jftime'] = microtime(true); |
|
235 | 235 | $rule_update = true; |
236 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '>, file saved <' . $jprops['file'] . '>', 3 ); |
|
237 | - } elseif ( 'GOOD' == $apireq['resultStatus'] && ( 'WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'] ) ) { |
|
236 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].'>, file saved <'.$jprops['file'].'>', 3); |
|
237 | + } elseif ('GOOD' == $apireq['resultStatus'] && ('WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'])) { |
|
238 | 238 | // SUCCESS: GOOD job with WARN or BAD validation |
239 | 239 | // Update job properties. |
240 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, true ); |
|
240 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, true); |
|
241 | 241 | $jprops['jqstat'] = $apireq['status']; |
242 | 242 | $jprops['jrstat'] = $apireq['resultStatus']; |
243 | 243 | $jprops['jvstat'] = $apireq['validationStatus']; |
244 | - $jprops['jftime'] = microtime( true ); |
|
244 | + $jprops['jftime'] = microtime(true); |
|
245 | 245 | $rule_update = true; |
246 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file saved <' . $jprops['file'] . '> but requires REVIEW', 3 ); |
|
247 | - } elseif ( 'GOOD' != $apireq['resultStatus'] && ( 'GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'] ) ) { |
|
246 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].', file saved <'.$jprops['file'].'> but requires REVIEW', 3); |
|
247 | + } elseif ('GOOD' != $apireq['resultStatus'] && ('GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'])) { |
|
248 | 248 | // ERROR: no GOOD, WARN or BAD results |
249 | 249 | // Update job properties. |
250 | 250 | $jprops['jqstat'] = $apireq['status']; |
251 | 251 | $jprops['jrstat'] = $apireq['resultStatus']; |
252 | 252 | $jprops['jvstat'] = $apireq['validationStatus']; |
253 | - $jprops['jftime'] = microtime( true ); |
|
254 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
253 | + $jprops['jftime'] = microtime(true); |
|
254 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
255 | 255 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
256 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
256 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
257 | 257 | } else { |
258 | 258 | // UNKNOWN: unhandled JOB_DONE exception |
259 | 259 | // Update job properties. |
260 | 260 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
261 | 261 | $jprops['jrstat'] = $apireq['resultStatus']; |
262 | 262 | $jprops['jvstat'] = $apireq['validationStatus']; |
263 | - $jprops['jftime'] = microtime( true ); |
|
264 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job is UNKNOWN, status now is <' . $jprops['jqstat'] . '>', 2 ); |
|
263 | + $jprops['jftime'] = microtime(true); |
|
264 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job is UNKNOWN, status now is <'.$jprops['jqstat'].'>', 2); |
|
265 | 265 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
266 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
266 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
267 | 267 | } |
268 | - } elseif ( 'JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
268 | + } elseif ('JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
269 | 269 | // ERROR: failed job |
270 | 270 | // Update job properties. |
271 | 271 | $jprops['jqstat'] = $apireq['job']['status']; |
272 | - if ( $apireq['error'] ) { |
|
272 | + if ($apireq['error']) { |
|
273 | 273 | $jprops['jrstat'] = $apireq['job']['error']; |
274 | 274 | } else { |
275 | 275 | } |
276 | 276 | $jprops['jvstat'] = 'NONE'; |
277 | - $jprops['jftime'] = microtime( true ); |
|
278 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
279 | - } elseif ( 'This css no longer exists. Please re-generate it.' == $apireq['error'] ) { |
|
277 | + $jprops['jftime'] = microtime(true); |
|
278 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
279 | + } elseif ('This css no longer exists. Please re-generate it.' == $apireq['error']) { |
|
280 | 280 | // ERROR: CSS doesn't exist |
281 | 281 | // Update job properties. |
282 | 282 | $jprops['jqstat'] = 'NO_CSS'; |
283 | 283 | $jprops['jrstat'] = $apireq['error']; |
284 | 284 | $jprops['jvstat'] = 'NONE'; |
285 | - $jprops['jftime'] = microtime( true ); |
|
286 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
287 | - } elseif ( empty( $apireq ) ) { |
|
285 | + $jprops['jftime'] = microtime(true); |
|
286 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
287 | + } elseif (empty($apireq)) { |
|
288 | 288 | // ERROR: no response |
289 | 289 | // Update job properties. |
290 | 290 | $jprops['jqstat'] = 'NO_RESPONSE'; |
291 | 291 | $jprops['jrstat'] = 'NONE'; |
292 | 292 | $jprops['jvstat'] = 'NONE'; |
293 | - $jprops['jftime'] = microtime( true ); |
|
294 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
293 | + $jprops['jftime'] = microtime(true); |
|
294 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> request has no response, status now is <'.$jprops['jqstat'].'>', 3); |
|
295 | 295 | } else { |
296 | 296 | // UNKNOWN: unhandled results exception |
297 | 297 | // Update job properties. |
298 | 298 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
299 | 299 | $jprops['jrstat'] = 'NONE'; |
300 | 300 | $jprops['jvstat'] = 'NONE'; |
301 | - $jprops['jftime'] = microtime( true ); |
|
302 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
|
301 | + $jprops['jftime'] = microtime(true); |
|
302 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request has an UNKNOWN condition, status now is <'.$jprops['jqstat'].'>, check log messages above for more information', 2); |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | // Set queue update flag. |
@@ -307,40 +307,40 @@ discard block |
||
307 | 307 | } |
308 | 308 | |
309 | 309 | // Mark DONE jobs for removal. |
310 | - if ( 'JOB_DONE' == $jprops['jqstat'] ) { |
|
310 | + if ('JOB_DONE' == $jprops['jqstat']) { |
|
311 | 311 | $update = true; |
312 | 312 | $deljob = true; |
313 | 313 | } |
314 | 314 | |
315 | 315 | // Persist updated queue object. |
316 | - if ( $update ) { |
|
317 | - if ( ! $deljob ) { |
|
316 | + if ($update) { |
|
317 | + if (!$deljob) { |
|
318 | 318 | // Update properties of a NEW or PENDING job... |
319 | - $ao_ccss_queue[ $path ] = $jprops; |
|
319 | + $ao_ccss_queue[$path] = $jprops; |
|
320 | 320 | } else { |
321 | 321 | // ...or remove the DONE job. |
322 | - unset( $ao_ccss_queue[ $path ] ); |
|
323 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> is DONE and was removed from the queue', 3 ); |
|
322 | + unset($ao_ccss_queue[$path]); |
|
323 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> is DONE and was removed from the queue', 3); |
|
324 | 324 | } |
325 | 325 | |
326 | 326 | // Update queue object. |
327 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
328 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
329 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue updated by job id <' . $jprops['ljid'] . '>', 3 ); |
|
327 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
328 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
329 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue updated by job id <'.$jprops['ljid'].'>', 3); |
|
330 | 330 | |
331 | 331 | // Update target rule. |
332 | - if ( $rule_update ) { |
|
333 | - $this->ao_ccss_rule_update( $jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash'] ); |
|
334 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> updated the target rule <' . $jprops['rtarget'] . '>', 3 ); |
|
332 | + if ($rule_update) { |
|
333 | + $this->ao_ccss_rule_update($jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash']); |
|
334 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> updated the target rule <'.$jprops['rtarget'].'>', 3); |
|
335 | 335 | } |
336 | 336 | } else { |
337 | 337 | // Or log no queue action. |
338 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Nothing to do on this job', 3 ); |
|
338 | + autoptimizeCriticalCSSCore::ao_ccss_log('Nothing to do on this job', 3); |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | // Break the loop if request limit is set and was reached. |
342 | - if ( $ao_ccss_rlimit && $ao_ccss_rlimit == $jr ) { |
|
343 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'The limit of ' . $ao_ccss_rlimit . ' request(s) to criticalcss.com was reached, queue control must finish now', 3 ); |
|
342 | + if ($ao_ccss_rlimit && $ao_ccss_rlimit == $jr) { |
|
343 | + autoptimizeCriticalCSSCore::ao_ccss_log('The limit of '.$ao_ccss_rlimit.' request(s) to criticalcss.com was reached, queue control must finish now', 3); |
|
344 | 344 | break; |
345 | 345 | } |
346 | 346 | |
@@ -349,46 +349,46 @@ discard block |
||
349 | 349 | } |
350 | 350 | |
351 | 351 | // Remove the lock file and log the queue end. |
352 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
353 | - unlink( AO_CCSS_LOCK ); |
|
354 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control unlocked', 3 ); |
|
352 | + if (file_exists(AO_CCSS_LOCK)) { |
|
353 | + unlink(AO_CCSS_LOCK); |
|
354 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control unlocked', 3); |
|
355 | 355 | } |
356 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control finished', 3 ); |
|
356 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control finished', 3); |
|
357 | 357 | |
358 | 358 | // Log that queue is locked. |
359 | 359 | } else { |
360 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue is already running, skipping the attempt to run it again', 3 ); |
|
360 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue is already running, skipping the attempt to run it again', 3); |
|
361 | 361 | } |
362 | 362 | } |
363 | 363 | |
364 | - public function ao_ccss_diff_hashes( $ljid, $hash, $hashes, $rule ) { |
|
364 | + public function ao_ccss_diff_hashes($ljid, $hash, $hashes, $rule) { |
|
365 | 365 | // Compare job hashes |
366 | 366 | // STEP 1: update job hashes. |
367 | - if ( 1 == count( $hashes ) ) { |
|
367 | + if (1 == count($hashes)) { |
|
368 | 368 | // Job with a single hash |
369 | 369 | // Set job hash. |
370 | 370 | $hash = $hashes[0]; |
371 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with SINGLE hash <' . $hash . '>', 3 ); |
|
371 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with SINGLE hash <'.$hash.'>', 3); |
|
372 | 372 | } else { |
373 | 373 | // Job with multiple hashes |
374 | 374 | // Loop through hashes to concatenate them. |
375 | 375 | $nhash = ''; |
376 | - foreach ( $hashes as $shash ) { |
|
376 | + foreach ($hashes as $shash) { |
|
377 | 377 | $nhash .= $shash; |
378 | 378 | } |
379 | 379 | |
380 | 380 | // Set job hash. |
381 | - $hash = md5( $nhash ); |
|
382 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with a COMPOSITE hash <' . $hash . '>', 3 ); |
|
381 | + $hash = md5($nhash); |
|
382 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with a COMPOSITE hash <'.$hash.'>', 3); |
|
383 | 383 | } |
384 | 384 | |
385 | 385 | // STEP 2: compare job to existing jobs to prevent double submission for same type+hash. |
386 | 386 | global $ao_ccss_queue; |
387 | 387 | |
388 | - foreach ( $ao_ccss_queue as $queue_item ) { |
|
389 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Comparing <' . $rule . $hash . '> with <' . $queue_item['rtarget'] . $queue_item['hash'] . '>', 3 ); |
|
390 | - if ( $queue_item['hash'] == $hash && $queue_item['rtarget'] == $rule && in_array( $queue_item['jqstat'], array( 'JOB_QUEUED', 'JOB_ONGOING', 'JOB_DONE' ) ) ) { |
|
391 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the already pending job <' . $queue_item['ljid'] . '>', 3 ); |
|
388 | + foreach ($ao_ccss_queue as $queue_item) { |
|
389 | + autoptimizeCriticalCSSCore::ao_ccss_log('Comparing <'.$rule.$hash.'> with <'.$queue_item['rtarget'].$queue_item['hash'].'>', 3); |
|
390 | + if ($queue_item['hash'] == $hash && $queue_item['rtarget'] == $rule && in_array($queue_item['jqstat'], array('JOB_QUEUED', 'JOB_ONGOING', 'JOB_DONE'))) { |
|
391 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> matches the already pending job <'.$queue_item['ljid'].'>', 3); |
|
392 | 392 | return false; |
393 | 393 | } |
394 | 394 | } |
@@ -398,32 +398,32 @@ discard block |
||
398 | 398 | global $ao_ccss_rules; |
399 | 399 | |
400 | 400 | // Prepare rule variables. |
401 | - $trule = explode( '|', $rule ); |
|
402 | - $srule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
401 | + $trule = explode('|', $rule); |
|
402 | + $srule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
403 | 403 | |
404 | 404 | // Check if a MANUAL rule exist and return false. |
405 | - if ( ! empty( $srule ) && ( 0 == $srule['hash'] && 0 != $srule['file'] ) ) { |
|
406 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the MANUAL rule <' . $trule[0] . '|' . $trule[1] . '>', 3 ); |
|
405 | + if (!empty($srule) && (0 == $srule['hash'] && 0 != $srule['file'])) { |
|
406 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> matches the MANUAL rule <'.$trule[0].'|'.$trule[1].'>', 3); |
|
407 | 407 | return false; |
408 | - } elseif ( ! empty( $srule ) ) { |
|
408 | + } elseif (!empty($srule)) { |
|
409 | 409 | // Check if an AUTO rule exist. |
410 | - if ( $hash === $srule['hash'] && is_file( AO_CCSS_DIR . $srule['file'] ) && 0 != filesize( AO_CCSS_DIR . $srule['file'] ) ) { |
|
410 | + if ($hash === $srule['hash'] && is_file(AO_CCSS_DIR.$srule['file']) && 0 != filesize(AO_CCSS_DIR.$srule['file'])) { |
|
411 | 411 | // Check if job hash matches rule, if the CCSS file exists said file is not empty and return FALSE is so. |
412 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '>', 3 ); |
|
412 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> MATCH the one in rule <'.$trule[0].'|'.$trule[1].'>', 3); |
|
413 | 413 | return false; |
414 | 414 | } else { |
415 | 415 | // Or return the new hash if they differ. |
416 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> DOES NOT MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '> or rule\'s CCSS file was invalid.', 3 ); |
|
416 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> DOES NOT MATCH the one in rule <'.$trule[0].'|'.$trule[1].'> or rule\'s CCSS file was invalid.', 3); |
|
417 | 417 | return $hash; |
418 | 418 | } |
419 | 419 | } else { |
420 | 420 | // Or just return the hash if no rule exist yet. |
421 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> has no rule yet', 3 ); |
|
421 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> has no rule yet', 3); |
|
422 | 422 | return $hash; |
423 | 423 | } |
424 | 424 | } |
425 | 425 | |
426 | - public function ao_ccss_api_generate( $path, $debug, $dcode ) { |
|
426 | + public function ao_ccss_api_generate($path, $debug, $dcode) { |
|
427 | 427 | // POST jobs to criticalcss.com and return responses |
428 | 428 | // Get key and key status. |
429 | 429 | global $ao_ccss_key; |
@@ -435,35 +435,35 @@ discard block |
||
435 | 435 | global $ao_ccss_noptimize; |
436 | 436 | |
437 | 437 | $site_host = get_site_url(); |
438 | - $site_path = parse_url( $site_host, PHP_URL_PATH ); |
|
438 | + $site_path = parse_url($site_host, PHP_URL_PATH); |
|
439 | 439 | |
440 | - if ( ! empty( $site_path ) ) { |
|
441 | - $site_host = str_replace( $site_path, '', $site_host ); |
|
440 | + if (!empty($site_path)) { |
|
441 | + $site_host = str_replace($site_path, '', $site_host); |
|
442 | 442 | } |
443 | 443 | |
444 | 444 | // Logic to bind to one domain to avoid site clones of sites would |
445 | 445 | // automatically begin spawning requests to criticalcss.com which has |
446 | 446 | // a per domain cost. |
447 | 447 | global $ao_ccss_domain; |
448 | - if ( empty( $ao_ccss_domain ) ) { |
|
448 | + if (empty($ao_ccss_domain)) { |
|
449 | 449 | // first request being done, update option to allow future requests are only allowed if from same domain. |
450 | - update_option( 'autoptimize_ccss_domain', str_rot13( $site_host ) ); |
|
451 | - } elseif ( trim( $ao_ccss_domain, '\'"' ) !== 'none' && parse_url( $site_host, PHP_URL_HOST ) !== parse_url( $ao_ccss_domain, PHP_URL_HOST ) && apply_filters( 'autoptimize_filter_ccss_bind_domain', true ) ) { |
|
450 | + update_option('autoptimize_ccss_domain', str_rot13($site_host)); |
|
451 | + } elseif (trim($ao_ccss_domain, '\'"') !== 'none' && parse_url($site_host, PHP_URL_HOST) !== parse_url($ao_ccss_domain, PHP_URL_HOST) && apply_filters('autoptimize_filter_ccss_bind_domain', true)) { |
|
452 | 452 | // not the same domain, log as error and return without posting to criticalcss.com. |
453 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Request for domain ' . $site_host . ' does not match bound domain ' . $ao_ccss_domain . ' so not proceeding.', 2 ); |
|
453 | + autoptimizeCriticalCSSCore::ao_ccss_log('Request for domain '.$site_host.' does not match bound domain '.$ao_ccss_domain.' so not proceeding.', 2); |
|
454 | 454 | return false; |
455 | 455 | } |
456 | 456 | |
457 | - $src_url = $site_host . $path; |
|
457 | + $src_url = $site_host.$path; |
|
458 | 458 | |
459 | 459 | // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO. |
460 | - if ( ! empty( $ao_ccss_noptimize ) ) { |
|
460 | + if (!empty($ao_ccss_noptimize)) { |
|
461 | 461 | $src_url .= '?ao_noptirocket=1'; |
462 | - } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) { |
|
462 | + } elseif (class_exists('autoptimizeImages', false) && autoptimizeImages::should_lazyload_wrapper()) { |
|
463 | 463 | $src_url .= '?ao_nolazy=1'; |
464 | 464 | } |
465 | 465 | |
466 | - $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url ); |
|
466 | + $src_url = apply_filters('autoptimize_filter_ccss_cron_srcurl', $src_url); |
|
467 | 467 | |
468 | 468 | // Initialize request body. |
469 | 469 | $body = array(); |
@@ -473,85 +473,85 @@ discard block |
||
473 | 473 | |
474 | 474 | // Prepare and add viewport size to the body if available. |
475 | 475 | $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport(); |
476 | - if ( ! empty( $viewport['w'] ) && ! empty( $viewport['h'] ) ) { |
|
476 | + if (!empty($viewport['w']) && !empty($viewport['h'])) { |
|
477 | 477 | $body['width'] = $viewport['w']; |
478 | 478 | $body['height'] = $viewport['h']; |
479 | 479 | } |
480 | 480 | |
481 | 481 | // Prepare and add forceInclude to the body if available. |
482 | 482 | global $ao_ccss_finclude; |
483 | - $finclude = $this->ao_ccss_finclude( $ao_ccss_finclude ); |
|
484 | - if ( ! empty( $finclude ) ) { |
|
483 | + $finclude = $this->ao_ccss_finclude($ao_ccss_finclude); |
|
484 | + if (!empty($finclude)) { |
|
485 | 485 | $body['forceInclude'] = $finclude; |
486 | 486 | } |
487 | 487 | |
488 | 488 | // Body must be json and log it. |
489 | - $body = json_encode( $body ); |
|
490 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request body is ' . $body, 3 ); |
|
489 | + $body = json_encode($body); |
|
490 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request body is '.$body, 3); |
|
491 | 491 | |
492 | 492 | // Prepare the request. |
493 | - $url = esc_url_raw( AO_CCSS_API . 'generate?aover=' . AO_CCSS_VER ); |
|
493 | + $url = esc_url_raw(AO_CCSS_API.'generate?aover='.AO_CCSS_VER); |
|
494 | 494 | $args = array( |
495 | 495 | 'headers' => array( |
496 | - 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER, |
|
496 | + 'User-Agent' => 'Autoptimize v'.AO_CCSS_VER, |
|
497 | 497 | 'Content-type' => 'application/json; charset=utf-8', |
498 | - 'Authorization' => 'JWT ' . $key, |
|
498 | + 'Authorization' => 'JWT '.$key, |
|
499 | 499 | 'Connection' => 'close', |
500 | 500 | ), |
501 | 501 | 'body' => $body, |
502 | 502 | ); |
503 | 503 | |
504 | 504 | // Dispatch the request and store its response code. |
505 | - $req = wp_safe_remote_post( $url, $args ); |
|
506 | - $code = wp_remote_retrieve_response_code( $req ); |
|
507 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
505 | + $req = wp_safe_remote_post($url, $args); |
|
506 | + $code = wp_remote_retrieve_response_code($req); |
|
507 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
508 | 508 | |
509 | - if ( $debug && $dcode ) { |
|
509 | + if ($debug && $dcode) { |
|
510 | 510 | // If queue debug is active, change response code. |
511 | 511 | $code = $dcode; |
512 | 512 | } |
513 | 513 | |
514 | - if ( 200 == $code ) { |
|
514 | + if (200 == $code) { |
|
515 | 515 | // Response code is OK. |
516 | 516 | // Workaround criticalcss.com non-RESTful reponses. |
517 | - if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) { |
|
517 | + if ('JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status']) { |
|
518 | 518 | // Log successful and return encoded request body. |
519 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied successfully', 3 ); |
|
519 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied successfully', 3); |
|
520 | 520 | |
521 | 521 | // This code also means the key is valid, so cache key status for 24h if not already cached. |
522 | - if ( ( ! $key_status || 2 != $key_status ) && $key ) { |
|
523 | - update_option( 'autoptimize_ccss_keyst', 2 ); |
|
524 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 ); |
|
522 | + if ((!$key_status || 2 != $key_status) && $key) { |
|
523 | + update_option('autoptimize_ccss_keyst', 2); |
|
524 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is valid, updating key status', 3); |
|
525 | 525 | } |
526 | 526 | |
527 | 527 | // Return the request body. |
528 | 528 | return $body; |
529 | 529 | } else { |
530 | 530 | // Log successful requests with invalid reponses. |
531 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 ); |
|
532 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
531 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied with code <'.$code.'> and an UNKNOWN error condition, body follows...', 2); |
|
532 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
533 | 533 | return $body; |
534 | 534 | } |
535 | 535 | } else { |
536 | 536 | // Response code is anything else. |
537 | 537 | // Log failed request with a valid response code and return body. |
538 | - if ( $code ) { |
|
539 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with error code <' . $code . '>, body follows...', 2 ); |
|
540 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
538 | + if ($code) { |
|
539 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied with error code <'.$code.'>, body follows...', 2); |
|
540 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
541 | 541 | |
542 | - if ( 401 == $code ) { |
|
542 | + if (401 == $code) { |
|
543 | 543 | // If request is unauthorized, also clear key status. |
544 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
545 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
544 | + update_option('autoptimize_ccss_keyst', 1); |
|
545 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
546 | 546 | } |
547 | 547 | |
548 | 548 | // Return the request body. |
549 | 549 | return $body; |
550 | 550 | } else { |
551 | 551 | // Log failed request with no response and return false. |
552 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> has no response, this could be a service timeout', 2 ); |
|
553 | - if ( is_wp_error( $req ) ) { |
|
554 | - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 ); |
|
552 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> has no response, this could be a service timeout', 2); |
|
553 | + if (is_wp_error($req)) { |
|
554 | + autoptimizeCriticalCSSCore::ao_ccss_log($req->get_error_message(), 2); |
|
555 | 555 | } |
556 | 556 | |
557 | 557 | return false; |
@@ -559,76 +559,76 @@ discard block |
||
559 | 559 | } |
560 | 560 | } |
561 | 561 | |
562 | - public function ao_ccss_api_results( $jobid, $debug, $dcode ) { |
|
562 | + public function ao_ccss_api_results($jobid, $debug, $dcode) { |
|
563 | 563 | // GET jobs from criticalcss.com and return responses |
564 | 564 | // Get key. |
565 | 565 | global $ao_ccss_key; |
566 | 566 | $key = $ao_ccss_key; |
567 | 567 | |
568 | 568 | // Prepare the request. |
569 | - $url = AO_CCSS_API . 'results?resultId=' . $jobid; |
|
569 | + $url = AO_CCSS_API.'results?resultId='.$jobid; |
|
570 | 570 | $args = array( |
571 | 571 | 'headers' => array( |
572 | - 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v' . AO_CCSS_VER, |
|
573 | - 'Authorization' => 'JWT ' . $key, |
|
572 | + 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v'.AO_CCSS_VER, |
|
573 | + 'Authorization' => 'JWT '.$key, |
|
574 | 574 | 'Connection' => 'close', |
575 | 575 | ), |
576 | 576 | ); |
577 | 577 | |
578 | 578 | // Dispatch the request and store its response code. |
579 | - $req = wp_safe_remote_get( $url, $args ); |
|
580 | - $code = wp_remote_retrieve_response_code( $req ); |
|
581 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
579 | + $req = wp_safe_remote_get($url, $args); |
|
580 | + $code = wp_remote_retrieve_response_code($req); |
|
581 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
582 | 582 | |
583 | - if ( $debug && $dcode ) { |
|
583 | + if ($debug && $dcode) { |
|
584 | 584 | // If queue debug is active, change response code. |
585 | 585 | $code = $dcode; |
586 | 586 | } |
587 | 587 | |
588 | - if ( 200 == $code ) { |
|
588 | + if (200 == $code) { |
|
589 | 589 | // Response code is OK. |
590 | - if ( is_array( $body ) && ( array_key_exists( 'status', $body ) || array_key_exists( 'job', $body ) ) && ( 'JOB_QUEUED' == $body['status'] || 'JOB_ONGOING' == $body['status'] || 'JOB_DONE' == $body['status'] || 'JOB_FAILED' == $body['status'] || 'JOB_UNKNOWN' == $body['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) ) { |
|
590 | + if (is_array($body) && (array_key_exists('status', $body) || array_key_exists('job', $body)) && ('JOB_QUEUED' == $body['status'] || 'JOB_ONGOING' == $body['status'] || 'JOB_DONE' == $body['status'] || 'JOB_FAILED' == $body['status'] || 'JOB_UNKNOWN' == $body['status'] || 'STATUS_JOB_BAD' == $body['job']['status'])) { |
|
591 | 591 | // Workaround criticalcss.com non-RESTful reponses |
592 | 592 | // Log successful and return encoded request body. |
593 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully', 3 ); |
|
593 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied successfully', 3); |
|
594 | 594 | return $body; |
595 | - } elseif ( is_array( $body ) && ( array_key_exists( 'error', $body ) && 'This css no longer exists. Please re-generate it.' == $body['error'] ) ) { |
|
595 | + } elseif (is_array($body) && (array_key_exists('error', $body) && 'This css no longer exists. Please re-generate it.' == $body['error'])) { |
|
596 | 596 | // Handle no CSS reply |
597 | 597 | // Log no CSS error and return encoded request body. |
598 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully but the CSS for it does not exist anymore', 3 ); |
|
598 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied successfully but the CSS for it does not exist anymore', 3); |
|
599 | 599 | return $body; |
600 | 600 | } else { |
601 | 601 | // Log failed request and return false. |
602 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 ); |
|
603 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
602 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied with code <'.$code.'> and an UNKNOWN error condition, body follows...', 2); |
|
603 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
604 | 604 | return false; |
605 | 605 | } |
606 | 606 | } else { |
607 | 607 | // Response code is anything else |
608 | 608 | // Log failed request with a valid response code and return body. |
609 | - if ( $code ) { |
|
610 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with error code <' . $code . '>, body follows...', 2 ); |
|
611 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
612 | - if ( 401 == $code ) { |
|
609 | + if ($code) { |
|
610 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied with error code <'.$code.'>, body follows...', 2); |
|
611 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
612 | + if (401 == $code) { |
|
613 | 613 | // If request is unauthorized, also clear key status. |
614 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
615 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
614 | + update_option('autoptimize_ccss_keyst', 1); |
|
615 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
616 | 616 | } |
617 | 617 | |
618 | 618 | // Return the request body. |
619 | 619 | return $body; |
620 | 620 | } else { |
621 | 621 | // Log failed request with no response and return false. |
622 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> has no response, this could be a service timeout', 2 ); |
|
622 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> has no response, this could be a service timeout', 2); |
|
623 | 623 | return false; |
624 | 624 | } |
625 | 625 | } |
626 | 626 | } |
627 | 627 | |
628 | - public function ao_ccss_save_file( $ccss, $target, $review ) { |
|
628 | + public function ao_ccss_save_file($ccss, $target, $review) { |
|
629 | 629 | // Save critical CSS into the filesystem and return its filename |
630 | 630 | // Prepare review mark. |
631 | - if ( $review ) { |
|
631 | + if ($review) { |
|
632 | 632 | $rmark = '_R'; |
633 | 633 | } else { |
634 | 634 | $rmark = ''; |
@@ -638,21 +638,21 @@ discard block |
||
638 | 638 | $filename = false; |
639 | 639 | $content = $ccss; |
640 | 640 | |
641 | - if ( autoptimizeCriticalCSSCore::ao_ccss_check_contents( $content ) ) { |
|
641 | + if (autoptimizeCriticalCSSCore::ao_ccss_check_contents($content)) { |
|
642 | 642 | // Sanitize content, set filename and try to save file. |
643 | - $file = AO_CCSS_DIR . 'ccss_' . md5( $ccss . $target[1] ) . $rmark . '.css'; |
|
644 | - $status = file_put_contents( $file, $content, LOCK_EX ); |
|
645 | - $filename = pathinfo( $file, PATHINFO_BASENAME ); |
|
646 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file for the rule <' . $target[0] . '|' . $target[1] . '> was saved as <' . $filename . '>, size in bytes is <' . $status . '>', 3 ); |
|
643 | + $file = AO_CCSS_DIR.'ccss_'.md5($ccss.$target[1]).$rmark.'.css'; |
|
644 | + $status = file_put_contents($file, $content, LOCK_EX); |
|
645 | + $filename = pathinfo($file, PATHINFO_BASENAME); |
|
646 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS file for the rule <'.$target[0].'|'.$target[1].'> was saved as <'.$filename.'>, size in bytes is <'.$status.'>', 3); |
|
647 | 647 | |
648 | - if ( ! $status ) { |
|
648 | + if (!$status) { |
|
649 | 649 | // If file has not been saved, reset filename. |
650 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file <' . $filename . '> could not be not saved', 2 ); |
|
650 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS file <'.$filename.'> could not be not saved', 2); |
|
651 | 651 | $filename = false; |
652 | 652 | return $filename; |
653 | 653 | } |
654 | 654 | } else { |
655 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not pass content check', 2 ); |
|
655 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received did not pass content check', 2); |
|
656 | 656 | return $filename; |
657 | 657 | } |
658 | 658 | |
@@ -662,15 +662,15 @@ discard block |
||
662 | 662 | global $ao_ccss_rules; |
663 | 663 | |
664 | 664 | // Prepare rule variables. |
665 | - $srule = $ao_ccss_rules[ $target[0] ][ $target[1] ]; |
|
665 | + $srule = $ao_ccss_rules[$target[0]][$target[1]]; |
|
666 | 666 | $oldfile = $srule['file']; |
667 | 667 | |
668 | - if ( $oldfile && $oldfile !== $filename ) { |
|
669 | - $delfile = AO_CCSS_DIR . $oldfile; |
|
670 | - if ( file_exists( $delfile ) ) { |
|
671 | - $unlinkst = unlink( $delfile ); |
|
672 | - if ( $unlinkst ) { |
|
673 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'A previous critical CSS file <' . $oldfile . '> was removed for the rule <' . $target[0] . '|' . $target[1] . '>', 3 ); |
|
668 | + if ($oldfile && $oldfile !== $filename) { |
|
669 | + $delfile = AO_CCSS_DIR.$oldfile; |
|
670 | + if (file_exists($delfile)) { |
|
671 | + $unlinkst = unlink($delfile); |
|
672 | + if ($unlinkst) { |
|
673 | + autoptimizeCriticalCSSCore::ao_ccss_log('A previous critical CSS file <'.$oldfile.'> was removed for the rule <'.$target[0].'|'.$target[1].'>', 3); |
|
674 | 674 | } |
675 | 675 | } |
676 | 676 | } |
@@ -679,29 +679,29 @@ discard block |
||
679 | 679 | return $filename; |
680 | 680 | } |
681 | 681 | |
682 | - public function ao_ccss_rule_update( $ljid, $srule, $file, $hash ) { |
|
682 | + public function ao_ccss_rule_update($ljid, $srule, $file, $hash) { |
|
683 | 683 | // Update or create a rule |
684 | 684 | // Attach required arrays. |
685 | 685 | global $ao_ccss_rules; |
686 | 686 | |
687 | 687 | // Prepare rule variables. |
688 | - $trule = explode( '|', $srule ); |
|
689 | - $rule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
688 | + $trule = explode('|', $srule); |
|
689 | + $rule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
690 | 690 | $action = false; |
691 | 691 | $rtype = ''; |
692 | 692 | |
693 | - if ( 0 === $rule['hash'] && 0 !== $rule['file'] ) { |
|
693 | + if (0 === $rule['hash'] && 0 !== $rule['file']) { |
|
694 | 694 | // manual rule, don't ever overwrite. |
695 | 695 | $action = 'NOT UPDATED'; |
696 | 696 | $rtype = 'MANUAL'; |
697 | - } elseif ( 0 === $rule['hash'] && 0 === $rule['file'] ) { |
|
697 | + } elseif (0 === $rule['hash'] && 0 === $rule['file']) { |
|
698 | 698 | // If this is an user created AUTO rule with no hash and file yet, update its hash and filename |
699 | 699 | // Set rule hash, file and action flag. |
700 | 700 | $rule['hash'] = $hash; |
701 | 701 | $rule['file'] = $file; |
702 | 702 | $action = 'UPDATED'; |
703 | 703 | $rtype = 'AUTO'; |
704 | - } elseif ( 0 !== $rule['hash'] && ctype_alnum( $rule['hash'] ) ) { |
|
704 | + } elseif (0 !== $rule['hash'] && ctype_alnum($rule['hash'])) { |
|
705 | 705 | // If this is an genuine AUTO rule, update its hash and filename |
706 | 706 | // Set rule hash, file and action flag. |
707 | 707 | $rule['hash'] = $hash; |
@@ -711,7 +711,7 @@ discard block |
||
711 | 711 | } else { |
712 | 712 | // If rule doesn't exist, create an AUTO rule |
713 | 713 | // AUTO rules were only for types, but will now also work for paths. |
714 | - if ( 'types' == $trule[0] || 'paths' == $trule[0] ) { |
|
714 | + if ('types' == $trule[0] || 'paths' == $trule[0]) { |
|
715 | 715 | // Set rule hash and file and action flag. |
716 | 716 | $rule['hash'] = $hash; |
717 | 717 | $rule['file'] = $file; |
@@ -719,47 +719,47 @@ discard block |
||
719 | 719 | $rtype = 'AUTO'; |
720 | 720 | } else { |
721 | 721 | // Log that no rule was created. |
722 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Exception, no AUTO rule created', 3 ); |
|
722 | + autoptimizeCriticalCSSCore::ao_ccss_log('Exception, no AUTO rule created', 3); |
|
723 | 723 | } |
724 | 724 | } |
725 | 725 | |
726 | - if ( $action ) { |
|
726 | + if ($action) { |
|
727 | 727 | // If a rule creation/update is required, persist updated rules object. |
728 | - $ao_ccss_rules[ $trule[0] ][ $trule[1] ] = $rule; |
|
729 | - $ao_ccss_rules_raw = json_encode( $ao_ccss_rules ); |
|
730 | - update_option( 'autoptimize_ccss_rules', $ao_ccss_rules_raw ); |
|
731 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Target rule <' . $srule . '> of type <' . $rtype . '> was ' . $action . ' for job id <' . $ljid . '>', 3 ); |
|
728 | + $ao_ccss_rules[$trule[0]][$trule[1]] = $rule; |
|
729 | + $ao_ccss_rules_raw = json_encode($ao_ccss_rules); |
|
730 | + update_option('autoptimize_ccss_rules', $ao_ccss_rules_raw); |
|
731 | + autoptimizeCriticalCSSCore::ao_ccss_log('Target rule <'.$srule.'> of type <'.$rtype.'> was '.$action.' for job id <'.$ljid.'>', 3); |
|
732 | 732 | } else { |
733 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No rule action required', 3 ); |
|
733 | + autoptimizeCriticalCSSCore::ao_ccss_log('No rule action required', 3); |
|
734 | 734 | } |
735 | 735 | } |
736 | 736 | |
737 | - function ao_ccss_finclude( $finclude_raw ) { |
|
737 | + function ao_ccss_finclude($finclude_raw) { |
|
738 | 738 | // Prepare forceInclude object. |
739 | - if ( ! empty( $finclude_raw ) ) { |
|
739 | + if (!empty($finclude_raw)) { |
|
740 | 740 | // If there are any content |
741 | 741 | // Convert raw string into arra and initialize the returning object. |
742 | - $fincludes = explode( ',', $finclude_raw ); |
|
742 | + $fincludes = explode(',', $finclude_raw); |
|
743 | 743 | $finclude = array(); |
744 | 744 | |
745 | 745 | // Interacts over every rule. |
746 | 746 | $i = 0; |
747 | - foreach ( $fincludes as $include ) { |
|
747 | + foreach ($fincludes as $include) { |
|
748 | 748 | // Trim leading and trailing whitespaces. |
749 | - $include = trim( $include ); |
|
749 | + $include = trim($include); |
|
750 | 750 | |
751 | - if ( substr( $include, 0, 2 ) === '//' ) { |
|
751 | + if (substr($include, 0, 2) === '//') { |
|
752 | 752 | // Regex rule |
753 | 753 | // Format value as required. |
754 | - $include = str_replace( '//', '/', $include ); |
|
755 | - $include = $include . '/i'; |
|
754 | + $include = str_replace('//', '/', $include); |
|
755 | + $include = $include.'/i'; |
|
756 | 756 | |
757 | 757 | // Store regex object. |
758 | - $finclude[ $i ]['type'] = 'RegExp'; |
|
759 | - $finclude[ $i ]['value'] = $include; |
|
758 | + $finclude[$i]['type'] = 'RegExp'; |
|
759 | + $finclude[$i]['value'] = $include; |
|
760 | 760 | } else { |
761 | 761 | // Simple value rule. |
762 | - $finclude[ $i ]['value'] = $include; |
|
762 | + $finclude[$i]['value'] = $include; |
|
763 | 763 | } |
764 | 764 | |
765 | 765 | $i++; |
@@ -776,54 +776,54 @@ discard block |
||
776 | 776 | public static function ao_ccss_cleaning() { |
777 | 777 | // Perform plugin maintenance |
778 | 778 | // Truncate log file >= 1MB . |
779 | - if ( file_exists( AO_CCSS_LOG ) ) { |
|
780 | - if ( filesize( AO_CCSS_LOG ) >= 1048576 ) { |
|
781 | - $logfile = fopen( AO_CCSS_LOG, 'w' ); |
|
782 | - fclose( $logfile ); |
|
779 | + if (file_exists(AO_CCSS_LOG)) { |
|
780 | + if (filesize(AO_CCSS_LOG) >= 1048576) { |
|
781 | + $logfile = fopen(AO_CCSS_LOG, 'w'); |
|
782 | + fclose($logfile); |
|
783 | 783 | } |
784 | 784 | } |
785 | 785 | |
786 | 786 | // Remove lock file. |
787 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
788 | - unlink( AO_CCSS_LOCK ); |
|
787 | + if (file_exists(AO_CCSS_LOCK)) { |
|
788 | + unlink(AO_CCSS_LOCK); |
|
789 | 789 | } |
790 | 790 | |
791 | 791 | // Make sure queue processing is scheduled, recreate if not. |
792 | - if ( ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
793 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
792 | + if (!wp_next_scheduled('ao_ccss_queue')) { |
|
793 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
794 | 794 | } |
795 | 795 | |
796 | 796 | // Queue cleaning. |
797 | 797 | global $ao_ccss_queue; |
798 | 798 | $queue_purge_threshold = 100; |
799 | - $queue_purge_age = 24 * 60 * 60; |
|
800 | - $queue_length = count( $ao_ccss_queue ); |
|
801 | - $timestamp_yesterday = microtime( true ) - $queue_purge_age; |
|
799 | + $queue_purge_age = 24*60*60; |
|
800 | + $queue_length = count($ao_ccss_queue); |
|
801 | + $timestamp_yesterday = microtime(true) - $queue_purge_age; |
|
802 | 802 | $remove_old_new = false; |
803 | 803 | $queue_altered = false; |
804 | 804 | |
805 | - if ( $queue_length > $queue_purge_threshold ) { |
|
805 | + if ($queue_length > $queue_purge_threshold) { |
|
806 | 806 | $remove_old_new = true; |
807 | 807 | } |
808 | 808 | |
809 | - foreach ( $ao_ccss_queue as $path => $job ) { |
|
810 | - if ( ( $remove_old_new && 'NEW' == $job['jqstat'] && $job['jctime'] < $timestamp_yesterday ) || in_array( $job['jqstat'], array( 'JOB_FAILED', 'STATUS_JOB_BAD', 'NO_CSS', 'NO_RESPONSE' ) ) ) { |
|
811 | - unset( $ao_ccss_queue[ $path ] ); |
|
809 | + foreach ($ao_ccss_queue as $path => $job) { |
|
810 | + if (($remove_old_new && 'NEW' == $job['jqstat'] && $job['jctime'] < $timestamp_yesterday) || in_array($job['jqstat'], array('JOB_FAILED', 'STATUS_JOB_BAD', 'NO_CSS', 'NO_RESPONSE'))) { |
|
811 | + unset($ao_ccss_queue[$path]); |
|
812 | 812 | $queue_altered = true; |
813 | 813 | } |
814 | 814 | } |
815 | 815 | |
816 | 816 | // save queue to options! |
817 | - if ( $queue_altered ) { |
|
818 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
819 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
820 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue cleaning done.', 3 ); |
|
817 | + if ($queue_altered) { |
|
818 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
819 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
820 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue cleaning done.', 3); |
|
821 | 821 | } |
822 | 822 | |
823 | 823 | // re-check key if invalid. |
824 | 824 | global $ao_ccss_keyst; |
825 | - if ( 1 == $ao_ccss_keyst ) { |
|
826 | - $this->ao_ccss_api_generate( '', '', '' ); |
|
825 | + if (1 == $ao_ccss_keyst) { |
|
826 | + $this->ao_ccss_api_generate('', '', ''); |
|
827 | 827 | } |
828 | 828 | } |
829 | 829 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * General helpers. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -16,15 +16,15 @@ discard block |
||
16 | 16 | * |
17 | 17 | * @return bool |
18 | 18 | */ |
19 | - public static function mbstring_available( $override = null ) |
|
19 | + public static function mbstring_available($override = null) |
|
20 | 20 | { |
21 | 21 | static $available = null; |
22 | 22 | |
23 | - if ( null === $available ) { |
|
24 | - $available = \extension_loaded( 'mbstring' ); |
|
23 | + if (null === $available) { |
|
24 | + $available = \extension_loaded('mbstring'); |
|
25 | 25 | } |
26 | 26 | |
27 | - if ( null !== $override ) { |
|
27 | + if (null !== $override) { |
|
28 | 28 | $available = $override; |
29 | 29 | } |
30 | 30 | |
@@ -42,12 +42,12 @@ discard block |
||
42 | 42 | * |
43 | 43 | * @return int|false |
44 | 44 | */ |
45 | - public static function strpos( $haystack, $needle, $offset = 0, $encoding = null ) |
|
45 | + public static function strpos($haystack, $needle, $offset = 0, $encoding = null) |
|
46 | 46 | { |
47 | - if ( self::mbstring_available() ) { |
|
48 | - return ( null === $encoding ) ? \mb_strpos( $haystack, $needle, $offset ) : \mb_strpos( $haystack, $needle, $offset, $encoding ); |
|
47 | + if (self::mbstring_available()) { |
|
48 | + return (null === $encoding) ? \mb_strpos($haystack, $needle, $offset) : \mb_strpos($haystack, $needle, $offset, $encoding); |
|
49 | 49 | } else { |
50 | - return \strpos( $haystack, $needle, $offset ); |
|
50 | + return \strpos($haystack, $needle, $offset); |
|
51 | 51 | } |
52 | 52 | } |
53 | 53 | |
@@ -62,12 +62,12 @@ discard block |
||
62 | 62 | * @return int Number of characters or bytes in given $string |
63 | 63 | * (characters if/when supported, bytes otherwise). |
64 | 64 | */ |
65 | - public static function strlen( $string, $encoding = null ) |
|
65 | + public static function strlen($string, $encoding = null) |
|
66 | 66 | { |
67 | - if ( self::mbstring_available() ) { |
|
68 | - return ( null === $encoding ) ? \mb_strlen( $string ) : \mb_strlen( $string, $encoding ); |
|
67 | + if (self::mbstring_available()) { |
|
68 | + return (null === $encoding) ? \mb_strlen($string) : \mb_strlen($string, $encoding); |
|
69 | 69 | } else { |
70 | - return \strlen( $string ); |
|
70 | + return \strlen($string); |
|
71 | 71 | } |
72 | 72 | } |
73 | 73 | |
@@ -85,44 +85,44 @@ discard block |
||
85 | 85 | * |
86 | 86 | * @return string |
87 | 87 | */ |
88 | - public static function substr_replace( $string, $replacement, $start, $length = null, $encoding = null ) |
|
88 | + public static function substr_replace($string, $replacement, $start, $length = null, $encoding = null) |
|
89 | 89 | { |
90 | - if ( self::mbstring_available() ) { |
|
91 | - $strlen = self::strlen( $string, $encoding ); |
|
90 | + if (self::mbstring_available()) { |
|
91 | + $strlen = self::strlen($string, $encoding); |
|
92 | 92 | |
93 | - if ( $start < 0 ) { |
|
93 | + if ($start < 0) { |
|
94 | 94 | if ( -$start < $strlen ) { |
95 | 95 | $start = $strlen + $start; |
96 | 96 | } else { |
97 | 97 | $start = 0; |
98 | 98 | } |
99 | - } elseif ( $start > $strlen ) { |
|
99 | + } elseif ($start > $strlen) { |
|
100 | 100 | $start = $strlen; |
101 | 101 | } |
102 | 102 | |
103 | - if ( null === $length || '' === $length ) { |
|
103 | + if (null === $length || '' === $length) { |
|
104 | 104 | $start2 = $strlen; |
105 | - } elseif ( $length < 0 ) { |
|
105 | + } elseif ($length < 0) { |
|
106 | 106 | $start2 = $strlen + $length; |
107 | - if ( $start2 < $start ) { |
|
107 | + if ($start2 < $start) { |
|
108 | 108 | $start2 = $start; |
109 | 109 | } |
110 | 110 | } else { |
111 | 111 | $start2 = $start + $length; |
112 | 112 | } |
113 | 113 | |
114 | - if ( null === $encoding ) { |
|
115 | - $leader = $start ? \mb_substr( $string, 0, $start ) : ''; |
|
116 | - $trailer = ( $start2 < $strlen ) ? \mb_substr( $string, $start2, null ) : ''; |
|
114 | + if (null === $encoding) { |
|
115 | + $leader = $start ? \mb_substr($string, 0, $start) : ''; |
|
116 | + $trailer = ($start2 < $strlen) ? \mb_substr($string, $start2, null) : ''; |
|
117 | 117 | } else { |
118 | - $leader = $start ? \mb_substr( $string, 0, $start, $encoding ) : ''; |
|
119 | - $trailer = ( $start2 < $strlen ) ? \mb_substr( $string, $start2, null, $encoding ) : ''; |
|
118 | + $leader = $start ? \mb_substr($string, 0, $start, $encoding) : ''; |
|
119 | + $trailer = ($start2 < $strlen) ? \mb_substr($string, $start2, null, $encoding) : ''; |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | return "{$leader}{$replacement}{$trailer}"; |
123 | 123 | } |
124 | 124 | |
125 | - return ( null === $length ) ? \substr_replace( $string, $replacement, $start ) : \substr_replace( $string, $replacement, $start, $length ); |
|
125 | + return (null === $length) ? \substr_replace($string, $replacement, $start) : \substr_replace($string, $replacement, $start, $length); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
@@ -132,16 +132,16 @@ discard block |
||
132 | 132 | * |
133 | 133 | * @return bool |
134 | 134 | */ |
135 | - public static function siteurl_not_root( $override = null ) |
|
135 | + public static function siteurl_not_root($override = null) |
|
136 | 136 | { |
137 | 137 | static $subdir = null; |
138 | 138 | |
139 | - if ( null === $subdir ) { |
|
139 | + if (null === $subdir) { |
|
140 | 140 | $parts = self::get_ao_wp_site_url_parts(); |
141 | - $subdir = ( isset( $parts['path'] ) && ( '/' !== $parts['path'] ) ); |
|
141 | + $subdir = (isset($parts['path']) && ('/' !== $parts['path'])); |
|
142 | 142 | } |
143 | 143 | |
144 | - if ( null !== $override ) { |
|
144 | + if (null !== $override) { |
|
145 | 145 | $subdir = $override; |
146 | 146 | } |
147 | 147 | |
@@ -158,8 +158,8 @@ discard block |
||
158 | 158 | { |
159 | 159 | static $parts = array(); |
160 | 160 | |
161 | - if ( empty( $parts ) ) { |
|
162 | - $parts = \parse_url( AUTOPTIMIZE_WP_SITE_URL ); |
|
161 | + if (empty($parts)) { |
|
162 | + $parts = \parse_url(AUTOPTIMIZE_WP_SITE_URL); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | return $parts; |
@@ -174,33 +174,33 @@ discard block |
||
174 | 174 | * |
175 | 175 | * @return string |
176 | 176 | */ |
177 | - public static function tweak_cdn_url_if_needed( $cdn_url, $force_cache_miss = false ) |
|
177 | + public static function tweak_cdn_url_if_needed($cdn_url, $force_cache_miss = false) |
|
178 | 178 | { |
179 | 179 | static $results = array(); |
180 | 180 | |
181 | - if ( ! isset( $results[ $cdn_url ] ) || $force_cache_miss ) { |
|
181 | + if (!isset($results[$cdn_url]) || $force_cache_miss) { |
|
182 | 182 | |
183 | 183 | // In order to return unmodified input when there's no need to tweak. |
184 | - $results[ $cdn_url ] = $cdn_url; |
|
184 | + $results[$cdn_url] = $cdn_url; |
|
185 | 185 | |
186 | 186 | // Behind a default true filter for backcompat, and only for sites |
187 | 187 | // in a subfolder/subdirectory, but still easily turned off if |
188 | 188 | // not wanted/needed... |
189 | - if ( autoptimizeUtils::siteurl_not_root() ) { |
|
190 | - $check = apply_filters( 'autoptimize_filter_cdn_magic_path_check', true, $cdn_url ); |
|
191 | - if ( $check ) { |
|
189 | + if (autoptimizeUtils::siteurl_not_root()) { |
|
190 | + $check = apply_filters('autoptimize_filter_cdn_magic_path_check', true, $cdn_url); |
|
191 | + if ($check) { |
|
192 | 192 | $site_url_parts = autoptimizeUtils::get_ao_wp_site_url_parts(); |
193 | - $cdn_url_parts = \parse_url( $cdn_url ); |
|
194 | - $schemeless = self::is_protocol_relative( $cdn_url ); |
|
195 | - $cdn_url_parts = self::maybe_replace_cdn_path( $site_url_parts, $cdn_url_parts ); |
|
196 | - if ( false !== $cdn_url_parts ) { |
|
197 | - $results[ $cdn_url ] = self::assemble_parsed_url( $cdn_url_parts, $schemeless ); |
|
193 | + $cdn_url_parts = \parse_url($cdn_url); |
|
194 | + $schemeless = self::is_protocol_relative($cdn_url); |
|
195 | + $cdn_url_parts = self::maybe_replace_cdn_path($site_url_parts, $cdn_url_parts); |
|
196 | + if (false !== $cdn_url_parts) { |
|
197 | + $results[$cdn_url] = self::assemble_parsed_url($cdn_url_parts, $schemeless); |
|
198 | 198 | } |
199 | 199 | } |
200 | 200 | } |
201 | 201 | } |
202 | 202 | |
203 | - return $results[ $cdn_url ]; |
|
203 | + return $results[$cdn_url]; |
|
204 | 204 | } |
205 | 205 | |
206 | 206 | /** |
@@ -214,10 +214,10 @@ discard block |
||
214 | 214 | * |
215 | 215 | * @return array|false |
216 | 216 | */ |
217 | - public static function maybe_replace_cdn_path( array $site_url_parts, array $cdn_url_parts ) |
|
217 | + public static function maybe_replace_cdn_path(array $site_url_parts, array $cdn_url_parts) |
|
218 | 218 | { |
219 | - if ( isset( $site_url_parts['path'] ) && '/' !== $site_url_parts['path'] ) { |
|
220 | - if ( ! isset( $cdn_url_parts['path'] ) || '/' === $cdn_url_parts['path'] ) { |
|
219 | + if (isset($site_url_parts['path']) && '/' !== $site_url_parts['path']) { |
|
220 | + if (!isset($cdn_url_parts['path']) || '/' === $cdn_url_parts['path']) { |
|
221 | 221 | $cdn_url_parts['path'] = $site_url_parts['path']; |
222 | 222 | return $cdn_url_parts; |
223 | 223 | } |
@@ -237,20 +237,20 @@ discard block |
||
237 | 237 | * |
238 | 238 | * @return string |
239 | 239 | */ |
240 | - public static function assemble_parsed_url( array $parsed_url, $schemeless = false ) |
|
240 | + public static function assemble_parsed_url(array $parsed_url, $schemeless = false) |
|
241 | 241 | { |
242 | - $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : ''; |
|
243 | - if ( $schemeless ) { |
|
242 | + $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'].'://' : ''; |
|
243 | + if ($schemeless) { |
|
244 | 244 | $scheme = '//'; |
245 | 245 | } |
246 | - $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; |
|
247 | - $port = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : ''; |
|
248 | - $user = isset( $parsed_url['user'] ) ? $parsed_url['user'] : ''; |
|
249 | - $pass = isset( $parsed_url['pass'] ) ? ':' . $parsed_url['pass'] : ''; |
|
250 | - $pass = ( $user || $pass ) ? "$pass@" : ''; |
|
251 | - $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; |
|
252 | - $query = isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : ''; |
|
253 | - $fragment = isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : ''; |
|
246 | + $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
|
247 | + $port = isset($parsed_url['port']) ? ':'.$parsed_url['port'] : ''; |
|
248 | + $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; |
|
249 | + $pass = isset($parsed_url['pass']) ? ':'.$parsed_url['pass'] : ''; |
|
250 | + $pass = ($user || $pass) ? "$pass@" : ''; |
|
251 | + $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; |
|
252 | + $query = isset($parsed_url['query']) ? '?'.$parsed_url['query'] : ''; |
|
253 | + $fragment = isset($parsed_url['fragment']) ? '#'.$parsed_url['fragment'] : ''; |
|
254 | 254 | |
255 | 255 | return "$scheme$user$pass$host$port$path$query$fragment"; |
256 | 256 | } |
@@ -262,12 +262,12 @@ discard block |
||
262 | 262 | * |
263 | 263 | * @return bool |
264 | 264 | */ |
265 | - public static function is_protocol_relative( $url ) |
|
265 | + public static function is_protocol_relative($url) |
|
266 | 266 | { |
267 | 267 | $result = false; |
268 | 268 | |
269 | - if ( ! empty( $url ) ) { |
|
270 | - $result = ( 0 === strpos( $url, '//' ) ); |
|
269 | + if (!empty($url)) { |
|
270 | + $result = (0 === strpos($url, '//')); |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | return $result; |
@@ -280,9 +280,9 @@ discard block |
||
280 | 280 | * |
281 | 281 | * @return string |
282 | 282 | */ |
283 | - public static function path_canonicalize( $path ) |
|
283 | + public static function path_canonicalize($path) |
|
284 | 284 | { |
285 | - $patterns = array( |
|
285 | + $patterns = array( |
|
286 | 286 | '~/{2,}~', |
287 | 287 | '~/(\./)+~', |
288 | 288 | '~([^/\.]+/(?R)*\.{2,}/)~', |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | '', |
296 | 296 | ); |
297 | 297 | |
298 | - return preg_replace( $patterns, $replacements, $path ); |
|
298 | + return preg_replace($patterns, $replacements, $path); |
|
299 | 299 | } |
300 | 300 | |
301 | 301 | /** |
@@ -307,15 +307,15 @@ discard block |
||
307 | 307 | * |
308 | 308 | * @return null|array Service status or null. |
309 | 309 | */ |
310 | - public static function check_service_availability( $return_result = false ) |
|
310 | + public static function check_service_availability($return_result = false) |
|
311 | 311 | { |
312 | - $service_availability_resp = wp_remote_get( 'https://misc.optimizingmatters.com/api/autoptimize_service_availablity.json?from=aomain&ver=' . AUTOPTIMIZE_PLUGIN_VERSION ); |
|
313 | - if ( ! is_wp_error( $service_availability_resp ) ) { |
|
314 | - if ( '200' == wp_remote_retrieve_response_code( $service_availability_resp ) ) { |
|
315 | - $availabilities = json_decode( wp_remote_retrieve_body( $service_availability_resp ), true ); |
|
316 | - if ( is_array( $availabilities ) ) { |
|
317 | - autoptimizeOptionWrapper::update_option( 'autoptimize_service_availablity', $availabilities ); |
|
318 | - if ( $return_result ) { |
|
312 | + $service_availability_resp = wp_remote_get('https://misc.optimizingmatters.com/api/autoptimize_service_availablity.json?from=aomain&ver='.AUTOPTIMIZE_PLUGIN_VERSION); |
|
313 | + if (!is_wp_error($service_availability_resp)) { |
|
314 | + if ('200' == wp_remote_retrieve_response_code($service_availability_resp)) { |
|
315 | + $availabilities = json_decode(wp_remote_retrieve_body($service_availability_resp), true); |
|
316 | + if (is_array($availabilities)) { |
|
317 | + autoptimizeOptionWrapper::update_option('autoptimize_service_availablity', $availabilities); |
|
318 | + if ($return_result) { |
|
319 | 319 | return $availabilities; |
320 | 320 | } |
321 | 321 | } |
@@ -331,10 +331,10 @@ discard block |
||
331 | 331 | * |
332 | 332 | * @return bool |
333 | 333 | */ |
334 | - public static function str_is_valid_regex( $string ) |
|
334 | + public static function str_is_valid_regex($string) |
|
335 | 335 | { |
336 | - set_error_handler( function() {}, E_WARNING ); |
|
337 | - $is_regex = ( false !== preg_match( $string, '' ) ); |
|
336 | + set_error_handler(function() {}, E_WARNING); |
|
337 | + $is_regex = (false !== preg_match($string, '')); |
|
338 | 338 | restore_error_handler(); |
339 | 339 | |
340 | 340 | return $is_regex; |
@@ -347,17 +347,17 @@ discard block |
||
347 | 347 | * |
348 | 348 | * @return bool |
349 | 349 | */ |
350 | - public static function is_plugin_active( $plugin_file ) |
|
350 | + public static function is_plugin_active($plugin_file) |
|
351 | 351 | { |
352 | 352 | static $ipa_exists = null; |
353 | - if ( null === $ipa_exists ) { |
|
354 | - if ( ! function_exists( '\is_plugin_active' ) ) { |
|
355 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
353 | + if (null === $ipa_exists) { |
|
354 | + if (!function_exists('\is_plugin_active')) { |
|
355 | + require_once ABSPATH.'wp-admin/includes/plugin.php'; |
|
356 | 356 | } |
357 | - $ipa_exists = function_exists( '\is_plugin_active' ); |
|
357 | + $ipa_exists = function_exists('\is_plugin_active'); |
|
358 | 358 | } |
359 | 359 | |
360 | - return $ipa_exists && \is_plugin_active( $plugin_file ); |
|
360 | + return $ipa_exists && \is_plugin_active($plugin_file); |
|
361 | 361 | } |
362 | 362 | |
363 | 363 | /** |
@@ -367,11 +367,11 @@ discard block |
||
367 | 367 | * |
368 | 368 | * @return string |
369 | 369 | */ |
370 | - public static function remove_id_from_node( $node ) { |
|
371 | - if ( strpos( $node, 'id=' ) === false || apply_filters( 'autoptimize_filter_utils_keep_ids', false ) ) { |
|
370 | + public static function remove_id_from_node($node) { |
|
371 | + if (strpos($node, 'id=') === false || apply_filters('autoptimize_filter_utils_keep_ids', false)) { |
|
372 | 372 | return $node; |
373 | 373 | } else { |
374 | - return preg_replace( '#(.*) id=[\'|"].*[\'|"] (.*)#Um', '$1 $2', $node ); |
|
374 | + return preg_replace('#(.*) id=[\'|"].*[\'|"] (.*)#Um', '$1 $2', $node); |
|
375 | 375 | } |
376 | 376 | } |
377 | 377 | |
@@ -383,15 +383,15 @@ discard block |
||
383 | 383 | * |
384 | 384 | * @return bool |
385 | 385 | */ |
386 | - public static function str_ends_in( $str, $test ) |
|
386 | + public static function str_ends_in($str, $test) |
|
387 | 387 | { |
388 | 388 | // @codingStandardsIgnoreStart |
389 | 389 | // substr_compare() is bugged on 5.5.11: https://3v4l.org/qGYBH |
390 | 390 | // return ( 0 === substr_compare( $str, $test, -strlen( $test ) ) ); |
391 | 391 | // @codingStandardsIgnoreEnd |
392 | 392 | |
393 | - $length = strlen( $test ); |
|
393 | + $length = strlen($test); |
|
394 | 394 | |
395 | - return ( substr( $str, -$length, $length ) === $test ); |
|
395 | + return (substr($str, -$length, $length) === $test); |
|
396 | 396 | } |
397 | 397 | } |
@@ -16,66 +16,66 @@ discard block |
||
16 | 16 | */ |
17 | 17 | |
18 | 18 | |
19 | -if ( ! defined( 'ABSPATH' ) ) { |
|
19 | +if (!defined('ABSPATH')) { |
|
20 | 20 | exit; |
21 | 21 | } |
22 | 22 | |
23 | -define( 'AUTOPTIMIZE_PLUGIN_VERSION', '2.7.0' ); |
|
23 | +define('AUTOPTIMIZE_PLUGIN_VERSION', '2.7.0'); |
|
24 | 24 | |
25 | 25 | // plugin_dir_path() returns the trailing slash! |
26 | -define( 'AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
|
27 | -define( 'AUTOPTIMIZE_PLUGIN_FILE', __FILE__ ); |
|
26 | +define('AUTOPTIMIZE_PLUGIN_DIR', plugin_dir_path(__FILE__)); |
|
27 | +define('AUTOPTIMIZE_PLUGIN_FILE', __FILE__); |
|
28 | 28 | |
29 | 29 | // Bail early if attempting to run on non-supported php versions. |
30 | -if ( version_compare( PHP_VERSION, '5.6', '<' ) ) { |
|
30 | +if (version_compare(PHP_VERSION, '5.6', '<')) { |
|
31 | 31 | function autoptimize_incompatible_admin_notice() { |
32 | - echo '<div class="error"><p>' . __( 'Autoptimize requires PHP 5.6 (or higher) to function properly. Please upgrade PHP. The Plugin has been auto-deactivated.', 'autoptimize' ) . '</p></div>'; |
|
33 | - if ( isset( $_GET['activate'] ) ) { |
|
34 | - unset( $_GET['activate'] ); |
|
32 | + echo '<div class="error"><p>'.__('Autoptimize requires PHP 5.6 (or higher) to function properly. Please upgrade PHP. The Plugin has been auto-deactivated.', 'autoptimize').'</p></div>'; |
|
33 | + if (isset($_GET['activate'])) { |
|
34 | + unset($_GET['activate']); |
|
35 | 35 | } |
36 | 36 | } |
37 | 37 | function autoptimize_deactivate_self() { |
38 | - deactivate_plugins( plugin_basename( AUTOPTIMIZE_PLUGIN_FILE ) ); |
|
38 | + deactivate_plugins(plugin_basename(AUTOPTIMIZE_PLUGIN_FILE)); |
|
39 | 39 | } |
40 | - add_action( 'admin_notices', 'autoptimize_incompatible_admin_notice' ); |
|
41 | - add_action( 'admin_init', 'autoptimize_deactivate_self' ); |
|
40 | + add_action('admin_notices', 'autoptimize_incompatible_admin_notice'); |
|
41 | + add_action('admin_init', 'autoptimize_deactivate_self'); |
|
42 | 42 | return; |
43 | 43 | } |
44 | 44 | |
45 | -function autoptimize_autoload( $class_name ) { |
|
46 | - if ( in_array( $class_name, array( 'Minify_HTML', 'JSMin' ) ) ) { |
|
47 | - $file = strtolower( $class_name ); |
|
48 | - $file = str_replace( '_', '-', $file ); |
|
49 | - $path = dirname( __FILE__ ) . '/classes/external/php/'; |
|
50 | - $filepath = $path . $file . '.php'; |
|
51 | - } elseif ( false !== strpos( $class_name, 'Autoptimize\\tubalmartin\\CssMin' ) ) { |
|
52 | - $file = str_replace( 'Autoptimize\\tubalmartin\\CssMin\\', '', $class_name ); |
|
53 | - $path = dirname( __FILE__ ) . '/classes/external/php/yui-php-cssmin-bundled/'; |
|
54 | - $filepath = $path . $file . '.php'; |
|
55 | - } elseif ( 'autoptimize' === substr( $class_name, 0, 11 ) ) { |
|
45 | +function autoptimize_autoload($class_name) { |
|
46 | + if (in_array($class_name, array('Minify_HTML', 'JSMin'))) { |
|
47 | + $file = strtolower($class_name); |
|
48 | + $file = str_replace('_', '-', $file); |
|
49 | + $path = dirname(__FILE__).'/classes/external/php/'; |
|
50 | + $filepath = $path.$file.'.php'; |
|
51 | + } elseif (false !== strpos($class_name, 'Autoptimize\\tubalmartin\\CssMin')) { |
|
52 | + $file = str_replace('Autoptimize\\tubalmartin\\CssMin\\', '', $class_name); |
|
53 | + $path = dirname(__FILE__).'/classes/external/php/yui-php-cssmin-bundled/'; |
|
54 | + $filepath = $path.$file.'.php'; |
|
55 | + } elseif ('autoptimize' === substr($class_name, 0, 11)) { |
|
56 | 56 | // One of our "old" classes. |
57 | 57 | $file = $class_name; |
58 | - $path = dirname( __FILE__ ) . '/classes/'; |
|
59 | - $filepath = $path . $file . '.php'; |
|
60 | - } elseif ( 'PAnD' === $class_name ) { |
|
58 | + $path = dirname(__FILE__).'/classes/'; |
|
59 | + $filepath = $path.$file.'.php'; |
|
60 | + } elseif ('PAnD' === $class_name) { |
|
61 | 61 | $file = 'persist-admin-notices-dismissal'; |
62 | - $path = dirname( __FILE__ ) . '/classes/external/php/persist-admin-notices-dismissal/'; |
|
63 | - $filepath = $path . $file . '.php'; |
|
62 | + $path = dirname(__FILE__).'/classes/external/php/persist-admin-notices-dismissal/'; |
|
63 | + $filepath = $path.$file.'.php'; |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | // If we didn't match one of our rules, bail! |
67 | - if ( ! isset( $filepath ) ) { |
|
67 | + if (!isset($filepath)) { |
|
68 | 68 | return; |
69 | 69 | } |
70 | 70 | |
71 | 71 | require $filepath; |
72 | 72 | } |
73 | 73 | |
74 | -spl_autoload_register( 'autoptimize_autoload' ); |
|
74 | +spl_autoload_register('autoptimize_autoload'); |
|
75 | 75 | |
76 | 76 | // Load WP CLI command(s) on demand. |
77 | -if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
78 | - require AUTOPTIMIZE_PLUGIN_DIR . 'classes/autoptimizeCLI.php'; |
|
77 | +if (defined('WP_CLI') && WP_CLI) { |
|
78 | + require AUTOPTIMIZE_PLUGIN_DIR.'classes/autoptimizeCLI.php'; |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | /** |
@@ -86,8 +86,8 @@ discard block |
||
86 | 86 | function autoptimize() { |
87 | 87 | static $plugin = null; |
88 | 88 | |
89 | - if ( null === $plugin ) { |
|
90 | - $plugin = new autoptimizeMain( AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE ); |
|
89 | + if (null === $plugin) { |
|
90 | + $plugin = new autoptimizeMain(AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | return $plugin; |