Skip to content

Commit

Permalink
Merge pull request #176 from Automattic/wpsc_delete_files
Browse files Browse the repository at this point in the history
Remove most calls to get_all_supercache_filenames() and replace with wpsc_delete_files() and wpsc_rebuild_files()
  • Loading branch information
donnchawp authored Feb 9, 2017
2 parents f447a47 + 67e87cc commit 41d582b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 48 deletions.
47 changes: 47 additions & 0 deletions wp-cache-phase1.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,53 @@ function get_current_url_supercache_dir( $post_id = 0 ) {
return $dir;
}

/*
* Delete (or rebuild) all the files in one directory.
* Checks if it is in the cache directory but doesn't allow files in the following directories to be deleted:
* wp-content/cache/
* wp-content/cache/blogs/
* wp-content/cache/supercache/
*
*/
function wpsc_rebuild_files( $dir ) {
return wpsc_delete_files( $dir, false );
}

function wpsc_delete_files( $dir, $delete = true ) {
global $cache_path, $blog_cache_dir;
static $rp_cache_path = '';
static $protected = '';

// only do this once, this function will be called many times
if ( $rp_cache_path == '' ) {
$protected = array( $cache_path, $cache_path . $blog_cache_dir, get_supercache_dir() );
$protected = array_walk( array_walk( $protected, 'realpath' ), 'trailingslashit' );
$rp_cache_path = trailingslashit( realpath( $cache_path ) );
}

$dir = trailingslashit( realpath( $dir ) );
if ( substr( $dir, 0, strlen( $rp_cache_path ) ) != $rp_cache_path )
return false;

if ( in_array( $dir, $protected ) )
return false;

if ( is_dir( $dir ) && $dh = @opendir( $dir ) ) {
while ( ( $file = readdir( $dh ) ) !== false ) {
if ( $file != '.' && $file != '..' && $file != '.htaccess' && is_file( $dir . $file ) )
if ( $delete )
@unlink( $dir . $file );
else
@wp_cache_rebuild_or_delete( $dir . $file );
}
closedir( $dh );

if ( $delete )
@rmdir( $dir );
}
return true;
}

