Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize cache enable/disable and update_mod_rewrite_rules #500

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 57 additions & 32 deletions wp-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

?>
<a name="modrewrite"></a><fieldset class="options">
<h3><?php _e( 'Mod Rewrite Rules', 'wp-super-cache' ); ?></h3>
Expand Down Expand Up @@ -2085,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' );
Expand All @@ -2101,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 "<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 @@ -3814,14 +3840,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 @@ -3833,6 +3854,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