@@ -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.1' ); |
|
23 | +define('AUTOPTIMIZE_PLUGIN_VERSION', '2.7.1'); |
|
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; |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Class for JS optimization. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -198,127 +198,127 @@ discard block |
||
198 | 198 | * |
199 | 199 | * @param array $options all options. |
200 | 200 | */ |
201 | - public function read( $options ) |
|
201 | + public function read($options) |
|
202 | 202 | { |
203 | - $noptimize_js = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ); |
|
204 | - if ( $noptimize_js ) { |
|
203 | + $noptimize_js = apply_filters('autoptimize_filter_js_noptimize', false, $this->content); |
|
204 | + if ($noptimize_js) { |
|
205 | 205 | return false; |
206 | 206 | } |
207 | 207 | |
208 | 208 | // only optimize known good JS? |
209 | - $whitelist_js = apply_filters( 'autoptimize_filter_js_whitelist', '', $this->content ); |
|
210 | - if ( ! empty( $whitelist_js ) ) { |
|
211 | - $this->whitelist = array_filter( array_map( 'trim', explode( ',', $whitelist_js ) ) ); |
|
209 | + $whitelist_js = apply_filters('autoptimize_filter_js_whitelist', '', $this->content); |
|
210 | + if (!empty($whitelist_js)) { |
|
211 | + $this->whitelist = array_filter(array_map('trim', explode(',', $whitelist_js))); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | // is there JS we should simply remove? |
215 | - $removable_js = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); |
|
216 | - if ( ! empty( $removable_js ) ) { |
|
217 | - $this->jsremovables = array_filter( array_map( 'trim', explode( ',', $removable_js ) ) ); |
|
215 | + $removable_js = apply_filters('autoptimize_filter_js_removables', '', $this->content); |
|
216 | + if (!empty($removable_js)) { |
|
217 | + $this->jsremovables = array_filter(array_map('trim', explode(',', $removable_js))); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | // only header? |
221 | - if ( apply_filters( 'autoptimize_filter_js_justhead', $options['justhead'] ) ) { |
|
222 | - $content = explode( '</head>', $this->content, 2 ); |
|
223 | - $this->content = $content[0] . '</head>'; |
|
221 | + if (apply_filters('autoptimize_filter_js_justhead', $options['justhead'])) { |
|
222 | + $content = explode('</head>', $this->content, 2); |
|
223 | + $this->content = $content[0].'</head>'; |
|
224 | 224 | $this->restofcontent = $content[1]; |
225 | 225 | } |
226 | 226 | |
227 | 227 | // Determine whether we're doing JS-files aggregation or not. |
228 | - if ( ! $options['aggregate'] ) { |
|
228 | + if (!$options['aggregate']) { |
|
229 | 229 | $this->aggregate = false; |
230 | 230 | } |
231 | 231 | // Returning true for "dontaggregate" turns off aggregation. |
232 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) { |
|
232 | + if ($this->aggregate && apply_filters('autoptimize_filter_js_dontaggregate', false)) { |
|
233 | 233 | $this->aggregate = false; |
234 | 234 | } |
235 | 235 | |
236 | 236 | // include inline? |
237 | - if ( apply_filters( 'autoptimize_js_include_inline', $options['include_inline'] ) ) { |
|
237 | + if (apply_filters('autoptimize_js_include_inline', $options['include_inline'])) { |
|
238 | 238 | $this->include_inline = true; |
239 | 239 | } |
240 | 240 | |
241 | 241 | // filter to "late inject minified JS", default to true for now (it is faster). |
242 | - $this->inject_min_late = apply_filters( 'autoptimize_filter_js_inject_min_late', true ); |
|
242 | + $this->inject_min_late = apply_filters('autoptimize_filter_js_inject_min_late', true); |
|
243 | 243 | |
244 | 244 | // filters to override hardcoded do(nt)move(last) array contents (array in, array out!). |
245 | - $this->dontmove = apply_filters( 'autoptimize_filter_js_dontmove', $this->dontmove ); |
|
246 | - $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast ); |
|
247 | - $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); |
|
245 | + $this->dontmove = apply_filters('autoptimize_filter_js_dontmove', $this->dontmove); |
|
246 | + $this->domovelast = apply_filters('autoptimize_filter_js_movelast', $this->domovelast); |
|
247 | + $this->domove = apply_filters('autoptimize_filter_js_domove', $this->domove); |
|
248 | 248 | |
249 | 249 | // Determine whether excluded files should be minified if not yet so. |
250 | - if ( ! $options['minify_excluded'] && $options['aggregate'] ) { |
|
250 | + if (!$options['minify_excluded'] && $options['aggregate']) { |
|
251 | 251 | $this->minify_excluded = false; |
252 | 252 | } |
253 | - $this->minify_excluded = apply_filters( 'autoptimize_filter_js_minify_excluded', $this->minify_excluded, '' ); |
|
253 | + $this->minify_excluded = apply_filters('autoptimize_filter_js_minify_excluded', $this->minify_excluded, ''); |
|
254 | 254 | |
255 | 255 | // get extra exclusions settings or filter. |
256 | 256 | $exclude_js = $options['js_exclude']; |
257 | - $exclude_js = apply_filters( 'autoptimize_filter_js_exclude', $exclude_js, $this->content ); |
|
258 | - |
|
259 | - if ( '' !== $exclude_js ) { |
|
260 | - if ( is_array( $exclude_js ) ) { |
|
261 | - $remove_keys = array_keys( $exclude_js, 'remove' ); |
|
262 | - if ( false !== $remove_keys ) { |
|
263 | - foreach ( $remove_keys as $remove_key ) { |
|
264 | - unset( $exclude_js[ $remove_key ] ); |
|
257 | + $exclude_js = apply_filters('autoptimize_filter_js_exclude', $exclude_js, $this->content); |
|
258 | + |
|
259 | + if ('' !== $exclude_js) { |
|
260 | + if (is_array($exclude_js)) { |
|
261 | + $remove_keys = array_keys($exclude_js, 'remove'); |
|
262 | + if (false !== $remove_keys) { |
|
263 | + foreach ($remove_keys as $remove_key) { |
|
264 | + unset($exclude_js[$remove_key]); |
|
265 | 265 | $this->jsremovables[] = $remove_key; |
266 | 266 | } |
267 | 267 | } |
268 | - $excl_js_arr = array_keys( $exclude_js ); |
|
268 | + $excl_js_arr = array_keys($exclude_js); |
|
269 | 269 | } else { |
270 | - $excl_js_arr = array_filter( array_map( 'trim', explode( ',', $exclude_js ) ) ); |
|
270 | + $excl_js_arr = array_filter(array_map('trim', explode(',', $exclude_js))); |
|
271 | 271 | } |
272 | - $this->dontmove = array_merge( $excl_js_arr, $this->dontmove ); |
|
272 | + $this->dontmove = array_merge($excl_js_arr, $this->dontmove); |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | // Should we add try-catch? |
276 | - if ( $options['trycatch'] ) { |
|
276 | + if ($options['trycatch']) { |
|
277 | 277 | $this->trycatch = true; |
278 | 278 | } |
279 | 279 | |
280 | 280 | // force js in head? |
281 | - if ( $options['forcehead'] ) { |
|
281 | + if ($options['forcehead']) { |
|
282 | 282 | $this->forcehead = true; |
283 | 283 | } else { |
284 | 284 | $this->forcehead = false; |
285 | 285 | } |
286 | 286 | |
287 | - $this->forcehead = apply_filters( 'autoptimize_filter_js_forcehead', $this->forcehead ); |
|
287 | + $this->forcehead = apply_filters('autoptimize_filter_js_forcehead', $this->forcehead); |
|
288 | 288 | |
289 | 289 | // get cdn url. |
290 | 290 | $this->cdn_url = $options['cdn_url']; |
291 | 291 | |
292 | 292 | // noptimize me. |
293 | - $this->content = $this->hide_noptimize( $this->content ); |
|
293 | + $this->content = $this->hide_noptimize($this->content); |
|
294 | 294 | |
295 | 295 | // Save IE hacks. |
296 | - $this->content = $this->hide_iehacks( $this->content ); |
|
296 | + $this->content = $this->hide_iehacks($this->content); |
|
297 | 297 | |
298 | 298 | // comments. |
299 | - $this->content = $this->hide_comments( $this->content ); |
|
299 | + $this->content = $this->hide_comments($this->content); |
|
300 | 300 | |
301 | 301 | // Get script files. |
302 | - if ( preg_match_all( '#<script.*</script>#Usmi', $this->content, $matches ) ) { |
|
303 | - foreach ( $matches[0] as $tag ) { |
|
302 | + if (preg_match_all('#<script.*</script>#Usmi', $this->content, $matches)) { |
|
303 | + foreach ($matches[0] as $tag) { |
|
304 | 304 | // only consider script aggregation for types whitelisted in should_aggregate-function. |
305 | - $should_aggregate = $this->should_aggregate( $tag ); |
|
306 | - if ( ! $should_aggregate ) { |
|
305 | + $should_aggregate = $this->should_aggregate($tag); |
|
306 | + if (!$should_aggregate) { |
|
307 | 307 | $tag = ''; |
308 | 308 | continue; |
309 | 309 | } |
310 | 310 | |
311 | - if ( preg_match( '#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source ) ) { |
|
311 | + if (preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source)) { |
|
312 | 312 | // non-inline script. |
313 | - if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
314 | - $this->content = str_replace( $tag, '', $this->content ); |
|
313 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
314 | + $this->content = str_replace($tag, '', $this->content); |
|
315 | 315 | continue; |
316 | 316 | } |
317 | 317 | |
318 | 318 | $orig_tag = null; |
319 | - $url = current( explode( '?', $source[2], 2 ) ); |
|
320 | - $path = $this->getpath( $url ); |
|
321 | - if ( false !== $path && preg_match( '#\.js$#', $path ) && $this->ismergeable( $tag ) ) { |
|
319 | + $url = current(explode('?', $source[2], 2)); |
|
320 | + $path = $this->getpath($url); |
|
321 | + if (false !== $path && preg_match('#\.js$#', $path) && $this->ismergeable($tag)) { |
|
322 | 322 | // ok to optimize, add to array. |
323 | 323 | $this->scripts[] = $path; |
324 | 324 | } else { |
@@ -326,11 +326,11 @@ discard block |
||
326 | 326 | $new_tag = $tag; |
327 | 327 | |
328 | 328 | // non-mergeable script (excluded or dynamic or external). |
329 | - if ( is_array( $exclude_js ) ) { |
|
329 | + if (is_array($exclude_js)) { |
|
330 | 330 | // should we add flags? |
331 | - foreach ( $exclude_js as $excl_tag => $excl_flags ) { |
|
332 | - if ( false !== strpos( $orig_tag, $excl_tag ) && in_array( $excl_flags, array( 'async', 'defer' ) ) ) { |
|
333 | - $new_tag = str_replace( '<script ', '<script ' . $excl_flags . ' ', $new_tag ); |
|
331 | + foreach ($exclude_js as $excl_tag => $excl_flags) { |
|
332 | + if (false !== strpos($orig_tag, $excl_tag) && in_array($excl_flags, array('async', 'defer'))) { |
|
333 | + $new_tag = str_replace('<script ', '<script '.$excl_flags.' ', $new_tag); |
|
334 | 334 | } |
335 | 335 | } |
336 | 336 | } |
@@ -338,13 +338,13 @@ discard block |
||
338 | 338 | // Should we minify the non-aggregated script? |
339 | 339 | // -> if aggregate is on and exclude minify is on |
340 | 340 | // -> if aggregate is off and the file is not in dontmove. |
341 | - if ( $path && $this->minify_excluded ) { |
|
342 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
343 | - if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) { |
|
344 | - $minified_url = $this->minify_single( $path ); |
|
345 | - if ( ! empty( $minified_url ) ) { |
|
341 | + if ($path && $this->minify_excluded) { |
|
342 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
343 | + if ((false === $this->aggregate && str_replace($this->dontmove, '', $path) === $path) || (true === $this->aggregate && (false === $consider_minified_array || str_replace($consider_minified_array, '', $path) === $path))) { |
|
344 | + $minified_url = $this->minify_single($path); |
|
345 | + if (!empty($minified_url)) { |
|
346 | 346 | // Replace original URL with minified URL from cache. |
347 | - $new_tag = str_replace( $url, $minified_url, $new_tag ); |
|
347 | + $new_tag = str_replace($url, $minified_url, $new_tag); |
|
348 | 348 | } else { |
349 | 349 | // Remove the original script tag, because cache content is empty. |
350 | 350 | $new_tag = ''; |
@@ -352,17 +352,17 @@ discard block |
||
352 | 352 | } |
353 | 353 | } |
354 | 354 | |
355 | - if ( $this->ismovable( $new_tag ) ) { |
|
355 | + if ($this->ismovable($new_tag)) { |
|
356 | 356 | // can be moved, flags and all. |
357 | - if ( $this->movetolast( $new_tag ) ) { |
|
357 | + if ($this->movetolast($new_tag)) { |
|
358 | 358 | $this->move['last'][] = $new_tag; |
359 | 359 | } else { |
360 | 360 | $this->move['first'][] = $new_tag; |
361 | 361 | } |
362 | 362 | } else { |
363 | 363 | // cannot be moved, so if flag was added re-inject altered tag immediately. |
364 | - if ( ( '' !== $new_tag && $orig_tag !== $new_tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_js_remove_empty_files', false ) ) ) { |
|
365 | - $this->content = str_replace( $orig_tag, $new_tag, $this->content ); |
|
364 | + if (('' !== $new_tag && $orig_tag !== $new_tag) || ('' === $new_tag && apply_filters('autoptimize_filter_js_remove_empty_files', false))) { |
|
365 | + $this->content = str_replace($orig_tag, $new_tag, $this->content); |
|
366 | 366 | $orig_tag = ''; |
367 | 367 | } |
368 | 368 | // and forget about the $tag (not to be touched any more). |
@@ -371,23 +371,23 @@ discard block |
||
371 | 371 | } |
372 | 372 | } else { |
373 | 373 | // Inline script. |
374 | - if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
375 | - $this->content = str_replace( $tag, '', $this->content ); |
|
374 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
375 | + $this->content = str_replace($tag, '', $this->content); |
|
376 | 376 | continue; |
377 | 377 | } |
378 | 378 | |
379 | 379 | // unhide comments, as javascript may be wrapped in comment-tags for old times' sake. |
380 | - $tag = $this->restore_comments( $tag ); |
|
381 | - if ( $this->ismergeable( $tag ) && $this->include_inline ) { |
|
382 | - preg_match( '#<script.*>(.*)</script>#Usmi', $tag, $code ); |
|
383 | - $code = preg_replace( '#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1] ); |
|
384 | - $code = preg_replace( '/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code ); |
|
385 | - $this->scripts[] = 'INLINE;' . $code; |
|
380 | + $tag = $this->restore_comments($tag); |
|
381 | + if ($this->ismergeable($tag) && $this->include_inline) { |
|
382 | + preg_match('#<script.*>(.*)</script>#Usmi', $tag, $code); |
|
383 | + $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1]); |
|
384 | + $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code); |
|
385 | + $this->scripts[] = 'INLINE;'.$code; |
|
386 | 386 | } else { |
387 | 387 | // Can we move this? |
388 | - $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag ); |
|
389 | - if ( $this->ismovable( $tag ) || '' !== $autoptimize_js_moveable ) { |
|
390 | - if ( $this->movetolast( $tag ) || 'last' === $autoptimize_js_moveable ) { |
|
388 | + $autoptimize_js_moveable = apply_filters('autoptimize_js_moveable', '', $tag); |
|
389 | + if ($this->ismovable($tag) || '' !== $autoptimize_js_moveable) { |
|
390 | + if ($this->movetolast($tag) || 'last' === $autoptimize_js_moveable) { |
|
391 | 391 | $this->move['last'][] = $tag; |
392 | 392 | } else { |
393 | 393 | $this->move['first'][] = $tag; |
@@ -398,11 +398,11 @@ discard block |
||
398 | 398 | } |
399 | 399 | } |
400 | 400 | // Re-hide comments to be able to do the removal based on tag from $this->content. |
401 | - $tag = $this->hide_comments( $tag ); |
|
401 | + $tag = $this->hide_comments($tag); |
|
402 | 402 | } |
403 | 403 | |
404 | 404 | // Remove the original script tag. |
405 | - $this->content = str_replace( $tag, '', $this->content ); |
|
405 | + $this->content = str_replace($tag, '', $this->content); |
|
406 | 406 | } |
407 | 407 | |
408 | 408 | return true; |
@@ -427,30 +427,30 @@ discard block |
||
427 | 427 | * @param string $tag Script node & child(ren). |
428 | 428 | * @return bool |
429 | 429 | */ |
430 | - public function should_aggregate( $tag ) |
|
430 | + public function should_aggregate($tag) |
|
431 | 431 | { |
432 | - if ( empty( $tag ) ) { |
|
432 | + if (empty($tag)) { |
|
433 | 433 | return false; |
434 | 434 | } |
435 | 435 | |
436 | 436 | // We're only interested in the type attribute of the <script> tag itself, not any possible |
437 | 437 | // inline code that might just contain the 'type=' string... |
438 | 438 | $tag_parts = array(); |
439 | - preg_match( '#<(script[^>]*)>#i', $tag, $tag_parts ); |
|
439 | + preg_match('#<(script[^>]*)>#i', $tag, $tag_parts); |
|
440 | 440 | $tag_without_contents = null; |
441 | - if ( ! empty( $tag_parts[1] ) ) { |
|
441 | + if (!empty($tag_parts[1])) { |
|
442 | 442 | $tag_without_contents = $tag_parts[1]; |
443 | 443 | } |
444 | 444 | |
445 | - $has_type = ( strpos( $tag_without_contents, 'type' ) !== false ); |
|
445 | + $has_type = (strpos($tag_without_contents, 'type') !== false); |
|
446 | 446 | |
447 | 447 | $type_valid = false; |
448 | - if ( $has_type ) { |
|
449 | - $type_valid = (bool) preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents ); |
|
448 | + if ($has_type) { |
|
449 | + $type_valid = (bool) preg_match('/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents); |
|
450 | 450 | } |
451 | 451 | |
452 | 452 | $should_aggregate = false; |
453 | - if ( ! $has_type || $type_valid ) { |
|
453 | + if (!$has_type || $type_valid) { |
|
454 | 454 | $should_aggregate = true; |
455 | 455 | } |
456 | 456 | |
@@ -462,75 +462,75 @@ discard block |
||
462 | 462 | */ |
463 | 463 | public function minify() |
464 | 464 | { |
465 | - foreach ( $this->scripts as $script ) { |
|
466 | - if ( empty( $script ) ) { |
|
465 | + foreach ($this->scripts as $script) { |
|
466 | + if (empty($script)) { |
|
467 | 467 | continue; |
468 | 468 | } |
469 | 469 | |
470 | 470 | // TODO/FIXME: some duplicate code here, can be reduced/simplified. |
471 | - if ( preg_match( '#^INLINE;#', $script ) ) { |
|
471 | + if (preg_match('#^INLINE;#', $script)) { |
|
472 | 472 | // Inline script. |
473 | - $script = preg_replace( '#^INLINE;#', '', $script ); |
|
474 | - $script = rtrim( $script, ";\n\t\r" ) . ';'; |
|
473 | + $script = preg_replace('#^INLINE;#', '', $script); |
|
474 | + $script = rtrim($script, ";\n\t\r").';'; |
|
475 | 475 | // Add try-catch? |
476 | - if ( $this->trycatch ) { |
|
477 | - $script = 'try{' . $script . '}catch(e){}'; |
|
476 | + if ($this->trycatch) { |
|
477 | + $script = 'try{'.$script.'}catch(e){}'; |
|
478 | 478 | } |
479 | - $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, '' ); |
|
480 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscript ) ) { |
|
479 | + $tmpscript = apply_filters('autoptimize_js_individual_script', $script, ''); |
|
480 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscript)) { |
|
481 | 481 | $script = $tmpscript; |
482 | 482 | $this->alreadyminified = true; |
483 | 483 | } |
484 | - $this->jscode .= "\n" . $script; |
|
484 | + $this->jscode .= "\n".$script; |
|
485 | 485 | } else { |
486 | 486 | // External script. |
487 | - if ( false !== $script && file_exists( $script ) && is_readable( $script ) ) { |
|
488 | - $scriptsrc = file_get_contents( $script ); |
|
489 | - $scriptsrc = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc ); |
|
490 | - $scriptsrc = rtrim( $scriptsrc, ";\n\t\r" ) . ';'; |
|
487 | + if (false !== $script && file_exists($script) && is_readable($script)) { |
|
488 | + $scriptsrc = file_get_contents($script); |
|
489 | + $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc); |
|
490 | + $scriptsrc = rtrim($scriptsrc, ";\n\t\r").';'; |
|
491 | 491 | // Add try-catch? |
492 | - if ( $this->trycatch ) { |
|
493 | - $scriptsrc = 'try{' . $scriptsrc . '}catch(e){}'; |
|
492 | + if ($this->trycatch) { |
|
493 | + $scriptsrc = 'try{'.$scriptsrc.'}catch(e){}'; |
|
494 | 494 | } |
495 | - $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script ); |
|
496 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscriptsrc ) ) { |
|
495 | + $tmpscriptsrc = apply_filters('autoptimize_js_individual_script', $scriptsrc, $script); |
|
496 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc)) { |
|
497 | 497 | $scriptsrc = $tmpscriptsrc; |
498 | 498 | $this->alreadyminified = true; |
499 | - } elseif ( $this->can_inject_late( $script ) ) { |
|
500 | - $scriptsrc = self::build_injectlater_marker( $script, md5( $scriptsrc ) ); |
|
499 | + } elseif ($this->can_inject_late($script)) { |
|
500 | + $scriptsrc = self::build_injectlater_marker($script, md5($scriptsrc)); |
|
501 | 501 | } |
502 | - $this->jscode .= "\n" . $scriptsrc; |
|
502 | + $this->jscode .= "\n".$scriptsrc; |
|
503 | 503 | } |
504 | 504 | } |
505 | 505 | } |
506 | 506 | |
507 | 507 | // Check for already-minified code. |
508 | - $this->md5hash = md5( $this->jscode ); |
|
509 | - $ccheck = new autoptimizeCache( $this->md5hash, 'js' ); |
|
510 | - if ( $ccheck->check() ) { |
|
508 | + $this->md5hash = md5($this->jscode); |
|
509 | + $ccheck = new autoptimizeCache($this->md5hash, 'js'); |
|
510 | + if ($ccheck->check()) { |
|
511 | 511 | $this->jscode = $ccheck->retrieve(); |
512 | 512 | return true; |
513 | 513 | } |
514 | - unset( $ccheck ); |
|
514 | + unset($ccheck); |
|
515 | 515 | |
516 | 516 | // $this->jscode has all the uncompressed code now. |
517 | - if ( true !== $this->alreadyminified ) { |
|
518 | - if ( apply_filters( 'autoptimize_js_do_minify', true ) ) { |
|
519 | - $tmp_jscode = trim( JSMin::minify( $this->jscode ) ); |
|
520 | - if ( ! empty( $tmp_jscode ) ) { |
|
517 | + if (true !== $this->alreadyminified) { |
|
518 | + if (apply_filters('autoptimize_js_do_minify', true)) { |
|
519 | + $tmp_jscode = trim(JSMin::minify($this->jscode)); |
|
520 | + if (!empty($tmp_jscode)) { |
|
521 | 521 | $this->jscode = $tmp_jscode; |
522 | - unset( $tmp_jscode ); |
|
522 | + unset($tmp_jscode); |
|
523 | 523 | } |
524 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
525 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
524 | + $this->jscode = $this->inject_minified($this->jscode); |
|
525 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
526 | 526 | return true; |
527 | 527 | } else { |
528 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
528 | + $this->jscode = $this->inject_minified($this->jscode); |
|
529 | 529 | return false; |
530 | 530 | } |
531 | 531 | } |
532 | 532 | |
533 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
533 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
534 | 534 | return true; |
535 | 535 | } |
536 | 536 | |
@@ -539,13 +539,13 @@ discard block |
||
539 | 539 | */ |
540 | 540 | public function cache() |
541 | 541 | { |
542 | - $cache = new autoptimizeCache( $this->md5hash, 'js' ); |
|
543 | - if ( ! $cache->check() ) { |
|
542 | + $cache = new autoptimizeCache($this->md5hash, 'js'); |
|
543 | + if (!$cache->check()) { |
|
544 | 544 | // Cache our code. |
545 | - $cache->cache( $this->jscode, 'text/javascript' ); |
|
545 | + $cache->cache($this->jscode, 'text/javascript'); |
|
546 | 546 | } |
547 | - $this->url = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
548 | - $this->url = $this->url_replace_cdn( $this->url ); |
|
547 | + $this->url = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
548 | + $this->url = $this->url_replace_cdn($this->url); |
|
549 | 549 | } |
550 | 550 | |
551 | 551 | /** |
@@ -554,47 +554,47 @@ discard block |
||
554 | 554 | public function getcontent() |
555 | 555 | { |
556 | 556 | // Restore the full content. |
557 | - if ( ! empty( $this->restofcontent ) ) { |
|
557 | + if (!empty($this->restofcontent)) { |
|
558 | 558 | $this->content .= $this->restofcontent; |
559 | 559 | $this->restofcontent = ''; |
560 | 560 | } |
561 | 561 | |
562 | 562 | // Add the scripts taking forcehead/ deferred (default) into account. |
563 | - if ( $this->forcehead ) { |
|
564 | - $replace_tag = array( '</head>', 'before' ); |
|
563 | + if ($this->forcehead) { |
|
564 | + $replace_tag = array('</head>', 'before'); |
|
565 | 565 | $defer = ''; |
566 | 566 | } else { |
567 | - $replace_tag = array( '</body>', 'before' ); |
|
567 | + $replace_tag = array('</body>', 'before'); |
|
568 | 568 | $defer = 'defer '; |
569 | 569 | } |
570 | 570 | |
571 | - $defer = apply_filters( 'autoptimize_filter_js_defer', $defer ); |
|
571 | + $defer = apply_filters('autoptimize_filter_js_defer', $defer); |
|
572 | 572 | $type_js = ''; |
573 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
573 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
574 | 574 | $type_js = 'type="text/javascript" '; |
575 | 575 | } |
576 | 576 | |
577 | - $bodyreplacementpayload = '<script ' . $type_js . $defer . 'src="' . $this->url . '"></script>'; |
|
578 | - $bodyreplacementpayload = apply_filters( 'autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload ); |
|
577 | + $bodyreplacementpayload = '<script '.$type_js.$defer.'src="'.$this->url.'"></script>'; |
|
578 | + $bodyreplacementpayload = apply_filters('autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload); |
|
579 | 579 | |
580 | - $bodyreplacement = implode( '', $this->move['first'] ); |
|
580 | + $bodyreplacement = implode('', $this->move['first']); |
|
581 | 581 | $bodyreplacement .= $bodyreplacementpayload; |
582 | - $bodyreplacement .= implode( '', $this->move['last'] ); |
|
582 | + $bodyreplacement .= implode('', $this->move['last']); |
|
583 | 583 | |
584 | - $replace_tag = apply_filters( 'autoptimize_filter_js_replacetag', $replace_tag ); |
|
584 | + $replace_tag = apply_filters('autoptimize_filter_js_replacetag', $replace_tag); |
|
585 | 585 | |
586 | - if ( strlen( $this->jscode ) > 0 ) { |
|
587 | - $this->inject_in_html( $bodyreplacement, $replace_tag ); |
|
586 | + if (strlen($this->jscode) > 0) { |
|
587 | + $this->inject_in_html($bodyreplacement, $replace_tag); |
|
588 | 588 | } |
589 | 589 | |
590 | 590 | // Restore comments. |
591 | - $this->content = $this->restore_comments( $this->content ); |
|
591 | + $this->content = $this->restore_comments($this->content); |
|
592 | 592 | |
593 | 593 | // Restore IE hacks. |
594 | - $this->content = $this->restore_iehacks( $this->content ); |
|
594 | + $this->content = $this->restore_iehacks($this->content); |
|
595 | 595 | |
596 | 596 | // Restore noptimize. |
597 | - $this->content = $this->restore_noptimize( $this->content ); |
|
597 | + $this->content = $this->restore_noptimize($this->content); |
|
598 | 598 | |
599 | 599 | // Return the modified HTML. |
600 | 600 | return $this->content; |
@@ -605,34 +605,34 @@ discard block |
||
605 | 605 | * |
606 | 606 | * @param string $tag JS tag. |
607 | 607 | */ |
608 | - private function ismergeable( $tag ) |
|
608 | + private function ismergeable($tag) |
|
609 | 609 | { |
610 | - if ( empty( $tag ) || ! $this->aggregate ) { |
|
610 | + if (empty($tag) || !$this->aggregate) { |
|
611 | 611 | return false; |
612 | 612 | } |
613 | 613 | |
614 | - if ( ! empty( $this->whitelist ) ) { |
|
615 | - foreach ( $this->whitelist as $match ) { |
|
616 | - if ( false !== strpos( $tag, $match ) ) { |
|
614 | + if (!empty($this->whitelist)) { |
|
615 | + foreach ($this->whitelist as $match) { |
|
616 | + if (false !== strpos($tag, $match)) { |
|
617 | 617 | return true; |
618 | 618 | } |
619 | 619 | } |
620 | 620 | // No match with whitelist. |
621 | 621 | return false; |
622 | 622 | } else { |
623 | - foreach ( $this->domove as $match ) { |
|
624 | - if ( false !== strpos( $tag, $match ) ) { |
|
623 | + foreach ($this->domove as $match) { |
|
624 | + if (false !== strpos($tag, $match)) { |
|
625 | 625 | // Matched something. |
626 | 626 | return false; |
627 | 627 | } |
628 | 628 | } |
629 | 629 | |
630 | - if ( $this->movetolast( $tag ) ) { |
|
630 | + if ($this->movetolast($tag)) { |
|
631 | 631 | return false; |
632 | 632 | } |
633 | 633 | |
634 | - foreach ( $this->dontmove as $match ) { |
|
635 | - if ( false !== strpos( $tag, $match ) ) { |
|
634 | + foreach ($this->dontmove as $match) { |
|
635 | + if (false !== strpos($tag, $match)) { |
|
636 | 636 | // Matched something. |
637 | 637 | return false; |
638 | 638 | } |
@@ -648,25 +648,25 @@ discard block |
||
648 | 648 | * |
649 | 649 | * @param string $tag tag to check for blacklist (exclusions). |
650 | 650 | */ |
651 | - private function ismovable( $tag ) |
|
651 | + private function ismovable($tag) |
|
652 | 652 | { |
653 | - if ( empty( $tag ) || true !== $this->include_inline || apply_filters( 'autoptimize_filter_js_unmovable', true ) ) { |
|
653 | + if (empty($tag) || true !== $this->include_inline || apply_filters('autoptimize_filter_js_unmovable', true)) { |
|
654 | 654 | return false; |
655 | 655 | } |
656 | 656 | |
657 | - foreach ( $this->domove as $match ) { |
|
658 | - if ( false !== strpos( $tag, $match ) ) { |
|
657 | + foreach ($this->domove as $match) { |
|
658 | + if (false !== strpos($tag, $match)) { |
|
659 | 659 | // Matched something. |
660 | 660 | return true; |
661 | 661 | } |
662 | 662 | } |
663 | 663 | |
664 | - if ( $this->movetolast( $tag ) ) { |
|
664 | + if ($this->movetolast($tag)) { |
|
665 | 665 | return true; |
666 | 666 | } |
667 | 667 | |
668 | - foreach ( $this->dontmove as $match ) { |
|
669 | - if ( false !== strpos( $tag, $match ) ) { |
|
668 | + foreach ($this->dontmove as $match) { |
|
669 | + if (false !== strpos($tag, $match)) { |
|
670 | 670 | // Matched something. |
671 | 671 | return false; |
672 | 672 | } |
@@ -676,14 +676,14 @@ discard block |
||
676 | 676 | return true; |
677 | 677 | } |
678 | 678 | |
679 | - private function movetolast( $tag ) |
|
679 | + private function movetolast($tag) |
|
680 | 680 | { |
681 | - if ( empty( $tag ) ) { |
|
681 | + if (empty($tag)) { |
|
682 | 682 | return false; |
683 | 683 | } |
684 | 684 | |
685 | - foreach ( $this->domovelast as $match ) { |
|
686 | - if ( false !== strpos( $tag, $match ) ) { |
|
685 | + foreach ($this->domovelast as $match) { |
|
686 | + if (false !== strpos($tag, $match)) { |
|
687 | 687 | // Matched, return true. |
688 | 688 | return true; |
689 | 689 | } |
@@ -703,12 +703,12 @@ discard block |
||
703 | 703 | * @param string $js_path Path to JS file. |
704 | 704 | * @return bool |
705 | 705 | */ |
706 | - private function can_inject_late( $js_path ) { |
|
707 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
708 | - if ( true !== $this->inject_min_late ) { |
|
706 | + private function can_inject_late($js_path) { |
|
707 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
708 | + if (true !== $this->inject_min_late) { |
|
709 | 709 | // late-inject turned off. |
710 | 710 | return false; |
711 | - } elseif ( ( false === strpos( $js_path, 'min.js' ) ) && ( false === strpos( $js_path, 'wp-includes/js/jquery/jquery.js' ) ) && ( str_replace( $consider_minified_array, '', $js_path ) === $js_path ) ) { |
|
711 | + } elseif ((false === strpos($js_path, 'min.js')) && (false === strpos($js_path, 'wp-includes/js/jquery/jquery.js')) && (str_replace($consider_minified_array, '', $js_path) === $js_path)) { |
|
712 | 712 | // file not minified based on filename & filter. |
713 | 713 | return false; |
714 | 714 | } else { |
@@ -735,32 +735,32 @@ discard block |
||
735 | 735 | * |
736 | 736 | * @return bool|string Url pointing to the minified js file or false. |
737 | 737 | */ |
738 | - public function minify_single( $filepath, $cache_miss = false ) |
|
738 | + public function minify_single($filepath, $cache_miss = false) |
|
739 | 739 | { |
740 | - $contents = $this->prepare_minify_single( $filepath ); |
|
740 | + $contents = $this->prepare_minify_single($filepath); |
|
741 | 741 | |
742 | - if ( empty( $contents ) ) { |
|
742 | + if (empty($contents)) { |
|
743 | 743 | return false; |
744 | 744 | } |
745 | 745 | |
746 | 746 | // Check cache. |
747 | - $hash = 'single_' . md5( $contents ); |
|
748 | - $cache = new autoptimizeCache( $hash, 'js' ); |
|
747 | + $hash = 'single_'.md5($contents); |
|
748 | + $cache = new autoptimizeCache($hash, 'js'); |
|
749 | 749 | |
750 | 750 | // If not in cache already, minify... |
751 | - if ( ! $cache->check() || $cache_miss ) { |
|
752 | - $contents = trim( JSMin::minify( $contents ) ); |
|
751 | + if (!$cache->check() || $cache_miss) { |
|
752 | + $contents = trim(JSMin::minify($contents)); |
|
753 | 753 | |
754 | 754 | // Check if minified cache content is empty. |
755 | - if ( empty( $contents ) ) { |
|
755 | + if (empty($contents)) { |
|
756 | 756 | return false; |
757 | 757 | } |
758 | 758 | |
759 | 759 | // Store in cache. |
760 | - $cache->cache( $contents, 'text/javascript' ); |
|
760 | + $cache->cache($contents, 'text/javascript'); |
|
761 | 761 | } |
762 | 762 | |
763 | - $url = $this->build_minify_single_url( $cache ); |
|
763 | + $url = $this->build_minify_single_url($cache); |
|
764 | 764 | |
765 | 765 | return $url; |
766 | 766 | } |
@@ -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 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 | * Critical CSS base file (initializes all ccss files). |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -19,27 +19,27 @@ discard block |
||
19 | 19 | public function __construct() |
20 | 20 | { |
21 | 21 | // define constant, but only once. |
22 | - if ( ! defined( 'AO_CCSS_DIR' ) ) { |
|
22 | + if (!defined('AO_CCSS_DIR')) { |
|
23 | 23 | // Define plugin version. |
24 | - define( 'AO_CCSS_VER', 'AO_' . AUTOPTIMIZE_PLUGIN_VERSION ); |
|
24 | + define('AO_CCSS_VER', 'AO_'.AUTOPTIMIZE_PLUGIN_VERSION); |
|
25 | 25 | |
26 | 26 | // Define a constant with the directory to store critical CSS in. |
27 | - if ( is_multisite() ) { |
|
27 | + if (is_multisite()) { |
|
28 | 28 | $blog_id = get_current_blog_id(); |
29 | - define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' . $blog_id . '/' ); |
|
29 | + define('AO_CCSS_DIR', WP_CONTENT_DIR.'/uploads/ao_ccss/'.$blog_id.'/'); |
|
30 | 30 | } else { |
31 | - define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' ); |
|
31 | + define('AO_CCSS_DIR', WP_CONTENT_DIR.'/uploads/ao_ccss/'); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | // Define support files locations. |
35 | - define( 'AO_CCSS_LOCK', AO_CCSS_DIR . 'queue.lock' ); |
|
36 | - define( 'AO_CCSS_LOG', AO_CCSS_DIR . 'queuelog.html' ); |
|
37 | - define( 'AO_CCSS_DEBUG', AO_CCSS_DIR . 'queue.json' ); |
|
35 | + define('AO_CCSS_LOCK', AO_CCSS_DIR.'queue.lock'); |
|
36 | + define('AO_CCSS_LOG', AO_CCSS_DIR.'queuelog.html'); |
|
37 | + define('AO_CCSS_DEBUG', AO_CCSS_DIR.'queue.json'); |
|
38 | 38 | |
39 | 39 | // Define constants for criticalcss.com base path and API endpoints. |
40 | 40 | // fixme: AO_CCSS_URL should be read from the autoptimize availability json stored as option. |
41 | - define( 'AO_CCSS_URL', 'https://criticalcss.com' ); |
|
42 | - define( 'AO_CCSS_API', AO_CCSS_URL . '/api/premium/' ); |
|
41 | + define('AO_CCSS_URL', 'https://criticalcss.com'); |
|
42 | + define('AO_CCSS_API', AO_CCSS_URL.'/api/premium/'); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | $this->filepath = __FILE__; |
@@ -52,19 +52,19 @@ discard block |
||
52 | 52 | { |
53 | 53 | // get all options. |
54 | 54 | $all_options = $this->fetch_options(); |
55 | - foreach ( $all_options as $option => $value ) { |
|
55 | + foreach ($all_options as $option => $value) { |
|
56 | 56 | ${$option} = $value; |
57 | 57 | } |
58 | 58 | |
59 | 59 | // make sure the 10 minutes cron schedule is added. |
60 | - add_filter( 'cron_schedules', array( $this, 'ao_ccss_interval' ) ); |
|
60 | + add_filter('cron_schedules', array($this, 'ao_ccss_interval')); |
|
61 | 61 | |
62 | 62 | // check if we need to upgrade. |
63 | 63 | $this->check_upgrade(); |
64 | 64 | |
65 | 65 | // make sure ao_ccss_queue is scheduled OK if an API key is set. |
66 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
67 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
66 | + if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_queue')) { |
|
67 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
68 | 68 | } |
69 | 69 | } |
70 | 70 | |
@@ -72,12 +72,12 @@ discard block |
||
72 | 72 | // Required libs, core is always needed. |
73 | 73 | $criticalcss_core = new autoptimizeCriticalCSSCore(); |
74 | 74 | |
75 | - if ( defined( 'DOING_CRON' ) || is_admin() ) { |
|
75 | + if (defined('DOING_CRON') || is_admin()) { |
|
76 | 76 | // TODO: also include if overridden somehow to force queue processing to be executed? |
77 | 77 | $criticalcss_cron = new autoptimizeCriticalCSSCron(); |
78 | 78 | } |
79 | 79 | |
80 | - if ( is_admin() ) { |
|
80 | + if (is_admin()) { |
|
81 | 81 | $criticalcss_settings = new autoptimizeCriticalCSSSettings(); |
82 | 82 | } else { |
83 | 83 | // enqueuing only done when not wp-admin. |
@@ -87,48 +87,48 @@ discard block |
||
87 | 87 | |
88 | 88 | public static function fetch_options() { |
89 | 89 | // Get options. |
90 | - $autoptimize_ccss_options['ao_css_defer'] = get_option( 'autoptimize_css_defer' ); |
|
91 | - $autoptimize_ccss_options['ao_css_defer_inline'] = get_option( 'autoptimize_css_defer_inline' ); |
|
92 | - $autoptimize_ccss_options['ao_ccss_rules_raw'] = get_option( 'autoptimize_ccss_rules', false ); |
|
93 | - $autoptimize_ccss_options['ao_ccss_additional'] = get_option( 'autoptimize_ccss_additional' ); |
|
94 | - $autoptimize_ccss_options['ao_ccss_queue_raw'] = get_option( 'autoptimize_ccss_queue', false ); |
|
95 | - $autoptimize_ccss_options['ao_ccss_viewport'] = get_option( 'autoptimize_ccss_viewport', false ); |
|
96 | - $autoptimize_ccss_options['ao_ccss_finclude'] = get_option( 'autoptimize_ccss_finclude', false ); |
|
97 | - $autoptimize_ccss_options['ao_ccss_rlimit'] = get_option( 'autoptimize_ccss_rlimit', '5' ); |
|
98 | - $autoptimize_ccss_options['ao_ccss_noptimize'] = get_option( 'autoptimize_ccss_noptimize', false ); |
|
99 | - $autoptimize_ccss_options['ao_ccss_debug'] = get_option( 'autoptimize_ccss_debug', false ); |
|
100 | - $autoptimize_ccss_options['ao_ccss_key'] = get_option( 'autoptimize_ccss_key' ); |
|
101 | - $autoptimize_ccss_options['ao_ccss_keyst'] = get_option( 'autoptimize_ccss_keyst' ); |
|
102 | - $autoptimize_ccss_options['ao_ccss_loggedin'] = get_option( 'autoptimize_ccss_loggedin', '1' ); |
|
103 | - $autoptimize_ccss_options['ao_ccss_forcepath'] = get_option( 'autoptimize_ccss_forcepath', '1' ); |
|
104 | - $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option( 'autoptimize_service_availablity' ); |
|
105 | - $autoptimize_ccss_options['ao_ccss_deferjquery'] = get_option( 'autoptimize_ccss_deferjquery', false ); |
|
106 | - $autoptimize_ccss_options['ao_ccss_domain'] = get_option( 'autoptimize_ccss_domain' ); |
|
107 | - |
|
108 | - if ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) === false && strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'uggc' ) === 0 ) { |
|
109 | - $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ); |
|
110 | - } elseif ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) !== false ) { |
|
90 | + $autoptimize_ccss_options['ao_css_defer'] = get_option('autoptimize_css_defer'); |
|
91 | + $autoptimize_ccss_options['ao_css_defer_inline'] = get_option('autoptimize_css_defer_inline'); |
|
92 | + $autoptimize_ccss_options['ao_ccss_rules_raw'] = get_option('autoptimize_ccss_rules', false); |
|
93 | + $autoptimize_ccss_options['ao_ccss_additional'] = get_option('autoptimize_ccss_additional'); |
|
94 | + $autoptimize_ccss_options['ao_ccss_queue_raw'] = get_option('autoptimize_ccss_queue', false); |
|
95 | + $autoptimize_ccss_options['ao_ccss_viewport'] = get_option('autoptimize_ccss_viewport', false); |
|
96 | + $autoptimize_ccss_options['ao_ccss_finclude'] = get_option('autoptimize_ccss_finclude', false); |
|
97 | + $autoptimize_ccss_options['ao_ccss_rlimit'] = get_option('autoptimize_ccss_rlimit', '5'); |
|
98 | + $autoptimize_ccss_options['ao_ccss_noptimize'] = get_option('autoptimize_ccss_noptimize', false); |
|
99 | + $autoptimize_ccss_options['ao_ccss_debug'] = get_option('autoptimize_ccss_debug', false); |
|
100 | + $autoptimize_ccss_options['ao_ccss_key'] = get_option('autoptimize_ccss_key'); |
|
101 | + $autoptimize_ccss_options['ao_ccss_keyst'] = get_option('autoptimize_ccss_keyst'); |
|
102 | + $autoptimize_ccss_options['ao_ccss_loggedin'] = get_option('autoptimize_ccss_loggedin', '1'); |
|
103 | + $autoptimize_ccss_options['ao_ccss_forcepath'] = get_option('autoptimize_ccss_forcepath', '1'); |
|
104 | + $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option('autoptimize_service_availablity'); |
|
105 | + $autoptimize_ccss_options['ao_ccss_deferjquery'] = get_option('autoptimize_ccss_deferjquery', false); |
|
106 | + $autoptimize_ccss_options['ao_ccss_domain'] = get_option('autoptimize_ccss_domain'); |
|
107 | + |
|
108 | + if (strpos($autoptimize_ccss_options['ao_ccss_domain'], 'http') === false && strpos($autoptimize_ccss_options['ao_ccss_domain'], 'uggc') === 0) { |
|
109 | + $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13($autoptimize_ccss_options['ao_ccss_domain']); |
|
110 | + } elseif (strpos($autoptimize_ccss_options['ao_ccss_domain'], 'http') !== false) { |
|
111 | 111 | // not rot13'ed yet, do so now (goal; avoid migration plugins change the bound domain). |
112 | - update_option( 'autoptimize_ccss_domain', str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ) ); |
|
112 | + update_option('autoptimize_ccss_domain', str_rot13($autoptimize_ccss_options['ao_ccss_domain'])); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | // Setup the rules array. |
116 | - if ( empty( $autoptimize_ccss_options['ao_ccss_rules_raw'] ) ) { |
|
116 | + if (empty($autoptimize_ccss_options['ao_ccss_rules_raw'])) { |
|
117 | 117 | $autoptimize_ccss_options['ao_ccss_rules']['paths'] = array(); |
118 | 118 | $autoptimize_ccss_options['ao_ccss_rules']['types'] = array(); |
119 | 119 | } else { |
120 | - $autoptimize_ccss_options['ao_ccss_rules'] = json_decode( $autoptimize_ccss_options['ao_ccss_rules_raw'], true ); |
|
120 | + $autoptimize_ccss_options['ao_ccss_rules'] = json_decode($autoptimize_ccss_options['ao_ccss_rules_raw'], true); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | // Setup the queue array. |
124 | - if ( empty( $autoptimize_ccss_options['ao_ccss_queue_raw'] ) ) { |
|
124 | + if (empty($autoptimize_ccss_options['ao_ccss_queue_raw'])) { |
|
125 | 125 | $autoptimize_ccss_options['ao_ccss_queue'] = array(); |
126 | 126 | } else { |
127 | - $autoptimize_ccss_options['ao_ccss_queue'] = json_decode( $autoptimize_ccss_options['ao_ccss_queue_raw'], true ); |
|
127 | + $autoptimize_ccss_options['ao_ccss_queue'] = json_decode($autoptimize_ccss_options['ao_ccss_queue_raw'], true); |
|
128 | 128 | } |
129 | 129 | |
130 | 130 | // Override API key if constant is defined. |
131 | - if ( defined( 'AUTOPTIMIZE_CRITICALCSS_API_KEY' ) ) { |
|
131 | + if (defined('AUTOPTIMIZE_CRITICALCSS_API_KEY')) { |
|
132 | 132 | $autoptimize_ccss_options['ao_ccss_key'] = AUTOPTIMIZE_CRITICALCSS_API_KEY; |
133 | 133 | } |
134 | 134 | |
@@ -139,49 +139,49 @@ discard block |
||
139 | 139 | global $ao_ccss_key; |
140 | 140 | |
141 | 141 | // Create the cache directory if it doesn't exist already. |
142 | - if ( ! file_exists( AO_CCSS_DIR ) ) { |
|
143 | - mkdir( AO_CCSS_DIR, 0755, true ); |
|
142 | + if (!file_exists(AO_CCSS_DIR)) { |
|
143 | + mkdir(AO_CCSS_DIR, 0755, true); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | // Create a scheduled event for the queue. |
147 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
148 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
147 | + if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_queue')) { |
|
148 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | // Create a scheduled event for log maintenance. |
152 | - if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_maintenance' ) ) { |
|
153 | - wp_schedule_event( time(), 'twicedaily', 'ao_ccss_maintenance' ); |
|
152 | + if (isset($ao_ccss_key) && !empty($ao_ccss_key) && !wp_next_scheduled('ao_ccss_maintenance')) { |
|
153 | + wp_schedule_event(time(), 'twicedaily', 'ao_ccss_maintenance'); |
|
154 | 154 | } |
155 | 155 | } |
156 | 156 | |
157 | 157 | public function check_upgrade() { |
158 | - $db_version = get_option( 'autoptimize_ccss_version', '' ); |
|
159 | - if ( AO_CCSS_VER !== $db_version ) { |
|
158 | + $db_version = get_option('autoptimize_ccss_version', ''); |
|
159 | + if (AO_CCSS_VER !== $db_version) { |
|
160 | 160 | // check schedules & re-schedule if needed. |
161 | 161 | $this->on_upgrade(); |
162 | 162 | // and update db_version. |
163 | - update_option( 'autoptimize_ccss_version', AO_CCSS_VER ); |
|
163 | + update_option('autoptimize_ccss_version', AO_CCSS_VER); |
|
164 | 164 | } |
165 | 165 | } |
166 | 166 | |
167 | - public function ao_ccss_interval( $schedules ) { |
|
167 | + public function ao_ccss_interval($schedules) { |
|
168 | 168 | // Let interval be configurable. |
169 | - if ( ! defined( 'AO_CCSS_DEBUG_INTERVAL' ) ) { |
|
169 | + if (!defined('AO_CCSS_DEBUG_INTERVAL')) { |
|
170 | 170 | $intsec = 600; |
171 | 171 | } else { |
172 | 172 | $intsec = AO_CCSS_DEBUG_INTERVAL; |
173 | - if ( $intsec >= 120 ) { |
|
174 | - $inttxt = $intsec / 60 . ' minutes'; |
|
173 | + if ($intsec >= 120) { |
|
174 | + $inttxt = $intsec/60.' minutes'; |
|
175 | 175 | } else { |
176 | - $inttxt = $intsec . ' second(s)'; |
|
176 | + $inttxt = $intsec.' second(s)'; |
|
177 | 177 | } |
178 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Using custom WP-Cron interval of ' . $inttxt, 3 ); |
|
178 | + autoptimizeCriticalCSSCore::ao_ccss_log('Using custom WP-Cron interval of '.$inttxt, 3); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | // Attach interval to schedule. |
182 | 182 | $schedules['ao_ccss'] = array( |
183 | 183 | 'interval' => $intsec, |
184 | - 'display' => __( 'Autoptimize CriticalCSS' ), |
|
184 | + 'display' => __('Autoptimize CriticalCSS'), |
|
185 | 185 | ); |
186 | 186 | return $schedules; |
187 | 187 | } |