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
This commit is contained in:
Peter Wilson 2022-11-09 02:01:14 +00:00
parent 65a6a14ed6
commit 73cd0d0c47
2 changed files with 16 additions and 2 deletions

View File

@ -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.' ),
'<code>plugins_loaded</code>'
),
'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 ) {

View File

@ -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.