[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-user.php (source)

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


Generated : Sun Mar 9 08:20:01 2025 Cross-referenced by PHPXref