From 4a18c93f4264f8f71773b012ce16ce125c8491d6 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 8 Nov 2020 11:47:06 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-network-query.php | 14 ++++++++++++++ wp-includes/class-wp-site-query.php | 14 ++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/wp-includes/class-wp-network-query.php b/wp-includes/class-wp-network-query.php index 1e0cb54733..f12bc5c54f 100644 --- a/wp-includes/class-wp-network-query.php +++ b/wp-includes/class-wp-network-query.php @@ -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; } diff --git a/wp-includes/class-wp-site-query.php b/wp-includes/class-wp-site-query.php index e19a84c749..fb98fd9daa 100644 --- a/wp-includes/class-wp-site-query.php +++ b/wp-includes/class-wp-site-query.php @@ -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; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 8a3a8d781a..23e044fc87 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.