WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/22/2019 05:25:38 PM (23 months 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-site-query.php

    r44467 r44983  
    289289        }
    290290
    291         // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    292         $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    293 
    294         // Ignore the $fields argument as the queried result will be the same regardless.
    295         unset( $_args['fields'] );
    296 
    297         $key          = md5( serialize( $_args ) );
    298         $last_changed = wp_cache_get_last_changed( 'sites' );
    299 
    300         $cache_key   = "get_sites:$key:$last_changed";
    301         $cache_value = wp_cache_get( $cache_key, 'sites' );
    302 
    303         if ( false === $cache_value ) {
    304             $site_ids = $this->get_site_ids();
    305             if ( $site_ids ) {
    306                 $this->set_found_sites();
     291        $site_ids = null;
     292
     293        /**
     294         * Filter the sites array before the query takes place.
     295         *
     296         * Return a non-null value to bypass WordPress's default site queries.
     297         *
     298         *
     299         * @since 5.2.0
     300         *
     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.
     304         */
     305        $site_ids = apply_filters_ref_array( 'sites_pre_query', array( $site_ids, &$this ) );
     306
     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
     312            // Ignore the $fields argument as the queried result will be the same regardless.
     313            unset( $_args['fields'] );
     314
     315            $key          = md5( serialize( $_args ) );
     316            $last_changed = wp_cache_get_last_changed( 'sites' );
     317
     318            $cache_key   = "get_sites:$key:$last_changed";
     319            $cache_value = wp_cache_get( $cache_key, 'sites' );
     320
     321            if ( false === $cache_value ) {
     322                $site_ids = $this->get_site_ids();
     323                if ( $site_ids ) {
     324                    $this->set_found_sites();
     325                }
     326
     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'];
    307335            }
    308 
    309             $cache_value = array(
    310                 'site_ids'    => $site_ids,
    311                 'found_sites' => $this->found_sites,
    312             );
    313             wp_cache_add( $cache_key, $cache_value, 'sites' );
    314         } else {
    315             $site_ids          = $cache_value['site_ids'];
    316             $this->found_sites = $cache_value['found_sites'];
    317336        }
    318337
Note: See TracChangeset for help on using the changeset viewer.