Make WordPress Core

Changeset 54766 for trunk


Ignore:
Timestamp:
11/08/2022 05:29:48 PM (21 months ago)
Author:
TimothyBlynJacobs
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user-query.php

    r54133 r54766  
    777777        global $wpdb;
    778778
     779        if ( ! did_action( 'plugins_loaded' ) ) {
     780            _doing_it_wrong(
     781                'WP_User_Query::query',
     782                sprintf(
     783                /* translators: %s: plugins_loaded */
     784                    __( 'User queries should not be run before the %s hook.' ),
     785                    '<code>plugins_loaded</code>'
     786                ),
     787                '6.1.1'
     788            );
     789        }
     790
    779791        $qv =& $this->query_vars;
    780792
     
    841853            }
    842854        } elseif ( 'all_with_meta' === $qv['fields'] || 'all' === $qv['fields'] ) {
    843             cache_users( $this->results );
     855            if ( function_exists( 'cache_users' ) ) {
     856                cache_users( $this->results );
     857            }
    844858
    845859            $r = array();
Note: See TracChangeset for help on using the changeset viewer.