[ 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   * @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       * Return 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       * Determine 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       * Retrieve 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       * Determine 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       * Return 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       * Set 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       * Add 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          $this->caps[ $role ] = true;
 546          update_user_meta( $this->ID, $this->cap_key, $this->caps );
 547          $this->get_role_caps();
 548          $this->update_user_level_from_caps();
 549  
 550          /**
 551           * Fires immediately after the user has been given a new role.
 552           *
 553           * @since 4.3.0
 554           *
 555           * @param int    $user_id The user ID.
 556           * @param string $role    The new role.
 557           */
 558          do_action( 'add_user_role', $this->ID, $role );
 559      }
 560  
 561      /**
 562       * Remove role from user.
 563       *
 564       * @since 2.0.0
 565       *
 566       * @param string $role Role name.
 567       */
 568  	public function remove_role( $role ) {
 569          if ( ! in_array( $role, $this->roles, true ) ) {
 570              return;
 571          }
 572          unset( $this->caps[ $role ] );
 573          update_user_meta( $this->ID, $this->cap_key, $this->caps );
 574          $this->get_role_caps();
 575          $this->update_user_level_from_caps();
 576  
 577          /**
 578           * Fires immediately after a role as been removed from a user.
 579           *
 580           * @since 4.3.0
 581           *
 582           * @param int    $user_id The user ID.
 583           * @param string $role    The removed role.
 584           */
 585          do_action( 'remove_user_role', $this->ID, $role );
 586      }
 587  
 588      /**
 589       * Set the role of the user.
 590       *
 591       * This will remove the previous roles of the user and assign the user the
 592       * new one. You can set the role to an empty string and it will remove all
 593       * of the roles from the user.
 594       *
 595       * @since 2.0.0
 596       *
 597       * @param string $role Role name.
 598       */
 599  	public function set_role( $role ) {
 600          if ( 1 === count( $this->roles ) && current( $this->roles ) == $role ) {
 601              return;
 602          }
 603  
 604          foreach ( (array) $this->roles as $oldrole ) {
 605              unset( $this->caps[ $oldrole ] );
 606          }
 607  
 608          $old_roles = $this->roles;
 609          if ( ! empty( $role ) ) {
 610              $this->caps[ $role ] = true;
 611              $this->roles         = array( $role => true );
 612          } else {
 613              $this->roles = false;
 614          }
 615          update_user_meta( $this->ID, $this->cap_key, $this->caps );
 616          $this->get_role_caps();
 617          $this->update_user_level_from_caps();
 618  
 619          /**
 620           * Fires after the user's role has changed.
 621           *
 622           * @since 2.9.0
 623           * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
 624           *
 625           * @param int      $user_id   The user ID.
 626           * @param string   $role      The new role.
 627           * @param string[] $old_roles An array of the user's previous roles.
 628           */
 629          do_action( 'set_user_role', $this->ID, $role, $old_roles );
 630      }
 631  
 632      /**
 633       * Choose the maximum level the user has.
 634       *
 635       * Will compare the level from the $item parameter against the $max
 636       * parameter. If the item is incorrect, then just the $max parameter value
 637       * will be returned.
 638       *
 639       * Used to get the max level based on the capabilities the user has. This
 640       * is also based on roles, so if the user is assigned the Administrator role
 641       * then the capability 'level_10' will exist and the user will get that
 642       * value.
 643       *
 644       * @since 2.0.0
 645       *
 646       * @param int    $max  Max level of user.
 647       * @param string $item Level capability name.
 648       * @return int Max Level.
 649       */
 650  	public function level_reduction( $max, $item ) {
 651          if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
 652              $level = (int) $matches[1];
 653              return max( $max, $level );
 654          } else {
 655              return $max;
 656          }
 657      }
 658  
 659      /**
 660       * Update the maximum user level for the user.
 661       *
 662       * Updates the 'user_level' user metadata (includes prefix that is the
 663       * database table prefix) with the maximum user level. Gets the value from
 664       * the all of the capabilities that the user has.
 665       *
 666       * @since 2.0.0
 667       *
 668       * @global wpdb $wpdb WordPress database abstraction object.
 669       */
 670  	public function update_user_level_from_caps() {
 671          global $wpdb;
 672          $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
 673          update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
 674      }
 675  
 676      /**
 677       * Add capability and grant or deny access to capability.
 678       *
 679       * @since 2.0.0
 680       *
 681       * @param string $cap   Capability name.
 682       * @param bool   $grant Whether to grant capability to user.
 683       */
 684  	public function add_cap( $cap, $grant = true ) {
 685          $this->caps[ $cap ] = $grant;
 686          update_user_meta( $this->ID, $this->cap_key, $this->caps );
 687          $this->get_role_caps();
 688          $this->update_user_level_from_caps();
 689      }
 690  
 691      /**
 692       * Remove capability from user.
 693       *
 694       * @since 2.0.0
 695       *
 696       * @param string $cap Capability name.
 697       */
 698  	public function remove_cap( $cap ) {
 699          if ( ! isset( $this->caps[ $cap ] ) ) {
 700              return;
 701          }
 702          unset( $this->caps[ $cap ] );
 703          update_user_meta( $this->ID, $this->cap_key, $this->caps );
 704          $this->get_role_caps();
 705          $this->update_user_level_from_caps();
 706      }
 707  
 708      /**
 709       * Remove all of the capabilities of the user.
 710       *
 711       * @since 2.1.0
 712       *
 713       * @global wpdb $wpdb WordPress database abstraction object.
 714       */
 715  	public function remove_all_caps() {
 716          global $wpdb;
 717          $this->caps = array();
 718          delete_user_meta( $this->ID, $this->cap_key );
 719          delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
 720          $this->get_role_caps();
 721      }
 722  
 723      /**
 724       * Returns whether the user has the specified capability.
 725       *
 726       * This function also accepts an ID of an object to check against if the capability is a meta capability. Meta
 727       * capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to
 728       * map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`.
 729       *
 730       * Example usage:
 731       *
 732       *     $user->has_cap( 'edit_posts' );
 733       *     $user->has_cap( 'edit_post', $post->ID );
 734       *     $user->has_cap( 'edit_post_meta', $post->ID, $meta_key );
 735       *
 736       * While checking against a role in place of a capability is supported in part, this practice is discouraged as it
 737       * may produce unreliable results.
 738       *
 739       * @since 2.0.0
 740       * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 741       *              by adding it to the function signature.
 742       *
 743       * @see map_meta_cap()
 744       *
 745       * @param string $cap     Capability name.
 746       * @param mixed  ...$args Optional further parameters, typically starting with an object ID.
 747       * @return bool Whether the user has the given capability, or, if an object ID is passed, whether the user has
 748       *              the given capability for that object.
 749       */
 750  	public function has_cap( $cap, ...$args ) {
 751          if ( is_numeric( $cap ) ) {
 752              _deprecated_argument( __FUNCTION__, '2.0.0', __( 'Usage of user levels is deprecated. Use capabilities instead.' ) );
 753              $cap = $this->translate_level_to_cap( $cap );
 754          }
 755  
 756          $caps = map_meta_cap( $cap, $this->ID, ...$args );
 757  
 758          // Multisite super admin has all caps by definition, Unless specifically denied.
 759          if ( is_multisite() && is_super_admin( $this->ID ) ) {
 760              if ( in_array( 'do_not_allow', $caps, true ) ) {
 761                  return false;
 762              }
 763              return true;
 764          }
 765  
 766          // Maintain BC for the argument passed to the "user_has_cap" filter.
 767          $args = array_merge( array( $cap, $this->ID ), $args );
 768  
 769          /**
 770           * Dynamically filter a user's capabilities.
 771           *
 772           * @since 2.0.0
 773           * @since 3.7.0 Added the `$user` parameter.
 774           *
 775           * @param bool[]   $allcaps Array of key/value pairs where keys represent a capability name
 776           *                          and boolean values represent whether the user has that capability.
 777           * @param string[] $caps    Required primitive capabilities for the requested capability.
 778           * @param array    $args {
 779           *     Arguments that accompany the requested capability check.
 780           *
 781           *     @type string    $0 Requested capability.
 782           *     @type int       $1 Concerned user ID.
 783           *     @type mixed  ...$2 Optional second and further parameters, typically object ID.
 784           * }
 785           * @param WP_User  $user    The user object.
 786           */
 787          $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
 788  
 789          // Everyone is allowed to exist.
 790          $capabilities['exist'] = true;
 791  
 792          // Nobody is allowed to do things they are not allowed to do.
 793          unset( $capabilities['do_not_allow'] );
 794  
 795          // Must have ALL requested caps.
 796          foreach ( (array) $caps as $cap ) {
 797              if ( empty( $capabilities[ $cap ] ) ) {
 798                  return false;
 799              }
 800          }
 801  
 802          return true;
 803      }
 804  
 805      /**
 806       * Convert numeric level to level capability name.
 807       *
 808       * Prepends 'level_' to level number.
 809       *
 810       * @since 2.0.0
 811       *
 812       * @param int $level Level number, 1 to 10.
 813       * @return string
 814       */
 815  	public function translate_level_to_cap( $level ) {
 816          return 'level_' . $level;
 817      }
 818  
 819      /**
 820       * Set the site to operate on. Defaults to the current site.
 821       *
 822       * @since 3.0.0
 823       * @deprecated 4.9.0 Use WP_User::for_site()
 824       *
 825       * @param int $blog_id Optional. Site ID, defaults to current site.
 826       */
 827  	public function for_blog( $blog_id = '' ) {
 828          _deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' );
 829  
 830          $this->for_site( $blog_id );
 831      }
 832  
 833      /**
 834       * Sets the site to operate on. Defaults to the current site.
 835       *
 836       * @since 4.9.0
 837       *
 838       * @global wpdb $wpdb WordPress database abstraction object.
 839       *
 840       * @param int $site_id Site ID to initialize user capabilities for. Default is the current site.
 841       */
 842  	public function for_site( $site_id = '' ) {
 843          global $wpdb;
 844  
 845          if ( ! empty( $site_id ) ) {
 846              $this->site_id = absint( $site_id );
 847          } else {
 848              $this->site_id = get_current_blog_id();
 849          }
 850  
 851          $this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities';
 852  
 853          $this->caps = $this->get_caps_data();
 854  
 855          $this->get_role_caps();
 856      }
 857  
 858      /**
 859       * Gets the ID of the site for which the user's capabilities are currently initialized.
 860       *
 861       * @since 4.9.0
 862       *
 863       * @return int Site ID.
 864       */
 865  	public function get_site_id() {
 866          return $this->site_id;
 867      }
 868  
 869      /**
 870       * Gets the available user capabilities data.
 871       *
 872       * @since 4.9.0
 873       *
 874       * @return bool[] List of capabilities keyed by the capability name,
 875       *                e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
 876       */
 877  	private function get_caps_data() {
 878          $caps = get_user_meta( $this->ID, $this->cap_key, true );
 879  
 880          if ( ! is_array( $caps ) ) {
 881              return array();
 882          }
 883  
 884          return $caps;
 885      }
 886  }


Generated: Mon Jan 24 01:00:08 2022 Cross-referenced by PHPXref 0.7.1