Make WordPress Core


Ignore:
Timestamp:
10/08/2025 07:59:28 PM (6 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/multisite.php

    r60148 r60915  
    370370        switch_to_blog( $site_id );
    371371        $user = get_user_by( 'id', $user_id );
     372        $this->assertContains( 'subscriber', $user->roles, 'User should have subscriber role' );
    372373        restore_current_blog();
    373374
     
    375376        wpmu_delete_user( $user_id );
    376377
    377         $this->assertContains( 'subscriber', $user->roles );
     378        $this->assertContains( 'subscriber', $user->roles, 'User should still have subscriber role' );
    378379    }
    379380
Note: See TracChangeset for help on using the changeset viewer.