34 * @property string $locale |
34 * @property string $locale |
35 * @property string $rich_editing |
35 * @property string $rich_editing |
36 * @property string $syntax_highlighting |
36 * @property string $syntax_highlighting |
37 * @property string $use_ssl |
37 * @property string $use_ssl |
38 */ |
38 */ |
|
39 #[AllowDynamicProperties] |
39 class WP_User { |
40 class WP_User { |
40 /** |
41 /** |
41 * User data container. |
42 * User data container. |
42 * |
43 * |
43 * @since 2.0.0 |
44 * @since 2.0.0 |
113 * Constructor. |
114 * Constructor. |
114 * |
115 * |
115 * Retrieves the userdata and passes it to WP_User::init(). |
116 * Retrieves the userdata and passes it to WP_User::init(). |
116 * |
117 * |
117 * @since 2.0.0 |
118 * @since 2.0.0 |
|
119 * |
|
120 * @global wpdb $wpdb WordPress database abstraction object. |
118 * |
121 * |
119 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. |
122 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. |
120 * @param string $name Optional. User's username |
123 * @param string $name Optional. User's username |
121 * @param int $site_id Optional Site ID, defaults to current site. |
124 * @param int $site_id Optional Site ID, defaults to current site. |
122 */ |
125 */ |
123 public function __construct( $id = 0, $name = '', $site_id = '' ) { |
126 public function __construct( $id = 0, $name = '', $site_id = '' ) { |
|
127 global $wpdb; |
|
128 |
124 if ( ! isset( self::$back_compat_keys ) ) { |
129 if ( ! isset( self::$back_compat_keys ) ) { |
125 $prefix = $GLOBALS['wpdb']->prefix; |
130 $prefix = $wpdb->prefix; |
|
131 |
126 self::$back_compat_keys = array( |
132 self::$back_compat_keys = array( |
127 'user_firstname' => 'first_name', |
133 'user_firstname' => 'first_name', |
128 'user_lastname' => 'last_name', |
134 'user_lastname' => 'last_name', |
129 'user_description' => 'description', |
135 'user_description' => 'description', |
130 'user_level' => $prefix . 'user_level', |
136 'user_level' => $prefix . 'user_level', |
183 * @since 3.3.0 |
189 * @since 3.3.0 |
184 * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. |
190 * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. |
185 * |
191 * |
186 * @global wpdb $wpdb WordPress database abstraction object. |
192 * @global wpdb $wpdb WordPress database abstraction object. |
187 * |
193 * |
188 * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'. |
194 * @param string $field The field to query against: Accepts 'id', 'ID', 'slug', 'email' or 'login'. |
189 * @param string|int $value The field value. |
195 * @param string|int $value The field value. |
190 * @return object|false Raw user object. |
196 * @return object|false Raw user object. |
191 */ |
197 */ |
192 public static function get_data_by( $field, $value ) { |
198 public static function get_data_by( $field, $value ) { |
193 global $wpdb; |
199 global $wpdb; |
196 if ( 'ID' === $field ) { |
202 if ( 'ID' === $field ) { |
197 $field = 'id'; |
203 $field = 'id'; |
198 } |
204 } |
199 |
205 |
200 if ( 'id' === $field ) { |
206 if ( 'id' === $field ) { |
201 // Make sure the value is numeric to avoid casting objects, for example, |
207 // Make sure the value is numeric to avoid casting objects, for example, to int 1. |
202 // to int 1. |
|
203 if ( ! is_numeric( $value ) ) { |
208 if ( ! is_numeric( $value ) ) { |
204 return false; |
209 return false; |
205 } |
210 } |
206 $value = (int) $value; |
211 $value = (int) $value; |
207 if ( $value < 1 ) { |
212 if ( $value < 1 ) { |
498 * @return bool[] Array of key/value pairs where keys represent a capability name |
503 * @return bool[] Array of key/value pairs where keys represent a capability name |
499 * and boolean values represent whether the user has that capability. |
504 * and boolean values represent whether the user has that capability. |
500 */ |
505 */ |
501 public function get_role_caps() { |
506 public function get_role_caps() { |
502 $switch_site = false; |
507 $switch_site = false; |
503 if ( is_multisite() && get_current_blog_id() != $this->site_id ) { |
508 if ( is_multisite() && get_current_blog_id() !== $this->site_id ) { |
504 $switch_site = true; |
509 $switch_site = true; |
505 |
510 |
506 switch_to_blog( $this->site_id ); |
511 switch_to_blog( $this->site_id ); |
507 } |
512 } |
508 |
513 |
600 * @since 2.0.0 |
605 * @since 2.0.0 |
601 * |
606 * |
602 * @param string $role Role name. |
607 * @param string $role Role name. |
603 */ |
608 */ |
604 public function set_role( $role ) { |
609 public function set_role( $role ) { |
605 if ( 1 === count( $this->roles ) && current( $this->roles ) == $role ) { |
610 if ( 1 === count( $this->roles ) && current( $this->roles ) === $role ) { |
606 return; |
611 return; |
607 } |
612 } |
608 |
613 |
609 foreach ( (array) $this->roles as $oldrole ) { |
614 foreach ( (array) $this->roles as $oldrole ) { |
610 unset( $this->caps[ $oldrole ] ); |
615 unset( $this->caps[ $oldrole ] ); |
891 * Gets the available user capabilities data. |
896 * Gets the available user capabilities data. |
892 * |
897 * |
893 * @since 4.9.0 |
898 * @since 4.9.0 |
894 * |
899 * |
895 * @return bool[] List of capabilities keyed by the capability name, |
900 * @return bool[] List of capabilities keyed by the capability name, |
896 * e.g. array( 'edit_posts' => true, 'delete_posts' => false ). |
901 * e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`. |
897 */ |
902 */ |
898 private function get_caps_data() { |
903 private function get_caps_data() { |
899 $caps = get_user_meta( $this->ID, $this->cap_key, true ); |
904 $caps = get_user_meta( $this->ID, $this->cap_key, true ); |
900 |
905 |
901 if ( ! is_array( $caps ) ) { |
906 if ( ! is_array( $caps ) ) { |