Make WordPress Core


Ignore:
Timestamp:
11/08/2020 11:45:36 AM (4 years ago)
Author:
SergeyBiryukov
Message:

Networks and Sites: Assign the array of site or network data returned from filters to the respective class property:

  • The array of network data returned from the networks_pre_query filter is assigned to the networks property of the current WP_Network_Query instance.
  • The array of site data returned from the sites_pre_query filter is assigned to the sites property of the current WP_Site_Query instance.

This avoids the performance overhead of calling WP_Network_Query::get_networks() or WP_Site_Query::get_sites() twice: first when creating the object instance, then to retrieve the filtered results.

This also makes the filters a bit more consistent with other similar filters, e.g. posts_pre_query, terms_pre_query, comments_pre_query, or users_pre_query.

Follow-up to [46086], [48990].

Props yakimun, spacedmonkey.
Fixes #51333.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/networkQuery.php

    r48987 r49538  
    555555            return array( 555 );
    556556        }
     557
     558        /**
     559         * @ticket 51333
     560         */
     561        public function test_networks_pre_query_filter_should_set_networks_property() {
     562            add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10, 2 );
     563
     564            $q       = new WP_Network_Query();
     565            $results = $q->query( array() );
     566
     567            remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10 );
     568
     569            // Make sure the networks property is the same as the results.
     570            $this->assertSame( $results, $q->networks );
     571
     572            // Make sure the network domain is `wordpress.org`.
     573            $this->assertSame( 'wordpress.org', $q->networks[0]->domain );
     574        }
     575
     576        public static function filter_networks_pre_query_and_set_networks( $networks, $query ) {
     577            return array( get_network( self::$network_ids['wordpress.org/'] ) );
     578        }
    557579    }
    558580
Note: See TracChangeset for help on using the changeset viewer.