From 37a8fe8b9b22268965f652fe8a34de626050c696 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sun, 30 Jul 2017 14:55:17 +0000 Subject: [PATCH] Comments: Ignore the 'fields' parameter in the comment query cache. `WP_Comment_Query` always queries runs an ID query, and so is unaffected by the 'fields' parameter. As such, 'fields' can be ignored when building a cache key for the results of the ID query. Props spacedmonkey. Fixes #41348. git-svn-id: https://develop.svn.wordpress.org/trunk@41190 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-comment-query.php | 11 +++- tests/phpunit/tests/comment/query.php | 74 ++++++++++++++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 4365e1753f..c67584c908 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -375,10 +375,15 @@ class WP_Comment_Query { $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); } - // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. - $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); - $last_changed = wp_cache_get_last_changed( 'comment' ); + /* + * Only use the args defined in the query_var_defaults to compute the key, + * but ignore 'fields', which does not affect query results. + */ + $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); + unset( $_args['fields'] ); + $key = md5( serialize( $_args ) ); + $last_changed = wp_cache_get_last_changed( 'comment' ); $cache_key = "get_comments:$key:$last_changed"; $cache_value = wp_cache_get( $cache_key, 'comment' ); diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 60e111d544..1d17603f86 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -2877,4 +2877,78 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertSame( $num_queries, $wpdb->num_queries ); $this->assertEqualSets( array( $c ), $q->comments ); } + + /** + * @ticket 41348 + */ + public function test_count_query_should_miss_noncount_cache() { + global $wpdb; + + $q = new WP_Comment_Query(); + + $query_1 = $q->query( array( + 'fields' => 'ids', + 'number' => 3, + 'order' => 'ASC', + ) ); + + $number_of_queries = $wpdb->num_queries; + + $query_2 = $q->query( array( + 'fields' => 'ids', + 'number' => 3, + 'order' => 'ASC', + 'count' => true, + ) ); + $this->assertEquals( $number_of_queries + 1, $wpdb->num_queries ); + } + + /** + * @ticket 41348 + */ + public function test_count_query_should_hit_count_cache() { + global $wpdb; + + $q = new WP_Comment_Query(); + + $query_1 = $q->query( array( + 'fields' => 'ids', + 'number' => 3, + 'order' => 'ASC', + 'count' => true, + ) ); + $number_of_queries = $wpdb->num_queries; + + $query_2 = $q->query( array( + 'fields' => 'ids', + 'number' => 3, + 'order' => 'ASC', + 'count' => true, + ) ); + $this->assertEquals( $number_of_queries, $wpdb->num_queries ); + } + + /** + * @ticket 41348 + */ + public function test_different_values_of_fields_should_share_cached_values() { + global $wpdb; + + $q = new WP_Comment_Query(); + + $query_1 = $q->query( array( + 'fields' => 'all', + 'number' => 3, + 'order' => 'ASC', + ) ); + $number_of_queries = $wpdb->num_queries; + + $query_2 = $q->query( array( + 'fields' => 'ids', + 'number' => 3, + 'order' => 'ASC', + ) ); + + $this->assertEquals( $number_of_queries, $wpdb->num_queries ); + } }