Skip to content

Commit 2fc4e48

Browse files
committed
Comments: Don't do direct SQL query when fetching decendants.
The SQL query was built using the clauses compiled when querying for top-level comments. But in cases where the top-level comment query results are already in the cache, the SQL clauses are not built, and so are unavailable for `fill_descendants()`. Instead, we call `get_comments()`, using modified versions of the parameters passed to the main `WP_Comment_Query` class. Props Akeif, Rarst for testing. Fixes #37696. git-svn-id: https://develop.svn.wordpress.org/trunk@38446 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f80068e commit 2fc4e48

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/wp-includes/class-wp-comment-query.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,16 @@ protected function fill_descendants( $comments ) {
990990
}
991991

992992
if ( $uncached_parent_ids ) {
993-
$where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
994-
$level_comments = $this->db->get_results( "SELECT {$this->db->comments}.comment_ID, {$this->db->comments}.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
993+
// Fetch this level of comments.
994+
$parent_query_args = $this->query_vars;
995+
foreach ( $exclude_keys as $exclude_key ) {
996+
$parent_query_args[ $exclude_key ] = '';
997+
}
998+
$parent_query_args['parent__in'] = $uncached_parent_ids;
999+
$parent_query_args['no_found_rows'] = true;
1000+
$parent_query_args['hierarchical'] = false;
1001+
1002+
$level_comments = get_comments( $parent_query_args );
9951003

9961004
// Cache parent-child relationships.
9971005
$parent_map = array_fill_keys( $uncached_parent_ids, array() );

tests/phpunit/tests/comment/query.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,6 +2494,55 @@ public function test_cache_should_be_hit_when_querying_descendants() {
24942494
$this->assertSame( $num_queries, $wpdb->num_queries );
24952495
}
24962496

2497+
/**
2498+
* @ticket 37696
2499+
*/
2500+
public function test_hierarchy_should_be_filled_when_cache_is_incomplete() {
2501+
global $wpdb;
2502+
2503+
$p = self::factory()->post->create();
2504+
$comment_1 = self::factory()->comment->create( array(
2505+
'comment_post_ID' => $p,
2506+
'comment_approved' => '1',
2507+
) );
2508+
$comment_2 = self::factory()->comment->create( array(
2509+
'comment_post_ID' => $p,
2510+
'comment_approved' => '1',
2511+
'comment_parent' => $comment_1,
2512+
) );
2513+
$comment_3 = self::factory()->comment->create( array(
2514+
'comment_post_ID' => $p,
2515+
'comment_approved' => '1',
2516+
'comment_parent' => $comment_1,
2517+
) );
2518+
$comment_4 = self::factory()->comment->create( array(
2519+
'comment_post_ID' => $p,
2520+
'comment_approved' => '1',
2521+
'comment_parent' => $comment_2,
2522+
) );
2523+
2524+
// Prime cache.
2525+
$q1 = new WP_Comment_Query( array(
2526+
'post_id' => $p,
2527+
'hierarchical' => true,
2528+
) );
2529+
$q1_ids = wp_list_pluck( $q1->comments, 'comment_ID' );
2530+
$this->assertEqualSets( array( $comment_1, $comment_2, $comment_3, $comment_4 ), $q1_ids );
2531+
2532+
// Delete one of the parent caches.
2533+
$last_changed = wp_cache_get( 'last_changed', 'comment' );
2534+
$key = md5( serialize( wp_array_slice_assoc( $q1->query_vars, array_keys( $q1->query_var_defaults ) ) ) );
2535+
$cache_key = "get_comment_child_ids:$comment_2:$key:$last_changed";
2536+
wp_cache_delete( $cache_key, 'comment' );
2537+
2538+
$q2 = new WP_Comment_Query( array(
2539+
'post_id' => $p,
2540+
'hierarchical' => true,
2541+
) );
2542+
$q2_ids = wp_list_pluck( $q2->comments, 'comment_ID' );
2543+
$this->assertEqualSets( $q1_ids, $q2_ids );
2544+
}
2545+
24972546
/**
24982547
* @ticket 27571
24992548
*/

0 commit comments

Comments
 (0)