diff --git src/wp-includes/class-wp-network-query.php src/wp-includes/class-wp-network-query.php
index 9a0f97a912..e13bf25e40 100644
|
|
|
class WP_Network_Query { |
| 213 | 213 | */ |
| 214 | 214 | $network_ids = apply_filters_ref_array( 'networks_pre_query', array( $network_ids, &$this ) ); |
| 215 | 215 | |
| 216 | | if ( null === $network_ids ) { |
| | 216 | if ( null !== $network_ids ) { |
| | 217 | $this->networks = $network_ids; |
| 217 | 218 | |
| 218 | | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| 219 | | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
| | 219 | return $this->networks; |
| | 220 | } |
| 220 | 221 | |
| 221 | | // Ignore the $fields argument as the queried result will be the same regardless. |
| 222 | | unset( $_args['fields'] ); |
| | 222 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| | 223 | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
| 223 | 224 | |
| 224 | | $key = md5( serialize( $_args ) ); |
| 225 | | $last_changed = wp_cache_get_last_changed( 'networks' ); |
| | 225 | // Ignore the $fields argument as the queried result will be the same regardless. |
| | 226 | unset( $_args['fields'] ); |
| 226 | 227 | |
| 227 | | $cache_key = "get_network_ids:$key:$last_changed"; |
| 228 | | $cache_value = wp_cache_get( $cache_key, 'networks' ); |
| | 228 | $key = md5( serialize( $_args ) ); |
| | 229 | $last_changed = wp_cache_get_last_changed( 'networks' ); |
| 229 | 230 | |
| 230 | | if ( false === $cache_value ) { |
| 231 | | $network_ids = $this->get_network_ids(); |
| 232 | | if ( $network_ids ) { |
| 233 | | $this->set_found_networks(); |
| 234 | | } |
| | 231 | $cache_key = "get_network_ids:$key:$last_changed"; |
| | 232 | $cache_value = wp_cache_get( $cache_key, 'networks' ); |
| 235 | 233 | |
| 236 | | $cache_value = array( |
| 237 | | 'network_ids' => $network_ids, |
| 238 | | 'found_networks' => $this->found_networks, |
| 239 | | ); |
| 240 | | wp_cache_add( $cache_key, $cache_value, 'networks' ); |
| 241 | | } else { |
| 242 | | $network_ids = $cache_value['network_ids']; |
| 243 | | $this->found_networks = $cache_value['found_networks']; |
| | 234 | if ( false === $cache_value ) { |
| | 235 | $network_ids = $this->get_network_ids(); |
| | 236 | if ( $network_ids ) { |
| | 237 | $this->set_found_networks(); |
| 244 | 238 | } |
| | 239 | |
| | 240 | $cache_value = array( |
| | 241 | 'network_ids' => $network_ids, |
| | 242 | 'found_networks' => $this->found_networks, |
| | 243 | ); |
| | 244 | wp_cache_add( $cache_key, $cache_value, 'networks' ); |
| | 245 | } else { |
| | 246 | $network_ids = $cache_value['network_ids']; |
| | 247 | $this->found_networks = $cache_value['found_networks']; |
| 245 | 248 | } |
| 246 | 249 | |
| 247 | 250 | if ( $this->found_networks && $this->query_vars['number'] ) { |
diff --git src/wp-includes/class-wp-site-query.php src/wp-includes/class-wp-site-query.php
index ae91c8269e..a42bf1f7c3 100644
|
|
|
class WP_Site_Query { |
| 304 | 304 | */ |
| 305 | 305 | $site_ids = apply_filters_ref_array( 'sites_pre_query', array( $site_ids, &$this ) ); |
| 306 | 306 | |
| 307 | | if ( null === $site_ids ) { |
| | 307 | if ( null !== $site_ids ) { |
| | 308 | $this->sites = $site_ids; |
| 308 | 309 | |
| 309 | | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| 310 | | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
| | 310 | return $this->sites; |
| | 311 | } |
| 311 | 312 | |
| 312 | | // Ignore the $fields argument as the queried result will be the same regardless. |
| 313 | | unset( $_args['fields'] ); |
| | 313 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| | 314 | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
| 314 | 315 | |
| 315 | | $key = md5( serialize( $_args ) ); |
| 316 | | $last_changed = wp_cache_get_last_changed( 'sites' ); |
| | 316 | // Ignore the $fields argument as the queried result will be the same regardless. |
| | 317 | unset( $_args['fields'] ); |
| 317 | 318 | |
| 318 | | $cache_key = "get_sites:$key:$last_changed"; |
| 319 | | $cache_value = wp_cache_get( $cache_key, 'sites' ); |
| | 319 | $key = md5( serialize( $_args ) ); |
| | 320 | $last_changed = wp_cache_get_last_changed( 'sites' ); |
| 320 | 321 | |
| 321 | | if ( false === $cache_value ) { |
| 322 | | $site_ids = $this->get_site_ids(); |
| 323 | | if ( $site_ids ) { |
| 324 | | $this->set_found_sites(); |
| 325 | | } |
| | 322 | $cache_key = "get_sites:$key:$last_changed"; |
| | 323 | $cache_value = wp_cache_get( $cache_key, 'sites' ); |
| 326 | 324 | |
| 327 | | $cache_value = array( |
| 328 | | 'site_ids' => $site_ids, |
| 329 | | 'found_sites' => $this->found_sites, |
| 330 | | ); |
| 331 | | wp_cache_add( $cache_key, $cache_value, 'sites' ); |
| 332 | | } else { |
| 333 | | $site_ids = $cache_value['site_ids']; |
| 334 | | $this->found_sites = $cache_value['found_sites']; |
| | 325 | if ( false === $cache_value ) { |
| | 326 | $site_ids = $this->get_site_ids(); |
| | 327 | if ( $site_ids ) { |
| | 328 | $this->set_found_sites(); |
| 335 | 329 | } |
| | 330 | |
| | 331 | $cache_value = array( |
| | 332 | 'site_ids' => $site_ids, |
| | 333 | 'found_sites' => $this->found_sites, |
| | 334 | ); |
| | 335 | wp_cache_add( $cache_key, $cache_value, 'sites' ); |
| | 336 | } else { |
| | 337 | $site_ids = $cache_value['site_ids']; |
| | 338 | $this->found_sites = $cache_value['found_sites']; |
| 336 | 339 | } |
| 337 | 340 | |
| 338 | 341 | if ( $this->found_sites && $this->query_vars['number'] ) { |
diff --git tests/phpunit/tests/multisite/networkQuery.php tests/phpunit/tests/multisite/networkQuery.php
index 0b48fdc8d5..8953012386 100644
|
|
|
if ( is_multisite() ) : |
| 525 | 525 | |
| 526 | 526 | /** |
| 527 | 527 | * @ticket 45749 |
| | 528 | * @ticket 47599 |
| 528 | 529 | */ |
| 529 | 530 | public function test_networks_pre_query_filter_should_bypass_database_query() { |
| 530 | 531 | global $wpdb; |
| … |
… |
if ( is_multisite() ) : |
| 534 | 535 | $num_queries = $wpdb->num_queries; |
| 535 | 536 | |
| 536 | 537 | $q = new WP_Network_Query(); |
| 537 | | $results = $q->query( |
| 538 | | array( |
| 539 | | 'fields' => 'ids', |
| 540 | | ) |
| 541 | | ); |
| | 538 | $results = $q->query( array() ); |
| 542 | 539 | |
| 543 | 540 | remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 ); |
| 544 | 541 | |
diff --git tests/phpunit/tests/multisite/siteQuery.php tests/phpunit/tests/multisite/siteQuery.php
index c17f977932..e368642675 100644
|
|
|
if ( is_multisite() ) : |
| 914 | 914 | |
| 915 | 915 | /** |
| 916 | 916 | * @ticket 45749 |
| | 917 | * @ticket 47599 |
| 917 | 918 | */ |
| 918 | 919 | public function test_sites_pre_query_filter_should_bypass_database_query() { |
| 919 | 920 | global $wpdb; |
| … |
… |
if ( is_multisite() ) : |
| 923 | 924 | $num_queries = $wpdb->num_queries; |
| 924 | 925 | |
| 925 | 926 | $q = new WP_Site_Query(); |
| 926 | | $results = $q->query( |
| 927 | | array( |
| 928 | | 'fields' => 'ids', |
| 929 | | ) |
| 930 | | ); |
| | 927 | $results = $q->query( array() ); |
| 931 | 928 | |
| 932 | 929 | remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 ); |
| 933 | 930 | |