@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Handles disk-cache-related operations. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -38,22 +38,22 @@ discard block |
||
38 | 38 | * @param string $md5 Hash. |
39 | 39 | * @param string $ext Extension. |
40 | 40 | */ |
41 | - public function __construct( $md5, $ext = 'php' ) |
|
41 | + public function __construct($md5, $ext = 'php') |
|
42 | 42 | { |
43 | 43 | $_min_ext = ''; |
44 | - if ( apply_filters( 'autoptimize_filter_cache_url_add_min_ext', false ) ) { |
|
44 | + if (apply_filters('autoptimize_filter_cache_url_add_min_ext', false)) { |
|
45 | 45 | $_min_ext = '.min'; |
46 | 46 | } |
47 | 47 | |
48 | 48 | $this->cachedir = AUTOPTIMIZE_CACHE_DIR; |
49 | 49 | $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP; |
50 | - if ( ! $this->nogzip ) { |
|
51 | - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.php'; |
|
50 | + if (!$this->nogzip) { |
|
51 | + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.$_min_ext.'.php'; |
|
52 | 52 | } else { |
53 | - if ( in_array( $ext, array( 'js', 'css' ) ) ) { |
|
54 | - $this->filename = $ext . '/' . AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext; |
|
53 | + if (in_array($ext, array('js', 'css'))) { |
|
54 | + $this->filename = $ext.'/'.AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.$_min_ext.'.'.$ext; |
|
55 | 55 | } else { |
56 | - $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . $_min_ext . '.' . $ext; |
|
56 | + $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.$_min_ext.'.'.$ext; |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function check() |
67 | 67 | { |
68 | - return file_exists( $this->cachedir . $this->filename ); |
|
68 | + return file_exists($this->cachedir.$this->filename); |
|
69 | 69 | } |
70 | 70 | |
71 | 71 | /** |
@@ -75,11 +75,11 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function retrieve() |
77 | 77 | { |
78 | - if ( $this->check() ) { |
|
79 | - if ( false == $this->nogzip ) { |
|
80 | - return file_get_contents( $this->cachedir . $this->filename . '.none' ); |
|
78 | + if ($this->check()) { |
|
79 | + if (false == $this->nogzip) { |
|
80 | + return file_get_contents($this->cachedir.$this->filename.'.none'); |
|
81 | 81 | } else { |
82 | - return file_get_contents( $this->cachedir . $this->filename ); |
|
82 | + return file_get_contents($this->cachedir.$this->filename); |
|
83 | 83 | } |
84 | 84 | } |
85 | 85 | return false; |
@@ -93,50 +93,50 @@ discard block |
||
93 | 93 | * |
94 | 94 | * @return void |
95 | 95 | */ |
96 | - public function cache( $data, $mime ) |
|
96 | + public function cache($data, $mime) |
|
97 | 97 | { |
98 | 98 | // off by default; check if cachedirs exist every time before caching |
99 | 99 | // |
100 | 100 | // to be activated for users that experience these ugly errors; |
101 | 101 | // PHP Warning: file_put_contents failed to open stream: No such file or directory. |
102 | - if ( apply_filters( 'autoptimize_filter_cache_checkdirs_on_write', false ) ) { |
|
102 | + if (apply_filters('autoptimize_filter_cache_checkdirs_on_write', false)) { |
|
103 | 103 | $this->check_and_create_dirs(); |
104 | 104 | } |
105 | 105 | |
106 | - if ( false === $this->nogzip ) { |
|
106 | + if (false === $this->nogzip) { |
|
107 | 107 | // We handle gzipping ourselves. |
108 | 108 | $file = 'default.php'; |
109 | - $phpcode = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $file ); |
|
110 | - $phpcode = str_replace( array( '%%CONTENT%%', 'exit;' ), array( $mime, '' ), $phpcode ); |
|
109 | + $phpcode = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'config/'.$file); |
|
110 | + $phpcode = str_replace(array('%%CONTENT%%', 'exit;'), array($mime, ''), $phpcode); |
|
111 | 111 | |
112 | - file_put_contents( $this->cachedir . $this->filename, $phpcode ); |
|
113 | - file_put_contents( $this->cachedir . $this->filename . '.none', $data ); |
|
112 | + file_put_contents($this->cachedir.$this->filename, $phpcode); |
|
113 | + file_put_contents($this->cachedir.$this->filename.'.none', $data); |
|
114 | 114 | } else { |
115 | 115 | // Write code to cache without doing anything else. |
116 | - file_put_contents( $this->cachedir . $this->filename, $data ); |
|
116 | + file_put_contents($this->cachedir.$this->filename, $data); |
|
117 | 117 | |
118 | 118 | // save fallback .js or .css file if filter true (to be false by default) but not if snippet or single. |
119 | - if ( self::do_fallback() && strpos( $this->filename, '_snippet_' ) === false && strpos( $this->filename, '_single_' ) === false ) { |
|
120 | - $_extension = pathinfo( $this->filename, PATHINFO_EXTENSION ); |
|
121 | - $_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX . 'fallback.' . $_extension; |
|
122 | - if ( ( 'css' === $_extension || 'js' === $_extension ) && ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) { |
|
123 | - file_put_contents( $this->cachedir . $_extension . '/' . $_fallback_file, $data ); |
|
119 | + if (self::do_fallback() && strpos($this->filename, '_snippet_') === false && strpos($this->filename, '_single_') === false) { |
|
120 | + $_extension = pathinfo($this->filename, PATHINFO_EXTENSION); |
|
121 | + $_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX.'fallback.'.$_extension; |
|
122 | + if (('css' === $_extension || 'js' === $_extension) && !file_exists($this->cachedir.$_extension.'/'.$_fallback_file)) { |
|
123 | + file_put_contents($this->cachedir.$_extension.'/'.$_fallback_file, $data); |
|
124 | 124 | } |
125 | 125 | } |
126 | 126 | |
127 | - if ( apply_filters( 'autoptimize_filter_cache_create_static_gzip', false ) ) { |
|
127 | + if (apply_filters('autoptimize_filter_cache_create_static_gzip', false)) { |
|
128 | 128 | // Create an additional cached gzip file. |
129 | - file_put_contents( $this->cachedir . $this->filename . '.gz', gzencode( $data, 9, FORCE_GZIP ) ); |
|
129 | + file_put_contents($this->cachedir.$this->filename.'.gz', gzencode($data, 9, FORCE_GZIP)); |
|
130 | 130 | // If PHP Brotli extension is installed, create an additional cached Brotli file. |
131 | - if ( function_exists( 'brotli_compress' ) ) { |
|
132 | - file_put_contents( $this->cachedir . $this->filename . '.br', brotli_compress( $data, 11, BROTLI_GENERIC ) ); |
|
131 | + if (function_exists('brotli_compress')) { |
|
132 | + file_put_contents($this->cachedir.$this->filename.'.br', brotli_compress($data, 11, BROTLI_GENERIC)); |
|
133 | 133 | } |
134 | 134 | } |
135 | 135 | } |
136 | 136 | |
137 | 137 | // Provide 3rd party action hook for every cache file that is created. |
138 | 138 | // This hook can for example be used to inject a copy of the created cache file to a other domain. |
139 | - do_action( 'autoptimize_action_cache_file_created', $this->cachedir . $this->filename ); |
|
139 | + do_action('autoptimize_action_cache_file_created', $this->cachedir.$this->filename); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | // The original idea here was to provide 3rd party code a hook so that |
152 | 152 | // it can "listen" to all the complete autoptimized-urls that the page |
153 | 153 | // will emit... Or something to that effect I think? |
154 | - apply_filters( 'autoptimize_filter_cache_getname', AUTOPTIMIZE_CACHE_URL . $this->filename ); |
|
154 | + apply_filters('autoptimize_filter_cache_getname', AUTOPTIMIZE_CACHE_URL.$this->filename); |
|
155 | 155 | |
156 | 156 | return $this->filename; |
157 | 157 | } |
@@ -164,11 +164,11 @@ discard block |
||
164 | 164 | * @param string $file Filename. |
165 | 165 | * @return bool |
166 | 166 | */ |
167 | - protected static function is_valid_cache_file( $dir, $file ) |
|
167 | + protected static function is_valid_cache_file($dir, $file) |
|
168 | 168 | { |
169 | - if ( '.' !== $file && '..' !== $file && |
|
170 | - false !== strpos( $file, AUTOPTIMIZE_CACHEFILE_PREFIX ) && |
|
171 | - is_file( $dir . $file ) ) { |
|
169 | + if ('.' !== $file && '..' !== $file && |
|
170 | + false !== strpos($file, AUTOPTIMIZE_CACHEFILE_PREFIX) && |
|
171 | + is_file($dir.$file)) { |
|
172 | 172 | |
173 | 173 | // It's a valid file! |
174 | 174 | return true; |
@@ -186,16 +186,16 @@ discard block |
||
186 | 186 | protected static function clear_cache_classic() |
187 | 187 | { |
188 | 188 | $contents = self::get_cache_contents(); |
189 | - foreach ( $contents as $name => $files ) { |
|
190 | - $dir = rtrim( AUTOPTIMIZE_CACHE_DIR . $name, '/' ) . '/'; |
|
191 | - foreach ( $files as $file ) { |
|
192 | - if ( self::is_valid_cache_file( $dir, $file ) ) { |
|
193 | - @unlink( $dir . $file ); // @codingStandardsIgnoreLine |
|
189 | + foreach ($contents as $name => $files) { |
|
190 | + $dir = rtrim(AUTOPTIMIZE_CACHE_DIR.$name, '/').'/'; |
|
191 | + foreach ($files as $file) { |
|
192 | + if (self::is_valid_cache_file($dir, $file)) { |
|
193 | + @unlink($dir.$file); // @codingStandardsIgnoreLine |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | } |
197 | 197 | |
198 | - @unlink( AUTOPTIMIZE_CACHE_DIR . '/.htaccess' ); // @codingStandardsIgnoreLine |
|
198 | + @unlink(AUTOPTIMIZE_CACHE_DIR.'/.htaccess'); // @codingStandardsIgnoreLine |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
@@ -206,19 +206,19 @@ discard block |
||
206 | 206 | * |
207 | 207 | * @return bool |
208 | 208 | */ |
209 | - protected static function rmdir( $pathname ) |
|
209 | + protected static function rmdir($pathname) |
|
210 | 210 | { |
211 | - $files = self::get_dir_contents( $pathname ); |
|
212 | - foreach ( $files as $file ) { |
|
213 | - $path = $pathname . '/' . $file; |
|
214 | - if ( is_dir( $path ) ) { |
|
215 | - self::rmdir( $path ); |
|
211 | + $files = self::get_dir_contents($pathname); |
|
212 | + foreach ($files as $file) { |
|
213 | + $path = $pathname.'/'.$file; |
|
214 | + if (is_dir($path)) { |
|
215 | + self::rmdir($path); |
|
216 | 216 | } else { |
217 | - unlink( $path ); |
|
217 | + unlink($path); |
|
218 | 218 | } |
219 | 219 | } |
220 | 220 | |
221 | - return rmdir( $pathname ); |
|
221 | + return rmdir($pathname); |
|
222 | 222 | } |
223 | 223 | |
224 | 224 | /** |
@@ -239,12 +239,12 @@ discard block |
||
239 | 239 | $new_name = self::get_unique_name(); |
240 | 240 | |
241 | 241 | // Makes sure the new pathname is on the same level... |
242 | - $new_pathname = dirname( $dir ) . '/' . $new_name; |
|
243 | - $renamed = @rename( $dir, $new_pathname ); // @codingStandardsIgnoreLine |
|
242 | + $new_pathname = dirname($dir).'/'.$new_name; |
|
243 | + $renamed = @rename($dir, $new_pathname); // @codingStandardsIgnoreLine |
|
244 | 244 | |
245 | 245 | // When renamed, re-create the default cache directory back so it's |
246 | 246 | // available again... |
247 | - if ( $renamed ) { |
|
247 | + if ($renamed) { |
|
248 | 248 | $ok = self::cacheavail(); |
249 | 249 | } |
250 | 250 | |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | */ |
259 | 259 | public static function advanced_cache_clear_enabled() |
260 | 260 | { |
261 | - return apply_filters( 'autoptimize_filter_cache_clear_advanced', false ); |
|
261 | + return apply_filters('autoptimize_filter_cache_clear_advanced', false); |
|
262 | 262 | } |
263 | 263 | |
264 | 264 | /** |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | protected static function get_unique_name() |
270 | 270 | { |
271 | 271 | $prefix = self::get_advanced_cache_clear_prefix(); |
272 | - $new_name = uniqid( $prefix, true ); |
|
272 | + $new_name = uniqid($prefix, true); |
|
273 | 273 | |
274 | 274 | return $new_name; |
275 | 275 | } |
@@ -282,8 +282,8 @@ discard block |
||
282 | 282 | protected static function get_advanced_cache_clear_prefix() |
283 | 283 | { |
284 | 284 | $pathname = self::get_pathname_base(); |
285 | - $basename = basename( $pathname ); |
|
286 | - $prefix = $basename . '-artifact-'; |
|
285 | + $basename = basename($pathname); |
|
286 | + $prefix = $basename.'-artifact-'; |
|
287 | 287 | |
288 | 288 | return $prefix; |
289 | 289 | } |
@@ -296,9 +296,9 @@ discard block |
||
296 | 296 | * |
297 | 297 | * @return array |
298 | 298 | */ |
299 | - protected static function get_dir_contents( $pathname ) |
|
299 | + protected static function get_dir_contents($pathname) |
|
300 | 300 | { |
301 | - return array_slice( scandir( $pathname ), 2 ); |
|
301 | + return array_slice(scandir($pathname), 2); |
|
302 | 302 | } |
303 | 303 | |
304 | 304 | /** |
@@ -311,24 +311,24 @@ discard block |
||
311 | 311 | public static function delete_advanced_cache_clear_artifacts() |
312 | 312 | { |
313 | 313 | // Don't go through these motions (called from the cachechecker) if advanced cache clear isn't even active. |
314 | - if ( ! self::advanced_cache_clear_enabled() ) { |
|
314 | + if (!self::advanced_cache_clear_enabled()) { |
|
315 | 315 | return false; |
316 | 316 | } |
317 | 317 | |
318 | 318 | $dir = self::get_pathname_base(); |
319 | 319 | $prefix = self::get_advanced_cache_clear_prefix(); |
320 | - $parent = dirname( $dir ); |
|
320 | + $parent = dirname($dir); |
|
321 | 321 | $ok = false; |
322 | 322 | |
323 | 323 | // Returns the list of files without '.' and '..' elements. |
324 | - $files = self::get_dir_contents( $parent ); |
|
325 | - if ( is_array( $files ) && ! empty( $files ) ) { |
|
326 | - foreach ( $files as $file ) { |
|
327 | - $path = $parent . '/' . $file; |
|
328 | - $prefixed = ( false !== strpos( $path, $prefix ) ); |
|
324 | + $files = self::get_dir_contents($parent); |
|
325 | + if (is_array($files) && !empty($files)) { |
|
326 | + foreach ($files as $file) { |
|
327 | + $path = $parent.'/'.$file; |
|
328 | + $prefixed = (false !== strpos($path, $prefix)); |
|
329 | 329 | // Removing only our own (prefixed) directories... |
330 | - if ( is_dir( $path ) && $prefixed ) { |
|
331 | - $ok = self::rmdir( $path ); |
|
330 | + if (is_dir($path) && $prefixed) { |
|
331 | + $ok = self::rmdir($path); |
|
332 | 332 | } |
333 | 333 | } |
334 | 334 | } |
@@ -348,9 +348,9 @@ discard block |
||
348 | 348 | { |
349 | 349 | $pathname = self::get_pathname_base(); |
350 | 350 | |
351 | - if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches', true ) ) { |
|
351 | + if (is_multisite() && apply_filters('autoptimize_separate_blog_caches', true)) { |
|
352 | 352 | $blog_id = get_current_blog_id(); |
353 | - $pathname .= $blog_id . '/'; |
|
353 | + $pathname .= $blog_id.'/'; |
|
354 | 354 | } |
355 | 355 | |
356 | 356 | return $pathname; |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | */ |
364 | 364 | protected static function get_pathname_base() |
365 | 365 | { |
366 | - $pathname = WP_CONTENT_DIR . AUTOPTIMIZE_CACHE_CHILD_DIR; |
|
366 | + $pathname = WP_CONTENT_DIR.AUTOPTIMIZE_CACHE_CHILD_DIR; |
|
367 | 367 | |
368 | 368 | return $pathname; |
369 | 369 | } |
@@ -375,46 +375,46 @@ discard block |
||
375 | 375 | * |
376 | 376 | * @return bool |
377 | 377 | */ |
378 | - public static function clearall( $propagate = true ) |
|
378 | + public static function clearall($propagate = true) |
|
379 | 379 | { |
380 | - if ( ! self::cacheavail() ) { |
|
380 | + if (!self::cacheavail()) { |
|
381 | 381 | return false; |
382 | 382 | } |
383 | 383 | |
384 | 384 | // TODO/FIXME: If cache is big, switch to advanced/new cache clearing automatically? |
385 | - if ( self::advanced_cache_clear_enabled() ) { |
|
385 | + if (self::advanced_cache_clear_enabled()) { |
|
386 | 386 | self::clear_cache_via_rename(); |
387 | 387 | } else { |
388 | 388 | self::clear_cache_classic(); |
389 | 389 | } |
390 | 390 | |
391 | 391 | // Remove 404 handler if required. |
392 | - if ( self::do_fallback() ) { |
|
393 | - $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . 'autoptimize_404_handler.php'; |
|
394 | - @unlink( $_fallback_php ); // @codingStandardsIgnoreLine |
|
392 | + if (self::do_fallback()) { |
|
393 | + $_fallback_php = trailingslashit(WP_CONTENT_DIR).'autoptimize_404_handler.php'; |
|
394 | + @unlink($_fallback_php); // @codingStandardsIgnoreLine |
|
395 | 395 | } |
396 | 396 | |
397 | 397 | // Remove the transient so it gets regenerated... |
398 | - delete_transient( 'autoptimize_stats' ); |
|
398 | + delete_transient('autoptimize_stats'); |
|
399 | 399 | |
400 | 400 | // Cache was just purged, clear page cache and allow others to hook into our purging... |
401 | - if ( true === $propagate ) { |
|
402 | - if ( ! function_exists( 'autoptimize_do_cachepurged_action' ) ) { |
|
401 | + if (true === $propagate) { |
|
402 | + if (!function_exists('autoptimize_do_cachepurged_action')) { |
|
403 | 403 | function autoptimize_do_cachepurged_action() { |
404 | - do_action( 'autoptimize_action_cachepurged' ); |
|
404 | + do_action('autoptimize_action_cachepurged'); |
|
405 | 405 | } |
406 | 406 | } |
407 | - add_action( 'shutdown', 'autoptimize_do_cachepurged_action', 11 ); |
|
408 | - add_action( 'autoptimize_action_cachepurged', array( 'autoptimizeCache', 'flushPageCache' ), 10, 0 ); |
|
407 | + add_action('shutdown', 'autoptimize_do_cachepurged_action', 11); |
|
408 | + add_action('autoptimize_action_cachepurged', array('autoptimizeCache', 'flushPageCache'), 10, 0); |
|
409 | 409 | } |
410 | 410 | |
411 | 411 | // Warm cache (part of speedupper)! |
412 | - if ( apply_filters( 'autoptimize_filter_speedupper', true ) && false == get_transient( 'autoptimize_cache_warmer_protector' ) ) { |
|
413 | - set_transient( 'autoptimize_cache_warmer_protector', 'I shall not warm cache for another 10 minutes.', 60 * 10 ); |
|
414 | - $url = site_url() . '/?ao_speedup_cachebuster=' . rand( 1, 100000 ); |
|
415 | - $url = apply_filters( 'autoptimize_filter_cache_warmer_url', $url ); |
|
416 | - $cache = @wp_remote_get( $url ); // @codingStandardsIgnoreLine |
|
417 | - unset( $cache ); |
|
412 | + if (apply_filters('autoptimize_filter_speedupper', true) && false == get_transient('autoptimize_cache_warmer_protector')) { |
|
413 | + set_transient('autoptimize_cache_warmer_protector', 'I shall not warm cache for another 10 minutes.', 60*10); |
|
414 | + $url = site_url().'/?ao_speedup_cachebuster='.rand(1, 100000); |
|
415 | + $url = apply_filters('autoptimize_filter_cache_warmer_url', $url); |
|
416 | + $cache = @wp_remote_get($url); // @codingStandardsIgnoreLine |
|
417 | + unset($cache); |
|
418 | 418 | } |
419 | 419 | |
420 | 420 | return true; |
@@ -429,7 +429,7 @@ discard block |
||
429 | 429 | */ |
430 | 430 | public static function clearall_actionless() |
431 | 431 | { |
432 | - return self::clearall( false ); |
|
432 | + return self::clearall(false); |
|
433 | 433 | } |
434 | 434 | |
435 | 435 | /** |
@@ -441,8 +441,8 @@ discard block |
||
441 | 441 | { |
442 | 442 | $contents = array(); |
443 | 443 | |
444 | - foreach ( array( '', 'js', 'css' ) as $dir ) { |
|
445 | - $contents[ $dir ] = scandir( AUTOPTIMIZE_CACHE_DIR . $dir ); |
|
444 | + foreach (array('', 'js', 'css') as $dir) { |
|
445 | + $contents[$dir] = scandir(AUTOPTIMIZE_CACHE_DIR.$dir); |
|
446 | 446 | } |
447 | 447 | |
448 | 448 | return $contents; |
@@ -455,21 +455,21 @@ discard block |
||
455 | 455 | */ |
456 | 456 | public static function stats() |
457 | 457 | { |
458 | - $stats = get_transient( 'autoptimize_stats' ); |
|
458 | + $stats = get_transient('autoptimize_stats'); |
|
459 | 459 | |
460 | 460 | // If no transient, do the actual scan! |
461 | - if ( ! is_array( $stats ) ) { |
|
462 | - if ( ! self::cacheavail() ) { |
|
461 | + if (!is_array($stats)) { |
|
462 | + if (!self::cacheavail()) { |
|
463 | 463 | return 0; |
464 | 464 | } |
465 | 465 | $stats = self::stats_scan(); |
466 | 466 | $count = $stats[0]; |
467 | - if ( $count > 100 ) { |
|
467 | + if ($count > 100) { |
|
468 | 468 | // Store results in transient. |
469 | 469 | set_transient( |
470 | 470 | 'autoptimize_stats', |
471 | 471 | $stats, |
472 | - apply_filters( 'autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS ) |
|
472 | + apply_filters('autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS) |
|
473 | 473 | ); |
474 | 474 | } |
475 | 475 | } |
@@ -492,30 +492,30 @@ discard block |
||
492 | 492 | $size = 0; |
493 | 493 | |
494 | 494 | // Scan everything in our cache directories. |
495 | - foreach ( self::get_cache_contents() as $name => $files ) { |
|
496 | - $dir = rtrim( AUTOPTIMIZE_CACHE_DIR . $name, '/' ) . '/'; |
|
497 | - foreach ( $files as $file ) { |
|
498 | - if ( self::is_valid_cache_file( $dir, $file ) ) { |
|
499 | - if ( AUTOPTIMIZE_CACHE_NOGZIP && |
|
495 | + foreach (self::get_cache_contents() as $name => $files) { |
|
496 | + $dir = rtrim(AUTOPTIMIZE_CACHE_DIR.$name, '/').'/'; |
|
497 | + foreach ($files as $file) { |
|
498 | + if (self::is_valid_cache_file($dir, $file)) { |
|
499 | + if (AUTOPTIMIZE_CACHE_NOGZIP && |
|
500 | 500 | ( |
501 | - false !== strpos( $file, '.js' ) || |
|
502 | - false !== strpos( $file, '.css' ) || |
|
503 | - false !== strpos( $file, '.img' ) || |
|
504 | - false !== strpos( $file, '.txt' ) |
|
501 | + false !== strpos($file, '.js') || |
|
502 | + false !== strpos($file, '.css') || |
|
503 | + false !== strpos($file, '.img') || |
|
504 | + false !== strpos($file, '.txt') |
|
505 | 505 | ) |
506 | 506 | ) { |
507 | 507 | // Web server is gzipping, we count .js|.css|.img|.txt files. |
508 | 508 | $count++; |
509 | - } elseif ( ! AUTOPTIMIZE_CACHE_NOGZIP && false !== strpos( $file, '.none' ) ) { |
|
509 | + } elseif (!AUTOPTIMIZE_CACHE_NOGZIP && false !== strpos($file, '.none')) { |
|
510 | 510 | // We are gzipping ourselves via php, counting only .none files. |
511 | 511 | $count++; |
512 | 512 | } |
513 | - $size += filesize( $dir . $file ); |
|
513 | + $size += filesize($dir.$file); |
|
514 | 514 | } |
515 | 515 | } |
516 | 516 | } |
517 | 517 | |
518 | - $stats = array( $count, $size, time() ); |
|
518 | + $stats = array($count, $size, time()); |
|
519 | 519 | |
520 | 520 | return $stats; |
521 | 521 | } |
@@ -529,22 +529,22 @@ discard block |
||
529 | 529 | */ |
530 | 530 | public static function cacheavail() |
531 | 531 | { |
532 | - if ( false === autoptimizeCache::check_and_create_dirs() ) { |
|
532 | + if (false === autoptimizeCache::check_and_create_dirs()) { |
|
533 | 533 | return false; |
534 | 534 | } |
535 | 535 | |
536 | 536 | // Using .htaccess inside our cache folder to overrule wp-super-cache. |
537 | - $htaccess = AUTOPTIMIZE_CACHE_DIR . '/.htaccess'; |
|
538 | - if ( ! is_file( $htaccess ) ) { |
|
537 | + $htaccess = AUTOPTIMIZE_CACHE_DIR.'/.htaccess'; |
|
538 | + if (!is_file($htaccess)) { |
|
539 | 539 | /** |
540 | 540 | * Create `wp-content/AO_htaccess_tmpl` file with |
541 | 541 | * whatever htaccess rules you might need |
542 | 542 | * if you want to override default AO htaccess |
543 | 543 | */ |
544 | - $htaccess_tmpl = WP_CONTENT_DIR . '/AO_htaccess_tmpl'; |
|
545 | - if ( is_file( $htaccess_tmpl ) ) { |
|
546 | - $content = file_get_contents( $htaccess_tmpl ); |
|
547 | - } elseif ( is_multisite() || ! AUTOPTIMIZE_CACHE_NOGZIP ) { |
|
544 | + $htaccess_tmpl = WP_CONTENT_DIR.'/AO_htaccess_tmpl'; |
|
545 | + if (is_file($htaccess_tmpl)) { |
|
546 | + $content = file_get_contents($htaccess_tmpl); |
|
547 | + } elseif (is_multisite() || !AUTOPTIMIZE_CACHE_NOGZIP) { |
|
548 | 548 | $content = '<IfModule mod_expires.c> |
549 | 549 | ExpiresActive On |
550 | 550 | ExpiresByType text/css A30672000 |
@@ -598,13 +598,13 @@ discard block |
||
598 | 598 | </IfModule>'; |
599 | 599 | } |
600 | 600 | |
601 | - if ( self::do_fallback() === true ) { |
|
602 | - $content .= "\nErrorDocument 404 " . trailingslashit( parse_url( content_url(), PHP_URL_PATH ) ) . 'autoptimize_404_handler.php'; |
|
601 | + if (self::do_fallback() === true) { |
|
602 | + $content .= "\nErrorDocument 404 ".trailingslashit(parse_url(content_url(), PHP_URL_PATH)).'autoptimize_404_handler.php'; |
|
603 | 603 | } |
604 | - @file_put_contents( $htaccess, $content ); // @codingStandardsIgnoreLine |
|
604 | + @file_put_contents($htaccess, $content); // @codingStandardsIgnoreLine |
|
605 | 605 | } |
606 | 606 | |
607 | - if ( self::do_fallback() ) { |
|
607 | + if (self::do_fallback()) { |
|
608 | 608 | self::check_fallback_php(); |
609 | 609 | } |
610 | 610 | |
@@ -619,17 +619,17 @@ discard block |
||
619 | 619 | */ |
620 | 620 | public static function check_fallback_php() { |
621 | 621 | $_fallback_filename = 'autoptimize_404_handler.php'; |
622 | - $_fallback_php = trailingslashit( WP_CONTENT_DIR ) . $_fallback_filename; |
|
622 | + $_fallback_php = trailingslashit(WP_CONTENT_DIR).$_fallback_filename; |
|
623 | 623 | $_fallback_status = true; |
624 | 624 | |
625 | - if ( ! file_exists( $_fallback_php ) ) { |
|
626 | - $_fallback_php_contents = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $_fallback_filename ); |
|
627 | - $_fallback_php_contents = str_replace( '<?php exit;', '<?php', $_fallback_php_contents ); |
|
628 | - $_fallback_php_contents = str_replace( '<!--ao-cache-dir-->', AUTOPTIMIZE_CACHE_DIR, $_fallback_php_contents ); |
|
629 | - if ( apply_filters( 'autoptimize_filter_cache_fallback_log_errors', false ) ) { |
|
630 | - $_fallback_php_contents = str_replace( '// error_log', 'error_log', $_fallback_php_contents ); |
|
625 | + if (!file_exists($_fallback_php)) { |
|
626 | + $_fallback_php_contents = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'config/'.$_fallback_filename); |
|
627 | + $_fallback_php_contents = str_replace('<?php exit;', '<?php', $_fallback_php_contents); |
|
628 | + $_fallback_php_contents = str_replace('<!--ao-cache-dir-->', AUTOPTIMIZE_CACHE_DIR, $_fallback_php_contents); |
|
629 | + if (apply_filters('autoptimize_filter_cache_fallback_log_errors', false)) { |
|
630 | + $_fallback_php_contents = str_replace('// error_log', 'error_log', $_fallback_php_contents); |
|
631 | 631 | } |
632 | - $_fallback_status = file_put_contents( $_fallback_php, $_fallback_php_contents ); |
|
632 | + $_fallback_status = file_put_contents($_fallback_php, $_fallback_php_contents); |
|
633 | 633 | } |
634 | 634 | |
635 | 635 | return $_fallback_status; |
@@ -646,8 +646,8 @@ discard block |
||
646 | 646 | public static function do_fallback() { |
647 | 647 | static $_do_fallback = null; |
648 | 648 | |
649 | - if ( null === $_do_fallback ) { |
|
650 | - $_do_fallback = (bool) apply_filters( 'autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option( 'autoptimize_cache_fallback', '' ) ); |
|
649 | + if (null === $_do_fallback) { |
|
650 | + $_do_fallback = (bool) apply_filters('autoptimize_filter_cache_do_fallback', autoptimizeOptionWrapper::get_option('autoptimize_cache_fallback', '')); |
|
651 | 651 | } |
652 | 652 | |
653 | 653 | return $_do_fallback; |
@@ -659,26 +659,26 @@ discard block |
||
659 | 659 | * and 410'ing ("Gone") if fallback not available. |
660 | 660 | */ |
661 | 661 | public static function wordpress_notfound_fallback() { |
662 | - $original_request = strtok( $_SERVER['REQUEST_URI'], '?' ); |
|
663 | - if ( strpos( $original_request, wp_basename( WP_CONTENT_DIR ) . AUTOPTIMIZE_CACHE_CHILD_DIR ) !== false && is_404() ) { |
|
662 | + $original_request = strtok($_SERVER['REQUEST_URI'], '?'); |
|
663 | + if (strpos($original_request, wp_basename(WP_CONTENT_DIR).AUTOPTIMIZE_CACHE_CHILD_DIR) !== false && is_404()) { |
|
664 | 664 | // make sure this is not considered a 404. |
665 | 665 | global $wp_query; |
666 | 666 | $wp_query->is_404 = false; |
667 | 667 | |
668 | 668 | // set fallback path. |
669 | - $js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION ); |
|
670 | - $fallback_path = AUTOPTIMIZE_CACHE_DIR . $js_or_css . '/autoptimize_fallback.' . $js_or_css; |
|
669 | + $js_or_css = pathinfo($original_request, PATHINFO_EXTENSION); |
|
670 | + $fallback_path = AUTOPTIMIZE_CACHE_DIR.$js_or_css.'/autoptimize_fallback.'.$js_or_css; |
|
671 | 671 | |
672 | 672 | // set fallback URL. |
673 | - $fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request ); |
|
673 | + $fallback_target = preg_replace('/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request); |
|
674 | 674 | |
675 | 675 | // redirect to fallback if possible. |
676 | - if ( $original_request !== $fallback_target && file_exists( $fallback_path ) ) { |
|
676 | + if ($original_request !== $fallback_target && file_exists($fallback_path)) { |
|
677 | 677 | // redirect to fallback. |
678 | - wp_redirect( $fallback_target, 302 ); |
|
678 | + wp_redirect($fallback_target, 302); |
|
679 | 679 | } else { |
680 | 680 | // return HTTP 410 (gone) reponse. |
681 | - status_header( 410 ); |
|
681 | + status_header(410); |
|
682 | 682 | } |
683 | 683 | } |
684 | 684 | } |
@@ -690,13 +690,13 @@ discard block |
||
690 | 690 | * @return bool |
691 | 691 | */ |
692 | 692 | public static function check_and_create_dirs() { |
693 | - if ( ! defined( 'AUTOPTIMIZE_CACHE_DIR' ) ) { |
|
693 | + if (!defined('AUTOPTIMIZE_CACHE_DIR')) { |
|
694 | 694 | // We didn't set a cache. |
695 | 695 | return false; |
696 | 696 | } |
697 | 697 | |
698 | - foreach ( array( '', 'js', 'css' ) as $dir ) { |
|
699 | - if ( ! self::check_cache_dir( AUTOPTIMIZE_CACHE_DIR . $dir ) ) { |
|
698 | + foreach (array('', 'js', 'css') as $dir) { |
|
699 | + if (!self::check_cache_dir(AUTOPTIMIZE_CACHE_DIR.$dir)) { |
|
700 | 700 | return false; |
701 | 701 | } |
702 | 702 | } |
@@ -711,25 +711,25 @@ discard block |
||
711 | 711 | * |
712 | 712 | * @return bool |
713 | 713 | */ |
714 | - protected static function check_cache_dir( $dir ) |
|
714 | + protected static function check_cache_dir($dir) |
|
715 | 715 | { |
716 | 716 | // Try creating the dir if it doesn't exist. |
717 | - if ( ! file_exists( $dir ) ) { |
|
718 | - @mkdir( $dir, 0775, true ); // @codingStandardsIgnoreLine |
|
719 | - if ( ! file_exists( $dir ) ) { |
|
717 | + if (!file_exists($dir)) { |
|
718 | + @mkdir($dir, 0775, true); // @codingStandardsIgnoreLine |
|
719 | + if (!file_exists($dir)) { |
|
720 | 720 | return false; |
721 | 721 | } |
722 | 722 | } |
723 | 723 | |
724 | 724 | // If we still cannot write, bail. |
725 | - if ( ! is_writable( $dir ) ) { |
|
725 | + if (!is_writable($dir)) { |
|
726 | 726 | return false; |
727 | 727 | } |
728 | 728 | |
729 | 729 | // Create an index.html in there to avoid prying eyes! |
730 | - $idx_file = rtrim( $dir, '/\\' ) . '/index.html'; |
|
731 | - if ( ! is_file( $idx_file ) ) { |
|
732 | - @file_put_contents( $idx_file, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>' ); // @codingStandardsIgnoreLine |
|
730 | + $idx_file = rtrim($dir, '/\\').'/index.html'; |
|
731 | + if (!is_file($idx_file)) { |
|
732 | + @file_put_contents($idx_file, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>'); // @codingStandardsIgnoreLine |
|
733 | 733 | } |
734 | 734 | |
735 | 735 | return true; |
@@ -743,50 +743,50 @@ discard block |
||
743 | 743 | // @codingStandardsIgnoreStart |
744 | 744 | public static function flushPageCache() |
745 | 745 | { |
746 | - if ( function_exists( 'wp_cache_clear_cache' ) ) { |
|
747 | - if ( is_multisite() ) { |
|
746 | + if (function_exists('wp_cache_clear_cache')) { |
|
747 | + if (is_multisite()) { |
|
748 | 748 | $blog_id = get_current_blog_id(); |
749 | - wp_cache_clear_cache( $blog_id ); |
|
749 | + wp_cache_clear_cache($blog_id); |
|
750 | 750 | } else { |
751 | 751 | wp_cache_clear_cache(); |
752 | 752 | } |
753 | - } elseif ( has_action( 'cachify_flush_cache' ) ) { |
|
754 | - do_action( 'cachify_flush_cache' ); |
|
755 | - } elseif ( function_exists( 'w3tc_pgcache_flush' ) ) { |
|
753 | + } elseif (has_action('cachify_flush_cache')) { |
|
754 | + do_action('cachify_flush_cache'); |
|
755 | + } elseif (function_exists('w3tc_pgcache_flush')) { |
|
756 | 756 | w3tc_pgcache_flush(); |
757 | - } elseif ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) { |
|
757 | + } elseif (function_exists('wp_fast_cache_bulk_delete_all')) { |
|
758 | 758 | wp_fast_cache_bulk_delete_all(); |
759 | - } elseif ( class_exists( 'WpFastestCache' ) ) { |
|
759 | + } elseif (class_exists('WpFastestCache')) { |
|
760 | 760 | $wpfc = new WpFastestCache(); |
761 | 761 | $wpfc->deleteCache(); |
762 | - } elseif ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) { |
|
762 | + } elseif (class_exists('c_ws_plugin__qcache_purging_routines')) { |
|
763 | 763 | c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache |
764 | - } elseif ( class_exists( 'zencache' ) ) { |
|
764 | + } elseif (class_exists('zencache')) { |
|
765 | 765 | zencache::clear(); |
766 | - } elseif ( class_exists( 'comet_cache' ) ) { |
|
766 | + } elseif (class_exists('comet_cache')) { |
|
767 | 767 | comet_cache::clear(); |
768 | - } elseif ( class_exists( 'WpeCommon' ) ) { |
|
768 | + } elseif (class_exists('WpeCommon')) { |
|
769 | 769 | // WPEngine cache purge/flush methods to call by default |
770 | 770 | $wpe_methods = array( |
771 | 771 | 'purge_varnish_cache', |
772 | 772 | ); |
773 | 773 | |
774 | 774 | // More agressive clear/flush/purge behind a filter |
775 | - if ( apply_filters( 'autoptimize_flush_wpengine_aggressive', false ) ) { |
|
776 | - $wpe_methods = array_merge( $wpe_methods, array( 'purge_memcached', 'clear_maxcdn_cache' ) ); |
|
775 | + if (apply_filters('autoptimize_flush_wpengine_aggressive', false)) { |
|
776 | + $wpe_methods = array_merge($wpe_methods, array('purge_memcached', 'clear_maxcdn_cache')); |
|
777 | 777 | } |
778 | 778 | |
779 | 779 | // Filtering the entire list of WpeCommon methods to be called (for advanced usage + easier testing) |
780 | - $wpe_methods = apply_filters( 'autoptimize_flush_wpengine_methods', $wpe_methods ); |
|
780 | + $wpe_methods = apply_filters('autoptimize_flush_wpengine_methods', $wpe_methods); |
|
781 | 781 | |
782 | - foreach ( $wpe_methods as $wpe_method ) { |
|
783 | - if ( method_exists( 'WpeCommon', $wpe_method ) ) { |
|
782 | + foreach ($wpe_methods as $wpe_method) { |
|
783 | + if (method_exists('WpeCommon', $wpe_method)) { |
|
784 | 784 | WpeCommon::$wpe_method(); |
785 | 785 | } |
786 | 786 | } |
787 | - } elseif ( function_exists( 'sg_cachepress_purge_cache' ) ) { |
|
787 | + } elseif (function_exists('sg_cachepress_purge_cache')) { |
|
788 | 788 | sg_cachepress_purge_cache(); |
789 | - } elseif ( array_key_exists( 'KINSTA_CACHE_ZONE', $_SERVER ) ) { |
|
789 | + } elseif (array_key_exists('KINSTA_CACHE_ZONE', $_SERVER)) { |
|
790 | 790 | $_kinsta_clear_cache_url = 'https://localhost/kinsta-clear-cache-all'; |
791 | 791 | $_kinsta_response = wp_remote_get( |
792 | 792 | $_kinsta_clear_cache_url, |
@@ -795,18 +795,18 @@ discard block |
||
795 | 795 | 'timeout' => 5, |
796 | 796 | ) |
797 | 797 | ); |
798 | - } elseif ( defined('NGINX_HELPER_BASENAME') ) { |
|
799 | - do_action( 'rt_nginx_helper_purge_all' ); |
|
800 | - } elseif ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) { |
|
798 | + } elseif (defined('NGINX_HELPER_BASENAME')) { |
|
799 | + do_action('rt_nginx_helper_purge_all'); |
|
800 | + } elseif (file_exists(WP_CONTENT_DIR.'/wp-cache-config.php') && function_exists('prune_super_cache')) { |
|
801 | 801 | // fallback for WP-Super-Cache |
802 | 802 | global $cache_path; |
803 | - if ( is_multisite() ) { |
|
803 | + if (is_multisite()) { |
|
804 | 804 | $blog_id = get_current_blog_id(); |
805 | - prune_super_cache( get_supercache_dir( $blog_id ), true ); |
|
806 | - prune_super_cache( $cache_path . 'blogs/', true ); |
|
805 | + prune_super_cache(get_supercache_dir($blog_id), true); |
|
806 | + prune_super_cache($cache_path.'blogs/', true); |
|
807 | 807 | } else { |
808 | - prune_super_cache( $cache_path . 'supercache/', true ); |
|
809 | - prune_super_cache( $cache_path, true ); |
|
808 | + prune_super_cache($cache_path.'supercache/', true); |
|
809 | + prune_super_cache($cache_path, true); |
|
810 | 810 | } |
811 | 811 | } |
812 | 812 | } |
@@ -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,14 +338,14 @@ 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 ); |
|
348 | - } elseif ( apply_filters( 'autoptimize_filter_ccsjs_remove_empty_minified_url', false ) ) { |
|
347 | + $new_tag = str_replace($url, $minified_url, $new_tag); |
|
348 | + } elseif (apply_filters('autoptimize_filter_ccsjs_remove_empty_minified_url', false)) { |
|
349 | 349 | // Remove the original script tag, because cache content is empty but only if filter |
350 | 350 | // is trued because $minified_url is also false if original JS is minified already. |
351 | 351 | $new_tag = ''; |
@@ -353,17 +353,17 @@ discard block |
||
353 | 353 | } |
354 | 354 | } |
355 | 355 | |
356 | - if ( $this->ismovable( $new_tag ) ) { |
|
356 | + if ($this->ismovable($new_tag)) { |
|
357 | 357 | // can be moved, flags and all. |
358 | - if ( $this->movetolast( $new_tag ) ) { |
|
358 | + if ($this->movetolast($new_tag)) { |
|
359 | 359 | $this->move['last'][] = $new_tag; |
360 | 360 | } else { |
361 | 361 | $this->move['first'][] = $new_tag; |
362 | 362 | } |
363 | 363 | } else { |
364 | 364 | // cannot be moved, so if flag was added re-inject altered tag immediately. |
365 | - if ( ( '' !== $new_tag && $orig_tag !== $new_tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_js_remove_empty_files', false ) ) ) { |
|
366 | - $this->content = str_replace( $orig_tag, $new_tag, $this->content ); |
|
365 | + if (('' !== $new_tag && $orig_tag !== $new_tag) || ('' === $new_tag && apply_filters('autoptimize_filter_js_remove_empty_files', false))) { |
|
366 | + $this->content = str_replace($orig_tag, $new_tag, $this->content); |
|
367 | 367 | $orig_tag = ''; |
368 | 368 | } |
369 | 369 | // and forget about the $tag (not to be touched any more). |
@@ -372,23 +372,23 @@ discard block |
||
372 | 372 | } |
373 | 373 | } else { |
374 | 374 | // Inline script. |
375 | - if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
376 | - $this->content = str_replace( $tag, '', $this->content ); |
|
375 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
376 | + $this->content = str_replace($tag, '', $this->content); |
|
377 | 377 | continue; |
378 | 378 | } |
379 | 379 | |
380 | 380 | // unhide comments, as javascript may be wrapped in comment-tags for old times' sake. |
381 | - $tag = $this->restore_comments( $tag ); |
|
382 | - if ( $this->ismergeable( $tag ) && $this->include_inline ) { |
|
383 | - preg_match( '#<script.*>(.*)</script>#Usmi', $tag, $code ); |
|
384 | - $code = preg_replace( '#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1] ); |
|
385 | - $code = preg_replace( '/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code ); |
|
386 | - $this->scripts[] = 'INLINE;' . $code; |
|
381 | + $tag = $this->restore_comments($tag); |
|
382 | + if ($this->ismergeable($tag) && $this->include_inline) { |
|
383 | + preg_match('#<script.*>(.*)</script>#Usmi', $tag, $code); |
|
384 | + $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1]); |
|
385 | + $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code); |
|
386 | + $this->scripts[] = 'INLINE;'.$code; |
|
387 | 387 | } else { |
388 | 388 | // Can we move this? |
389 | - $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag ); |
|
390 | - if ( $this->ismovable( $tag ) || '' !== $autoptimize_js_moveable ) { |
|
391 | - if ( $this->movetolast( $tag ) || 'last' === $autoptimize_js_moveable ) { |
|
389 | + $autoptimize_js_moveable = apply_filters('autoptimize_js_moveable', '', $tag); |
|
390 | + if ($this->ismovable($tag) || '' !== $autoptimize_js_moveable) { |
|
391 | + if ($this->movetolast($tag) || 'last' === $autoptimize_js_moveable) { |
|
392 | 392 | $this->move['last'][] = $tag; |
393 | 393 | } else { |
394 | 394 | $this->move['first'][] = $tag; |
@@ -399,11 +399,11 @@ discard block |
||
399 | 399 | } |
400 | 400 | } |
401 | 401 | // Re-hide comments to be able to do the removal based on tag from $this->content. |
402 | - $tag = $this->hide_comments( $tag ); |
|
402 | + $tag = $this->hide_comments($tag); |
|
403 | 403 | } |
404 | 404 | |
405 | 405 | // Remove the original script tag. |
406 | - $this->content = str_replace( $tag, '', $this->content ); |
|
406 | + $this->content = str_replace($tag, '', $this->content); |
|
407 | 407 | } |
408 | 408 | |
409 | 409 | return true; |
@@ -428,30 +428,30 @@ discard block |
||
428 | 428 | * @param string $tag Script node & child(ren). |
429 | 429 | * @return bool |
430 | 430 | */ |
431 | - public function should_aggregate( $tag ) |
|
431 | + public function should_aggregate($tag) |
|
432 | 432 | { |
433 | - if ( empty( $tag ) ) { |
|
433 | + if (empty($tag)) { |
|
434 | 434 | return false; |
435 | 435 | } |
436 | 436 | |
437 | 437 | // We're only interested in the type attribute of the <script> tag itself, not any possible |
438 | 438 | // inline code that might just contain the 'type=' string... |
439 | 439 | $tag_parts = array(); |
440 | - preg_match( '#<(script[^>]*)>#i', $tag, $tag_parts ); |
|
440 | + preg_match('#<(script[^>]*)>#i', $tag, $tag_parts); |
|
441 | 441 | $tag_without_contents = null; |
442 | - if ( ! empty( $tag_parts[1] ) ) { |
|
442 | + if (!empty($tag_parts[1])) { |
|
443 | 443 | $tag_without_contents = $tag_parts[1]; |
444 | 444 | } |
445 | 445 | |
446 | - $has_type = ( strpos( $tag_without_contents, 'type' ) !== false ); |
|
446 | + $has_type = (strpos($tag_without_contents, 'type') !== false); |
|
447 | 447 | |
448 | 448 | $type_valid = false; |
449 | - if ( $has_type ) { |
|
450 | - $type_valid = (bool) preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents ); |
|
449 | + if ($has_type) { |
|
450 | + $type_valid = (bool) preg_match('/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents); |
|
451 | 451 | } |
452 | 452 | |
453 | 453 | $should_aggregate = false; |
454 | - if ( ! $has_type || $type_valid ) { |
|
454 | + if (!$has_type || $type_valid) { |
|
455 | 455 | $should_aggregate = true; |
456 | 456 | } |
457 | 457 | |
@@ -463,75 +463,75 @@ discard block |
||
463 | 463 | */ |
464 | 464 | public function minify() |
465 | 465 | { |
466 | - foreach ( $this->scripts as $script ) { |
|
467 | - if ( empty( $script ) ) { |
|
466 | + foreach ($this->scripts as $script) { |
|
467 | + if (empty($script)) { |
|
468 | 468 | continue; |
469 | 469 | } |
470 | 470 | |
471 | 471 | // TODO/FIXME: some duplicate code here, can be reduced/simplified. |
472 | - if ( preg_match( '#^INLINE;#', $script ) ) { |
|
472 | + if (preg_match('#^INLINE;#', $script)) { |
|
473 | 473 | // Inline script. |
474 | - $script = preg_replace( '#^INLINE;#', '', $script ); |
|
475 | - $script = rtrim( $script, ";\n\t\r" ) . ';'; |
|
474 | + $script = preg_replace('#^INLINE;#', '', $script); |
|
475 | + $script = rtrim($script, ";\n\t\r").';'; |
|
476 | 476 | // Add try-catch? |
477 | - if ( $this->trycatch ) { |
|
478 | - $script = 'try{' . $script . '}catch(e){}'; |
|
477 | + if ($this->trycatch) { |
|
478 | + $script = 'try{'.$script.'}catch(e){}'; |
|
479 | 479 | } |
480 | - $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, '' ); |
|
481 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscript ) ) { |
|
480 | + $tmpscript = apply_filters('autoptimize_js_individual_script', $script, ''); |
|
481 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscript)) { |
|
482 | 482 | $script = $tmpscript; |
483 | 483 | $this->alreadyminified = true; |
484 | 484 | } |
485 | - $this->jscode .= "\n" . $script; |
|
485 | + $this->jscode .= "\n".$script; |
|
486 | 486 | } else { |
487 | 487 | // External script. |
488 | - if ( false !== $script && file_exists( $script ) && is_readable( $script ) ) { |
|
489 | - $scriptsrc = file_get_contents( $script ); |
|
490 | - $scriptsrc = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc ); |
|
491 | - $scriptsrc = rtrim( $scriptsrc, ";\n\t\r" ) . ';'; |
|
488 | + if (false !== $script && file_exists($script) && is_readable($script)) { |
|
489 | + $scriptsrc = file_get_contents($script); |
|
490 | + $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc); |
|
491 | + $scriptsrc = rtrim($scriptsrc, ";\n\t\r").';'; |
|
492 | 492 | // Add try-catch? |
493 | - if ( $this->trycatch ) { |
|
494 | - $scriptsrc = 'try{' . $scriptsrc . '}catch(e){}'; |
|
493 | + if ($this->trycatch) { |
|
494 | + $scriptsrc = 'try{'.$scriptsrc.'}catch(e){}'; |
|
495 | 495 | } |
496 | - $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script ); |
|
497 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscriptsrc ) ) { |
|
496 | + $tmpscriptsrc = apply_filters('autoptimize_js_individual_script', $scriptsrc, $script); |
|
497 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc)) { |
|
498 | 498 | $scriptsrc = $tmpscriptsrc; |
499 | 499 | $this->alreadyminified = true; |
500 | - } elseif ( $this->can_inject_late( $script ) ) { |
|
501 | - $scriptsrc = self::build_injectlater_marker( $script, md5( $scriptsrc ) ); |
|
500 | + } elseif ($this->can_inject_late($script)) { |
|
501 | + $scriptsrc = self::build_injectlater_marker($script, md5($scriptsrc)); |
|
502 | 502 | } |
503 | - $this->jscode .= "\n" . $scriptsrc; |
|
503 | + $this->jscode .= "\n".$scriptsrc; |
|
504 | 504 | } |
505 | 505 | } |
506 | 506 | } |
507 | 507 | |
508 | 508 | // Check for already-minified code. |
509 | - $this->md5hash = md5( $this->jscode ); |
|
510 | - $ccheck = new autoptimizeCache( $this->md5hash, 'js' ); |
|
511 | - if ( $ccheck->check() ) { |
|
509 | + $this->md5hash = md5($this->jscode); |
|
510 | + $ccheck = new autoptimizeCache($this->md5hash, 'js'); |
|
511 | + if ($ccheck->check()) { |
|
512 | 512 | $this->jscode = $ccheck->retrieve(); |
513 | 513 | return true; |
514 | 514 | } |
515 | - unset( $ccheck ); |
|
515 | + unset($ccheck); |
|
516 | 516 | |
517 | 517 | // $this->jscode has all the uncompressed code now. |
518 | - if ( true !== $this->alreadyminified ) { |
|
519 | - if ( apply_filters( 'autoptimize_js_do_minify', true ) ) { |
|
520 | - $tmp_jscode = trim( JSMin::minify( $this->jscode ) ); |
|
521 | - if ( ! empty( $tmp_jscode ) ) { |
|
518 | + if (true !== $this->alreadyminified) { |
|
519 | + if (apply_filters('autoptimize_js_do_minify', true)) { |
|
520 | + $tmp_jscode = trim(JSMin::minify($this->jscode)); |
|
521 | + if (!empty($tmp_jscode)) { |
|
522 | 522 | $this->jscode = $tmp_jscode; |
523 | - unset( $tmp_jscode ); |
|
523 | + unset($tmp_jscode); |
|
524 | 524 | } |
525 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
526 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
525 | + $this->jscode = $this->inject_minified($this->jscode); |
|
526 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
527 | 527 | return true; |
528 | 528 | } else { |
529 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
529 | + $this->jscode = $this->inject_minified($this->jscode); |
|
530 | 530 | return false; |
531 | 531 | } |
532 | 532 | } |
533 | 533 | |
534 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
534 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
535 | 535 | return true; |
536 | 536 | } |
537 | 537 | |
@@ -540,13 +540,13 @@ discard block |
||
540 | 540 | */ |
541 | 541 | public function cache() |
542 | 542 | { |
543 | - $cache = new autoptimizeCache( $this->md5hash, 'js' ); |
|
544 | - if ( ! $cache->check() ) { |
|
543 | + $cache = new autoptimizeCache($this->md5hash, 'js'); |
|
544 | + if (!$cache->check()) { |
|
545 | 545 | // Cache our code. |
546 | - $cache->cache( $this->jscode, 'text/javascript' ); |
|
546 | + $cache->cache($this->jscode, 'text/javascript'); |
|
547 | 547 | } |
548 | - $this->url = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
549 | - $this->url = $this->url_replace_cdn( $this->url ); |
|
548 | + $this->url = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
549 | + $this->url = $this->url_replace_cdn($this->url); |
|
550 | 550 | } |
551 | 551 | |
552 | 552 | /** |
@@ -555,47 +555,47 @@ discard block |
||
555 | 555 | public function getcontent() |
556 | 556 | { |
557 | 557 | // Restore the full content. |
558 | - if ( ! empty( $this->restofcontent ) ) { |
|
558 | + if (!empty($this->restofcontent)) { |
|
559 | 559 | $this->content .= $this->restofcontent; |
560 | 560 | $this->restofcontent = ''; |
561 | 561 | } |
562 | 562 | |
563 | 563 | // Add the scripts taking forcehead/ deferred (default) into account. |
564 | - if ( $this->forcehead ) { |
|
565 | - $replace_tag = array( '</head>', 'before' ); |
|
564 | + if ($this->forcehead) { |
|
565 | + $replace_tag = array('</head>', 'before'); |
|
566 | 566 | $defer = ''; |
567 | 567 | } else { |
568 | - $replace_tag = array( '</body>', 'before' ); |
|
568 | + $replace_tag = array('</body>', 'before'); |
|
569 | 569 | $defer = 'defer '; |
570 | 570 | } |
571 | 571 | |
572 | - $defer = apply_filters( 'autoptimize_filter_js_defer', $defer ); |
|
572 | + $defer = apply_filters('autoptimize_filter_js_defer', $defer); |
|
573 | 573 | $type_js = ''; |
574 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
574 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
575 | 575 | $type_js = 'type="text/javascript" '; |
576 | 576 | } |
577 | 577 | |
578 | - $bodyreplacementpayload = '<script ' . $type_js . $defer . 'src="' . $this->url . '"></script>'; |
|
579 | - $bodyreplacementpayload = apply_filters( 'autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload ); |
|
578 | + $bodyreplacementpayload = '<script '.$type_js.$defer.'src="'.$this->url.'"></script>'; |
|
579 | + $bodyreplacementpayload = apply_filters('autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload); |
|
580 | 580 | |
581 | - $bodyreplacement = implode( '', $this->move['first'] ); |
|
581 | + $bodyreplacement = implode('', $this->move['first']); |
|
582 | 582 | $bodyreplacement .= $bodyreplacementpayload; |
583 | - $bodyreplacement .= implode( '', $this->move['last'] ); |
|
583 | + $bodyreplacement .= implode('', $this->move['last']); |
|
584 | 584 | |
585 | - $replace_tag = apply_filters( 'autoptimize_filter_js_replacetag', $replace_tag ); |
|
585 | + $replace_tag = apply_filters('autoptimize_filter_js_replacetag', $replace_tag); |
|
586 | 586 | |
587 | - if ( strlen( $this->jscode ) > 0 ) { |
|
588 | - $this->inject_in_html( $bodyreplacement, $replace_tag ); |
|
587 | + if (strlen($this->jscode) > 0) { |
|
588 | + $this->inject_in_html($bodyreplacement, $replace_tag); |
|
589 | 589 | } |
590 | 590 | |
591 | 591 | // Restore comments. |
592 | - $this->content = $this->restore_comments( $this->content ); |
|
592 | + $this->content = $this->restore_comments($this->content); |
|
593 | 593 | |
594 | 594 | // Restore IE hacks. |
595 | - $this->content = $this->restore_iehacks( $this->content ); |
|
595 | + $this->content = $this->restore_iehacks($this->content); |
|
596 | 596 | |
597 | 597 | // Restore noptimize. |
598 | - $this->content = $this->restore_noptimize( $this->content ); |
|
598 | + $this->content = $this->restore_noptimize($this->content); |
|
599 | 599 | |
600 | 600 | // Return the modified HTML. |
601 | 601 | return $this->content; |
@@ -606,34 +606,34 @@ discard block |
||
606 | 606 | * |
607 | 607 | * @param string $tag JS tag. |
608 | 608 | */ |
609 | - private function ismergeable( $tag ) |
|
609 | + private function ismergeable($tag) |
|
610 | 610 | { |
611 | - if ( empty( $tag ) || ! $this->aggregate ) { |
|
611 | + if (empty($tag) || !$this->aggregate) { |
|
612 | 612 | return false; |
613 | 613 | } |
614 | 614 | |
615 | - if ( ! empty( $this->whitelist ) ) { |
|
616 | - foreach ( $this->whitelist as $match ) { |
|
617 | - if ( false !== strpos( $tag, $match ) ) { |
|
615 | + if (!empty($this->whitelist)) { |
|
616 | + foreach ($this->whitelist as $match) { |
|
617 | + if (false !== strpos($tag, $match)) { |
|
618 | 618 | return true; |
619 | 619 | } |
620 | 620 | } |
621 | 621 | // No match with whitelist. |
622 | 622 | return false; |
623 | 623 | } else { |
624 | - foreach ( $this->domove as $match ) { |
|
625 | - if ( false !== strpos( $tag, $match ) ) { |
|
624 | + foreach ($this->domove as $match) { |
|
625 | + if (false !== strpos($tag, $match)) { |
|
626 | 626 | // Matched something. |
627 | 627 | return false; |
628 | 628 | } |
629 | 629 | } |
630 | 630 | |
631 | - if ( $this->movetolast( $tag ) ) { |
|
631 | + if ($this->movetolast($tag)) { |
|
632 | 632 | return false; |
633 | 633 | } |
634 | 634 | |
635 | - foreach ( $this->dontmove as $match ) { |
|
636 | - if ( false !== strpos( $tag, $match ) ) { |
|
635 | + foreach ($this->dontmove as $match) { |
|
636 | + if (false !== strpos($tag, $match)) { |
|
637 | 637 | // Matched something. |
638 | 638 | return false; |
639 | 639 | } |
@@ -649,25 +649,25 @@ discard block |
||
649 | 649 | * |
650 | 650 | * @param string $tag tag to check for blacklist (exclusions). |
651 | 651 | */ |
652 | - private function ismovable( $tag ) |
|
652 | + private function ismovable($tag) |
|
653 | 653 | { |
654 | - if ( empty( $tag ) || true !== $this->include_inline || apply_filters( 'autoptimize_filter_js_unmovable', true ) ) { |
|
654 | + if (empty($tag) || true !== $this->include_inline || apply_filters('autoptimize_filter_js_unmovable', true)) { |
|
655 | 655 | return false; |
656 | 656 | } |
657 | 657 | |
658 | - foreach ( $this->domove as $match ) { |
|
659 | - if ( false !== strpos( $tag, $match ) ) { |
|
658 | + foreach ($this->domove as $match) { |
|
659 | + if (false !== strpos($tag, $match)) { |
|
660 | 660 | // Matched something. |
661 | 661 | return true; |
662 | 662 | } |
663 | 663 | } |
664 | 664 | |
665 | - if ( $this->movetolast( $tag ) ) { |
|
665 | + if ($this->movetolast($tag)) { |
|
666 | 666 | return true; |
667 | 667 | } |
668 | 668 | |
669 | - foreach ( $this->dontmove as $match ) { |
|
670 | - if ( false !== strpos( $tag, $match ) ) { |
|
669 | + foreach ($this->dontmove as $match) { |
|
670 | + if (false !== strpos($tag, $match)) { |
|
671 | 671 | // Matched something. |
672 | 672 | return false; |
673 | 673 | } |
@@ -677,14 +677,14 @@ discard block |
||
677 | 677 | return true; |
678 | 678 | } |
679 | 679 | |
680 | - private function movetolast( $tag ) |
|
680 | + private function movetolast($tag) |
|
681 | 681 | { |
682 | - if ( empty( $tag ) ) { |
|
682 | + if (empty($tag)) { |
|
683 | 683 | return false; |
684 | 684 | } |
685 | 685 | |
686 | - foreach ( $this->domovelast as $match ) { |
|
687 | - if ( false !== strpos( $tag, $match ) ) { |
|
686 | + foreach ($this->domovelast as $match) { |
|
687 | + if (false !== strpos($tag, $match)) { |
|
688 | 688 | // Matched, return true. |
689 | 689 | return true; |
690 | 690 | } |
@@ -704,12 +704,12 @@ discard block |
||
704 | 704 | * @param string $js_path Path to JS file. |
705 | 705 | * @return bool |
706 | 706 | */ |
707 | - private function can_inject_late( $js_path ) { |
|
708 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
709 | - if ( true !== $this->inject_min_late ) { |
|
707 | + private function can_inject_late($js_path) { |
|
708 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
709 | + if (true !== $this->inject_min_late) { |
|
710 | 710 | // late-inject turned off. |
711 | 711 | return false; |
712 | - } 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 | + } 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)) { |
|
713 | 713 | // file not minified based on filename & filter. |
714 | 714 | return false; |
715 | 715 | } else { |
@@ -736,32 +736,32 @@ discard block |
||
736 | 736 | * |
737 | 737 | * @return bool|string Url pointing to the minified js file or false. |
738 | 738 | */ |
739 | - public function minify_single( $filepath, $cache_miss = false ) |
|
739 | + public function minify_single($filepath, $cache_miss = false) |
|
740 | 740 | { |
741 | - $contents = $this->prepare_minify_single( $filepath ); |
|
741 | + $contents = $this->prepare_minify_single($filepath); |
|
742 | 742 | |
743 | - if ( empty( $contents ) ) { |
|
743 | + if (empty($contents)) { |
|
744 | 744 | return false; |
745 | 745 | } |
746 | 746 | |
747 | 747 | // Check cache. |
748 | - $hash = 'single_' . md5( $contents ); |
|
749 | - $cache = new autoptimizeCache( $hash, 'js' ); |
|
748 | + $hash = 'single_'.md5($contents); |
|
749 | + $cache = new autoptimizeCache($hash, 'js'); |
|
750 | 750 | |
751 | 751 | // If not in cache already, minify... |
752 | - if ( ! $cache->check() || $cache_miss ) { |
|
753 | - $contents = trim( JSMin::minify( $contents ) ); |
|
752 | + if (!$cache->check() || $cache_miss) { |
|
753 | + $contents = trim(JSMin::minify($contents)); |
|
754 | 754 | |
755 | 755 | // Check if minified cache content is empty. |
756 | - if ( empty( $contents ) ) { |
|
756 | + if (empty($contents)) { |
|
757 | 757 | return false; |
758 | 758 | } |
759 | 759 | |
760 | 760 | // Store in cache. |
761 | - $cache->cache( $contents, 'text/javascript' ); |
|
761 | + $cache->cache($contents, 'text/javascript'); |
|
762 | 762 | } |
763 | 763 | |
764 | - $url = $this->build_minify_single_url( $cache ); |
|
764 | + $url = $this->build_minify_single_url($cache); |
|
765 | 765 | |
766 | 766 | return $url; |
767 | 767 | } |
@@ -12,29 +12,29 @@ |
||
12 | 12 | .ao_settings_div {background: white;border: 1px solid #ccc;padding: 1px 15px;margin: 15px 10px 10px 0;} |
13 | 13 | .ao_settings_div .form-table th {font-weight: normal;} |
14 | 14 | </style> |
15 | - <script>document.title = "Autoptimize: <?php _e( 'Critical CSS', 'autoptimize' ); ?> " + document.title;</script> |
|
15 | + <script>document.title = "Autoptimize: <?php _e('Critical CSS', 'autoptimize'); ?> " + document.title;</script> |
|
16 | 16 | <ul id="explain-panel"> |
17 | 17 | <div class="ao_settings_div"> |
18 | 18 | <?php |
19 | 19 | $ccss_explanation = ''; |
20 | 20 | |
21 | 21 | // get the HTML with the explanation of what critical CSS is. |
22 | - if ( apply_filters( 'autoptimize_settingsscreen_remotehttp', true ) ) { |
|
23 | - $ccss_explanation = get_transient( 'ao_ccss_explain' ); |
|
24 | - if ( empty( $ccss_explanation ) ) { |
|
25 | - $ccss_expl_resp = wp_remote_get( 'https://misc.optimizingmatters.com/autoptimize_ccss_explain_i18n.html?ao_ver=' . AUTOPTIMIZE_PLUGIN_VERSION ); |
|
26 | - if ( ! is_wp_error( $ccss_expl_resp ) ) { |
|
27 | - if ( '200' == wp_remote_retrieve_response_code( $ccss_expl_resp ) ) { |
|
28 | - $ccss_explanation = wp_kses_post( wp_remote_retrieve_body( $ccss_expl_resp ) ); |
|
29 | - set_transient( 'ao_ccss_explain', $ccss_explanation, WEEK_IN_SECONDS ); |
|
22 | + if (apply_filters('autoptimize_settingsscreen_remotehttp', true)) { |
|
23 | + $ccss_explanation = get_transient('ao_ccss_explain'); |
|
24 | + if (empty($ccss_explanation)) { |
|
25 | + $ccss_expl_resp = wp_remote_get('https://misc.optimizingmatters.com/autoptimize_ccss_explain_i18n.html?ao_ver='.AUTOPTIMIZE_PLUGIN_VERSION); |
|
26 | + if (!is_wp_error($ccss_expl_resp)) { |
|
27 | + if ('200' == wp_remote_retrieve_response_code($ccss_expl_resp)) { |
|
28 | + $ccss_explanation = wp_kses_post(wp_remote_retrieve_body($ccss_expl_resp)); |
|
29 | + set_transient('ao_ccss_explain', $ccss_explanation, WEEK_IN_SECONDS); |
|
30 | 30 | } |
31 | 31 | } |
32 | 32 | } |
33 | 33 | } |
34 | 34 | |
35 | 35 | // placeholder text in case HTML is empty. |
36 | - if ( empty( $ccss_explanation ) ) { |
|
37 | - $ccss_explanation = __( '<h2>Fix render-blocking CSS!</h2><p>Significantly improve your first-paint times by making CSS non-render-blocking.</p><p>The next step is to sign up at <a href="https://criticalcss.com/?aff=1" target="_blank">https://criticalcss.com</a> (this is a premium service, priced 2 GBP/month for membership and 5 GBP/month per domain) <strong>and get the API key, which you can copy from <a href="https://criticalcss.com/account/api-keys?aff=1" target="_blank">the API-keys page</a></strong> and paste below.</p><p>If you have any questions or need support, head on over to <a href="https://wordpress.org/support/plugin/autoptimize" target="_blank">our support forum</a> and we\'ll help you get up and running in no time!</p>', 'autoptimize' ); |
|
36 | + if (empty($ccss_explanation)) { |
|
37 | + $ccss_explanation = __('<h2>Fix render-blocking CSS!</h2><p>Significantly improve your first-paint times by making CSS non-render-blocking.</p><p>The next step is to sign up at <a href="https://criticalcss.com/?aff=1" target="_blank">https://criticalcss.com</a> (this is a premium service, priced 2 GBP/month for membership and 5 GBP/month per domain) <strong>and get the API key, which you can copy from <a href="https://criticalcss.com/account/api-keys?aff=1" target="_blank">the API-keys page</a></strong> and paste below.</p><p>If you have any questions or need support, head on over to <a href="https://wordpress.org/support/plugin/autoptimize" target="_blank">our support forum</a> and we\'ll help you get up and running in no time!</p>', 'autoptimize'); |
|
38 | 38 | } else { |
39 | 39 | // we were able to fetch the explenation, so add the JS to show correct language. |
40 | 40 | $ccss_explanation .= "<script>jQuery('.ao_i18n').hide();d=document;lang=d.getElementsByTagName('html')[0].getAttribute('lang').substring(0,2);if(d.getElementById(lang)!= null){jQuery('#'+lang).show();}else{jQuery('#default').show();}</script>"; |