From 3bd6670ca84e7f5643f11b62778beda0b86125de Mon Sep 17 00:00:00 2001 From: Donncha O Caoimh Date: Tue, 24 Oct 2017 13:15:26 +0000 Subject: [PATCH] If a CPT has no rewrite rule then don't clear the cache. Fixes #45 If a custom post type has no rewrite permalink then the plugin will clear the entire cache in wp_cache_post_id_gc() because get_current_url_supercache_dir() will strip the ?x=y from the URL, resulting in the URL being the homepage. The plugin then deletes everything below that. See also https://github.com/Automattic/wp-super-cache/pull/66 --- wp-cache-phase2.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/wp-cache-phase2.php b/wp-cache-phase2.php index 345062be..8f2db375 100644 --- a/wp-cache-phase2.php +++ b/wp-cache-phase2.php @@ -1437,9 +1437,13 @@ function wp_cache_post_id_gc( $post_id, $all = 'all' ) { $post_id = intval( $post_id ); if( $post_id == 0 ) - return; + return true; $permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( $post_id ) ) ); + if ( false !== strpos( $permalink, '?' ) ) { + wp_cache_debug( 'wp_cache_post_id_gc: NOT CLEARING CACHE. Permalink has a "?". ' . $permalink ); + return false; + } $dir = get_current_url_supercache_dir( $post_id ); wp_cache_debug( "wp_cache_post_id_gc post_id: $post_id " . get_permalink( $post_id ) . " clearing cache in $dir.", 4 ); if ( $all ) { @@ -1455,6 +1459,7 @@ function wp_cache_post_id_gc( $post_id, $all = 'all' ) { prune_super_cache( $dir, true, true ); do_action( 'gc_cache', 'prune', $permalink ); } + return true; } function wp_cache_post_change( $post_id ) { @@ -1500,7 +1505,11 @@ function wp_cache_post_change( $post_id ) { // Delete supercache files whenever a post change event occurs, even if supercache is currently disabled. $dir = get_supercache_dir(); // make sure the front page has a rebuild file - wp_cache_post_id_gc( $post_id, $all ); + if ( false == wp_cache_post_id_gc( $post_id, $all ) ) { + wp_cache_debug( "wp_cache_post_change: not deleting any cache files as GC of post returned false" ); + wp_cache_writers_exit(); + return false; + } if ( $all == true ) { wp_cache_debug( "Post change: supercache enabled: deleting cache files in " . $dir ); wpsc_rebuild_files( $dir );