From 03b9931b7a5dda8df4a2dd18f505ffb03708467e Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 17 Jul 2017 20:15:27 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-network-query.php | 7 +- .../phpunit/tests/multisite/networkQuery.php | 73 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-network-query.php b/src/wp-includes/class-wp-network-query.php index dfef0b3322..646491fbec 100644 --- a/src/wp-includes/class-wp-network-query.php +++ b/src/wp-includes/class-wp-network-query.php @@ -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"; diff --git a/tests/phpunit/tests/multisite/networkQuery.php b/tests/phpunit/tests/multisite/networkQuery.php index bec849ba28..9b5d9e85d1 100644 --- a/tests/phpunit/tests/multisite/networkQuery.php +++ b/tests/phpunit/tests/multisite/networkQuery.php @@ -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;