Comments: Improve caching for hierarchical queries.

Hierarchical comment queries work by first fetching the IDs of top-level
comments, and then filling the descendant tree one level at a time based on the
top-level results. When top-level comment IDs are found in the cache,
`WP_Comment_Query` does not generate the SQL used to fetch these comments. In
this case, the `fill_descendants()` query does not have enough information
to fill children. As a result, descendant comments were failing to be filled
in cases where the top-level comments were found in the cache.

This was a minor bug previously, because comment caches were not maintained
between pageloads. Since comment caches are now persistent [37613], the problem
becomes evident anywhere that a persistent object cache is in use.

The solution is to cache parent-child relationships, so that when top-level
comments are found in the cache, descendant comments should be found there as
well.

Fixes #36487.

git-svn-id: https://develop.svn.wordpress.org/trunk@37625 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-06-02 18:27:43 +00:00
parent 230f2986fb
commit 1f5147bf83
2 changed files with 86 additions and 11 deletions
+45
View File
@@ -2430,6 +2430,51 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$clauses['where'] .= $wpdb->prepare( ' AND comment_ID != %d AND comment_ID != %d', $this->to_exclude[0], $this->to_exclude[1] );
return $clauses;
}
/**
* @ticket 36487
*/
public function test_cache_should_be_hit_when_querying_descendants() {
global $wpdb;
$p = self::factory()->post->create();
$comment_1 = self::factory()->comment->create( array(
'comment_post_ID' => $p,
'comment_approved' => '1',
) );
$comment_2 = self::factory()->comment->create( array(
'comment_post_ID' => $p,
'comment_approved' => '1',
'comment_parent' => $comment_1,
) );
$comment_3 = self::factory()->comment->create( array(
'comment_post_ID' => $p,
'comment_approved' => '1',
'comment_parent' => $comment_1,
) );
$comment_4 = self::factory()->comment->create( array(
'comment_post_ID' => $p,
'comment_approved' => '1',
'comment_parent' => $comment_2,
) );
$q1 = new WP_Comment_Query( array(
'post_id' => $p,
'hierarchical' => true,
) );
$q1_ids = wp_list_pluck( $q1->comments, 'comment_ID' );
$num_queries = $wpdb->num_queries;
$q2 = new WP_Comment_Query( array(
'post_id' => $p,
'hierarchical' => true,
) );
$q2_ids = wp_list_pluck( $q2->comments, 'comment_ID' );
$this->assertEqualSets( $q1_ids, $q2_ids );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
/**
* @ticket 27571
*/