Skip to content

Commit d66c898

Browse files
stodorovicdonnchawp
authored andcommitted
Optimize cache enable/disable and update_mod_rewrite_rules (#500)
* Don't show htaccess form if cache is disabled * Prevent testing page if is false * Optimize cache enable/disable functions
1 parent 29ed792 commit d66c898

1 file changed

Lines changed: 57 additions & 32 deletions

File tree

wp-cache.php

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,17 +1381,20 @@ function wpsc_admin_tabs( $current = 0 ) {
13811381
}
13821382

13831383
function wsc_mod_rewrite() {
1384-
global $valid_nonce, $cache_path, $wp_cache_mod_rewrite, $is_nginx;
1384+
global $valid_nonce, $cache_path;
13851385

1386-
if ( $is_nginx ) {
1386+
if ( $GLOBALS['is_nginx'] ) {
13871387
return false;
13881388
}
13891389

1390-
if ( defined( 'WPSC_DISABLE_HTACCESS_UPDATE' ) )
1390+
if ( defined( 'WPSC_DISABLE_HTACCESS_UPDATE' ) ) {
13911391
return false;
1392+
}
13921393

1393-
if ( !$wp_cache_mod_rewrite )
1394+
if ( $GLOBALS['cache_enabled'] !== true || $GLOBALS['wp_cache_mod_rewrite'] !== 1 ) {
13941395
return false;
1396+
}
1397+
13951398
?>
13961399
<a name="modrewrite"></a><fieldset class="options">
13971400
<h3><?php _e( 'Mod Rewrite Rules', 'wp-super-cache' ); ?></h3>
@@ -2107,10 +2110,12 @@ function wp_cache_debug_settings() {
21072110
function wp_cache_enable() {
21082111
global $wp_cache_config_file, $cache_enabled;
21092112

2110-
if( wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = true;', $wp_cache_config_file) ) {
2111-
$cache_enabled = true;
2113+
if ( ! wp_cache_replace_line( '^\s*\$cache_enabled\s*=', '$cache_enabled = true;', $wp_cache_config_file ) ) {
2114+
return;
21122115
}
21132116

2117+
$cache_enabled = true;
2118+
21142119
if ( wpsc_set_default_gc() ) {
21152120
// gc might not be scheduled, check and schedule
21162121
$timestamp = wp_next_scheduled( 'wp_cache_gc' );
@@ -2123,52 +2128,73 @@ function wp_cache_enable() {
21232128
function wp_cache_disable() {
21242129
global $wp_cache_config_file, $cache_enabled;
21252130

2131+
if ( ! wp_cache_replace_line( '^\s*\$cache_enabled\s*=', '$cache_enabled = false;', $wp_cache_config_file ) ) {
2132+
return;
2133+
}
2134+
2135+
$cache_enabled = false;
2136+
21262137
wp_clear_scheduled_hook( 'wp_cache_check_site_hook' );
21272138
wp_clear_scheduled_hook( 'wp_cache_gc' );
21282139
wp_clear_scheduled_hook( 'wp_cache_gc_watcher' );
2129-
if (wp_cache_replace_line('^ *\$cache_enabled', '$cache_enabled = false;', $wp_cache_config_file)) {
2130-
$cache_enabled = false;
2131-
}
21322140
}
2141+
21332142
function wp_super_cache_enable() {
21342143
global $supercachedir, $wp_cache_config_file, $super_cache_enabled;
21352144

2136-
if( is_dir( $supercachedir . ".disabled" ) )
2137-
if( is_dir( $supercachedir ) ) {
2138-
prune_super_cache( $supercachedir . ".disabled", true );
2139-
@unlink( $supercachedir . ".disabled" );
2145+
if ( ! wp_cache_replace_line( '^\s*\$super_cache_enabled\s*=', '$super_cache_enabled = true;', $wp_cache_config_file ) ) {
2146+
return;
2147+
}
2148+
2149+
$super_cache_enabled = true;
2150+
2151+
if ( is_dir( $supercachedir . '.disabled' ) ) {
2152+
if ( is_dir( $supercachedir ) ) {
2153+
prune_super_cache( $supercachedir . '.disabled', true );
2154+
@unlink( $supercachedir . '.disabled' );
21402155
} else {
2141-
@rename( $supercachedir . ".disabled", $supercachedir );
2156+
@rename( $supercachedir . '.disabled', $supercachedir );
21422157
}
2143-
wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = true;', $wp_cache_config_file);
2144-
$super_cache_enabled = true;
2158+
}
21452159
}
21462160

21472161
function wp_super_cache_disable() {
21482162
global $cache_path, $supercachedir, $wp_cache_config_file, $super_cache_enabled;
21492163

2150-
wp_cache_replace_line('^ *\$super_cache_enabled', '$super_cache_enabled = false;', $wp_cache_config_file);
2151-
if( is_dir( $supercachedir ) )
2152-
@rename( $supercachedir, $supercachedir . ".disabled" );
2164+
if ( ! wp_cache_replace_line('^\s*\$super_cache_enabled\s*=', '$super_cache_enabled = false;', $wp_cache_config_file ) ) {
2165+
return;
2166+
}
2167+
21532168
$super_cache_enabled = false;
2169+
2170+
if ( is_dir( $supercachedir ) ) {
2171+
@rename( $supercachedir, $supercachedir . '.disabled' );
2172+
}
21542173
sleep( 1 ); // allow existing processes to write to the supercachedir and then delete it
2155-
if (function_exists ('prune_super_cache') && is_dir( $supercachedir ) ) {
2174+
if ( function_exists( 'prune_super_cache' ) && is_dir( $supercachedir ) ) {
21562175
prune_super_cache( $cache_path, true );
21572176
}
2177+
2178+
if ( $GLOBALS['wp_cache_mod_rewrite'] === 1 ) {
2179+
remove_mod_rewrite_rules();
2180+
}
21582181
}
21592182

21602183
function wp_cache_is_enabled() {
21612184
global $wp_cache_config_file;
21622185

2163-
if(get_option('gzipcompression')) {
2164-
echo "<strong>" . __( 'Warning', 'wp-super-cache' ) . "</strong>: " . __( "GZIP compression is enabled in WordPress, wp-cache will be bypassed until you disable gzip compression.", 'wp-super-cache' );
2186+
if ( get_option( 'gzipcompression' ) ) {
2187+
echo '<strong>' . __( 'Warning', 'wp-super-cache' ) . '</strong>: ' . __( 'GZIP compression is enabled in WordPress, wp-cache will be bypassed until you disable gzip compression.', 'wp-super-cache' );
21652188
return false;
21662189
}
2167-
$lines = file($wp_cache_config_file);
2168-
foreach($lines as $line) {
2169-
if (preg_match('/^ *\$cache_enabled *= *true *;/', $line))
2190+
2191+
$lines = file( $wp_cache_config_file );
2192+
foreach ( $lines as $line ) {
2193+
if ( preg_match( '/^\s*\$cache_enabled\s*=\s*true\s*;/', $line ) ) {
21702194
return true;
2195+
}
21712196
}
2197+
21722198
return false;
21732199
}
21742200

@@ -3843,14 +3869,9 @@ function update_mod_rewrite_rules( $add_rules = true ) {
38433869
}
38443870

38453871
$generated_rules = wpsc_get_htaccess_info();
3872+
$existing_rules = implode( "\n", extract_from_markers( $home_path . '.htaccess', 'WPSuperCache' ) );
38463873

3847-
if ( $add_rules ) {
3848-
$rules = $generated_rules[ 'rules' ];
3849-
} else {
3850-
$rules = '';
3851-
}
3852-
3853-
$existing_rules = implode( "\n", extract_from_markers( $home_path . '.htaccess', 'WPSuperCache' ) );
3874+
$rules = $add_rules ? $generated_rules[ 'rules' ] : '';
38543875

38553876
if ( $existing_rules == $rules ) {
38563877
$update_mod_rewrite_rules_error = "rules have not changed";
@@ -3862,6 +3883,10 @@ function update_mod_rewrite_rules( $add_rules = true ) {
38623883
return false;
38633884
}
38643885

3886+
if ( empty( $rules ) ) {
3887+
return insert_with_markers( $home_path . '.htaccess', 'WPSuperCache', array() );
3888+
}
3889+
38653890
$url = trailingslashit( get_bloginfo( 'url' ) );
38663891
$original_page = wp_remote_get( $url, array( 'timeout' => 60, 'blocking' => true ) );
38673892
if ( is_wp_error( $original_page ) ) {

0 commit comments

Comments
 (0)