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.
Built from https://develop.svn.wordpress.org/trunk@49538


git-svn-id: http://core.svn.wordpress.org/trunk@49276 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-11-08 11:47:06 +00:00
parent 5683c46277
commit 4a18c93f42
3 changed files with 29 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

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.6-beta3-49537';
$wp_version = '5.6-beta3-49538';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.