Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions src/wp-includes/class-wp-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,14 @@ public static function get_instance( $id ) {
return false;
}

// Not wp_cache_add(), since an unusable cached value may still be present and must be replaced.
wp_cache_set( $_comment->comment_ID, $_comment, 'comment' );
/*
* Not wp_cache_add(), since an unusable cached value may still be present and must be
* replaced. add() checks wp_suspend_cache_addition() and set() does not, so the check
* moves to the call site.
Comment on lines +277 to +279

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Not wp_cache_add(), since an unusable cached value may still be present and must be
* replaced. add() checks wp_suspend_cache_addition() and set() does not, so the check
* moves to the call site.
* Not wp_cache_add(), since an unusable cached value may still be present and must be replaced.
* Note that wp_cache_add() checks wp_suspend_cache_addition() and wp_cache_set() does not.

Could be applied to the other instances below as well.

*/
if ( ! wp_suspend_cache_addition() ) {
wp_cache_set( $_comment->comment_ID, $_comment, 'comment' );
}
}

return new WP_Comment( $_comment );
Expand Down
10 changes: 8 additions & 2 deletions src/wp-includes/class-wp-network.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ public static function get_instance( $network_id ) {
$_network = -1;
}

// Not wp_cache_add(), since an unusable cached value may still be present and must be replaced.
wp_cache_set( $network_id, $_network, 'networks' );
/*
* Not wp_cache_add(), since an unusable cached value may still be present and must be
* replaced. add() checks wp_suspend_cache_addition() and set() does not, so the check
* moves to the call site.
*/
if ( ! wp_suspend_cache_addition() ) {
wp_cache_set( $network_id, $_network, 'networks' );
}
}

if ( is_numeric( $_network ) ) {
Expand Down
10 changes: 8 additions & 2 deletions src/wp-includes/class-wp-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,14 @@ public static function get_instance( $post_id ) {

$_post = sanitize_post( $_post, 'raw' );

// Not wp_cache_add(), since an unusable cached value may still be present and must be replaced.
wp_cache_set( (int) $_post->ID, $_post, 'posts' );
/*
* Not wp_cache_add(), since an unusable cached value may still be present and must be
* replaced. add() checks wp_suspend_cache_addition() and set() does not, so the check
* moves to the call site.
*/
if ( ! wp_suspend_cache_addition() ) {
wp_cache_set( (int) $_post->ID, $_post, 'posts' );
}
} elseif ( empty( $_post->filter ) || 'raw' !== $_post->filter ) {
$_post = sanitize_post( $_post, 'raw' );
}
Expand Down
10 changes: 8 additions & 2 deletions src/wp-includes/class-wp-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,14 @@ public static function get_instance( $site_id ) {
$_site = -1;
}

// Not wp_cache_add(), since an unusable cached value may still be present and must be replaced.
wp_cache_set( $site_id, $_site, 'sites' );
/*
* Not wp_cache_add(), since an unusable cached value may still be present and must be
* replaced. add() checks wp_suspend_cache_addition() and set() does not, so the check
* moves to the call site.
*/
if ( ! wp_suspend_cache_addition() ) {
wp_cache_set( $site_id, $_site, 'sites' );
}
}

if ( is_numeric( $_site ) ) {
Expand Down
10 changes: 8 additions & 2 deletions src/wp-includes/class-wp-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,14 @@ public static function get_instance( $term_id, $taxonomy = null ) {

// Don't cache terms that are shared between taxonomies.
if ( 1 === count( $terms ) ) {
// Not wp_cache_add(), since an unusable cached value may still be present and must be replaced.
wp_cache_set( $term_id, $_term, 'terms' );
/*
* Not wp_cache_add(), since an unusable cached value may still be present and must be
* replaced. add() checks wp_suspend_cache_addition() and set() does not, so the check
* moves to the call site.
*/
if ( ! wp_suspend_cache_addition() ) {
wp_cache_set( $term_id, $_term, 'terms' );
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/tests/comment/wpComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,25 @@ public function test_post_fields_should_be_null_for_an_unattached_comment(): voi
$this->assertFalse( $is_set, 'Expected __isset() to return false for an unattached comment.' );
$this->assertNull( $title, 'Expected __get() not to read the field from the global post.' );
}

/**
* Tests that a cache miss is not stored while cache addition is suspended.
*
* @ticket 66006
*/
public function test_get_instance_does_not_cache_a_miss_while_cache_addition_is_suspended(): void {
clean_comment_cache( self::$comment_id );
$this->assertFalse( wp_cache_get( self::$comment_id, 'comment' ), 'The comment was cached before the test began.' );

$suspend = wp_suspend_cache_addition();
wp_suspend_cache_addition( true );

$comment = WP_Comment::get_instance( self::$comment_id );

wp_suspend_cache_addition( $suspend );

$this->assertInstanceOf( WP_Comment::class, $comment, 'A comment object was not returned.' );
$this->assertSame( self::$comment_id, (int) $comment->comment_ID, 'The wrong comment was returned.' );
$this->assertFalse( wp_cache_get( self::$comment_id, 'comment' ), 'The comment was added to the cache.' );
}
}
23 changes: 23 additions & 0 deletions tests/phpunit/tests/multisite/network.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,4 +760,27 @@ protected function _get_next_network_id() {
static::factory()->network->create();
return (int) $wpdb->get_var( 'SELECT id FROM ' . $wpdb->site . ' ORDER BY id DESC LIMIT 1' ) + 1;
}

/**
* Tests that a cache miss is not stored while cache addition is suspended.
*
* @ticket 66006
*
* @covers WP_Network::get_instance
*/
public function test_get_instance_does_not_cache_a_miss_while_cache_addition_is_suspended(): void {
clean_network_cache( self::$different_network_id );
$this->assertFalse( wp_cache_get( self::$different_network_id, 'networks' ), 'The network was cached before the test began.' );

$suspend = wp_suspend_cache_addition();
wp_suspend_cache_addition( true );

$network = WP_Network::get_instance( self::$different_network_id );

wp_suspend_cache_addition( $suspend );

$this->assertInstanceOf( WP_Network::class, $network, 'A network object was not returned.' );
$this->assertSame( self::$different_network_id, (int) $network->id, 'The wrong network was returned.' );
$this->assertFalse( wp_cache_get( self::$different_network_id, 'networks' ), 'The network was added to the cache.' );
}
}
21 changes: 21 additions & 0 deletions tests/phpunit/tests/multisite/wpSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,25 @@ public function data_get_instance_treats_a_numeric_cache_value_as_a_cached_miss(
'a float as a string' => array( '3.5' ),
);
}

