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.
Fixes #56952.

Built from https://develop.svn.wordpress.org/trunk@54766


git-svn-id: http://core.svn.wordpress.org/trunk@54318 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
TimothyBlynJacobs 2022-11-08 17:31:13 +00:00
parent 03f4c7972f
commit 72cc029989
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.2-alpha-54765';
$wp_version = '6.2-alpha-54766';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.