function get_all_supercache_filenames( $dir = '' ) {
global $wp_cache_mobile_enabled, $cache_path;

Expand Down
83 changes: 41 additions & 42 deletions wp-cache-phase2.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,40 @@ function wp_cache_phase2() {
}

function wpcache_do_rebuild( $dir ) {
global $do_rebuild_list;
$dir = trailingslashit( $dir );
global $do_rebuild_list, $cache_path, $blog_cache_dir;

$dir = trailingslashit( realpath( $dir ) );
$protected = array( $cache_path, $cache_path . $blog_cache_dir, get_supercache_dir() );
$protected = array_walk( array_walk( $protected, 'realpath' ), 'trailingslashit' );
$rp_cache_path = trailingslashit( realpath( $cache_path ) );

if ( substr( $dir, 0, strlen( $rp_cache_path ) ) != $rp_cache_path )
return false;

if ( in_array( $dir, $protected ) )
return false;

if ( isset( $do_rebuild_list[ $dir ] ) )
return false;
$files_to_check = get_all_supercache_filenames( $dir );
foreach( $files_to_check as $cache_file ) {
$cache_file = $dir . $cache_file;
if( !@file_exists( $cache_file . '.needs-rebuild' ) )
continue;
$mtime = @filemtime($cache_file . '.needs-rebuild');
if( $mtime && (time() - $mtime) < 10 ) {
wp_cache_debug( "Rebuild file renamed to cache file temporarily: $cache_file", 3 );
@rename( $cache_file . '.needs-rebuild', $cache_file );
$do_rebuild_list[ $dir ] = 1;
}
// cleanup old files or if rename fails
if( @file_exists( $cache_file . '.needs-rebuild' ) ) {
wp_cache_debug( "Rebuild file deleted: {$cache_file}.needs-rebuild", 3 );
@unlink( $cache_file . '.needs-rebuild' );

if ( is_dir( $dir ) && $dh = @opendir( $dir ) ) {
while ( ( $file = readdir( $dh ) ) !== false ) {
if ( $file != '.' && $file != '..' && is_file( $dir . $file ) ) {
$cache_file = $dir . $file;
if( !@file_exists( $cache_file . '.needs-rebuild' ) )
continue;
$mtime = @filemtime( $cache_file . '.needs-rebuild' );
if( $mtime && ( time() - $mtime ) < 10 ) {
wp_cache_debug( "Rebuild file renamed to cache file temporarily: $cache_file" );
@rename( $cache_file . '.needs-rebuild', $cache_file );
$do_rebuild_list[ $dir ] = 1;
}
// cleanup old files or if rename fails
if( @file_exists( $cache_file . '.needs-rebuild' ) ) {
wp_cache_debug( "Rebuild file deleted: {$cache_file}.needs-rebuild", 3 );
@unlink( $cache_file . '.needs-rebuild' );
}
}
}
}
}
Expand Down Expand Up @@ -332,11 +347,7 @@ function wp_cache_ob_callback( $buffer ) {
foreach( $do_rebuild_list as $dir => $n ) {
if ( wp_cache_confirm_delete( $dir ) ) {
wp_cache_debug( 'wp_cache_ob_callback clearing rebuilt files in ' . $dir );
$files_to_check = get_all_supercache_filenames( $dir );
foreach( $files_to_check as $cache_file ) {
$cache_file = $dir . $cache_file;
@unlink( $cache_file );
}
wpsc_delete_files( $dir );
}
}
}
Expand Down Expand Up @@ -1223,12 +1234,8 @@ function wp_cache_post_change( $post_id ) {
// make sure the front page has a rebuild file
wp_cache_post_id_gc( $siteurl, $post_id, $all );
if ( $all == true ) {
wp_cache_debug( "Post change: supercache enabled: deleting cache files in " . $cache_path . 'supercache/' . $siteurl, 4 );
$files_to_check = get_all_supercache_filenames( $dir );
foreach( $files_to_check as $cache_file ) {
wp_cache_debug( "Post change: deleting cache file: " . $dir . $cache_file, 4 );
prune_super_cache( $dir . $cache_file, true, true );
}
wp_cache_debug( "Post change: supercache enabled: deleting cache files in " . $dir );
wpsc_rebuild_files( $dir );
do_action( 'gc_cache', 'prune', 'homepage' );
} else {
wp_cache_debug( "wp_cache_post_change: not deleting all pages.", 4 );
Expand All @@ -1237,10 +1244,8 @@ function wp_cache_post_change( $post_id ) {
wp_cache_debug( "Post change: deleting page_on_front and page_for_posts pages.", 4 );
wp_cache_debug( "Post change: page_on_front " . get_option( 'page_on_front' ), 4 );
$permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( get_option( 'page_for_posts' ) ) ) );
$files_to_check = get_all_supercache_filenames( $dir . $permalink );
foreach( $files_to_check as $cache_file ) {
prune_super_cache( $dir . $permalink . $cache_file, true, true );
}
wp_cache_debug( "Post change: Deleting files in: " . str_replace( '//', '/', $dir . $permalink ) );
wpsc_rebuild_files( $dir . $permalink );
do_action( 'gc_cache', 'prune', $permalink );
} else {
wp_cache_debug( "wp_cache_post_change: not deleting front static page.", 4 );
Expand Down Expand Up @@ -1272,11 +1277,8 @@ function wp_cache_post_change( $post_id ) {
@unlink( $blog_cache_dir . 'meta/' . $file );
@unlink( $blog_cache_dir . $file );
if ( false == $supercache_files_deleted && $super_cache_enabled == true ) {
$files_to_check = get_all_supercache_filenames( $dir . $permalink );
wp_cache_debug( "Post change: deleting supercache files for {$permalink}: $file " . print_r( $files_to_check, 1 ), 4 );
foreach( $files_to_check as $cache_file ) {
@wp_cache_rebuild_or_delete( $dir . trailingslashit( $permalink ) . $cache_file );
}
wp_cache_debug( "Post change: deleting supercache files for {$permalink}" );
wpsc_rebuild_files( $dir . $permalink );
$supercache_files_deleted = true;
do_action( 'gc_cache', 'rebuild', $permalink );
}
Expand All @@ -1286,11 +1288,8 @@ function wp_cache_post_change( $post_id ) {
@unlink( $blog_cache_dir . 'meta/' . $file );
@unlink( $blog_cache_dir . $file );
if ( $super_cache_enabled == true ) {
$files_to_check = get_all_supercache_filenames( $dir . $meta[ 'uri' ] );
wp_cache_debug( "Post change: deleting supercache files for {$meta[ 'uri' ]}: $file " . print_r( $files_to_check, 1 ), 4 );
foreach( $files_to_check as $cache_file ) {
@wp_cache_rebuild_or_delete( $dir . trailingslashit( $meta[ 'uri' ] ) . $cache_file );
}
wp_cache_debug( "Post change: deleting supercache files for {$meta[ 'uri' ]}" );
wpsc_rebuild_files( $dir . $meta[ 'uri' ] );
do_action( 'gc_cache', 'rebuild', trailingslashit( $meta[ 'uri' ] ) );
}
}
Expand Down
8 changes: 2 additions & 6 deletions wp-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,7 @@ function admin_bar_delete_page() {
return false; // Directory not found. Probably not cached.
if ( false == wp_cache_confirm_delete( $path ) || substr( $path, 0, strlen( get_supercache_dir() ) ) != get_supercache_dir() )
die( "Could not delete directory" );
$files = get_all_supercache_filenames( $path );
foreach( $files as $cache_file )
prune_super_cache( $path . $cache_file, true );

wpsc_delete_files( $path );
wp_redirect( preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', $_GET[ 'path' ] ) );
die();
}
Expand Down Expand Up @@ -2482,8 +2479,7 @@ function wp_cache_files() {
$supercacheuri = trailingslashit( realpath( $cache_path . 'supercache/' . $supercacheuri ) );
if ( wp_cache_confirm_delete( $supercacheuri ) ) {
printf( __( "Deleting supercache file: <strong>%s</strong><br />", 'wp-super-cache' ), $supercacheuri );
@unlink( $supercacheuri . 'index.html' );
@unlink( $supercacheuri . 'index.html.gz' );
wpsc_delete_files( $supercacheuri );
prune_super_cache( $supercacheuri . 'page', true );
@rmdir( $supercacheuri );
}
Expand Down

0 comments on commit 41d582b

Please sign in to comment.