/**
* Tests that a cache miss is not stored while cache addition is suspended.
*
* @ticket 66006
*/
public function test_get_instance_does_not_cache_a_miss_while_cache_addition_is_suspended(): void {
clean_blog_cache( self::$site_id );
$this->assertFalse( wp_cache_get( self::$site_id, 'sites' ), 'The site was cached before the test began.' );

$suspend = wp_suspend_cache_addition();
wp_suspend_cache_addition( true );

$site = WP_Site::get_instance( self::$site_id );

wp_suspend_cache_addition( $suspend );

$this->assertInstanceOf( WP_Site::class, $site, 'A site object was not returned.' );
$this->assertSame( self::$site_id, (int) $site->blog_id, 'The wrong site was returned.' );
$this->assertFalse( wp_cache_get( self::$site_id, 'sites' ), 'The site was added to the cache.' );
}
}
23 changes: 23 additions & 0 deletions tests/phpunit/tests/post/wpPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,27 @@ public function data_get_instance_treats_a_poisoned_cache_value_as_a_cache_miss(
'a WP_Post without ID' => array( new WP_Post( new stdClass() ) ),
);
}

/**
* Tests that a cache miss is not stored while cache addition is suspended.
*
* @ticket 66006
*
* @covers WP_Post::get_instance
*/
public function test_get_instance_does_not_cache_a_miss_while_cache_addition_is_suspended(): void {
clean_post_cache( self::$post_id );
$this->assertFalse( wp_cache_get( self::$post_id, 'posts' ), 'The post was cached before the test began.' );

$suspend = wp_suspend_cache_addition();
wp_suspend_cache_addition( true );

$post = WP_Post::get_instance( self::$post_id );

wp_suspend_cache_addition( $suspend );

$this->assertInstanceOf( WP_Post::class, $post, 'A post object was not returned.' );
$this->assertSame( self::$post_id, $post->ID, 'The wrong post was returned.' );
$this->assertFalse( wp_cache_get( self::$post_id, 'posts' ), 'The post was added to the cache.' );
}
}
23 changes: 23 additions & 0 deletions tests/phpunit/tests/term/wpTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,27 @@ public function data_get_instance_treats_a_poisoned_cache_value_as_a_cache_miss(
'a WP_Term without term_id' => array( new WP_Term( new stdClass() ) ),
);
}

/**
* Tests that a cache miss is not stored while cache addition is suspended.
*
* @ticket 66006
*
* @covers WP_Term::get_instance
*/
public function test_get_instance_does_not_cache_a_miss_while_cache_addition_is_suspended(): void {
clean_term_cache( self::$term_id, 'wptests_tax' );
$this->assertFalse( wp_cache_get( self::$term_id, 'terms' ), 'The term was cached before the test began.' );

$suspend = wp_suspend_cache_addition();
wp_suspend_cache_addition( true );

$term = WP_Term::get_instance( self::$term_id, 'wptests_tax' );

wp_suspend_cache_addition( $suspend );

$this->assertInstanceOf( WP_Term::class, $term, 'A term object was not returned.' );
$this->assertSame( self::$term_id, $term->term_id, 'The wrong term was returned.' );
$this->assertFalse( wp_cache_get( self::$term_id, 'terms' ), 'The term was added to the cache.' );
}
}
Loading