Multisite: Improve caching in `WP_Network_Query` by ignoring the `$fields` argument.

Prior to this change there were two different cache keys used for the same query. That is because regardless of the `$fields` argument, the query response will be the same. This was already fixed for `WP_Site_Query` in [41059].

Props spacedmonkey.
Fixes #41347.


git-svn-id: https://develop.svn.wordpress.org/trunk@41063 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2017-07-17 20:15:27 +00:00
parent eb78c8e092
commit 03b9931b7a
2 changed files with 79 additions and 1 deletions

View File

@ -208,7 +208,12 @@ class WP_Network_Query {
do_action_ref_array( 'pre_get_networks', array( &$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 ) ) ) );
$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
// Ignore the $fields argument as the queried result will be the same regardless.
unset( $_args['fields'] );
$key = md5( serialize( $_args ) );
$last_changed = wp_cache_get_last_changed( 'networks' );
$cache_key = "get_network_ids:$key:$last_changed";

View File

@ -379,6 +379,79 @@ class Tests_Multisite_Network_Query extends WP_UnitTestCase {
$this->assertEquals( $expected, $found );
}
/**
* @ticket 41347
*/
public function test_wp_network_query_cache_with_different_fields_no_count() {
global $wpdb;
$q = new WP_Network_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 );
}
/**
* @ticket 41347
*/
public function test_wp_network_query_cache_with_different_fields_active_count() {
global $wpdb;
$q = new WP_Network_Query();
$query_1 = $q->query( array(
'fields' => 'all',
'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 41347
*/
public function test_wp_network_query_cache_with_same_fields_different_count() {
global $wpdb;
$q = new WP_Network_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 );
}
}
endif;