[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * User API: WP_User class 4 * 5 * @package WordPress 6 * @subpackage Users 7 * @since 4.4.0 8 */ 9 10 /** 11 * Core class used to implement the WP_User object. 12 * 13 * @since 2.0.0 14 * 15 * @property string $nickname 16 * @property string $description 17 * @property string $user_description 18 * @property string $first_name 19 * @property string $user_firstname 20 * @property string $last_name 21 * @property string $user_lastname 22 * @property string $user_login 23 * @property string $user_pass 24 * @property string $user_nicename 25 * @property string $user_email 26 * @property string $user_url 27 * @property string $user_registered 28 * @property string $user_activation_key 29 * @property string $user_status 30 * @property int $user_level 31 * @property string $display_name 32 * @property string $spam 33 * @property string $deleted 34 * @property string $locale 35 * @property string $rich_editing 36 * @property string $syntax_highlighting 37 * @property string $use_ssl 38 */ 39 class WP_User { 40 /** 41 * User data container. 42 * 43 * @since 2.0.0 44 * @var stdClass 45 */ 46 public $data; 47 48 /** 49 * The user's ID. 50 * 51 * @since 2.1.0 52 * @var int 53 */ 54 public $ID = 0; 55 56 /** 57 * Capabilities that the individual user has been granted outside of those inherited from their role. 58 * 59 * @since 2.0.0 60 * @var bool[] Array of key/value pairs where keys represent a capability name 61 * and boolean values represent whether the user has that capability. 62 */ 63 public $caps = array(); 64 65 /** 66 * User metadata option name. 67 * 68 * @since 2.0.0 69 * @var string 70 */ 71 public $cap_key; 72 73 /** 74 * The roles the user is part of. 75 * 76 * @since 2.0.0 77 * @var string[] 78 */ 79 public $roles = array(); 80 81 /** 82 * All capabilities the user has, including individual and role based. 83 * 84 * @since 2.0.0 85 * @var bool[] Array of key/value pairs where keys represent a capability name 86 * and boolean values represent whether the user has that capability. 87 */ 88 public $allcaps = array(); 89 90 /** 91 * The filter context applied to user data fields. 92 * 93 * @since 2.9.0 94 * @var string 95 */ 96 public $filter = null; 97 98 /** 99 * The site ID the capabilities of this user are initialized for. 100 * 101 * @since 4.9.0 102 * @var int 103 */ 104 private $site_id = 0; 105 106 /** 107 * @since 3.3.0 108 * @var array 109 */ 110 private static $back_compat_keys; 111 112 /** 113 * Constructor. 114 * 115 * Retrieves the userdata and passes it to WP_User::init(). 116 * 117 * @since 2.0.0 118 * 119 * @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 121 * @param int $site_id Optional Site ID, defaults to current site. 122 */ 123 public function __construct( $id = 0, $name = '', $site_id = '' ) { 124 if ( ! isset( self::$back_compat_keys ) ) { 125 $prefix = $GLOBALS['wpdb']->prefix; 126 self::$back_compat_keys = array( 127 'user_firstname' => 'first_name', 128 'user_lastname' => 'last_name', 129 'user_description' => 'description', 130 'user_level' => $prefix . 'user_level', 131 $prefix . 'usersettings' => $prefix . 'user-settings', 132 $prefix . 'usersettingstime' => $prefix . 'user-settings-time', 133 ); 134 } 135 136 if ( $id instanceof WP_User ) { 137 $this->init( $id->data, $site_id ); 138 return; 139 } elseif ( is_object( $id ) ) { 140 $this->init( $id, $site_id ); 141 return; 142 } 143 144 if ( ! empty( $id ) && ! is_numeric( $id ) ) { 145 $name = $id; 146 $id = 0; 147 } 148 149 if ( $id ) { 150 $data = self::get_data_by( 'id', $id ); 151 } else { 152 $data = self::get_data_by( 'login', $name ); 153 } 154 155 if ( $data ) { 156 $this->init( $data, $site_id ); 157 } else { 158 $this->data = new stdClass; 159 } 160 } 161 162 /** 163 * Sets up object properties, including capabilities. 164 * 165 * @since 3.3.0 166 * 167 * @param object $data User DB row object. 168 * @param int $site_id Optional. The site ID to initialize for. 169 */ 170 public function init( $data, $site_id = '' ) { 171 if ( ! isset( $data->ID ) ) { 172 $data->ID = 0; 173 } 174 $this->data = $data; 175 $this->ID = (int) $data->ID; 176 177 $this->for_site( $site_id ); 178 } 179 180 /** 181 * Returns only the main user fields. 182 * 183 * @since 3.3.0 184 * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. 185 * 186 * @global wpdb $wpdb WordPress database abstraction object. 187 * 188 * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'. 189 * @param string|int $value The field value. 190 * @return object|false Raw user object. 191 */ 192 public static function get_data_by( $field, $value ) { 193 global $wpdb; 194 195 // 'ID' is an alias of 'id'. 196 if ( 'ID' === $field ) { 197 $field = 'id'; 198 } 199 200 if ( 'id' === $field ) { 201 // Make sure the value is numeric to avoid casting objects, for example, 202 // to int 1. 203 if ( ! is_numeric( $value ) ) { 204 return false; 205 } 206 $value = (int) $value; 207 if ( $value < 1 ) { 208 return false; 209 } 210 } else { 211 $value = trim( $value ); 212 } 213 214 if ( ! $value ) { 215 return false; 216 } 217 218 switch ( $field ) { 219 case 'id': 220 $user_id = $value; 221 $db_field = 'ID'; 222 break; 223 case 'slug': 224 $user_id = wp_cache_get( $value, 'userslugs' ); 225 $db_field = 'user_nicename'; 226 break; 227 case 'email': 228 $user_id = wp_cache_get( $value, 'useremail' ); 229 $db_field = 'user_email'; 230 break; 231 case 'login': 232 $value = sanitize_user( $value ); 233 $user_id = wp_cache_get( $value, 'userlogins' ); 234 $db_field = 'user_login'; 235 break; 236 default: 237 return false; 238 } 239 240 if ( false !== $user_id ) { 241 $user = wp_cache_get( $user_id, 'users' ); 242 if ( $user ) { 243 return $user; 244 } 245 } 246 247 $user = $wpdb->get_row( 248 $wpdb->prepare( 249 "SELECT * FROM $wpdb->users WHERE $db_field = %s LIMIT 1", 250 $value 251 ) 252 ); 253 if ( ! $user ) { 254 return false; 255 } 256 257 update_user_caches( $user ); 258 259 return $user; 260 } 261 262 /** 263 * Magic method for checking the existence of a certain custom field. 264 * 265 * @since 3.3.0 266 * 267 * @param string $key User meta key to check if set. 268 * @return bool Whether the given user meta key is set. 269 */ 270 public function __isset( $key ) { 271 if ( 'id' === $key ) { 272 _deprecated_argument( 273 'WP_User->id', 274 '2.1.0', 275 sprintf( 276 /* translators: %s: WP_User->ID */ 277 __( 'Use %s instead.' ), 278 '<code>WP_User->ID</code>' 279 ) 280 ); 281 $key = 'ID'; 282 } 283 284 if ( isset( $this->data->$key ) ) { 285 return true; 286 } 287 288 if ( isset( self::$back_compat_keys[ $key ] ) ) { 289 $key = self::$back_compat_keys[ $key ]; 290 } 291 292 return metadata_exists( 'user', $this->ID, $key ); 293 } 294 295 /** 296 * Magic method for accessing custom fields. 297 * 298 * @since 3.3.0 299 * 300 * @param string $key User meta key to retrieve. 301 * @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID. 302 */ 303 public function __get( $key ) { 304 if ( 'id' === $key ) { 305 _deprecated_argument( 306 'WP_User->id', 307 '2.1.0', 308 sprintf( 309 /* translators: %s: WP_User->ID */ 310 __( 'Use %s instead.' ), 311 '<code>WP_User->ID</code>' 312 ) 313 ); 314 return $this->ID; 315 } 316 317 if ( isset( $this->data->$key ) ) { 318 $value = $this->data->$key; 319 } else { 320 if ( isset( self::$back_compat_keys[ $key ] ) ) { 321 $key = self::$back_compat_keys[ $key ]; 322 } 323 $value = get_user_meta( $this->ID, $key, true ); 324 } 325 326 if ( $this->filter ) { 327 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter ); 328 } 329 330 return $value; 331 } 332 333 /** 334 * Magic method for setting custom user fields. 335 * 336 * This method does not update custom fields in the database. It only stores 337 * the value on the WP_User instance. 338 * 339 * @since 3.3.0 340 * 341 * @param string $key User meta key. 342 * @param mixed $value User meta value. 343 */ 344 public function __set( $key, $value ) { 345 if ( 'id' === $key ) { 346 _deprecated_argument( 347 'WP_User->id', 348 '2.1.0', 349 sprintf( 350 /* translators: %s: WP_User->ID */ 351 __( 'Use %s instead.' ), 352 '<code>WP_User->ID</code>' 353 ) 354 ); 355 $this->ID = $value; 356 return; 357 } 358 359 $this->data->$key = $value; 360 } 361 362 /** 363 * Magic method for unsetting a certain custom field. 364 * 365 * @since 4.4.0 366 * 367 * @param string $key User meta key to unset. 368 */ 369 public function __unset( $key ) { 370 if ( 'id' === $key ) { 371 _deprecated_argument( 372 'WP_User->id', 373 '2.1.0', 374 sprintf( 375 /* translators: %s: WP_User->ID */ 376 __( 'Use %s instead.' ), 377 '<code>WP_User->ID</code>' 378 ) 379 ); 380 } 381 382 if ( isset( $this->data->$key ) ) { 383 unset( $this->data->$key ); 384 } 385 386 if ( isset( self::$back_compat_keys[ $key ] ) ) { 387 unset( self::$back_compat_keys[ $key ] ); 388 } 389 } 390 391 /** 392 * Determines whether the user exists in the database. 393 * 394 * @since 3.4.0 395 * 396 * @return bool True if user exists in the database, false if not. 397 */ 398 public function exists() { 399 return ! empty( $this->ID ); 400 } 401 402 /** 403 * Retrieves the value of a property or meta key. 404 * 405 * Retrieves from the users and usermeta table. 406 * 407 * @since 3.3.0 408 * 409 * @param string $key Property 410 * @return mixed 411 */ 412 public function get( $key ) { 413 return $this->__get( $key ); 414 } 415 416 /** 417 * Determines whether a property or meta key is set. 418 * 419 * Consults the users and usermeta tables. 420 * 421 * @since 3.3.0 422 * 423 * @param string $key Property. 424 * @return bool 425 */ 426 public function has_prop( $key ) { 427 return $this->__isset( $key ); 428 } 429 430 /** 431 * Returns an array representation. 432 * 433 * @since 3.5.0 434 * 435 * @return array Array representation. 436 */ 437 public function to_array() { 438 return get_object_vars( $this->data ); 439 } 440 441 /** 442 * Makes private/protected methods readable for backward compatibility. 443 * 444 * @since 4.3.0 445 * 446 * @param string $name Method to call. 447 * @param array $arguments Arguments to pass when calling. 448 * @return mixed|false Return value of the callback, false otherwise. 449 */ 450 public function __call( $name, $arguments ) { 451 if ( '_init_caps' === $name ) { 452 return $this->_init_caps( ...$arguments ); 453 } 454 return false; 455 } 456 457 /** 458 * Sets up capability object properties. 459 * 460 * Will set the value for the 'cap_key' property to current database table 461 * prefix, followed by 'capabilities'. Will then check to see if the 462 * property matching the 'cap_key' exists and is an array. If so, it will be 463 * used. 464 * 465 * @since 2.1.0 466 * @deprecated 4.9.0 Use WP_User::for_site() 467 * 468 * @global wpdb $wpdb WordPress database abstraction object. 469 * 470 * @param string $cap_key Optional capability key 471 */ 472 protected function _init_caps( $cap_key = '' ) { 473 global $wpdb; 474 475 _deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' ); 476 477 if ( empty( $cap_key ) ) { 478 $this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities'; 479 } else { 480 $this->cap_key = $cap_key; 481 } 482 483 $this->caps = $this->get_caps_data(); 484 485 $this->get_role_caps(); 486 } 487 488 /** 489 * Retrieves all of the capabilities of the user's roles, and merges them with 490 * individual user capabilities. 491 * 492 * All of the capabilities of the user's roles are merged with the user's individual 493 * capabilities. This means that the user can be denied specific capabilities that 494 * their role might have, but the user is specifically denied. 495 * 496 * @since 2.0.0 497 * 498 * @return bool[] Array of key/value pairs where keys represent a capability name 499 * and boolean values represent whether the user has that capability. 500 */ 501 public function get_role_caps() { 502 $switch_site = false; 503 if ( is_multisite() && get_current_blog_id() != $this->site_id ) { 504 $switch_site = true; 505 506 switch_to_blog( $this->site_id ); 507 } 508 509 $wp_roles = wp_roles(); 510 511 // Filter out caps that are not role names and assign to $this->roles. 512 if ( is_array( $this->caps ) ) { 513 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) ); 514 } 515 516 // Build $allcaps from role caps, overlay user's $caps. 517 $this->allcaps = array(); 518 foreach ( (array) $this->roles as $role ) { 519 $the_role = $wp_roles->get_role( $role ); 520 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities ); 521 } 522 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); 523 524 if ( $switch_site ) { 525 restore_current_blog(); 526 } 527 528 return $this->allcaps; 529 } 530 531 /** 532 * Adds role to user. 533 * 534 * Updates the user's meta data option with capabilities and roles. 535 * 536 * @since 2.0.0 537 * 538 * @param string $role Role name. 539 */ 540 public function add_role( $role ) { 541 if ( empty( $role ) ) { 542 return; 543 } 544 545 if ( in_array( $role, $this->roles, true ) ) { 546 return; 547 } 548 549 $this->caps[ $role ] = true; 550 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 551 $this->get_role_caps(); 552 $this->update_user_level_from_caps(); 553 554 /** 555 * Fires immediately after the user has been given a new role. 556 * 557 * @since 4.3.0 558 * 559 * @param int $user_id The user ID. 560 * @param string $role The new role. 561 */ 562 do_action( 'add_user_role', $this->ID, $role ); 563 } 564 565 /** 566 * Removes role from user. 567 * 568 * @since 2.0.0 569 * 570 * @param string $role Role name. 571 */ 572 public function remove_role( $role ) { 573 if ( ! in_array( $role, $this->roles, true ) ) { 574 return; 575 } 576 577 unset( $this->caps[ $role ] ); 578 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 579 $this->get_role_caps(); 580 $this->update_user_level_from_caps(); 581 582 /** 583 * Fires immediately after a role as been removed from a user. 584 * 585 * @since 4.3.0 586 * 587 * @param int $user_id The user ID. 588 * @param string $role The removed role. 589 */ 590 do_action( 'remove_user_role', $this->ID, $role ); 591 } 592 593 /** 594 * Sets the role of the user. 595 * 596 * This will remove the previous roles of the user and assign the user the 597 * new one. You can set the role to an empty string and it will remove all 598 * of the roles from the user. 599 * 600 * @since 2.0.0 601 * 602 * @param string $role Role name. 603 */ 604 public function set_role( $role ) { 605 if ( 1 === count( $this->roles ) && current( $this->roles ) == $role ) { 606 return; 607 } 608 609 foreach ( (array) $this->roles as $oldrole ) { 610 unset( $this->caps[ $oldrole ] ); 611 } 612 613 $old_roles = $this->roles; 614 615 if ( ! empty( $role ) ) { 616 $this->caps[ $role ] = true; 617 $this->roles = array( $role => true ); 618 } else { 619 $this->roles = array(); 620 } 621 622 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 623 $this->get_role_caps(); 624 $this->update_user_level_from_caps(); 625 626 foreach ( $old_roles as $old_role ) { 627 if ( ! $old_role || $old_role === $role ) { 628 continue; 629 } 630 631 /** This action is documented in wp-includes/class-wp-user.php */ 632 do_action( 'remove_user_role', $this->ID, $old_role ); 633 } 634 635 if ( $role && ! in_array( $role, $old_roles, true ) ) { 636 /** This action is documented in wp-includes/class-wp-user.php */ 637 do_action( 'add_user_role', $this->ID, $role ); 638 } 639 640 /** 641 * Fires after the user's role has changed. 642 * 643 * @since 2.9.0 644 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles. 645 * 646 * @param int $user_id The user ID. 647 * @param string $role The new role. 648 * @param string[] $old_roles An array of the user's previous roles. 649 */ 650 do_action( 'set_user_role', $this->ID, $role, $old_roles ); 651 } 652 653 /** 654 * Chooses the maximum level the user has. 655 * 656 * Will compare the level from the $item parameter against the $max 657 * parameter. If the item is incorrect, then just the $max parameter value 658 * will be returned. 659 * 660 * Used to get the max level based on the capabilities the user has. This 661 * is also based on roles, so if the user is assigned the Administrator role 662 * then the capability 'level_10' will exist and the user will get that 663 * value. 664 * 665 * @since 2.0.0 666 * 667 * @param int $max Max level of user. 668 * @param string $item Level capability name. 669 * @return int Max Level. 670 */ 671 public function level_reduction( $max, $item ) { 672 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) { 673 $level = (int) $matches[1]; 674 return max( $max, $level ); 675 } else { 676 return $max; 677 } 678 } 679 680 /** 681 * Updates the maximum user level for the user. 682 * 683 * Updates the 'user_level' user metadata (includes prefix that is the 684 * database table prefix) with the maximum user level. Gets the value from 685 * the all of the capabilities that the user has. 686 * 687 * @since 2.0.0 688 * 689 * @global wpdb $wpdb WordPress database abstraction object. 690 */ 691 public function update_user_level_from_caps() { 692 global $wpdb; 693 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 ); 694 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level ); 695 } 696 697 /** 698 * Adds capability and grant or deny access to capability. 699 * 700 * @since 2.0.0 701 * 702 * @param string $cap Capability name. 703 * @param bool $grant Whether to grant capability to user. 704 */ 705 public function add_cap( $cap, $grant = true ) { 706 $this->caps[ $cap ] = $grant; 707 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 708 $this->get_role_caps(); 709 $this->update_user_level_from_caps(); 710 } 711 712 /** 713 * Removes capability from user. 714 * 715 * @since 2.0.0 716 * 717 * @param string $cap Capability name. 718 */ 719 public function remove_cap( $cap ) { 720 if ( ! isset( $this->caps[ $cap ] ) ) { 721 return; 722 } 723 unset( $this->caps[ $cap ] ); 724 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 725 $this->get_role_caps(); 726 $this->update_user_level_from_caps(); 727 } 728 729 /** 730 * Removes all of the capabilities of the user. 731 * 732 * @since 2.1.0 733 * 734 * @global wpdb $wpdb WordPress database abstraction object. 735 */ 736 public function remove_all_caps() { 737 global $wpdb; 738 $this->caps = array(); 739 delete_user_meta( $this->ID, $this->cap_key ); 740 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' ); 741 $this->get_role_caps(); 742 } 743 744 /** 745 * Returns whether the user has the specified capability. 746 * 747 * This function also accepts an ID of an object to check against if the capability is a meta capability. Meta 748 * capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to 749 * map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. 750 * 751 * Example usage: 752 * 753 * $user->has_cap( 'edit_posts' ); 754 * $user->has_cap( 'edit_post', $post->ID ); 755 * $user->has_cap( 'edit_post_meta', $post->ID, $meta_key ); 756 * 757 * While checking against a role in place of a capability is supported in part, this practice is discouraged as it 758 * may produce unreliable results. 759 * 760 * @since 2.0.0 761 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter 762 * by adding it to the function signature. 763 * 764 * @see map_meta_cap() 765 * 766 * @param string $cap Capability name. 767 * @param mixed ...$args Optional further parameters, typically starting with an object ID. 768 * @return bool Whether the user has the given capability, or, if an object ID is passed, whether the user has 769 * the given capability for that object. 770 */ 771 public function has_cap( $cap, ...$args ) { 772 if ( is_numeric( $cap ) ) { 773 _deprecated_argument( __FUNCTION__, '2.0.0', __( 'Usage of user levels is deprecated. Use capabilities instead.' ) ); 774 $cap = $this->translate_level_to_cap( $cap ); 775 } 776 777 $caps = map_meta_cap( $cap, $this->ID, ...$args ); 778 779 // Multisite super admin has all caps by definition, Unless specifically denied. 780 if ( is_multisite() && is_super_admin( $this->ID ) ) { 781 if ( in_array( 'do_not_allow', $caps, true ) ) { 782 return false; 783 } 784 return true; 785 } 786 787 // Maintain BC for the argument passed to the "user_has_cap" filter. 788 $args = array_merge( array( $cap, $this->ID ), $args ); 789 790 /** 791 * Dynamically filter a user's capabilities. 792 * 793 * @since 2.0.0 794 * @since 3.7.0 Added the `$user` parameter. 795 * 796 * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name 797 * and boolean values represent whether the user has that capability. 798 * @param string[] $caps Required primitive capabilities for the requested capability. 799 * @param array $args { 800 * Arguments that accompany the requested capability check. 801 * 802 * @type string $0 Requested capability. 803 * @type int $1 Concerned user ID. 804 * @type mixed ...$2 Optional second and further parameters, typically object ID. 805 * } 806 * @param WP_User $user The user object. 807 */ 808 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this ); 809 810 // Everyone is allowed to exist. 811 $capabilities['exist'] = true; 812 813 // Nobody is allowed to do things they are not allowed to do. 814 unset( $capabilities['do_not_allow'] ); 815 816 // Must have ALL requested caps. 817 foreach ( (array) $caps as $cap ) { 818 if ( empty( $capabilities[ $cap ] ) ) { 819 return false; 820 } 821 } 822 823 return true; 824 } 825 826 /** 827 * Converts numeric level to level capability name. 828 * 829 * Prepends 'level_' to level number. 830 * 831 * @since 2.0.0 832 * 833 * @param int $level Level number, 1 to 10. 834 * @return string 835 */ 836 public function translate_level_to_cap( $level ) { 837 return 'level_' . $level; 838 } 839 840 /** 841 * Sets the site to operate on. Defaults to the current site. 842 * 843 * @since 3.0.0 844 * @deprecated 4.9.0 Use WP_User::for_site() 845 * 846 * @param int $blog_id Optional. Site ID, defaults to current site. 847 */ 848 public function for_blog( $blog_id = '' ) { 849 _deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' ); 850 851 $this->for_site( $blog_id ); 852 } 853 854 /** 855 * Sets the site to operate on. Defaults to the current site. 856 * 857 * @since 4.9.0 858 * 859 * @global wpdb $wpdb WordPress database abstraction object. 860 * 861 * @param int $site_id Site ID to initialize user capabilities for. Default is the current site. 862 */ 863 public function for_site( $site_id = '' ) { 864 global $wpdb; 865 866 if ( ! empty( $site_id ) ) { 867 $this->site_id = absint( $site_id ); 868 } else { 869 $this->site_id = get_current_blog_id(); 870 } 871 872 $this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities'; 873 874 $this->caps = $this->get_caps_data(); 875 876 $this->get_role_caps(); 877 } 878 879 /** 880 * Gets the ID of the site for which the user's capabilities are currently initialized. 881 * 882 * @since 4.9.0 883 * 884 * @return int Site ID. 885 */ 886 public function get_site_id() { 887 return $this->site_id; 888 } 889 890 /** 891 * Gets the available user capabilities data. 892 * 893 * @since 4.9.0 894 * 895 * @return bool[] List of capabilities keyed by the capability name, 896 * e.g. array( 'edit_posts' => true, 'delete_posts' => false ). 897 */ 898 private function get_caps_data() { 899 $caps = get_user_meta( $this->ID, $this->cap_key, true ); 900 901 if ( ! is_array( $caps ) ) { 902 return array(); 903 } 904 905 return $caps; 906 } 907 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |