Make WordPress Core

Ticket #47599: 47599.3.diff

File 47599.3.diff, 7.8 KB (added by adamsilverstein, 7 years ago)
  • src/wp-includes/class-wp-network-query.php

    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 { 
    213213                 */
    214214                $network_ids = apply_filters_ref_array( 'networks_pre_query', array( $network_ids, &$this ) );
    215215
    216                 if ( null === $network_ids ) {
     216                if ( null !== $network_ids ) {
     217                        $this->networks = $network_ids;
    217218
    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                }
    220221
    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 ) );
    223224
    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'] );
    226227
    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' );
    229230
    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' );
    235233
    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();
    244238                        }
     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'];
    245248                }
    246249
    247250                if ( $this->found_networks && $this->query_vars['number'] ) {
  • src/wp-includes/class-wp-site-query.php

    diff --git src/wp-includes/class-wp-site-query.php src/wp-includes/class-wp-site-query.php
    index ae91c8269e..5925c9239e 100644
    class WP_Site_Query { 
    288288                        $this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
    289289                }
    290290
    291                 $site_ids = null;
     291                $site_data = null;
    292292
    293293                /**
    294                  * Filter the sites array before the query takes place.
     294                 * Filter the site data before the get_sites query takes place.
    295295                 *
    296296                 * Return a non-null value to bypass WordPress's default site queries.
    297297                 *
     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.
    298302                 *
    299303                 * @since 5.2.0
    300304                 *
    301                  * @param array|null    $site_ids Return an array of site data to short-circuit WP's site query,
    302                  *                                or null to allow WP to run its normal queries.
    303                  * @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 (ints, objects or a count) to short-circuit
     306                 *                                  WordPress's site query, or null to run the normal queries.
     307                 * @param WP_Site_Query  $this      The WP_Site_Query instance, passed by reference.
    304308                 */
    305                 $site_ids = apply_filters_ref_array( 'sites_pre_query', array( $site_ids, &$this ) );
     309                $site_data = apply_filters_ref_array( 'sites_pre_query', array( $site_data, &$this ) );
    306310
    307                 if ( null === $site_ids ) {
    308 
    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 ) );
     311                if ( null !== $site_data ) {
     312                        return $site_data;
     313                }
    311314
    312                         // Ignore the $fields argument as the queried result will be the same regardless.
    313                         unset( $_args['fields'] );
     315                // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
     316                $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    314317
    315                         $key          = md5( serialize( $_args ) );
    316                         $last_changed = wp_cache_get_last_changed( 'sites' );
     318                // Ignore the $fields argument as the queried result will be the same regardless.
     319                unset( $_args['fields'] );
    317320
    318                         $cache_key   = "get_sites:$key:$last_changed";
    319                         $cache_value = wp_cache_get( $cache_key, 'sites' );
     321                $key          = md5( serialize( $_args ) );
     322                $last_changed = wp_cache_get_last_changed( 'sites' );
    320323
    321                         if ( false === $cache_value ) {
    322                                 $site_ids = $this->get_site_ids();
    323                                 if ( $site_ids ) {
    324                                         $this->set_found_sites();
    325                                 }
     324                $cache_key   = "get_sites:$key:$last_changed";
     325                $cache_value = wp_cache_get( $cache_key, 'sites' );
    326326
    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'];
     327                if ( false === $cache_value ) {
     328                        $site_ids = $this->get_site_ids();
     329                        if ( $site_ids ) {
     330                                $this->set_found_sites();
    335331                        }
     332
     333                        $cache_value = array(
     334                                'site_ids'    => $site_ids,
     335                                'found_sites' => $this->found_sites,
     336                        );
     337                        wp_cache_add( $cache_key, $cache_value, 'sites' );
     338                } else {
     339                        $site_ids          = $cache_value['site_ids'];
     340                        $this->found_sites = $cache_value['found_sites'];
    336341                }
    337342
    338343                if ( $this->found_sites && $this->query_vars['number'] ) {
  • tests/phpunit/tests/multisite/networkQuery.php

    diff --git tests/phpunit/tests/multisite/networkQuery.php tests/phpunit/tests/multisite/networkQuery.php
    index 0b48fdc8d5..8953012386 100644
    if ( is_multisite() ) : 
    525525
    526526                /**
    527527                 * @ticket 45749
     528                 * @ticket 47599
    528529                 */
    529530                public function test_networks_pre_query_filter_should_bypass_database_query() {
    530531                        global $wpdb;
    if ( is_multisite() ) : 
    534535                        $num_queries = $wpdb->num_queries;
    535536
    536537                        $q       = new WP_Network_Query();
    537                         $results = $q->query(
    538                                 array(
    539                                         'fields' => 'ids',
    540                                 )
    541                         );
     538                        $results = $q->query( array() );
    542539
    543540                        remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
    544541
  • tests/phpunit/tests/multisite/siteQuery.php

    diff --git tests/phpunit/tests/multisite/siteQuery.php tests/phpunit/tests/multisite/siteQuery.php
    index c17f977932..e368642675 100644
    if ( is_multisite() ) : 
    914914
    915915                /**
    916916                 * @ticket 45749
     917                 * @ticket 47599
    917918                 */
    918919                public function test_sites_pre_query_filter_should_bypass_database_query() {
    919920                        global $wpdb;
    if ( is_multisite() ) : 
    923924                        $num_queries = $wpdb->num_queries;
    924925
    925926                        $q       = new WP_Site_Query();
    926                         $results = $q->query(
    927                                 array(
    928                                         'fields' => 'ids',
    929                                 )
    930                         );
     927                        $results = $q->query( array() );
    931928
    932929                        remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
    933930