Make WordPress Core


Ignore:
Timestamp:
07/05/2022 09:26:21 AM (2 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.