From c8dd2d480d569907f6acd3338a07ad3e85fdda3d Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 4 Nov 2015 21:09:01 +0000 Subject: [PATCH] `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 --- src/wp-includes/class-wp-comment-query.php | 2 +- tests/phpunit/tests/comment/query.php | 45 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 11376d0003..1a8bff8d6b 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -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; } } diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 4ccfe8c93b..5d250eac90 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -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 ); + } }