Make WordPress Core


Ignore:
Timestamp:
03/22/2019 05:25:38 PM (8 years ago)
Author:
adamsilverstein
Message:

Multisite: add new sites_pre_query and networks_pre_query filters to short circuit WP_Site_Query and WP_Network_Query queries.

Similar to the posts_pre_query filter for WP_Query added in #36687. These filters lets you short circuit the queries to return your own results.

Add a new filter sites_pre_query - which returns null by default. Return a non-null value to bypass WordPress's default get_sites queries.

Developers should note that filtering functions that require pagination information are encouraged to set the found_sites property of the WP_Site_Query object, passed to the filter by reference. If WP_Site_Query does not perform a database query, it will not have enough information to generate these values itself.

Add a new filter networks_pre_query - which returns null by default. Return a non-null value to bypass WordPress's default get_networks queries.

Developers should note that filtering functions that require pagination information are encouraged to set the found_networks property of the WP_Network_Query object, passed to the filter by reference. If WP_Network_Query does not perform a database query, it will not have enough information to generate these values itself.

Props spacedmonkey.
Fixes #45749.

File:
1 edited

Legend:

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

    r44166 r44983  
    198198                do_action_ref_array( 'pre_get_networks', array( &$this ) );
    199199
    200                 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    201                 $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    202 
    203                 // Ignore the $fields argument as the queried result will be the same regardless.
    204                 unset( $_args['fields'] );
    205 
    206                 $key          = md5( serialize( $_args ) );
    207                 $last_changed = wp_cache_get_last_changed( 'networks' );
    208 
    209                 $cache_key   = "get_network_ids:$key:$last_changed";
    210                 $cache_value = wp_cache_get( $cache_key, 'networks' );
    211 
    212                 if ( false === $cache_value ) {
    213                         $network_ids = $this->get_network_ids();
    214                         if ( $network_ids ) {
    215                                 $this->set_found_networks();
     200                $network_ids = null;
     201
     202                /**
     203                 * Filter the sites array before the query takes place.
     204                 *
     205                 * Return a non-null value to bypass WordPress's default site queries.
     206                 *
     207                 *
     208                 * @since 5.2.0
     209                 *
     210                 * @param array|null       $site_ids Return an array of site data to short-circuit WP's site query,
     211                 *                                   or null to allow WP to run its normal queries.
     212                 * @param WP_Network_Query $this     The WP_Network_Query instance, passed by reference.
     213                 */
     214                $network_ids = apply_filters_ref_array( 'networks_pre_query', array( $network_ids, &$this ) );
     215
     216                if ( null === $network_ids ) {
     217
     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 ) );
     220
     221                        // Ignore the $fields argument as the queried result will be the same regardless.
     222                        unset( $_args['fields'] );
     223
     224                        $key          = md5( serialize( $_args ) );
     225                        $last_changed = wp_cache_get_last_changed( 'networks' );
     226
     227                        $cache_key   = "get_network_ids:$key:$last_changed";
     228                        $cache_value = wp_cache_get( $cache_key, 'networks' );
     229
     230                        if ( false === $cache_value ) {
     231                                $network_ids = $this->get_network_ids();
     232                                if ( $network_ids ) {
     233                                        $this->set_found_networks();
     234                                }
     235
     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'];
    216244                        }
    217 
    218                         $cache_value = array(
    219                                 'network_ids'    => $network_ids,
    220                                 'found_networks' => $this->found_networks,
    221                         );
    222                         wp_cache_add( $cache_key, $cache_value, 'networks' );
    223                 } else {
    224                         $network_ids          = $cache_value['network_ids'];
    225                         $this->found_networks = $cache_value['found_networks'];
    226245                }
    227246
Note: See TracChangeset for help on using the changeset viewer.