@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | |
| 51 | 51 | // WP CLI |
| 52 | 52 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 53 | - require_once AUTOPTIMIZE_PLUGIN_DIR . 'classes/autoptimizeCLI.php'; |
|
| 53 | + require_once AUTOPTIMIZE_PLUGIN_DIR . 'classes/autoptimizeCLI.php'; |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | // Initialize the cache at least once |
@@ -137,21 +137,21 @@ discard block |
||
| 137 | 137 | $ao_noptimize = true; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - // if setting says not to optimize logged in user and user is logged in |
|
| 141 | - if ( get_option('autoptimize_optimize_logged','on') !== 'on' && is_user_logged_in() && current_user_can('edit_posts') ) { |
|
| 142 | - $ao_noptimize = true; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // if setting says not to optimize cart/ checkout |
|
| 146 | - if ( get_option('autoptimize_optimize_checkout','on') !== 'on' ) { |
|
| 147 | - // checking for woocommerce, easy digital downloads and wp ecommerce |
|
| 148 | - foreach ( array("is_checkout","is_cart","edd_is_checkout","wpsc_is_cart","wpsc_is_checkout") as $shopCond ) { |
|
| 149 | - if ( function_exists($shopCond) && $shopCond() ) { |
|
| 150 | - $ao_noptimize = true; |
|
| 151 | - break; |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - } |
|
| 140 | + // if setting says not to optimize logged in user and user is logged in |
|
| 141 | + if ( get_option('autoptimize_optimize_logged','on') !== 'on' && is_user_logged_in() && current_user_can('edit_posts') ) { |
|
| 142 | + $ao_noptimize = true; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // if setting says not to optimize cart/ checkout |
|
| 146 | + if ( get_option('autoptimize_optimize_checkout','on') !== 'on' ) { |
|
| 147 | + // checking for woocommerce, easy digital downloads and wp ecommerce |
|
| 148 | + foreach ( array("is_checkout","is_cart","edd_is_checkout","wpsc_is_cart","wpsc_is_checkout") as $shopCond ) { |
|
| 149 | + if ( function_exists($shopCond) && $shopCond() ) { |
|
| 150 | + $ao_noptimize = true; |
|
| 151 | + break; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | 156 | // filter you can use to block autoptimization on your own terms |
| 157 | 157 | $ao_noptimize = (bool) apply_filters( 'autoptimize_filter_noptimize', $ao_noptimize ); |
@@ -1,21 +1,21 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | // Exit if accessed directly |
| 3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | - exit; |
|
| 4 | + exit; |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | class autoptimizeCLI extends WP_CLI_Command { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * Clears the cache. |
|
| 11 | - * |
|
| 12 | - * @subcommand clear |
|
| 13 | - */ |
|
| 14 | - public function clear( $args, $args_assoc ) { |
|
| 15 | - WP_CLI::line( esc_html__( 'Flushing the cache...', 'autoptimize' ) ); |
|
| 16 | - autoptimizeCache::clearall(); |
|
| 17 | - WP_CLI::success( esc_html__( 'Cache flushed.', 'autoptimize' ) ); |
|
| 18 | - } |
|
| 9 | + /** |
|
| 10 | + * Clears the cache. |
|
| 11 | + * |
|
| 12 | + * @subcommand clear |
|
| 13 | + */ |
|
| 14 | + public function clear( $args, $args_assoc ) { |
|
| 15 | + WP_CLI::line( esc_html__( 'Flushing the cache...', 'autoptimize' ) ); |
|
| 16 | + autoptimizeCache::clearall(); |
|
| 17 | + WP_CLI::success( esc_html__( 'Cache flushed.', 'autoptimize' ) ); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | 20 | } |
| 21 | 21 | |
@@ -85,13 +85,13 @@ |
||
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | function ao_css_speedup_cleanup($cssin) { |
| 88 | - // speedupper results in aggregated CSS not being minified, so the filestart-marker AO adds when aggregating need to be removed |
|
| 89 | - return trim(str_replace(array('/*FILESTART*/','/*FILESTART2*/'),'',$cssin)); |
|
| 88 | + // speedupper results in aggregated CSS not being minified, so the filestart-marker AO adds when aggregating need to be removed |
|
| 89 | + return trim(str_replace(array('/*FILESTART*/','/*FILESTART2*/'),'',$cssin)); |
|
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | function ao_js_speedup_cleanup($jsin) { |
| 93 | - // cleanup |
|
| 94 | - return trim($jsin); |
|
| 93 | + // cleanup |
|
| 94 | + return trim($jsin); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | // conditionally attach filters |
@@ -343,45 +343,45 @@ discard block |
||
| 343 | 343 | } |
| 344 | 344 | |
| 345 | 345 | protected function minify_single($pathIn) { |
| 346 | - // determine JS or CSS and set var (also mimetype), return false if neither |
|
| 347 | - if ( $this->str_ends_in($pathIn,".js") === true ) { |
|
| 348 | - $codeType="js"; |
|
| 349 | - $codeMime="text/javascript"; |
|
| 350 | - } else if ( $this->str_ends_in($pathIn,".css") === true ) { |
|
| 351 | - $codeType="css"; |
|
| 352 | - $codeMime="text/css"; |
|
| 353 | - } else { |
|
| 354 | - return false; |
|
| 355 | - } |
|
| 346 | + // determine JS or CSS and set var (also mimetype), return false if neither |
|
| 347 | + if ( $this->str_ends_in($pathIn,".js") === true ) { |
|
| 348 | + $codeType="js"; |
|
| 349 | + $codeMime="text/javascript"; |
|
| 350 | + } else if ( $this->str_ends_in($pathIn,".css") === true ) { |
|
| 351 | + $codeType="css"; |
|
| 352 | + $codeMime="text/css"; |
|
| 353 | + } else { |
|
| 354 | + return false; |
|
| 355 | + } |
|
| 356 | 356 | |
| 357 | - // if min.js or min.css return false |
|
| 358 | - if (( $this->str_ends_in($pathIn,"-min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,".min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,"js/jquery/jquery.js") === true ) ) { |
|
| 359 | - return false; |
|
| 360 | - } |
|
| 357 | + // if min.js or min.css return false |
|
| 358 | + if (( $this->str_ends_in($pathIn,"-min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,".min.".$codeType) === true ) || ( $this->str_ends_in($pathIn,"js/jquery/jquery.js") === true ) ) { |
|
| 359 | + return false; |
|
| 360 | + } |
|
| 361 | 361 | |
| 362 | - // read file, return false if empty |
|
| 363 | - $_toMinify = file_get_contents($pathIn); |
|
| 364 | - if ( empty($_toMinify) ) return false; |
|
| 362 | + // read file, return false if empty |
|
| 363 | + $_toMinify = file_get_contents($pathIn); |
|
| 364 | + if ( empty($_toMinify) ) return false; |
|
| 365 | 365 | |
| 366 | - // check cache |
|
| 367 | - $_md5hash = "single_".md5($_toMinify); |
|
| 368 | - $_cache = new autoptimizeCache($_md5hash,$codeType); |
|
| 369 | - if ($_cache->check() ) { |
|
| 370 | - $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
|
| 371 | - } else { |
|
| 372 | - // if not in cache first minify |
|
| 373 | - $_Minified = $_toMinify; |
|
| 374 | - if ($codeType === "js") { |
|
| 375 | - if (class_exists('JSMin') && apply_filters( 'autoptimize_js_do_minify' , true)) { |
|
| 376 | - if (@is_callable(array("JSMin","minify"))) { |
|
| 377 | - $tmp_code = trim(JSMin::minify($_toMinify)); |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - } else if ($codeType === "css") { |
|
| 366 | + // check cache |
|
| 367 | + $_md5hash = "single_".md5($_toMinify); |
|
| 368 | + $_cache = new autoptimizeCache($_md5hash,$codeType); |
|
| 369 | + if ($_cache->check() ) { |
|
| 370 | + $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
|
| 371 | + } else { |
|
| 372 | + // if not in cache first minify |
|
| 373 | + $_Minified = $_toMinify; |
|
| 374 | + if ($codeType === "js") { |
|
| 375 | + if (class_exists('JSMin') && apply_filters( 'autoptimize_js_do_minify' , true)) { |
|
| 376 | + if (@is_callable(array("JSMin","minify"))) { |
|
| 377 | + $tmp_code = trim(JSMin::minify($_toMinify)); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + } else if ($codeType === "css") { |
|
| 381 | 381 | // make sure paths to background images/ imported css/ fonts .. are OK |
| 382 | 382 | $_toMinify = $this->fixurls($pathIn,$_toMinify); |
| 383 | 383 | if (class_exists('Minify_CSS_Compressor')) { |
| 384 | - $tmp_code = trim(Minify_CSS_Compressor::process($_toMinify)); |
|
| 384 | + $tmp_code = trim(Minify_CSS_Compressor::process($_toMinify)); |
|
| 385 | 385 | } else if(class_exists('CSSmin')) { |
| 386 | 386 | $cssmin = new CSSmin(); |
| 387 | 387 | if (method_exists($cssmin,"run")) { |
@@ -390,31 +390,31 @@ discard block |
||
| 390 | 390 | $tmp_code = trim(CssMin::minify($_toMinify)); |
| 391 | 391 | } |
| 392 | 392 | } |
| 393 | - } |
|
| 394 | - if (!empty($tmp_code)) { |
|
| 395 | - $_Minified = $tmp_code; |
|
| 396 | - unset($tmp_code); |
|
| 397 | - } |
|
| 398 | - // and then cache |
|
| 399 | - $_cache->cache($_Minified,$codeMime); |
|
| 400 | - $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
|
| 401 | - } |
|
| 402 | - unset($_cache); |
|
| 393 | + } |
|
| 394 | + if (!empty($tmp_code)) { |
|
| 395 | + $_Minified = $tmp_code; |
|
| 396 | + unset($tmp_code); |
|
| 397 | + } |
|
| 398 | + // and then cache |
|
| 399 | + $_cache->cache($_Minified,$codeMime); |
|
| 400 | + $_CachedMinifiedUrl = AUTOPTIMIZE_CACHE_URL.$_cache->getname(); |
|
| 401 | + } |
|
| 402 | + unset($_cache); |
|
| 403 | 403 | |
| 404 | - // if CDN, then CDN |
|
| 405 | - $_CachedMinifiedUrl = $this->url_replace_cdn($_CachedMinifiedUrl); |
|
| 404 | + // if CDN, then CDN |
|
| 405 | + $_CachedMinifiedUrl = $this->url_replace_cdn($_CachedMinifiedUrl); |
|
| 406 | 406 | |
| 407 | - return $_CachedMinifiedUrl; |
|
| 408 | - } |
|
| 407 | + return $_CachedMinifiedUrl; |
|
| 408 | + } |
|
| 409 | 409 | |
| 410 | - protected function str_ends_in($haystack,$needle) { |
|
| 411 | - $needleLength = strlen($needle); |
|
| 412 | - $haystackLength = strlen($haystack); |
|
| 413 | - $lastPos=strrpos($haystack,$needle); |
|
| 414 | - if ($lastPos === $haystackLength - $needleLength) { |
|
| 415 | - return true; |
|
| 416 | - } else { |
|
| 417 | - return false; |
|
| 418 | - } |
|
| 419 | - } |
|
| 410 | + protected function str_ends_in($haystack,$needle) { |
|
| 411 | + $needleLength = strlen($needle); |
|
| 412 | + $haystackLength = strlen($haystack); |
|
| 413 | + $lastPos=strrpos($haystack,$needle); |
|
| 414 | + if ($lastPos === $haystackLength - $needleLength) { |
|
| 415 | + return true; |
|
| 416 | + } else { |
|
| 417 | + return false; |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | 420 | } |