`WP_Comment_Query`: Fill comment objects from database when cache is unavailable.

This fixes a bug where widgets loaded in a preview or the Customizer are
rendered inside of a `wp_suspend_cache_addition()` block and thus could not
find comment objects in the cache.

Props rommelxcastro, stevehenty.
Fixes #34138.

git-svn-id: https://develop.svn.wordpress.org/trunk@35512 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-11-04 21:09:01 +00:00
parent 74b68afa3f
commit c8dd2d480d
2 changed files with 46 additions and 1 deletions

View File

@ -412,7 +412,7 @@ class WP_Comment_Query {
// Fetch full comment objects from the primed cache.
$_comments = array();
foreach ( $comment_ids as $comment_id ) {
if ( $_comment = wp_cache_get( $comment_id, 'comment' ) ) {
if ( $_comment = get_comment( $comment_id ) ) {
$_comments[] = $_comment;
}
}

View File

@ -2147,4 +2147,49 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertTrue( isset( $q->comments[0]->post_name ) );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 34138
*/
public function test_comment_objects_should_be_filled_from_cache() {
global $wpdb;
$comments = self::factory()->comment->create_many( 3, array( 'comment_post_ID' => $this->post_id ) );
clean_comment_cache( $comments );
$num_queries = $wpdb->num_queries;
$q = new WP_Comment_Query( array(
'post_id' => $this->post_id,
'no_found_rows' => true,
'update_comment_post_cache' => false,
'update_comment_meta_cache' => false,
) );
// 2 queries should have been fired: one for IDs, one to prime comment caches.
$num_queries += 2;
$found = wp_list_pluck( $q->comments, 'comment_ID' );
$this->assertEqualSets( $comments, $found );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 34138
*/
public function test_comment_objects_should_be_fetched_from_database_when_suspend_cache_addition() {
$suspend = wp_suspend_cache_addition();
wp_suspend_cache_addition( true );
$c = self::factory()->comment->create( array( 'comment_post_ID' => $this->post_id ) );
$q = new WP_Comment_Query( array(
'post_id' => $this->post_id,
) );
wp_suspend_cache_addition( $suspend );
$found = wp_list_pluck( $q->comments, 'comment_ID' );
$this->assertEqualSets( array( $c ), $found );
}
}