[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7