Skip to content

Commit ea3ed36

Browse files
authored
Fix: normalize $post_id and skip id 0 in post edit/change cache hooks (#1070)
wp_cache_post_edit() and wp_cache_post_change() assumed $post_id was always an integer. Defensively normalize a WP_Post object down to its ID, cast to int, and bail out early when the id is 0 so a stray zero-id invalidation can't wipe the front-page/root cache. Refs #959
1 parent 8aa4714 commit ea3ed36

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

wp-cache-phase2.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3287,6 +3287,16 @@ function wp_cache_post_edit( $post_id ) {
32873287
global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir;
32883288
static $last_post_edited = -1;
32893289

3290+
// Normalize $post_id to always be an integer (handles both int and WP_Post object)
3291+
if ( is_object( $post_id ) && isset( $post_id->ID ) ) {
3292+
$post_id = $post_id->ID;
3293+
}
3294+
$post_id = (int) $post_id;
3295+
3296+
if ( $post_id === 0 ) {
3297+
return $post_id;
3298+
}
3299+
32903300
if ( $post_id == $last_post_edited ) {
32913301
$action = current_filter();
32923302
wp_cache_debug( "wp_cache_post_edit({$action}): Already processed post $post_id.", 4 );
@@ -3360,6 +3370,16 @@ function wp_cache_post_change( $post_id ) {
33603370
global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $wp_cache_refresh_single_only;
33613371
static $last_processed = -1;
33623372

3373+
// Normalize $post_id to always be an integer (handles both int and WP_Post object)
3374+
if ( is_object( $post_id ) && isset( $post_id->ID ) ) {
3375+
$post_id = $post_id->ID;
3376+
}
3377+
$post_id = (int) $post_id;
3378+
3379+
if ( $post_id === 0 ) {
3380+
return $post_id;
3381+
}
3382+
33633383
if ( $post_id == $last_processed ) {
33643384
$action = current_filter();
33653385
wp_cache_debug( "wp_cache_post_change({$action}): Already processed post $post_id.", 4 );

0 commit comments

Comments
 (0)