From 3ee173fec1123c2e8b98e4b78780a6803e9cde7d Mon Sep 17 00:00:00 2001 From: stodorovic Date: Sat, 16 Dec 2017 19:11:26 +0100 Subject: [PATCH 1/3] Don't show htaccess form if cache is disabled --- wp-cache.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/wp-cache.php b/wp-cache.php index bb985612..ebb59e2d 100644 --- a/wp-cache.php +++ b/wp-cache.php @@ -1359,17 +1359,20 @@ function wpsc_admin_tabs( $current = 0 ) { } function wsc_mod_rewrite() { - global $valid_nonce, $cache_path, $wp_cache_mod_rewrite, $is_nginx; + global $valid_nonce, $cache_path; - if ( $is_nginx ) { + if ( $GLOBALS['is_nginx'] ) { return false; } - if ( defined( 'WPSC_DISABLE_HTACCESS_UPDATE' ) ) + if ( defined( 'WPSC_DISABLE_HTACCESS_UPDATE' ) ) { return false; + } - if ( !$wp_cache_mod_rewrite ) + if ( $GLOBALS['cache_enabled'] !== true || $GLOBALS['wp_cache_mod_rewrite'] !== 1 ) { return false; + } + ?>

From ed16b64f5c0cac5b4ce34953087c008872bc40b9 Mon Sep 17 00:00:00 2001 From: stodorovic Date: Sat, 16 Dec 2017 20:36:50 +0100 Subject: [PATCH 2/3] Prevent testing page if is false --- wp-cache.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/wp-cache.php b/wp-cache.php index ebb59e2d..89cca615 100644 --- a/wp-cache.php +++ b/wp-cache.php @@ -3817,14 +3817,9 @@ function update_mod_rewrite_rules( $add_rules = true ) { } $generated_rules = wpsc_get_htaccess_info(); + $existing_rules = implode( "\n", extract_from_markers( $home_path . '.htaccess', 'WPSuperCache' ) ); - if ( $add_rules ) { - $rules = $generated_rules[ 'rules' ]; - } else { - $rules = ''; - } - - $existing_rules = implode( "\n", extract_from_markers( $home_path . '.htaccess', 'WPSuperCache' ) ); + $rules = $add_rules ? $generated_rules[ 'rules' ] : ''; if ( $existing_rules == $rules ) { $update_mod_rewrite_rules_error = "rules have not changed"; @@ -3836,6 +3831,10 @@ function update_mod_rewrite_rules( $add_rules = true ) { return false; } + if ( empty( $rules ) ) { + return insert_with_markers( $home_path . '.htaccess', 'WPSuperCache', array() ); + } + $url = trailingslashit( get_bloginfo( 'url' ) ); $original_page = wp_remote_get( $url, array( 'timeout' => 60, 'blocking' => true ) ); if ( is_wp_error( $original_page ) ) { From 05710601a4875fdd692d9b75838015a377ecfd6e Mon Sep 17 00:00:00 2001 From: stodorovic Date: Mon, 18 Dec 2017 09:41:12 +0100 Subject: [PATCH 3/3] Optimize cache enable/disable functions --- wp-cache.php | 65 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/wp-cache.php b/wp-cache.php index 89cca615..adc560e8 100644 --- a/wp-cache.php +++ b/wp-cache.php @@ -2088,10 +2088,12 @@ function wp_cache_debug_settings() { function wp_cache_enable() { global $wp_cache_config_file, $cache_enabled; - if( wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = true;', $wp_cache_config_file) ) { - $cache_enabled = true; + if ( ! wp_cache_replace_line( '^\s*\$cache_enabled\s*=', '$cache_enabled = true;', $wp_cache_config_file ) ) { + return; } + $cache_enabled = true; + if ( wpsc_set_default_gc() ) { // gc might not be scheduled, check and schedule $timestamp = wp_next_scheduled( 'wp_cache_gc' ); @@ -2104,52 +2106,73 @@ function wp_cache_enable() { function wp_cache_disable() { global $wp_cache_config_file, $cache_enabled; + if ( ! wp_cache_replace_line( '^\s*\$cache_enabled\s*=', '$cache_enabled = false;', $wp_cache_config_file ) ) { + return; + } + + $cache_enabled = false; + wp_clear_scheduled_hook( 'wp_cache_check_site_hook' ); wp_clear_scheduled_hook( 'wp_cache_gc' ); wp_clear_scheduled_hook( 'wp_cache_gc_watcher' ); - if (wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = false;', $wp_cache_config_file)) { - $cache_enabled = false; - } } + function wp_super_cache_enable() { global $supercachedir, $wp_cache_config_file, $super_cache_enabled; - if( is_dir( $supercachedir . ".disabled" ) ) - if( is_dir( $supercachedir ) ) { - prune_super_cache( $supercachedir . ".disabled", true ); - @unlink( $supercachedir . ".disabled" ); + if ( ! wp_cache_replace_line( '^\s*\$super_cache_enabled\s*=', '$super_cache_enabled = true;', $wp_cache_config_file ) ) { + return; + } + + $super_cache_enabled = true; + + if ( is_dir( $supercachedir . '.disabled' ) ) { + if ( is_dir( $supercachedir ) ) { + prune_super_cache( $supercachedir . '.disabled', true ); + @unlink( $supercachedir . '.disabled' ); } else { - @rename( $supercachedir . ".disabled", $supercachedir ); + @rename( $supercachedir . '.disabled', $supercachedir ); } - wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = true;', $wp_cache_config_file); - $super_cache_enabled = true; + } } function wp_super_cache_disable() { global $cache_path, $supercachedir, $wp_cache_config_file, $super_cache_enabled; - wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = false;', $wp_cache_config_file); - if( is_dir( $supercachedir ) ) - @rename( $supercachedir, $supercachedir . ".disabled" ); + if ( ! wp_cache_replace_line('^\s*\$super_cache_enabled\s*=', '$super_cache_enabled = false;', $wp_cache_config_file ) ) { + return; + } + $super_cache_enabled = false; + + if ( is_dir( $supercachedir ) ) { + @rename( $supercachedir, $supercachedir . '.disabled' ); + } sleep( 1 ); // allow existing processes to write to the supercachedir and then delete it - if (function_exists ('prune_super_cache') && is_dir( $supercachedir ) ) { + if ( function_exists( 'prune_super_cache' ) && is_dir( $supercachedir ) ) { prune_super_cache( $cache_path, true ); } + + if ( $GLOBALS['wp_cache_mod_rewrite'] === 1 ) { + remove_mod_rewrite_rules(); + } } function wp_cache_is_enabled() { global $wp_cache_config_file; - if(get_option('gzipcompression')) { - echo "" . __( 'Warning', 'wp-super-cache' ) . ": " . __( "GZIP compression is enabled in WordPress, wp-cache will be bypassed until you disable gzip compression.", 'wp-super-cache' ); + if ( get_option( 'gzipcompression' ) ) { + echo '' . __( 'Warning', 'wp-super-cache' ) . ': ' . __( 'GZIP compression is enabled in WordPress, wp-cache will be bypassed until you disable gzip compression.', 'wp-super-cache' ); return false; } - $lines = file($wp_cache_config_file); - foreach($lines as $line) { - if (preg_match('/^ *\$cache_enabled *= *true *;/', $line)) + + $lines = file( $wp_cache_config_file ); + foreach ( $lines as $line ) { + if ( preg_match( '/^\s*\$cache_enabled\s*=\s*true\s*;/', $line ) ) { return true; + } } + return false; }