diff --git src/wp-includes/class-wp-network-query.php src/wp-includes/class-wp-network-query.php
index 9db0a60462..a12656c702 100644
|
|
|
class WP_Network_Query { |
| 197 | 197 | */ |
| 198 | 198 | do_action_ref_array( 'pre_get_networks', array( &$this ) ); |
| 199 | 199 | |
| 200 | | $network_ids = null; |
| | 200 | $network_data = null; |
| 201 | 201 | |
| 202 | 202 | /** |
| 203 | | * Filter the sites array before the query takes place. |
| | 203 | * Filter the network data before the query takes place. |
| 204 | 204 | * |
| 205 | | * Return a non-null value to bypass WordPress's default site queries. |
| | 205 | * Return a non-null value to bypass WordPress's default network queries. |
| | 206 | * |
| | 207 | * The expected return type from this filter depends on the value passed in the request query_vars. |
| | 208 | * When `$this->query_vars['count']` is set, the filter should return the network count as an int. |
| | 209 | * When `'ids' === $this->query_vars['fields']`, the filter should return an array of network ids. |
| | 210 | * Otherwise the filter should return an array of WP_Network objects. |
| 206 | 211 | * |
| 207 | 212 | * @since 5.2.0 |
| 208 | 213 | * |
| 209 | | * @param array|null $site_ids Return an array of site data to short-circuit WP's site query, |
| 210 | | * or null to allow WP to run its normal queries. |
| 211 | | * @param WP_Network_Query $this The WP_Network_Query instance, passed by reference. |
| | 214 | * @param array|null $network_data Return an array of network data to short-circuit WP's network query, |
| | 215 | * the network count as an integer if `$this->query_vars['count']` is set, |
| | 216 | * or null to allow WP to run its normal queries. |
| | 217 | * @param WP_Network_Query $this The WP_Network_Query instance, passed by reference. |
| 212 | 218 | */ |
| 213 | | $network_ids = apply_filters_ref_array( 'networks_pre_query', array( $network_ids, &$this ) ); |
| 214 | | |
| 215 | | if ( null === $network_ids ) { |
| | 219 | $network_data = apply_filters_ref_array( 'networks_pre_query', array( $network_data, &$this ) ); |
| 216 | 220 | |
| 217 | | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| 218 | | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
| | 221 | if ( null !== $network_data ) { |
| | 222 | return $network_data; |
| | 223 | } |
| 219 | 224 | |
| 220 | | // Ignore the $fields argument as the queried result will be the same regardless. |
| 221 | | unset( $_args['fields'] ); |
| | 225 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| | 226 | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
| 222 | 227 | |
| 223 | | $key = md5( serialize( $_args ) ); |
| 224 | | $last_changed = wp_cache_get_last_changed( 'networks' ); |
| | 228 | // Ignore the $fields argument as the queried result will be the same regardless. |
| | 229 | unset( $_args['fields'] ); |
| 225 | 230 | |
| 226 | | $cache_key = "get_network_ids:$key:$last_changed"; |
| 227 | | $cache_value = wp_cache_get( $cache_key, 'networks' ); |
| | 231 | $key = md5( serialize( $_args ) ); |
| | 232 | $last_changed = wp_cache_get_last_changed( 'networks' ); |
| 228 | 233 | |
| 229 | | if ( false === $cache_value ) { |
| 230 | | $network_ids = $this->get_network_ids(); |
| 231 | | if ( $network_ids ) { |
| 232 | | $this->set_found_networks(); |
| 233 | | } |
| | 234 | $cache_key = "get_network_ids:$key:$last_changed"; |
| | 235 | $cache_value = wp_cache_get( $cache_key, 'networks' ); |
| 234 | 236 | |
| 235 | | $cache_value = array( |
| 236 | | 'network_ids' => $network_ids, |
| 237 | | 'found_networks' => $this->found_networks, |
| 238 | | ); |
| 239 | | wp_cache_add( $cache_key, $cache_value, 'networks' ); |
| 240 | | } else { |
| 241 | | $network_ids = $cache_value['network_ids']; |
| 242 | | $this->found_networks = $cache_value['found_networks']; |
| | 237 | if ( false === $cache_value ) { |
| | 238 | $network_ids = $this->get_network_ids(); |
| | 239 | if ( $network_ids ) { |
| | 240 | $this->set_found_networks(); |
| 243 | 241 | } |
| | 242 | |
| | 243 | $cache_value = array( |
| | 244 | 'network_ids' => $network_ids, |
| | 245 | 'found_networks' => $this->found_networks, |
| | 246 | ); |
| | 247 | wp_cache_add( $cache_key, $cache_value, 'networks' ); |
| | 248 | } else { |
| | 249 | $network_ids = $cache_value['network_ids']; |
| | 250 | $this->found_networks = $cache_value['found_networks']; |
| 244 | 251 | } |
| 245 | 252 | |
| 246 | 253 | 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 ec42cde2d2..64b139dbbf 100644
|
|
|
class WP_Site_Query { |
| 288 | 288 | $this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this ); |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | | $site_ids = null; |
| | 291 | $site_data = null; |
| 292 | 292 | |
| 293 | 293 | /** |
| 294 | | * Filter the sites array before the query takes place. |
| | 294 | * Filter the site data before the get_sites query takes place. |
| 295 | 295 | * |
| 296 | 296 | * Return a non-null value to bypass WordPress's default site queries. |
| 297 | 297 | * |
| | 298 | * The expected return type from this filter depends on the value passed in the request query_vars: |
| | 299 | * When `$this->query_vars['count']` is set, the filter should return the site count as an int. |
| | 300 | * When `'ids' == $this->query_vars['fields']`, the filter should return an array of site ids. |
| | 301 | * Otherwise the filter should return an array of WP_Site objects. |
| | 302 | * |
| 298 | 303 | * @since 5.2.0 |
| 299 | 304 | * |
| 300 | | * @param array|null $site_ids Return an array of site data to short-circuit WP's site query, |
| 301 | | * or null to allow WP to run its normal queries. |
| 302 | | * @param WP_Site_Query $this The WP_Site_Query instance, passed by reference. |
| | 305 | * @param array|int|null $site_data Return an array of site data to short-circuit WP's site query, |
| | 306 | * the site count as an integer if `$this->query_vars['count']` is set, |
| | 307 | * or null to run the normal queries. |
| | 308 | * @param WP_Site_Query $this The WP_Site_Query instance, passed by reference. |
| 303 | 309 | */ |
| 304 | | $site_ids = apply_filters_ref_array( 'sites_pre_query', array( $site_ids, &$this ) ); |
| 305 | | |
| 306 | | if ( null === $site_ids ) { |
| | 310 | $site_data = apply_filters_ref_array( 'sites_pre_query', array( $site_data, &$this ) ); |
| 307 | 311 | |
| 308 | | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| 309 | | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
| | 312 | if ( null !== $site_data ) { |
| | 313 | return $site_data; |
| | 314 | } |
| 310 | 315 | |
| 311 | | // Ignore the $fields argument as the queried result will be the same regardless. |
| 312 | | unset( $_args['fields'] ); |
| | 316 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| | 317 | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
| 313 | 318 | |
| 314 | | $key = md5( serialize( $_args ) ); |
| 315 | | $last_changed = wp_cache_get_last_changed( 'sites' ); |
| | 319 | // Ignore the $fields argument as the queried result will be the same regardless. |
| | 320 | unset( $_args['fields'] ); |
| 316 | 321 | |
| 317 | | $cache_key = "get_sites:$key:$last_changed"; |
| 318 | | $cache_value = wp_cache_get( $cache_key, 'sites' ); |
| | 322 | $key = md5( serialize( $_args ) ); |
| | 323 | $last_changed = wp_cache_get_last_changed( 'sites' ); |
| 319 | 324 | |
| 320 | | if ( false === $cache_value ) { |
| 321 | | $site_ids = $this->get_site_ids(); |
| 322 | | if ( $site_ids ) { |
| 323 | | $this->set_found_sites(); |
| 324 | | } |
| | 325 | $cache_key = "get_sites:$key:$last_changed"; |
| | 326 | $cache_value = wp_cache_get( $cache_key, 'sites' ); |
| 325 | 327 | |
| 326 | | $cache_value = array( |
| 327 | | 'site_ids' => $site_ids, |
| 328 | | 'found_sites' => $this->found_sites, |
| 329 | | ); |
| 330 | | wp_cache_add( $cache_key, $cache_value, 'sites' ); |
| 331 | | } else { |
| 332 | | $site_ids = $cache_value['site_ids']; |
| 333 | | $this->found_sites = $cache_value['found_sites']; |
| | 328 | if ( false === $cache_value ) { |
| | 329 | $site_ids = $this->get_site_ids(); |
| | 330 | if ( $site_ids ) { |
| | 331 | $this->set_found_sites(); |
| 334 | 332 | } |
| | 333 | |
| | 334 | $cache_value = array( |
| | 335 | 'site_ids' => $site_ids, |
| | 336 | 'found_sites' => $this->found_sites, |
| | 337 | ); |
| | 338 | wp_cache_add( $cache_key, $cache_value, 'sites' ); |
| | 339 | } else { |
| | 340 | $site_ids = $cache_value['site_ids']; |
| | 341 | $this->found_sites = $cache_value['found_sites']; |
| 335 | 342 | } |
| 336 | 343 | |
| 337 | 344 | 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 | |