Skip to content

Commit

Permalink
Optimize cache enable/disable and update_mod_rewrite_rules (#500)
Browse files Browse the repository at this point in the history
* Don't show htaccess form if cache is disabled

* Prevent testing page if  is false

* Optimize cache enable/disable functions
  • Loading branch information
stodorovic authored and donnchawp committed Feb 14, 2018
1 parent 29ed792 commit d66c898
Showing 1 changed file with 57 additions and 32 deletions.
89 changes: 57 additions & 32 deletions wp-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -1381,17 +1381,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;
}

?>
<a name="modrewrite"></a><fieldset class="options">
<h3><?php _e( 'Mod Rewrite Rules', 'wp-super-cache' ); ?></h3>
Expand Down Expand Up @@ -2107,10 +2110,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' );
Expand All @@ -2123,52 +2128,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 "<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' );
if ( get_option( 'gzipcompression' ) ) {
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' );
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;
}

Expand Down Expand Up @@ -3843,14 +3869,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";
Expand All @@ -3862,6 +3883,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 ) ) {
Expand Down

0 comments on commit d66c898

Please sign in to comment.