From bce11933a371ddc483262cd2a79881e041213719 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Wed, 9 Nov 2022 01:59:44 +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. git-svn-id: https://develop.svn.wordpress.org/branches/6.1@54773 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-user-query.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-user-query.php b/src/wp-includes/class-wp-user-query.php index 330882494e..8fc1564bef 100644 --- a/src/wp-includes/class-wp-user-query.php +++ b/src/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 ) {