Make WordPress Core

Changeset 54773


Ignore:
Timestamp:
11/09/2022 01:59:44 AM (17 months ago)
Author:
peterwilsoncc
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, TimothyBlynJacobs.
Merges [54766] to the 6.1 branch.
Fixes #56952.

Location:
branches/6.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1

  • branches/6.1/src/wp-includes/class-wp-user-query.php

    r54133 r54773  
    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.