Make WordPress Core

Ticket #47599: 47599.2.diff

File 47599.2.diff, 6.5 KB (added by adamsilverstein, 6 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..a42bf1f7c3 100644
    class WP_Site_Query { 
    304304                 */
    305305                $site_ids = apply_filters_ref_array( 'sites_pre_query', array( $site_ids, &$this ) );
    306306
    307                 if ( null === $site_ids ) {
     307                if ( null !== $site_ids ) {
     308                        $this->sites = $site_ids;
    308309
    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                }
    311312
    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 ) );
    314315
    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'] );
    317318
    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' );
    320321
    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' );
    326324
    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();
    335329                        }
     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'];
    336339                }
    337340
    338341                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