diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 0920ca229f..825e9d882b 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -995,8 +995,16 @@ class WP_Comment_Query { } if ( $uncached_parent_ids ) { - $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')'; - $level_comments = $wpdb->get_results( "SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" ); + // Fetch this level of comments. + $parent_query_args = $this->query_vars; + foreach ( $exclude_keys as $exclude_key ) { + $parent_query_args[ $exclude_key ] = ''; + } + $parent_query_args['parent__in'] = $uncached_parent_ids; + $parent_query_args['no_found_rows'] = true; + $parent_query_args['hierarchical'] = false; + + $level_comments = get_comments( $parent_query_args ); // Cache parent-child relationships. $parent_map = array_fill_keys( $uncached_parent_ids, array() ); diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 219e2d1933..1c4544bd0b 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -2498,6 +2498,55 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertSame( $num_queries, $wpdb->num_queries ); } + /** + * @ticket 37696 + */ + public function test_hierarchy_should_be_filled_when_cache_is_incomplete() { + 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, + ) ); + + // Prime cache. + $q1 = new WP_Comment_Query( array( + 'post_id' => $p, + 'hierarchical' => true, + ) ); + $q1_ids = wp_list_pluck( $q1->comments, 'comment_ID' ); + $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3, $comment_4 ), $q1_ids ); + + // Delete one of the parent caches. + $last_changed = wp_cache_get( 'last_changed', 'comment' ); + $key = md5( serialize( wp_array_slice_assoc( $q1->query_vars, array_keys( $q1->query_var_defaults ) ) ) ); + $cache_key = "get_comment_child_ids:$comment_2:$key:$last_changed"; + wp_cache_delete( $cache_key, 'comment' ); + + $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 ); + } + /** * @ticket 27571 */