[ Index ]

PHP Cross Reference of WordPress

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


Generated: Wed Sep 23 01:00:03 2020 Cross-referenced by PHPXref 0.7.1