Make WordPress Core


Ignore:
Timestamp:
10/08/2025 07:59:28 PM (4 months ago)
Author:
spacedmonkey
Message:

Users: Lazy load user capabilities in WP_User object.

Convert the WP_User object properties caps, roles, and allcaps to protected, and introduce lazy loading for capabilities. These properties are now populated only when first accessed.

The existing magic methods (get, set, and unset) have been updated to maintain backward compatibility, ensuring that reading or modifying these formerly public properties continues to work as expected.

Ensure that these properties are initialised when calling remove_all_caps(), remove_cap(), has_cap(), add_role(), and set_role() methods.

Props spacedmonkey, flixos90, peterwilsoncc, mukesh27, westonruter, swissspidy, prettyboymp.
Fixes #58001.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/query.php

    r57750 r60915  
    163163        add_filter( 'update_user_metadata_cache', array( $filter, 'filter' ), 10, 2 );
    164164
    165         new WP_User_Query(
     165        $query = new WP_User_Query(
    166166            array(
    167167                'include' => self::$author_ids,
     
    169169            )
    170170        );
     171
     172        $users = $query->get_results();
     173        foreach ( $users as $user ) {
     174            $this->assertIsArray( $user->roles );
     175            foreach ( $user->roles as $role ) {
     176                $this->assertIsString( $role );
     177            }
     178        }
    171179
    172180        $args      = $filter->get_args();
Note: See TracChangeset for help on using the changeset viewer.