Make WordPress Core


Ignore:
Timestamp:
03/22/2019 05:25:38 PM (7 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/tests/phpunit/tests/multisite/siteQuery.php

    r43010 r44983  
    910910                $this->assertEqualSets( $expected, $found );
    911911            }
     912        }
     913
     914
     915        /**
     916         * @ticket 45749
     917         */
     918        public function test_sites_pre_query_filter_should_bypass_database_query() {
     919            global $wpdb;
     920
     921            add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
     922
     923            $num_queries = $wpdb->num_queries;
     924
     925            $q       = new WP_Site_Query();
     926            $results = $q->query(
     927                array(
     928                    'fields' => 'ids',
     929                )
     930            );
     931
     932            remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
     933
     934            // Make sure no queries were executed.
     935            $this->assertSame( $num_queries, $wpdb->num_queries );
     936
     937            // We manually inserted a non-existing site and overrode the results with it.
     938            $this->assertSame( array( 555 ), $q->sites );
     939
     940            // Make sure manually setting total_users doesn't get overwritten.
     941            $this->assertEquals( 1, $q->found_sites );
     942        }
     943
     944        public static function filter_sites_pre_query( $sites, $query ) {
     945            $query->found_sites = 1;
     946
     947            return array( 555 );
    912948        }
    913949
Note: See TracChangeset for help on using the changeset viewer.