wp/wp-includes/class-wp-user.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    32  * @property string $spam
    32  * @property string $spam
    33  * @property string $deleted
    33  * @property string $deleted
    34  * @property string $locale
    34  * @property string $locale
    35  * @property string $rich_editing
    35  * @property string $rich_editing
    36  * @property string $syntax_highlighting
    36  * @property string $syntax_highlighting
       
    37  * @property string $use_ssl
    37  */
    38  */
    38 class WP_User {
    39 class WP_User {
    39 	/**
    40 	/**
    40 	 * User data container.
    41 	 * User data container.
    41 	 *
    42 	 *
   175 
   176 
   176 		$this->for_site( $site_id );
   177 		$this->for_site( $site_id );
   177 	}
   178 	}
   178 
   179 
   179 	/**
   180 	/**
   180 	 * Return only the main user fields
   181 	 * Returns only the main user fields.
   181 	 *
   182 	 *
   182 	 * @since 3.3.0
   183 	 * @since 3.3.0
   183 	 * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
   184 	 * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
   184 	 *
   185 	 *
   185 	 * @global wpdb $wpdb WordPress database abstraction object.
   186 	 * @global wpdb $wpdb WordPress database abstraction object.
   186 	 *
   187 	 *
   187 	 * @param string     $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'.
   188 	 * @param string     $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'.
   188 	 * @param string|int $value The field value
   189 	 * @param string|int $value The field value.
   189 	 * @return object|false Raw user object
   190 	 * @return object|false Raw user object.
   190 	 */
   191 	 */
   191 	public static function get_data_by( $field, $value ) {
   192 	public static function get_data_by( $field, $value ) {
   192 		global $wpdb;
   193 		global $wpdb;
   193 
   194 
   194 		// 'ID' is an alias of 'id'.
   195 		// 'ID' is an alias of 'id'.
   386 			unset( self::$back_compat_keys[ $key ] );
   387 			unset( self::$back_compat_keys[ $key ] );
   387 		}
   388 		}
   388 	}
   389 	}
   389 
   390 
   390 	/**
   391 	/**
   391 	 * Determine whether the user exists in the database.
   392 	 * Determines whether the user exists in the database.
   392 	 *
   393 	 *
   393 	 * @since 3.4.0
   394 	 * @since 3.4.0
   394 	 *
   395 	 *
   395 	 * @return bool True if user exists in the database, false if not.
   396 	 * @return bool True if user exists in the database, false if not.
   396 	 */
   397 	 */
   397 	public function exists() {
   398 	public function exists() {
   398 		return ! empty( $this->ID );
   399 		return ! empty( $this->ID );
   399 	}
   400 	}
   400 
   401 
   401 	/**
   402 	/**
   402 	 * Retrieve the value of a property or meta key.
   403 	 * Retrieves the value of a property or meta key.
   403 	 *
   404 	 *
   404 	 * Retrieves from the users and usermeta table.
   405 	 * Retrieves from the users and usermeta table.
   405 	 *
   406 	 *
   406 	 * @since 3.3.0
   407 	 * @since 3.3.0
   407 	 *
   408 	 *
   411 	public function get( $key ) {
   412 	public function get( $key ) {
   412 		return $this->__get( $key );
   413 		return $this->__get( $key );
   413 	}
   414 	}
   414 
   415 
   415 	/**
   416 	/**
   416 	 * Determine whether a property or meta key is set
   417 	 * Determines whether a property or meta key is set.
   417 	 *
   418 	 *
   418 	 * Consults the users and usermeta tables.
   419 	 * Consults the users and usermeta tables.
   419 	 *
   420 	 *
   420 	 * @since 3.3.0
   421 	 * @since 3.3.0
   421 	 *
   422 	 *
   422 	 * @param string $key Property
   423 	 * @param string $key Property.
   423 	 * @return bool
   424 	 * @return bool
   424 	 */
   425 	 */
   425 	public function has_prop( $key ) {
   426 	public function has_prop( $key ) {
   426 		return $this->__isset( $key );
   427 		return $this->__isset( $key );
   427 	}
   428 	}
   428 
   429 
   429 	/**
   430 	/**
   430 	 * Return an array representation.
   431 	 * Returns an array representation.
   431 	 *
   432 	 *
   432 	 * @since 3.5.0
   433 	 * @since 3.5.0
   433 	 *
   434 	 *
   434 	 * @return array Array representation.
   435 	 * @return array Array representation.
   435 	 */
   436 	 */
   452 		}
   453 		}
   453 		return false;
   454 		return false;
   454 	}
   455 	}
   455 
   456 
   456 	/**
   457 	/**
   457 	 * Set up capability object properties.
   458 	 * Sets up capability object properties.
   458 	 *
   459 	 *
   459 	 * Will set the value for the 'cap_key' property to current database table
   460 	 * Will set the value for the 'cap_key' property to current database table
   460 	 * prefix, followed by 'capabilities'. Will then check to see if the
   461 	 * prefix, followed by 'capabilities'. Will then check to see if the
   461 	 * property matching the 'cap_key' exists and is an array. If so, it will be
   462 	 * property matching the 'cap_key' exists and is an array. If so, it will be
   462 	 * used.
   463 	 * used.
   526 
   527 
   527 		return $this->allcaps;
   528 		return $this->allcaps;
   528 	}
   529 	}
   529 
   530 
   530 	/**
   531 	/**
   531 	 * Add role to user.
   532 	 * Adds role to user.
   532 	 *
   533 	 *
   533 	 * Updates the user's meta data option with capabilities and roles.
   534 	 * Updates the user's meta data option with capabilities and roles.
   534 	 *
   535 	 *
   535 	 * @since 2.0.0
   536 	 * @since 2.0.0
   536 	 *
   537 	 *
   537 	 * @param string $role Role name.
   538 	 * @param string $role Role name.
   538 	 */
   539 	 */
   539 	public function add_role( $role ) {
   540 	public function add_role( $role ) {
   540 		if ( empty( $role ) ) {
   541 		if ( empty( $role ) ) {
       
   542 			return;
       
   543 		}
       
   544 
       
   545 		if ( in_array( $role, $this->roles, true ) ) {
   541 			return;
   546 			return;
   542 		}
   547 		}
   543 
   548 
   544 		$this->caps[ $role ] = true;
   549 		$this->caps[ $role ] = true;
   545 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   550 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   556 		 */
   561 		 */
   557 		do_action( 'add_user_role', $this->ID, $role );
   562 		do_action( 'add_user_role', $this->ID, $role );
   558 	}
   563 	}
   559 
   564 
   560 	/**
   565 	/**
   561 	 * Remove role from user.
   566 	 * Removes role from user.
   562 	 *
   567 	 *
   563 	 * @since 2.0.0
   568 	 * @since 2.0.0
   564 	 *
   569 	 *
   565 	 * @param string $role Role name.
   570 	 * @param string $role Role name.
   566 	 */
   571 	 */
   567 	public function remove_role( $role ) {
   572 	public function remove_role( $role ) {
   568 		if ( ! in_array( $role, $this->roles, true ) ) {
   573 		if ( ! in_array( $role, $this->roles, true ) ) {
   569 			return;
   574 			return;
   570 		}
   575 		}
       
   576 
   571 		unset( $this->caps[ $role ] );
   577 		unset( $this->caps[ $role ] );
   572 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   578 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   573 		$this->get_role_caps();
   579 		$this->get_role_caps();
   574 		$this->update_user_level_from_caps();
   580 		$this->update_user_level_from_caps();
   575 
   581 
   583 		 */
   589 		 */
   584 		do_action( 'remove_user_role', $this->ID, $role );
   590 		do_action( 'remove_user_role', $this->ID, $role );
   585 	}
   591 	}
   586 
   592 
   587 	/**
   593 	/**
   588 	 * Set the role of the user.
   594 	 * Sets the role of the user.
   589 	 *
   595 	 *
   590 	 * This will remove the previous roles of the user and assign the user the
   596 	 * This will remove the previous roles of the user and assign the user the
   591 	 * new one. You can set the role to an empty string and it will remove all
   597 	 * new one. You can set the role to an empty string and it will remove all
   592 	 * of the roles from the user.
   598 	 * of the roles from the user.
   593 	 *
   599 	 *
   603 		foreach ( (array) $this->roles as $oldrole ) {
   609 		foreach ( (array) $this->roles as $oldrole ) {
   604 			unset( $this->caps[ $oldrole ] );
   610 			unset( $this->caps[ $oldrole ] );
   605 		}
   611 		}
   606 
   612 
   607 		$old_roles = $this->roles;
   613 		$old_roles = $this->roles;
       
   614 
   608 		if ( ! empty( $role ) ) {
   615 		if ( ! empty( $role ) ) {
   609 			$this->caps[ $role ] = true;
   616 			$this->caps[ $role ] = true;
   610 			$this->roles         = array( $role => true );
   617 			$this->roles         = array( $role => true );
   611 		} else {
   618 		} else {
   612 			$this->roles = false;
   619 			$this->roles = array();
   613 		}
   620 		}
       
   621 
   614 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   622 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   615 		$this->get_role_caps();
   623 		$this->get_role_caps();
   616 		$this->update_user_level_from_caps();
   624 		$this->update_user_level_from_caps();
       
   625 
       
   626 		foreach ( $old_roles as $old_role ) {
       
   627 			if ( ! $old_role || $old_role === $role ) {
       
   628 				continue;
       
   629 			}
       
   630 
       
   631 			/** This action is documented in wp-includes/class-wp-user.php */
       
   632 			do_action( 'remove_user_role', $this->ID, $old_role );
       
   633 		}
       
   634 
       
   635 		if ( $role && ! in_array( $role, $old_roles, true ) ) {
       
   636 			/** This action is documented in wp-includes/class-wp-user.php */
       
   637 			do_action( 'add_user_role', $this->ID, $role );
       
   638 		}
   617 
   639 
   618 		/**
   640 		/**
   619 		 * Fires after the user's role has changed.
   641 		 * Fires after the user's role has changed.
   620 		 *
   642 		 *
   621 		 * @since 2.9.0
   643 		 * @since 2.9.0
   627 		 */
   649 		 */
   628 		do_action( 'set_user_role', $this->ID, $role, $old_roles );
   650 		do_action( 'set_user_role', $this->ID, $role, $old_roles );
   629 	}
   651 	}
   630 
   652 
   631 	/**
   653 	/**
   632 	 * Choose the maximum level the user has.
   654 	 * Chooses the maximum level the user has.
   633 	 *
   655 	 *
   634 	 * Will compare the level from the $item parameter against the $max
   656 	 * Will compare the level from the $item parameter against the $max
   635 	 * parameter. If the item is incorrect, then just the $max parameter value
   657 	 * parameter. If the item is incorrect, then just the $max parameter value
   636 	 * will be returned.
   658 	 * will be returned.
   637 	 *
   659 	 *
   654 			return $max;
   676 			return $max;
   655 		}
   677 		}
   656 	}
   678 	}
   657 
   679 
   658 	/**
   680 	/**
   659 	 * Update the maximum user level for the user.
   681 	 * Updates the maximum user level for the user.
   660 	 *
   682 	 *
   661 	 * Updates the 'user_level' user metadata (includes prefix that is the
   683 	 * Updates the 'user_level' user metadata (includes prefix that is the
   662 	 * database table prefix) with the maximum user level. Gets the value from
   684 	 * database table prefix) with the maximum user level. Gets the value from
   663 	 * the all of the capabilities that the user has.
   685 	 * the all of the capabilities that the user has.
   664 	 *
   686 	 *
   671 		$this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
   693 		$this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
   672 		update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
   694 		update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
   673 	}
   695 	}
   674 
   696 
   675 	/**
   697 	/**
   676 	 * Add capability and grant or deny access to capability.
   698 	 * Adds capability and grant or deny access to capability.
   677 	 *
   699 	 *
   678 	 * @since 2.0.0
   700 	 * @since 2.0.0
   679 	 *
   701 	 *
   680 	 * @param string $cap   Capability name.
   702 	 * @param string $cap   Capability name.
   681 	 * @param bool   $grant Whether to grant capability to user.
   703 	 * @param bool   $grant Whether to grant capability to user.
   686 		$this->get_role_caps();
   708 		$this->get_role_caps();
   687 		$this->update_user_level_from_caps();
   709 		$this->update_user_level_from_caps();
   688 	}
   710 	}
   689 
   711 
   690 	/**
   712 	/**
   691 	 * Remove capability from user.
   713 	 * Removes capability from user.
   692 	 *
   714 	 *
   693 	 * @since 2.0.0
   715 	 * @since 2.0.0
   694 	 *
   716 	 *
   695 	 * @param string $cap Capability name.
   717 	 * @param string $cap Capability name.
   696 	 */
   718 	 */
   703 		$this->get_role_caps();
   725 		$this->get_role_caps();
   704 		$this->update_user_level_from_caps();
   726 		$this->update_user_level_from_caps();
   705 	}
   727 	}
   706 
   728 
   707 	/**
   729 	/**
   708 	 * Remove all of the capabilities of the user.
   730 	 * Removes all of the capabilities of the user.
   709 	 *
   731 	 *
   710 	 * @since 2.1.0
   732 	 * @since 2.1.0
   711 	 *
   733 	 *
   712 	 * @global wpdb $wpdb WordPress database abstraction object.
   734 	 * @global wpdb $wpdb WordPress database abstraction object.
   713 	 */
   735 	 */
   800 
   822 
   801 		return true;
   823 		return true;
   802 	}
   824 	}
   803 
   825 
   804 	/**
   826 	/**
   805 	 * Convert numeric level to level capability name.
   827 	 * Converts numeric level to level capability name.
   806 	 *
   828 	 *
   807 	 * Prepends 'level_' to level number.
   829 	 * Prepends 'level_' to level number.
   808 	 *
   830 	 *
   809 	 * @since 2.0.0
   831 	 * @since 2.0.0
   810 	 *
   832 	 *
   814 	public function translate_level_to_cap( $level ) {
   836 	public function translate_level_to_cap( $level ) {
   815 		return 'level_' . $level;
   837 		return 'level_' . $level;
   816 	}
   838 	}
   817 
   839 
   818 	/**
   840 	/**
   819 	 * Set the site to operate on. Defaults to the current site.
   841 	 * Sets the site to operate on. Defaults to the current site.
   820 	 *
   842 	 *
   821 	 * @since 3.0.0
   843 	 * @since 3.0.0
   822 	 * @deprecated 4.9.0 Use WP_User::for_site()
   844 	 * @deprecated 4.9.0 Use WP_User::for_site()
   823 	 *
   845 	 *
   824 	 * @param int $blog_id Optional. Site ID, defaults to current site.
   846 	 * @param int $blog_id Optional. Site ID, defaults to current site.