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/tests/phpunit/tests/multisite/networkQuery.php

    r42343 r44983  
    523523                        $this->assertEquals( $number_of_queries + 1, $wpdb->num_queries );
    524524                }
     525
     526                /**
     527                 * @ticket 45749
     528                 */
     529                public function test_networks_pre_query_filter_should_bypass_database_query() {
     530                        global $wpdb;
     531
     532                        add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
     533
     534                        $num_queries = $wpdb->num_queries;
     535
     536                        $q       = new WP_Network_Query();
     537                        $results = $q->query(
     538                                array(
     539                                        'fields' => 'ids',
     540                                )
     541                        );
     542
     543                        remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query' ), 10, 2 );
     544
     545                        // Make sure no queries were executed.
     546                        $this->assertSame( $num_queries, $wpdb->num_queries );
     547
     548                        // We manually inserted a non-existing site and overrode the results with it.
     549                        $this->assertSame( array( 555 ), $q->networks );
     550
     551                        // Make sure manually setting total_users doesn't get overwritten.
     552                        $this->assertEquals( 1, $q->found_networks );
     553                }
     554
     555                public static function filter_networks_pre_query( $networks, $query ) {
     556                        $query->found_networks = 1;
     557
     558                        return array( 555 );
     559                }
    525560        }
    526561
Note: See TracChangeset for help on using the changeset viewer.