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.

git-svn-id: https://develop.svn.wordpress.org/trunk@49538 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-11-08 11:45:36 +00:00
parent 8f86eb9550
commit 31831c74d9
5 changed files with 73 additions and 1 deletions

View File

@ -212,7 +212,17 @@ class WP_Network_Query {
* an array of network IDs.
* - Otherwise the filter should return an array of WP_Network objects.
*
* Note that if the filter returns an array of network data, it will be assigned
* to the `networks` property of the current WP_Network_Query instance.
*
* Filtering functions that require pagination information are encouraged to set
* the `found_networks` and `max_num_pages` properties 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.
*
* @since 5.2.0
* @since 5.6.0 The returned array of network data is assigned to the `networks` property
* of the current WP_Network_Query instance.
*
* @param array|int|null $network_data Return an array of network data to short-circuit WP's network query,
* the network count as an integer if `$this->query_vars['count']` is set,
@ -222,6 +232,10 @@ class WP_Network_Query {
$network_data = apply_filters_ref_array( 'networks_pre_query', array( $network_data, &$this ) );
if ( null !== $network_data ) {
if ( is_array( $network_data ) && ! $this->query_vars['count'] ) {
$this->networks = $network_data;
}
return $network_data;
}

View File

@ -303,7 +303,17 @@ class WP_Site_Query {
* an array of site IDs.
* - Otherwise the filter should return an array of WP_Site objects.
*
* Note that if the filter returns an array of site data, it will be assigned
* to the `sites` property of the current WP_Site_Query instance.
*
* Filtering functions that require pagination information are encouraged to set
* the `found_sites` and `max_num_pages` properties 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.
*
* @since 5.2.0
* @since 5.6.0 The returned array of site data is assigned to the `sites` property
* of the current WP_Site_Query instance.
*
* @param array|int|null $site_data Return an array of site data to short-circuit WP's site query,
* the site count as an integer if `$this->query_vars['count']` is set,
@ -313,6 +323,10 @@ class WP_Site_Query {
$site_data = apply_filters_ref_array( 'sites_pre_query', array( $site_data, &$this ) );
if ( null !== $site_data ) {
if ( is_array( $site_data ) && ! $this->query_vars['count'] ) {
$this->sites = $site_data;
}
return $site_data;
}

View File

@ -4926,7 +4926,7 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$q = new WP_Comment_Query();
$results = $q->query( array() );
remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query_and_set_comments' ), 10, 2 );
remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query_and_set_comments' ), 10 );
// Make sure the comments property is the same as the results.
$this->assertSame( $results, $q->comments );

View File

@ -554,6 +554,28 @@ if ( is_multisite() ) :
return array( 555 );
}
/**
* @ticket 51333
*/
public function test_networks_pre_query_filter_should_set_networks_property() {
add_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10, 2 );
$q = new WP_Network_Query();
$results = $q->query( array() );
remove_filter( 'networks_pre_query', array( __CLASS__, 'filter_networks_pre_query_and_set_networks' ), 10 );
// Make sure the networks property is the same as the results.
$this->assertSame( $results, $q->networks );
// Make sure the network domain is `wordpress.org`.
$this->assertSame( 'wordpress.org', $q->networks[0]->domain );
}
public static function filter_networks_pre_query_and_set_networks( $networks, $query ) {
return array( get_network( self::$network_ids['wordpress.org/'] ) );
}
}
endif;

View File

@ -1095,6 +1095,28 @@ if ( is_multisite() ) :
return array( 555 );
}
/**
* @ticket 51333
*/
public function test_sites_pre_query_filter_should_set_sites_property() {
add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10, 2 );
$q = new WP_Site_Query();
$results = $q->query( array() );
remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query_and_set_sites' ), 10 );
// Make sure the sites property is the same as the results.
$this->assertSame( $results, $q->sites );
// Make sure the site domain is `wordpress.org`.
$this->assertSame( 'wordpress.org', $q->sites[0]->domain );
}
public static function filter_sites_pre_query_and_set_sites( $sites, $query ) {
return array( get_site( self::$site_ids['wordpress.org/'] ) );
}
}
endif;