From 73cd0d0c471d878534608256ea3aa8064775d66f Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Wed, 9 Nov 2022 02:01:14 +0000 Subject: [PATCH] Query: Don't attempt caching if running a WP_User_Query before plugins_loaded. In #55594 user meta caching was enabled by default when making a `WP_User_Query`. Previously, this was only enabled if a developer specifically queried for 'all_with_meta' fields. User meta caching is implemented using a pluggable function, `cache_users`. If a plugin runs a `WP_User_Query` before pluggable functions have been defined, this will now cause a fatal error. In this commit, a `function_exists` check is introduced to avoid calling `cache_users` if it's not defined. Additionally, a `_doing_it_wrong` notice is issued if the `WP_User_Query::query` method is called before the 'plugins_loaded' hook. Props carazo, subrataemfluence, oakesjosh, spacedmonkey, obenland, SergeyBiryukov, peterwilsoncc, TimothyBlynJacobs. Merges [54766] to the 6.1 branch. Fixes #56952. Built from https://develop.svn.wordpress.org/branches/6.1@54773 git-svn-id: http://core.svn.wordpress.org/branches/6.1@54325 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-user-query.php | 16 +++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/wp-includes/class-wp-user-query.php b/wp-includes/class-wp-user-query.php index 330882494e..8fc1564bef 100644 --- a/wp-includes/class-wp-user-query.php +++ b/wp-includes/class-wp-user-query.php @@ -776,6 +776,18 @@ class WP_User_Query { public function query() { global $wpdb; + if ( ! did_action( 'plugins_loaded' ) ) { + _doing_it_wrong( + 'WP_User_Query::query', + sprintf( + /* translators: %s: plugins_loaded */ + __( 'User queries should not be run before the %s hook.' ), + 'plugins_loaded' + ), + '6.1.1' + ); + } + $qv =& $this->query_vars; /** @@ -840,7 +852,9 @@ class WP_User_Query { $result->id = $result->ID; } } elseif ( 'all_with_meta' === $qv['fields'] || 'all' === $qv['fields'] ) { - cache_users( $this->results ); + if ( function_exists( 'cache_users' ) ) { + cache_users( $this->results ); + } $r = array(); foreach ( $this->results as $userid ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 38d45f11b1..660d345629 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1.1-alpha-54772'; +$wp_version = '6.1.1-alpha-54773'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.