Make WordPress Core

Changeset 53655 for trunk


Ignore:
Timestamp:
07/05/2022 09:26:21 AM (4 years ago)
Author:
spacedmonkey
Message:

Users: Prime user meta in WP_User_Query class.

When querying 'fields' equal to 'all' using the WP_User_Query class, this returns an array of WP_User objects. A WP_User object requires user meta to be primed, as the user's role is stored in user meta. Ensure that all users meta is primed in a single request by calling the cache_users function when querying 'fields' equal to 'all'. Soft deprecate fields equal to all_with_meta as it now acts the same as 'fields' equal to 'all'.

Props Spacedmonkey, peterwilsoncc, mehulkaklotar, timothyblynjacobs, furi3r.
Fixes #55594.

Location:
trunk
Files:
2 edited

Legend:

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

    r53469 r53655  
    236236         *                                                - 'spam' (only available on multisite installs)
    237237         *                                                - 'deleted' (only available on multisite installs)
    238          *                                                - 'all' for all fields
    239          *                                                - 'all_with_meta' to include meta fields.
     238         *                                                - 'all' for all fields and loads user meta.
     239         *                                                - 'all_with_meta' Deprecated. Use 'all'.
    240240         *                                                Default 'all'.
    241241         *     @type string          $who                 Type of users to query. Accepts 'authors'.
     
    311311                        }
    312312                        $this->query_fields = implode( ',', $this->query_fields );
    313                 } elseif ( 'all' === $qv['fields'] ) {
    314                         $this->query_fields = "$wpdb->users.*";
    315                 } elseif ( ! in_array( $qv['fields'], $allowed_fields, true ) ) {
     313                } elseif ( 'all_with_meta' === $qv['fields'] || 'all' === $qv['fields'] || ! in_array( $qv['fields'], $allowed_fields, true ) ) {
    316314                        $this->query_fields = "$wpdb->users.ID";
    317315                } else {
     
    809807                        ";
    810808
    811                         if ( is_array( $qv['fields'] ) || 'all' === $qv['fields'] ) {
     809                        if ( is_array( $qv['fields'] ) ) {
    812810                                $this->results = $wpdb->get_results( $this->request );
    813811                        } else {
     
    843841                                $result->id = $result->ID;
    844842                        }
    845                 } elseif ( 'all_with_meta' === $qv['fields'] ) {
     843                } elseif ( 'all_with_meta' === $qv['fields'] || 'all' === $qv['fields'] ) {
    846844                        cache_users( $this->results );
    847845
    848846                        $r = array();
    849847                        foreach ( $this->results as $userid ) {
    850                                 $r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] );
     848                                if ( 'all_with_meta' === $qv['fields'] ) {
     849                                        $r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] );
     850                                } else {
     851                                        $r[] = new WP_User( $userid, '', $qv['blog_id'] );
     852                                }
    851853                        }
    852854
    853855                        $this->results = $r;
    854                 } elseif ( 'all' === $qv['fields'] ) {
    855                         foreach ( $this->results as $key => $user ) {
    856                                 $this->results[ $key ] = new WP_User( $user, '', $qv['blog_id'] );
    857                         }
    858856                }
    859857        }
  • trunk/tests/phpunit/tests/user/query.php

    r53373 r53655  
    154154                        $this->assertInstanceOf( 'WP_User', $user );
    155155                }
     156        }
     157
     158        /**
     159         * @ticket 55594
     160         */
     161        public function test_get_all_primed_users() {
     162                $filter = new MockAction();
     163                add_filter( 'update_user_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     164
     165                new WP_User_Query(
     166                        array(
     167                                'include' => self::$author_ids,
     168                                'fields'  => 'all',
     169                        )
     170                );
     171
     172                $args      = $filter->get_args();
     173                $last_args = end( $args );
     174                $this->assertIsArray( $last_args[1] );
     175                $this->assertSameSets( self::$author_ids, $last_args[1], 'Ensure that user meta is primed' );
    156176        }
    157177
Note: See TracChangeset for help on using the changeset viewer.