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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7