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
This commit is contained in:
Boone Gorges 2017-07-30 14:55:17 +00:00
parent 56ba8d18f9
commit 37a8fe8b9b
2 changed files with 82 additions and 3 deletions

View File

@ -375,10 +375,15 @@ class WP_Comment_Query {
$this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); $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 ) ) ) ); * Only use the args defined in the query_var_defaults to compute the key,
$last_changed = wp_cache_get_last_changed( 'comment' ); * 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_key = "get_comments:$key:$last_changed";
$cache_value = wp_cache_get( $cache_key, 'comment' ); $cache_value = wp_cache_get( $cache_key, 'comment' );

View File

@ -2877,4 +2877,78 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertSame( $num_queries, $wpdb->num_queries ); $this->assertSame( $num_queries, $wpdb->num_queries );
$this->assertEqualSets( array( $c ), $q->comments ); $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 );
}
} }