3737 * @property string $rich_editing
3838 * @property string $syntax_highlighting
3939 * @property string $use_ssl
40- * @property array<string, bool> $caps
41- * @property string[] $roles
42- * @property array<string, bool> $allcaps
4340 */
4441#[AllowDynamicProperties]
4542class WP_User {
@@ -63,10 +60,10 @@ class WP_User {
6360 * Capabilities that the individual user has been granted outside of those inherited from their role.
6461 *
6562 * @since 2.0.0
66- * @var array<string, bool>|null Array of key/value pairs where keys represent a capability name
67- * and boolean values represent whether the user has that capability.
63+ * @var bool[] Array of key/value pairs where keys represent a capability name
64+ * and boolean values represent whether the user has that capability.
6865 */
69- protected $ caps = null ;
66+ public $ caps = array () ;
7067
7168 /**
7269 * User metadata option name.
@@ -82,16 +79,16 @@ class WP_User {
8279 * @since 2.0.0
8380 * @var string[]
8481 */
85- protected $ roles = array ();
82+ public $ roles = array ();
8683
8784 /**
8885 * All capabilities the user has, including individual and role based.
8986 *
9087 * @since 2.0.0
91- * @var array<string, bool> Array of key/value pairs where keys represent a capability name
92- * and boolean values represent whether the user has that capability.
88+ * @var bool[] Array of key/value pairs where keys represent a capability name
89+ * and boolean values represent whether the user has that capability.
9390 */
94- protected $ allcaps = array ();
91+ public $ allcaps = array ();
9592
9693 /**
9794 * The filter context applied to user data fields.
@@ -291,10 +288,6 @@ public function __isset( $key ) {
291288 $ key = 'ID ' ;
292289 }
293290
294- if ( in_array ( $ key , array ( 'caps ' , 'allcaps ' , 'roles ' ), true ) ) {
295- return true ;
296- }
297-
298291 if ( isset ( $ this ->data ->$ key ) ) {
299292 return true ;
300293 }
@@ -328,11 +321,6 @@ public function __get( $key ) {
328321 return $ this ->ID ;
329322 }
330323
331- if ( in_array ( $ key , array ( 'caps ' , 'allcaps ' , 'roles ' ), true ) ) {
332- $ this ->load_capability_data ();
333- return $ this ->$ key ;
334- }
335-
336324 if ( isset ( $ this ->data ->$ key ) ) {
337325 $ value = $ this ->data ->$ key ;
338326 } else {
@@ -375,13 +363,6 @@ public function __set( $key, $value ) {
375363 return ;
376364 }
377365
378- // Ensure capability data is loaded before setting related properties.
379- if ( in_array ( $ key , array ( 'caps ' , 'allcaps ' , 'roles ' ), true ) ) {
380- $ this ->load_capability_data ();
381- $ this ->$ key = $ value ;
382- return ;
383- }
384-
385366 $ this ->data ->$ key = $ value ;
386367 }
387368
@@ -405,10 +386,6 @@ public function __unset( $key ) {
405386 );
406387 }
407388
408- if ( in_array ( $ key , array ( 'caps ' , 'allcaps ' , 'roles ' ), true ) ) {
409- $ this ->$ key = null ;
410- }
411-
412389 if ( isset ( $ this ->data ->$ key ) ) {
413390 unset( $ this ->data ->$ key );
414391 }
@@ -538,11 +515,6 @@ public function get_role_caps() {
538515
539516 $ wp_roles = wp_roles ();
540517
541- // Edge case: In case someone calls this method before lazy initialization, we need to initialize on demand.
542- if ( ! isset ( $ this ->caps ) ) {
543- $ this ->caps = $ this ->get_caps_data ();
544- }
545-
546518 // Filter out caps that are not role names and assign to $this->roles.
547519 if ( is_array ( $ this ->caps ) ) {
548520 $ this ->roles = array_filter ( array_keys ( $ this ->caps ), array ( $ wp_roles , 'is_role ' ) );
@@ -576,7 +548,6 @@ public function add_role( $role ) {
576548 if ( empty ( $ role ) ) {
577549 return ;
578550 }
579- $ this ->load_capability_data ();
580551
581552 if ( in_array ( $ role , $ this ->roles , true ) ) {
582553 return ;
@@ -606,7 +577,6 @@ public function add_role( $role ) {
606577 * @param string $role Role name.
607578 */
608579 public function remove_role ( $ role ) {
609- $ this ->load_capability_data ();
610580 if ( ! in_array ( $ role , $ this ->roles , true ) ) {
611581 return ;
612582 }
@@ -639,7 +609,6 @@ public function remove_role( $role ) {
639609 * @param string $role Role name.
640610 */
641611 public function set_role ( $ role ) {
642- $ this ->load_capability_data ();
643612 if ( 1 === count ( $ this ->roles ) && current ( $ this ->roles ) === $ role ) {
644613 return ;
645614 }
@@ -741,7 +710,6 @@ public function update_user_level_from_caps() {
741710 * @param bool $grant Whether to grant capability to user.
742711 */
743712 public function add_cap ( $ cap , $ grant = true ) {
744- $ this ->load_capability_data ();
745713 $ this ->caps [ $ cap ] = $ grant ;
746714 update_user_meta ( $ this ->ID , $ this ->cap_key , $ this ->caps );
747715 $ this ->get_role_caps ();
@@ -756,7 +724,6 @@ public function add_cap( $cap, $grant = true ) {
756724 * @param string $cap Capability name.
757725 */
758726 public function remove_cap ( $ cap ) {
759- $ this ->load_capability_data ();
760727 if ( ! isset ( $ this ->caps [ $ cap ] ) ) {
761728 return ;
762729 }
@@ -775,10 +742,10 @@ public function remove_cap( $cap ) {
775742 */
776743 public function remove_all_caps () {
777744 global $ wpdb ;
778- $ this ->caps = null ;
745+ $ this ->caps = array () ;
779746 delete_user_meta ( $ this ->ID , $ this ->cap_key );
780747 delete_user_meta ( $ this ->ID , $ wpdb ->get_blog_prefix () . 'user_level ' );
781- $ this ->load_capability_data ();
748+ $ this ->get_role_caps ();
782749 }
783750
784751 /**
@@ -809,8 +776,6 @@ public function remove_all_caps() {
809776 * the given capability for that object.
810777 */
811778 public function has_cap ( $ cap , ...$ args ) {
812- $ this ->load_capability_data ();
813-
814779 if ( is_numeric ( $ cap ) ) {
815780 _deprecated_argument ( __FUNCTION__ , '2.0.0 ' , __ ( 'Usage of user levels is deprecated. Use capabilities instead. ' ) );
816781 $ cap = $ this ->translate_level_to_cap ( $ cap );
@@ -912,7 +877,10 @@ public function for_site( $site_id = 0 ) {
912877 }
913878
914879 $ this ->cap_key = $ wpdb ->get_blog_prefix ( $ this ->site_id ) . 'capabilities ' ;
915- $ this ->caps = null ;
880+
881+ $ this ->caps = $ this ->get_caps_data ();
882+
883+ $ this ->get_role_caps ();
916884 }
917885
918886 /**
@@ -943,17 +911,4 @@ private function get_caps_data() {
943911
944912 return $ caps ;
945913 }
946-
947- /**
948- * Loads capability data if it has not been loaded yet.
949- *
950- * @since 6.9.0
951- */
952- private function load_capability_data () {
953- if ( isset ( $ this ->caps ) ) {
954- return ;
955- }
956- $ this ->caps = $ this ->get_caps_data ();
957- $ this ->get_role_caps ();
958- }
959914}
0 commit comments