Make WordPress Core


Ignore:
Timestamp:
09/12/2019 10:16:08 PM (4 years ago)
Author:
adamsilverstein
Message:

Multisite: improve sites_pre_query and networks_pre_query filters, avoiding db queries.

Improve the pre_query filters in multisite classes introduced in r44983. Return (non null) values immediately,
avoiding the database queries entirely, similar to other pre_query filters.

Props spacedmonkey, SergeyBiryukov, felipeelia.
Fixes #47599.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-network-query.php

    r46087 r46100  
    198198        do_action_ref_array( 'pre_get_networks', array( &$this ) );
    199199
    200         $network_ids = null;
     200        $network_data = null;
    201201
    202202        /**
    203          * Filter the sites array before the query takes place.
    204          *
    205          * Return a non-null value to bypass WordPress's default site queries.
     203         * Filter the network data before the query takes place.
     204         *
     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.
    206211         *
    207212         * @since 5.2.0
    208213         *
    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.
    212218         */
    213         $network_ids = apply_filters_ref_array( 'networks_pre_query', array( $network_ids, &$this ) );
    214 
    215         if ( null === $network_ids ) {
    216 
    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 ) );
    219 
    220             // Ignore the $fields argument as the queried result will be the same regardless.
    221             unset( $_args['fields'] );
    222 
    223             $key          = md5( serialize( $_args ) );
    224             $last_changed = wp_cache_get_last_changed( 'networks' );
    225 
    226             $cache_key   = "get_network_ids:$key:$last_changed";
    227             $cache_value = wp_cache_get( $cache_key, 'networks' );
    228 
    229             if ( false === $cache_value ) {
    230                 $network_ids = $this->get_network_ids();
    231                 if ( $network_ids ) {
    232                     $this->set_found_networks();
    233                 }
    234 
    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'];
     219        $network_data = apply_filters_ref_array( 'networks_pre_query', array( $network_data, &$this ) );
     220
     221        if ( null !== $network_data ) {
     222            return $network_data;
     223        }
     224
     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 ) );
     227
     228        // Ignore the $fields argument as the queried result will be the same regardless.
     229        unset( $_args['fields'] );
     230
     231        $key          = md5( serialize( $_args ) );
     232        $last_changed = wp_cache_get_last_changed( 'networks' );
     233
     234        $cache_key   = "get_network_ids:$key:$last_changed";
     235        $cache_value = wp_cache_get( $cache_key, 'networks' );
     236
     237        if ( false === $cache_value ) {
     238            $network_ids = $this->get_network_ids();
     239            if ( $network_ids ) {
     240                $this->set_found_networks();
    243241            }
     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'];
    244251        }
    245252
Note: See TracChangeset for help on using the changeset viewer.