Changeset 33752
- Timestamp:
- 08/26/2015 04:57:48 AM (9 years ago)
- Location:
- trunk/src/wp-includes
- Files:
-
- 1 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/capabilities-functions.php
r33746 r33752 6 6 * @subpackage User 7 7 */ 8 9 /**10 * WordPress User Roles.11 *12 * The role option is simple, the structure is organized by role name that store13 * the name in value of the 'name' key. The capabilities are stored as an array14 * in the value of the 'capability' key.15 *16 * array (17 * 'rolename' => array (18 * 'name' => 'rolename',19 * 'capabilities' => array()20 * )21 * )22 *23 * @since 2.0.024 * @package WordPress25 * @subpackage User26 */27 class WP_Roles {28 /**29 * List of roles and capabilities.30 *31 * @since 2.0.032 * @access public33 * @var array34 */35 public $roles;36 37 /**38 * List of the role objects.39 *40 * @since 2.0.041 * @access public42 * @var array43 */44 public $role_objects = array();45 46 /**47 * List of role names.48 *49 * @since 2.0.050 * @access public51 * @var array52 */53 public $role_names = array();54 55 /**56 * Option name for storing role list.57 *58 * @since 2.0.059 * @access public60 * @var string61 */62 public $role_key;63 64 /**65 * Whether to use the database for retrieval and storage.66 *67 * @since 2.1.068 * @access public69 * @var bool70 */71 public $use_db = true;72 73 /**74 * Constructor75 *76 * @since 2.0.077 */78 public function __construct() {79 $this->_init();80 }81 82 /**83 * Make private/protected methods readable for backwards compatibility.84 *85 * @since 4.0.086 * @access public87 *88 * @param callable $name Method to call.89 * @param array $arguments Arguments to pass when calling.90 * @return mixed|false Return value of the callback, false otherwise.91 */92 public function __call( $name, $arguments ) {93 if ( '_init' === $name ) {94 return call_user_func_array( array( $this, $name ), $arguments );95 }96 return false;97 }98 99 /**100 * Set up the object properties.101 *102 * The role key is set to the current prefix for the $wpdb object with103 * 'user_roles' appended. If the $wp_user_roles global is set, then it will104 * be used and the role option will not be updated or used.105 *106 * @since 2.1.0107 * @access protected108 *109 * @global wpdb $wpdb WordPress database abstraction object.110 * @global array $wp_user_roles Used to set the 'roles' property value.111 */112 protected function _init() {113 global $wpdb, $wp_user_roles;114 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';115 if ( ! empty( $wp_user_roles ) ) {116 $this->roles = $wp_user_roles;117 $this->use_db = false;118 } else {119 $this->roles = get_option( $this->role_key );120 }121 122 if ( empty( $this->roles ) )123 return;124 125 $this->role_objects = array();126 $this->role_names = array();127 foreach ( array_keys( $this->roles ) as $role ) {128 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );129 $this->role_names[$role] = $this->roles[$role]['name'];130 }131 }132 133 /**134 * Reinitialize the object135 *136 * Recreates the role objects. This is typically called only by switch_to_blog()137 * after switching wpdb to a new blog ID.138 *139 * @since 3.5.0140 * @access public141 *142 * @global wpdb $wpdb143 */144 public function reinit() {145 // There is no need to reinit if using the wp_user_roles global.146 if ( ! $this->use_db )147 return;148 149 global $wpdb;150 151 // Duplicated from _init() to avoid an extra function call.152 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';153 $this->roles = get_option( $this->role_key );154 if ( empty( $this->roles ) )155 return;156 157 $this->role_objects = array();158 $this->role_names = array();159 foreach ( array_keys( $this->roles ) as $role ) {160 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );161 $this->role_names[$role] = $this->roles[$role]['name'];162 }163 }164 165 /**166 * Add role name with capabilities to list.167 *168 * Updates the list of roles, if the role doesn't already exist.169 *170 * The capabilities are defined in the following format `array( 'read' => true );`171 * To explicitly deny a role a capability you set the value for that capability to false.172 *173 * @since 2.0.0174 * @access public175 *176 * @param string $role Role name.177 * @param string $display_name Role display name.178 * @param array $capabilities List of role capabilities in the above format.179 * @return WP_Role|void WP_Role object, if role is added.180 */181 public function add_role( $role, $display_name, $capabilities = array() ) {182 if ( isset( $this->roles[$role] ) )183 return;184 185 $this->roles[$role] = array(186 'name' => $display_name,187 'capabilities' => $capabilities188 );189 if ( $this->use_db )190 update_option( $this->role_key, $this->roles );191 $this->role_objects[$role] = new WP_Role( $role, $capabilities );192 $this->role_names[$role] = $display_name;193 return $this->role_objects[$role];194 }195 196 /**197 * Remove role by name.198 *199 * @since 2.0.0200 * @access public201 *202 * @param string $role Role name.203 */204 public function remove_role( $role ) {205 if ( ! isset( $this->role_objects[$role] ) )206 return;207 208 unset( $this->role_objects[$role] );209 unset( $this->role_names[$role] );210 unset( $this->roles[$role] );211 212 if ( $this->use_db )213 update_option( $this->role_key, $this->roles );214 215 if ( get_option( 'default_role' ) == $role )216 update_option( 'default_role', 'subscriber' );217 }218 219 /**220 * Add capability to role.221 *222 * @since 2.0.0223 * @access public224 *225 * @param string $role Role name.226 * @param string $cap Capability name.227 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.228 */229 public function add_cap( $role, $cap, $grant = true ) {230 if ( ! isset( $this->roles[$role] ) )231 return;232 233 $this->roles[$role]['capabilities'][$cap] = $grant;234 if ( $this->use_db )235 update_option( $this->role_key, $this->roles );236 }237 238 /**239 * Remove capability from role.240 *241 * @since 2.0.0242 * @access public243 *244 * @param string $role Role name.245 * @param string $cap Capability name.246 */247 public function remove_cap( $role, $cap ) {248 if ( ! isset( $this->roles[$role] ) )249 return;250 251 unset( $this->roles[$role]['capabilities'][$cap] );252 if ( $this->use_db )253 update_option( $this->role_key, $this->roles );254 }255 256 /**257 * Retrieve role object by name.258 *259 * @since 2.0.0260 * @access public261 *262 * @param string $role Role name.263 * @return WP_Role|null WP_Role object if found, null if the role does not exist.264 */265 public function get_role( $role ) {266 if ( isset( $this->role_objects[$role] ) )267 return $this->role_objects[$role];268 else269 return null;270 }271 272 /**273 * Retrieve list of role names.274 *275 * @since 2.0.0276 * @access public277 *278 * @return array List of role names.279 */280 public function get_names() {281 return $this->role_names;282 }283 284 /**285 * Whether role name is currently in the list of available roles.286 *287 * @since 2.0.0288 * @access public289 *290 * @param string $role Role name to look up.291 * @return bool292 */293 public function is_role( $role ) {294 return isset( $this->role_names[$role] );295 }296 }297 298 /**299 * WordPress Role class.300 *301 * @since 2.0.0302 * @package WordPress303 * @subpackage User304 */305 class WP_Role {306 /**307 * Role name.308 *309 * @since 2.0.0310 * @access public311 * @var string312 */313 public $name;314 315 /**316 * List of capabilities the role contains.317 *318 * @since 2.0.0319 * @access public320 * @var array321 */322 public $capabilities;323 324 /**325 * Constructor - Set up object properties.326 *327 * The list of capabilities, must have the key as the name of the capability328 * and the value a boolean of whether it is granted to the role.329 *330 * @since 2.0.0331 * @access public332 *333 * @param string $role Role name.334 * @param array $capabilities List of capabilities.335 */336 public function __construct( $role, $capabilities ) {337 $this->name = $role;338 $this->capabilities = $capabilities;339 }340 341 /**342 * Assign role a capability.343 *344 * @since 2.0.0345 * @access public346 *347 * @param string $cap Capability name.348 * @param bool $grant Whether role has capability privilege.349 */350 public function add_cap( $cap, $grant = true ) {351 $this->capabilities[$cap] = $grant;352 wp_roles()->add_cap( $this->name, $cap, $grant );353 }354 355 /**356 * Remove capability from role.357 *358 * This is a container for {@link WP_Roles::remove_cap()} to remove the359 * capability from the role. That is to say, that {@link360 * WP_Roles::remove_cap()} implements the functionality, but it also makes361 * sense to use this class, because you don't need to enter the role name.362 *363 * @since 2.0.0364 * @access public365 *366 * @param string $cap Capability name.367 */368 public function remove_cap( $cap ) {369 unset( $this->capabilities[$cap] );370 wp_roles()->remove_cap( $this->name, $cap );371 }372 373 /**374 * Whether role has capability.375 *376 * The capabilities is passed through the 'role_has_cap' filter. The first377 * parameter for the hook is the list of capabilities the class has378 * assigned. The second parameter is the capability name to look for. The379 * third and final parameter for the hook is the role name.380 *381 * @since 2.0.0382 * @access public383 *384 * @param string $cap Capability name.385 * @return bool True, if user has capability. False, if doesn't have capability.386 */387 public function has_cap( $cap ) {388 /**389 * Filter which capabilities a role has.390 *391 * @since 2.0.0392 *393 * @param array $capabilities Array of role capabilities.394 * @param string $cap Capability name.395 * @param string $name Role name.396 */397 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );398 if ( !empty( $capabilities[$cap] ) )399 return $capabilities[$cap];400 else401 return false;402 }403 404 }405 406 /**407 * WordPress User class.408 *409 * @since 2.0.0410 * @package WordPress411 * @subpackage User412 *413 * @property string $nickname414 * @property string $user_description415 * @property string $user_firstname416 * @property string $user_lastname417 * @property string $user_login418 * @property string $user_pass419 * @property string $user_nicename420 * @property string $user_email421 * @property string $user_url422 * @property string $user_registered423 * @property string $user_activation_key424 * @property string $user_status425 * @property string $display_name426 * @property string $spam427 * @property string $deleted428 */429 class WP_User {430 /**431 * User data container.432 *433 * @since 2.0.0434 * @var object435 */436 public $data;437 438 /**439 * The user's ID.440 *441 * @since 2.1.0442 * @access public443 * @var int444 */445 public $ID = 0;446 447 /**448 * The individual capabilities the user has been given.449 *450 * @since 2.0.0451 * @access public452 * @var array453 */454 public $caps = array();455 456 /**457 * User metadata option name.458 *459 * @since 2.0.0460 * @access public461 * @var string462 */463 public $cap_key;464 465 /**466 * The roles the user is part of.467 *468 * @since 2.0.0469 * @access public470 * @var array471 */472 public $roles = array();473 474 /**475 * All capabilities the user has, including individual and role based.476 *477 * @since 2.0.0478 * @access public479 * @var array480 */481 public $allcaps = array();482 483 /**484 * The filter context applied to user data fields.485 *486 * @since 2.9.0487 * @access private488 * @var string489 */490 var $filter = null;491 492 /**493 * @static494 * @access private495 * @var array496 */497 private static $back_compat_keys;498 499 /**500 * Constructor501 *502 * Retrieves the userdata and passes it to {@link WP_User::init()}.503 *504 * @since 2.0.0505 * @access public506 *507 * @global wpdb $wpdb508 *509 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.510 * @param string $name Optional. User's username511 * @param int $blog_id Optional Blog ID, defaults to current blog.512 */513 public function __construct( $id = 0, $name = '', $blog_id = '' ) {514 if ( ! isset( self::$back_compat_keys ) ) {515 $prefix = $GLOBALS['wpdb']->prefix;516 self::$back_compat_keys = array(517 'user_firstname' => 'first_name',518 'user_lastname' => 'last_name',519 'user_description' => 'description',520 'user_level' => $prefix . 'user_level',521 $prefix . 'usersettings' => $prefix . 'user-settings',522 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',523 );524 }525 526 if ( $id instanceof WP_User ) {527 $this->init( $id->data, $blog_id );528 return;529 } elseif ( is_object( $id ) ) {530 $this->init( $id, $blog_id );531 return;532 }533 534 if ( ! empty( $id ) && ! is_numeric( $id ) ) {535 $name = $id;536 $id = 0;537 }538 539 if ( $id ) {540 $data = self::get_data_by( 'id', $id );541 } else {542 $data = self::get_data_by( 'login', $name );543 }544 545 if ( $data ) {546 $this->init( $data, $blog_id );547 } else {548 $this->data = new stdClass;549 }550 }551 552 /**553 * Sets up object properties, including capabilities.554 *555 * @param object $data User DB row object556 * @param int $blog_id Optional. The blog id to initialize for557 */558 public function init( $data, $blog_id = '' ) {559 $this->data = $data;560 $this->ID = (int) $data->ID;561 562 $this->for_blog( $blog_id );563 }564 565 /**566 * Return only the main user fields567 *568 * @since 3.3.0569 *570 * @static571 *572 * @global wpdb $wpdb573 *574 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'575 * @param string|int $value The field value576 * @return object|false Raw user object577 */578 public static function get_data_by( $field, $value ) {579 global $wpdb;580 581 if ( 'id' == $field ) {582 // Make sure the value is numeric to avoid casting objects, for example,583 // to int 1.584 if ( ! is_numeric( $value ) )585 return false;586 $value = intval( $value );587 if ( $value < 1 )588 return false;589 } else {590 $value = trim( $value );591 }592 593 if ( !$value )594 return false;595 596 switch ( $field ) {597 case 'id':598 $user_id = $value;599 $db_field = 'ID';600 break;601 case 'slug':602 $user_id = wp_cache_get($value, 'userslugs');603 $db_field = 'user_nicename';604 break;605 case 'email':606 $user_id = wp_cache_get($value, 'useremail');607 $db_field = 'user_email';608 break;609 case 'login':610 $value = sanitize_user( $value );611 $user_id = wp_cache_get($value, 'userlogins');612 $db_field = 'user_login';613 break;614 default:615 return false;616 }617 618 if ( false !== $user_id ) {619 if ( $user = wp_cache_get( $user_id, 'users' ) )620 return $user;621 }622 623 if ( !$user = $wpdb->get_row( $wpdb->prepare(624 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value625 ) ) )626 return false;627 628 update_user_caches( $user );629 630 return $user;631 }632 633 /**634 * Makes private/protected methods readable for backwards compatibility.635 *636 * @since 4.3.0637 * @access public638 *639 * @param callable $name Method to call.640 * @param array $arguments Arguments to pass when calling.641 * @return mixed|false Return value of the callback, false otherwise.642 */643 public function __call( $name, $arguments ) {644 if ( '_init_caps' === $name ) {645 return call_user_func_array( array( $this, $name ), $arguments );646 }647 return false;648 }649 650 /**651 * Magic method for checking the existence of a certain custom field652 *653 * @since 3.3.0654 * @param string $key655 * @return bool656 */657 public function __isset( $key ) {658 if ( 'id' == $key ) {659 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );660 $key = 'ID';661 }662 663 if ( isset( $this->data->$key ) )664 return true;665 666 if ( isset( self::$back_compat_keys[ $key ] ) )667 $key = self::$back_compat_keys[ $key ];668 669 return metadata_exists( 'user', $this->ID, $key );670 }671 672 /**673 * Magic method for accessing custom fields674 *675 * @since 3.3.0676 * @param string $key677 * @return mixed678 */679 public function __get( $key ) {680 if ( 'id' == $key ) {681 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );682 return $this->ID;683 }684 685 if ( isset( $this->data->$key ) ) {686 $value = $this->data->$key;687 } else {688 if ( isset( self::$back_compat_keys[ $key ] ) )689 $key = self::$back_compat_keys[ $key ];690 $value = get_user_meta( $this->ID, $key, true );691 }692 693 if ( $this->filter ) {694 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );695 }696 697 return $value;698 }699 700 /**701 * Magic method for setting custom fields702 *703 * @since 3.3.0704 */705 public function __set( $key, $value ) {706 if ( 'id' == $key ) {707 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );708 $this->ID = $value;709 return;710 }711 712 $this->data->$key = $value;713 }714 715 /**716 * Determine whether the user exists in the database.717 *718 * @since 3.4.0719 * @access public720 *721 * @return bool True if user exists in the database, false if not.722 */723 public function exists() {724 return ! empty( $this->ID );725 }726 727 /**728 * Retrieve the value of a property or meta key.729 *730 * Retrieves from the users and usermeta table.731 *732 * @since 3.3.0733 *734 * @param string $key Property735 * @return mixed736 */737 public function get( $key ) {738 return $this->__get( $key );739 }740 741 /**742 * Determine whether a property or meta key is set743 *744 * Consults the users and usermeta tables.745 *746 * @since 3.3.0747 *748 * @param string $key Property749 * @return bool750 */751 public function has_prop( $key ) {752 return $this->__isset( $key );753 }754 755 /**756 * Return an array representation.757 *758 * @since 3.5.0759 *760 * @return array Array representation.761 */762 public function to_array() {763 return get_object_vars( $this->data );764 }765 766 /**767 * Set up capability object properties.768 *769 * Will set the value for the 'cap_key' property to current database table770 * prefix, followed by 'capabilities'. Will then check to see if the771 * property matching the 'cap_key' exists and is an array. If so, it will be772 * used.773 *774 * @access protected775 * @since 2.1.0776 *777 * @global wpdb $wpdb778 *779 * @param string $cap_key Optional capability key780 */781 protected function _init_caps( $cap_key = '' ) {782 global $wpdb;783 784 if ( empty($cap_key) )785 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';786 else787 $this->cap_key = $cap_key;788 789 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );790 791 if ( ! is_array( $this->caps ) )792 $this->caps = array();793 794 $this->get_role_caps();795 }796 797 /**798 * Retrieve all of the role capabilities and merge with individual capabilities.799 *800 * All of the capabilities of the roles the user belongs to are merged with801 * the users individual roles. This also means that the user can be denied802 * specific roles that their role might have, but the specific user isn't803 * granted permission to.804 *805 * @since 2.0.0806 * @access public807 *808 * @return array List of all capabilities for the user.809 */810 public function get_role_caps() {811 $wp_roles = wp_roles();812 813 //Filter out caps that are not role names and assign to $this->roles814 if ( is_array( $this->caps ) )815 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );816 817 //Build $allcaps from role caps, overlay user's $caps818 $this->allcaps = array();819 foreach ( (array) $this->roles as $role ) {820 $the_role = $wp_roles->get_role( $role );821 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );822 }823 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );824 825 return $this->allcaps;826 }827 828 /**829 * Add role to user.830 *831 * Updates the user's meta data option with capabilities and roles.832 *833 * @since 2.0.0834 * @access public835 *836 * @param string $role Role name.837 */838 public function add_role( $role ) {839 $this->caps[$role] = true;840 update_user_meta( $this->ID, $this->cap_key, $this->caps );841 $this->get_role_caps();842 $this->update_user_level_from_caps();843 844 /**845 * Fires immediately after the user has been given a new role.846 *847 * @since 4.3.0848 *849 * @param int $user_id The user ID.850 * @param string $role The new role.851 */852 do_action( 'add_user_role', $this->ID, $role );853 }854 855 /**856 * Remove role from user.857 *858 * @since 2.0.0859 * @access public860 *861 * @param string $role Role name.862 */863 public function remove_role( $role ) {864 if ( !in_array($role, $this->roles) )865 return;866 unset( $this->caps[$role] );867 update_user_meta( $this->ID, $this->cap_key, $this->caps );868 $this->get_role_caps();869 $this->update_user_level_from_caps();870 871 /**872 * Fires immediately after a role as been removed from a user.873 *874 * @since 4.3.0875 *876 * @param int $user_id The user ID.877 * @param string $role The removed role.878 */879 do_action( 'remove_user_role', $this->ID, $role );880 }881 882 /**883 * Set the role of the user.884 *885 * This will remove the previous roles of the user and assign the user the886 * new one. You can set the role to an empty string and it will remove all887 * of the roles from the user.888 *889 * @since 2.0.0890 * @access public891 *892 * @param string $role Role name.893 */894 public function set_role( $role ) {895 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )896 return;897 898 foreach ( (array) $this->roles as $oldrole )899 unset( $this->caps[$oldrole] );900 901 $old_roles = $this->roles;902 if ( !empty( $role ) ) {903 $this->caps[$role] = true;904 $this->roles = array( $role => true );905 } else {906 $this->roles = false;907 }908 update_user_meta( $this->ID, $this->cap_key, $this->caps );909 $this->get_role_caps();910 $this->update_user_level_from_caps();911 912 /**913 * Fires after the user's role has changed.914 *915 * @since 2.9.0916 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.917 *918 * @param int $user_id The user ID.919 * @param string $role The new role.920 * @param array $old_roles An array of the user's previous roles.921 */922 do_action( 'set_user_role', $this->ID, $role, $old_roles );923 }924 925 /**926 * Choose the maximum level the user has.927 *928 * Will compare the level from the $item parameter against the $max929 * parameter. If the item is incorrect, then just the $max parameter value930 * will be returned.931 *932 * Used to get the max level based on the capabilities the user has. This933 * is also based on roles, so if the user is assigned the Administrator role934 * then the capability 'level_10' will exist and the user will get that935 * value.936 *937 * @since 2.0.0938 * @access public939 *940 * @param int $max Max level of user.941 * @param string $item Level capability name.942 * @return int Max Level.943 */944 public function level_reduction( $max, $item ) {945 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {946 $level = intval( $matches[1] );947 return max( $max, $level );948 } else {949 return $max;950 }951 }952 953 /**954 * Update the maximum user level for the user.955 *956 * Updates the 'user_level' user metadata (includes prefix that is the957 * database table prefix) with the maximum user level. Gets the value from958 * the all of the capabilities that the user has.959 *960 * @since 2.0.0961 * @access public962 *963 * @global wpdb $wpdb964 */965 public function update_user_level_from_caps() {966 global $wpdb;967 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );968 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );969 }970 971 /**972 * Add capability and grant or deny access to capability.973 *974 * @since 2.0.0975 * @access public976 *977 * @param string $cap Capability name.978 * @param bool $grant Whether to grant capability to user.979 */980 public function add_cap( $cap, $grant = true ) {981 $this->caps[$cap] = $grant;982 update_user_meta( $this->ID, $this->cap_key, $this->caps );983 $this->get_role_caps();984 $this->update_user_level_from_caps();985 }986 987 /**988 * Remove capability from user.989 *990 * @since 2.0.0991 * @access public992 *993 * @param string $cap Capability name.994 */995 public function remove_cap( $cap ) {996 if ( ! isset( $this->caps[ $cap ] ) ) {997 return;998 }999 unset( $this->caps[ $cap ] );1000 update_user_meta( $this->ID, $this->cap_key, $this->caps );1001 $this->get_role_caps();1002 $this->update_user_level_from_caps();1003 }1004 1005 /**1006 * Remove all of the capabilities of the user.1007 *1008 * @since 2.1.01009 * @access public1010 *1011 * @global wpdb $wpdb1012 */1013 public function remove_all_caps() {1014 global $wpdb;1015 $this->caps = array();1016 delete_user_meta( $this->ID, $this->cap_key );1017 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );1018 $this->get_role_caps();1019 }1020 1021 /**1022 * Whether user has capability or role name.1023 *1024 * This is useful for looking up whether the user has a specific role1025 * assigned to the user. The second optional parameter can also be used to1026 * check for capabilities against a specific object, such as a post or user.1027 *1028 * @since 2.0.01029 * @access public1030 *1031 * @param string|int $cap Capability or role name to search.1032 * @return bool True, if user has capability; false, if user does not have capability.1033 */1034 public function has_cap( $cap ) {1035 if ( is_numeric( $cap ) ) {1036 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );1037 $cap = $this->translate_level_to_cap( $cap );1038 }1039 1040 $args = array_slice( func_get_args(), 1 );1041 $args = array_merge( array( $cap, $this->ID ), $args );1042 $caps = call_user_func_array( 'map_meta_cap', $args );1043 1044 // Multisite super admin has all caps by definition, Unless specifically denied.1045 if ( is_multisite() && is_super_admin( $this->ID ) ) {1046 if ( in_array('do_not_allow', $caps) )1047 return false;1048 return true;1049 }1050 1051 /**1052 * Dynamically filter a user's capabilities.1053 *1054 * @since 2.0.01055 * @since 3.7.0 Added the user object.1056 *1057 * @param array $allcaps An array of all the user's capabilities.1058 * @param array $caps Actual capabilities for meta capability.1059 * @param array $args Optional parameters passed to has_cap(), typically object ID.1060 * @param WP_User $user The user object.1061 */1062 // Must have ALL requested caps1063 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );1064 $capabilities['exist'] = true; // Everyone is allowed to exist1065 foreach ( (array) $caps as $cap ) {1066 if ( empty( $capabilities[ $cap ] ) )1067 return false;1068 }1069 1070 return true;1071 }1072 1073 /**1074 * Convert numeric level to level capability name.1075 *1076 * Prepends 'level_' to level number.1077 *1078 * @since 2.0.01079 * @access public1080 *1081 * @param int $level Level number, 1 to 10.1082 * @return string1083 */1084 public function translate_level_to_cap( $level ) {1085 return 'level_' . $level;1086 }1087 1088 /**1089 * Set the blog to operate on. Defaults to the current blog.1090 *1091 * @since 3.0.01092 *1093 * @global wpdb $wpdb1094 *1095 * @param int $blog_id Optional Blog ID, defaults to current blog.1096 */1097 public function for_blog( $blog_id = '' ) {1098 global $wpdb;1099 if ( ! empty( $blog_id ) )1100 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';1101 else1102 $cap_key = '';1103 $this->_init_caps( $cap_key );1104 }1105 }1106 8 1107 9 /** -
trunk/src/wp-includes/capabilities.php
r33614 r33752 7 7 */ 8 8 9 /** 10 * WordPress User Roles. 11 * 12 * The role option is simple, the structure is organized by role name that store 13 * the name in value of the 'name' key. The capabilities are stored as an array 14 * in the value of the 'capability' key. 15 * 16 * array ( 17 * 'rolename' => array ( 18 * 'name' => 'rolename', 19 * 'capabilities' => array() 20 * ) 21 * ) 22 * 23 * @since 2.0.0 24 * @package WordPress 25 * @subpackage User 26 */ 27 class WP_Roles { 28 /** 29 * List of roles and capabilities. 30 * 31 * @since 2.0.0 32 * @access public 33 * @var array 34 */ 35 public $roles; 36 37 /** 38 * List of the role objects. 39 * 40 * @since 2.0.0 41 * @access public 42 * @var array 43 */ 44 public $role_objects = array(); 45 46 /** 47 * List of role names. 48 * 49 * @since 2.0.0 50 * @access public 51 * @var array 52 */ 53 public $role_names = array(); 54 55 /** 56 * Option name for storing role list. 57 * 58 * @since 2.0.0 59 * @access public 60 * @var string 61 */ 62 public $role_key; 63 64 /** 65 * Whether to use the database for retrieval and storage. 66 * 67 * @since 2.1.0 68 * @access public 69 * @var bool 70 */ 71 public $use_db = true; 72 73 /** 74 * Constructor 75 * 76 * @since 2.0.0 77 */ 78 public function __construct() { 79 $this->_init(); 80 } 81 82 /** 83 * Make private/protected methods readable for backwards compatibility. 84 * 85 * @since 4.0.0 86 * @access public 87 * 88 * @param callable $name Method to call. 89 * @param array $arguments Arguments to pass when calling. 90 * @return mixed|false Return value of the callback, false otherwise. 91 */ 92 public function __call( $name, $arguments ) { 93 if ( '_init' === $name ) { 94 return call_user_func_array( array( $this, $name ), $arguments ); 95 } 96 return false; 97 } 98 99 /** 100 * Set up the object properties. 101 * 102 * The role key is set to the current prefix for the $wpdb object with 103 * 'user_roles' appended. If the $wp_user_roles global is set, then it will 104 * be used and the role option will not be updated or used. 105 * 106 * @since 2.1.0 107 * @access protected 108 * 109 * @global wpdb $wpdb WordPress database abstraction object. 110 * @global array $wp_user_roles Used to set the 'roles' property value. 111 */ 112 protected function _init() { 113 global $wpdb, $wp_user_roles; 114 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles'; 115 if ( ! empty( $wp_user_roles ) ) { 116 $this->roles = $wp_user_roles; 117 $this->use_db = false; 118 } else { 119 $this->roles = get_option( $this->role_key ); 120 } 121 122 if ( empty( $this->roles ) ) 123 return; 124 125 $this->role_objects = array(); 126 $this->role_names = array(); 127 foreach ( array_keys( $this->roles ) as $role ) { 128 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] ); 129 $this->role_names[$role] = $this->roles[$role]['name']; 130 } 131 } 132 133 /** 134 * Reinitialize the object 135 * 136 * Recreates the role objects. This is typically called only by switch_to_blog() 137 * after switching wpdb to a new blog ID. 138 * 139 * @since 3.5.0 140 * @access public 141 * 142 * @global wpdb $wpdb 143 */ 144 public function reinit() { 145 // There is no need to reinit if using the wp_user_roles global. 146 if ( ! $this->use_db ) 147 return; 148 149 global $wpdb; 150 151 // Duplicated from _init() to avoid an extra function call. 152 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles'; 153 $this->roles = get_option( $this->role_key ); 154 if ( empty( $this->roles ) ) 155 return; 156 157 $this->role_objects = array(); 158 $this->role_names = array(); 159 foreach ( array_keys( $this->roles ) as $role ) { 160 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] ); 161 $this->role_names[$role] = $this->roles[$role]['name']; 162 } 163 } 164 165 /** 166 * Add role name with capabilities to list. 167 * 168 * Updates the list of roles, if the role doesn't already exist. 169 * 170 * The capabilities are defined in the following format `array( 'read' => true );` 171 * To explicitly deny a role a capability you set the value for that capability to false. 172 * 173 * @since 2.0.0 174 * @access public 175 * 176 * @param string $role Role name. 177 * @param string $display_name Role display name. 178 * @param array $capabilities List of role capabilities in the above format. 179 * @return WP_Role|void WP_Role object, if role is added. 180 */ 181 public function add_role( $role, $display_name, $capabilities = array() ) { 182 if ( isset( $this->roles[$role] ) ) 183 return; 184 185 $this->roles[$role] = array( 186 'name' => $display_name, 187 'capabilities' => $capabilities 188 ); 189 if ( $this->use_db ) 190 update_option( $this->role_key, $this->roles ); 191 $this->role_objects[$role] = new WP_Role( $role, $capabilities ); 192 $this->role_names[$role] = $display_name; 193 return $this->role_objects[$role]; 194 } 195 196 /** 197 * Remove role by name. 198 * 199 * @since 2.0.0 200 * @access public 201 * 202 * @param string $role Role name. 203 */ 204 public function remove_role( $role ) { 205 if ( ! isset( $this->role_objects[$role] ) ) 206 return; 207 208 unset( $this->role_objects[$role] ); 209 unset( $this->role_names[$role] ); 210 unset( $this->roles[$role] ); 211 212 if ( $this->use_db ) 213 update_option( $this->role_key, $this->roles ); 214 215 if ( get_option( 'default_role' ) == $role ) 216 update_option( 'default_role', 'subscriber' ); 217 } 218 219 /** 220 * Add capability to role. 221 * 222 * @since 2.0.0 223 * @access public 224 * 225 * @param string $role Role name. 226 * @param string $cap Capability name. 227 * @param bool $grant Optional, default is true. Whether role is capable of performing capability. 228 */ 229 public function add_cap( $role, $cap, $grant = true ) { 230 if ( ! isset( $this->roles[$role] ) ) 231 return; 232 233 $this->roles[$role]['capabilities'][$cap] = $grant; 234 if ( $this->use_db ) 235 update_option( $this->role_key, $this->roles ); 236 } 237 238 /** 239 * Remove capability from role. 240 * 241 * @since 2.0.0 242 * @access public 243 * 244 * @param string $role Role name. 245 * @param string $cap Capability name. 246 */ 247 public function remove_cap( $role, $cap ) { 248 if ( ! isset( $this->roles[$role] ) ) 249 return; 250 251 unset( $this->roles[$role]['capabilities'][$cap] ); 252 if ( $this->use_db ) 253 update_option( $this->role_key, $this->roles ); 254 } 255 256 /** 257 * Retrieve role object by name. 258 * 259 * @since 2.0.0 260 * @access public 261 * 262 * @param string $role Role name. 263 * @return WP_Role|null WP_Role object if found, null if the role does not exist. 264 */ 265 public function get_role( $role ) { 266 if ( isset( $this->role_objects[$role] ) ) 267 return $this->role_objects[$role]; 268 else 269 return null; 270 } 271 272 /** 273 * Retrieve list of role names. 274 * 275 * @since 2.0.0 276 * @access public 277 * 278 * @return array List of role names. 279 */ 280 public function get_names() { 281 return $this->role_names; 282 } 283 284 /** 285 * Whether role name is currently in the list of available roles. 286 * 287 * @since 2.0.0 288 * @access public 289 * 290 * @param string $role Role name to look up. 291 * @return bool 292 */ 293 public function is_role( $role ) { 294 return isset( $this->role_names[$role] ); 295 } 296 } 297 298 /** 299 * WordPress Role class. 300 * 301 * @since 2.0.0 302 * @package WordPress 303 * @subpackage User 304 */ 305 class WP_Role { 306 /** 307 * Role name. 308 * 309 * @since 2.0.0 310 * @access public 311 * @var string 312 */ 313 public $name; 314 315 /** 316 * List of capabilities the role contains. 317 * 318 * @since 2.0.0 319 * @access public 320 * @var array 321 */ 322 public $capabilities; 323 324 /** 325 * Constructor - Set up object properties. 326 * 327 * The list of capabilities, must have the key as the name of the capability 328 * and the value a boolean of whether it is granted to the role. 329 * 330 * @since 2.0.0 331 * @access public 332 * 333 * @param string $role Role name. 334 * @param array $capabilities List of capabilities. 335 */ 336 public function __construct( $role, $capabilities ) { 337 $this->name = $role; 338 $this->capabilities = $capabilities; 339 } 340 341 /** 342 * Assign role a capability. 343 * 344 * @since 2.0.0 345 * @access public 346 * 347 * @param string $cap Capability name. 348 * @param bool $grant Whether role has capability privilege. 349 */ 350 public function add_cap( $cap, $grant = true ) { 351 $this->capabilities[$cap] = $grant; 352 wp_roles()->add_cap( $this->name, $cap, $grant ); 353 } 354 355 /** 356 * Remove capability from role. 357 * 358 * This is a container for {@link WP_Roles::remove_cap()} to remove the 359 * capability from the role. That is to say, that {@link 360 * WP_Roles::remove_cap()} implements the functionality, but it also makes 361 * sense to use this class, because you don't need to enter the role name. 362 * 363 * @since 2.0.0 364 * @access public 365 * 366 * @param string $cap Capability name. 367 */ 368 public function remove_cap( $cap ) { 369 unset( $this->capabilities[$cap] ); 370 wp_roles()->remove_cap( $this->name, $cap ); 371 } 372 373 /** 374 * Whether role has capability. 375 * 376 * The capabilities is passed through the 'role_has_cap' filter. The first 377 * parameter for the hook is the list of capabilities the class has 378 * assigned. The second parameter is the capability name to look for. The 379 * third and final parameter for the hook is the role name. 380 * 381 * @since 2.0.0 382 * @access public 383 * 384 * @param string $cap Capability name. 385 * @return bool True, if user has capability. False, if doesn't have capability. 386 */ 387 public function has_cap( $cap ) { 388 /** 389 * Filter which capabilities a role has. 390 * 391 * @since 2.0.0 392 * 393 * @param array $capabilities Array of role capabilities. 394 * @param string $cap Capability name. 395 * @param string $name Role name. 396 */ 397 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name ); 398 if ( !empty( $capabilities[$cap] ) ) 399 return $capabilities[$cap]; 400 else 401 return false; 402 } 403 404 } 405 406 /** 407 * WordPress User class. 408 * 409 * @since 2.0.0 410 * @package WordPress 411 * @subpackage User 412 * 413 * @property string $nickname 414 * @property string $user_description 415 * @property string $user_firstname 416 * @property string $user_lastname 417 * @property string $user_login 418 * @property string $user_pass 419 * @property string $user_nicename 420 * @property string $user_email 421 * @property string $user_url 422 * @property string $user_registered 423 * @property string $user_activation_key 424 * @property string $user_status 425 * @property string $display_name 426 * @property string $spam 427 * @property string $deleted 428 */ 429 class WP_User { 430 /** 431 * User data container. 432 * 433 * @since 2.0.0 434 * @var object 435 */ 436 public $data; 437 438 /** 439 * The user's ID. 440 * 441 * @since 2.1.0 442 * @access public 443 * @var int 444 */ 445 public $ID = 0; 446 447 /** 448 * The individual capabilities the user has been given. 449 * 450 * @since 2.0.0 451 * @access public 452 * @var array 453 */ 454 public $caps = array(); 455 456 /** 457 * User metadata option name. 458 * 459 * @since 2.0.0 460 * @access public 461 * @var string 462 */ 463 public $cap_key; 464 465 /** 466 * The roles the user is part of. 467 * 468 * @since 2.0.0 469 * @access public 470 * @var array 471 */ 472 public $roles = array(); 473 474 /** 475 * All capabilities the user has, including individual and role based. 476 * 477 * @since 2.0.0 478 * @access public 479 * @var array 480 */ 481 public $allcaps = array(); 482 483 /** 484 * The filter context applied to user data fields. 485 * 486 * @since 2.9.0 487 * @access private 488 * @var string 489 */ 490 var $filter = null; 491 492 /** 493 * @static 494 * @access private 495 * @var array 496 */ 497 private static $back_compat_keys; 498 499 /** 500 * Constructor 501 * 502 * Retrieves the userdata and passes it to {@link WP_User::init()}. 503 * 504 * @since 2.0.0 505 * @access public 506 * 507 * @global wpdb $wpdb 508 * 509 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. 510 * @param string $name Optional. User's username 511 * @param int $blog_id Optional Blog ID, defaults to current blog. 512 */ 513 public function __construct( $id = 0, $name = '', $blog_id = '' ) { 514 if ( ! isset( self::$back_compat_keys ) ) { 515 $prefix = $GLOBALS['wpdb']->prefix; 516 self::$back_compat_keys = array( 517 'user_firstname' => 'first_name', 518 'user_lastname' => 'last_name', 519 'user_description' => 'description', 520 'user_level' => $prefix . 'user_level', 521 $prefix . 'usersettings' => $prefix . 'user-settings', 522 $prefix . 'usersettingstime' => $prefix . 'user-settings-time', 523 ); 524 } 525 526 if ( $id instanceof WP_User ) { 527 $this->init( $id->data, $blog_id ); 528 return; 529 } elseif ( is_object( $id ) ) { 530 $this->init( $id, $blog_id ); 531 return; 532 } 533 534 if ( ! empty( $id ) && ! is_numeric( $id ) ) { 535 $name = $id; 536 $id = 0; 537 } 538 539 if ( $id ) { 540 $data = self::get_data_by( 'id', $id ); 541 } else { 542 $data = self::get_data_by( 'login', $name ); 543 } 544 545 if ( $data ) { 546 $this->init( $data, $blog_id ); 547 } else { 548 $this->data = new stdClass; 549 } 550 } 551 552 /** 553 * Sets up object properties, including capabilities. 554 * 555 * @param object $data User DB row object 556 * @param int $blog_id Optional. The blog id to initialize for 557 */ 558 public function init( $data, $blog_id = '' ) { 559 $this->data = $data; 560 $this->ID = (int) $data->ID; 561 562 $this->for_blog( $blog_id ); 563 } 564 565 /** 566 * Return only the main user fields 567 * 568 * @since 3.3.0 569 * 570 * @static 571 * 572 * @global wpdb $wpdb 573 * 574 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login' 575 * @param string|int $value The field value 576 * @return object|false Raw user object 577 */ 578 public static function get_data_by( $field, $value ) { 579 global $wpdb; 580 581 if ( 'id' == $field ) { 582 // Make sure the value is numeric to avoid casting objects, for example, 583 // to int 1. 584 if ( ! is_numeric( $value ) ) 585 return false; 586 $value = intval( $value ); 587 if ( $value < 1 ) 588 return false; 589 } else { 590 $value = trim( $value ); 591 } 592 593 if ( !$value ) 594 return false; 595 596 switch ( $field ) { 597 case 'id': 598 $user_id = $value; 599 $db_field = 'ID'; 600 break; 601 case 'slug': 602 $user_id = wp_cache_get($value, 'userslugs'); 603 $db_field = 'user_nicename'; 604 break; 605 case 'email': 606 $user_id = wp_cache_get($value, 'useremail'); 607 $db_field = 'user_email'; 608 break; 609 case 'login': 610 $value = sanitize_user( $value ); 611 $user_id = wp_cache_get($value, 'userlogins'); 612 $db_field = 'user_login'; 613 break; 614 default: 615 return false; 616 } 617 618 if ( false !== $user_id ) { 619 if ( $user = wp_cache_get( $user_id, 'users' ) ) 620 return $user; 621 } 622 623 if ( !$user = $wpdb->get_row( $wpdb->prepare( 624 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value 625 ) ) ) 626 return false; 627 628 update_user_caches( $user ); 629 630 return $user; 631 } 632 633 /** 634 * Makes private/protected methods readable for backwards compatibility. 635 * 636 * @since 4.3.0 637 * @access public 638 * 639 * @param callable $name Method to call. 640 * @param array $arguments Arguments to pass when calling. 641 * @return mixed|false Return value of the callback, false otherwise. 642 */ 643 public function __call( $name, $arguments ) { 644 if ( '_init_caps' === $name ) { 645 return call_user_func_array( array( $this, $name ), $arguments ); 646 } 647 return false; 648 } 649 650 /** 651 * Magic method for checking the existence of a certain custom field 652 * 653 * @since 3.3.0 654 * @param string $key 655 * @return bool 656 */ 657 public function __isset( $key ) { 658 if ( 'id' == $key ) { 659 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) ); 660 $key = 'ID'; 661 } 662 663 if ( isset( $this->data->$key ) ) 664 return true; 665 666 if ( isset( self::$back_compat_keys[ $key ] ) ) 667 $key = self::$back_compat_keys[ $key ]; 668 669 return metadata_exists( 'user', $this->ID, $key ); 670 } 671 672 /** 673 * Magic method for accessing custom fields 674 * 675 * @since 3.3.0 676 * @param string $key 677 * @return mixed 678 */ 679 public function __get( $key ) { 680 if ( 'id' == $key ) { 681 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) ); 682 return $this->ID; 683 } 684 685 if ( isset( $this->data->$key ) ) { 686 $value = $this->data->$key; 687 } else { 688 if ( isset( self::$back_compat_keys[ $key ] ) ) 689 $key = self::$back_compat_keys[ $key ]; 690 $value = get_user_meta( $this->ID, $key, true ); 691 } 692 693 if ( $this->filter ) { 694 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter ); 695 } 696 697 return $value; 698 } 699 700 /** 701 * Magic method for setting custom fields 702 * 703 * @since 3.3.0 704 */ 705 public function __set( $key, $value ) { 706 if ( 'id' == $key ) { 707 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) ); 708 $this->ID = $value; 709 return; 710 } 711 712 $this->data->$key = $value; 713 } 714 715 /** 716 * Determine whether the user exists in the database. 717 * 718 * @since 3.4.0 719 * @access public 720 * 721 * @return bool True if user exists in the database, false if not. 722 */ 723 public function exists() { 724 return ! empty( $this->ID ); 725 } 726 727 /** 728 * Retrieve the value of a property or meta key. 729 * 730 * Retrieves from the users and usermeta table. 731 * 732 * @since 3.3.0 733 * 734 * @param string $key Property 735 * @return mixed 736 */ 737 public function get( $key ) { 738 return $this->__get( $key ); 739 } 740 741 /** 742 * Determine whether a property or meta key is set 743 * 744 * Consults the users and usermeta tables. 745 * 746 * @since 3.3.0 747 * 748 * @param string $key Property 749 * @return bool 750 */ 751 public function has_prop( $key ) { 752 return $this->__isset( $key ); 753 } 754 755 /** 756 * Return an array representation. 757 * 758 * @since 3.5.0 759 * 760 * @return array Array representation. 761 */ 762 public function to_array() { 763 return get_object_vars( $this->data ); 764 } 765 766 /** 767 * Set up capability object properties. 768 * 769 * Will set the value for the 'cap_key' property to current database table 770 * prefix, followed by 'capabilities'. Will then check to see if the 771 * property matching the 'cap_key' exists and is an array. If so, it will be 772 * used. 773 * 774 * @access protected 775 * @since 2.1.0 776 * 777 * @global wpdb $wpdb 778 * 779 * @param string $cap_key Optional capability key 780 */ 781 protected function _init_caps( $cap_key = '' ) { 782 global $wpdb; 783 784 if ( empty($cap_key) ) 785 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities'; 786 else 787 $this->cap_key = $cap_key; 788 789 $this->caps = get_user_meta( $this->ID, $this->cap_key, true ); 790 791 if ( ! is_array( $this->caps ) ) 792 $this->caps = array(); 793 794 $this->get_role_caps(); 795 } 796 797 /** 798 * Retrieve all of the role capabilities and merge with individual capabilities. 799 * 800 * All of the capabilities of the roles the user belongs to are merged with 801 * the users individual roles. This also means that the user can be denied 802 * specific roles that their role might have, but the specific user isn't 803 * granted permission to. 804 * 805 * @since 2.0.0 806 * @access public 807 * 808 * @return array List of all capabilities for the user. 809 */ 810 public function get_role_caps() { 811 $wp_roles = wp_roles(); 812 813 //Filter out caps that are not role names and assign to $this->roles 814 if ( is_array( $this->caps ) ) 815 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) ); 816 817 //Build $allcaps from role caps, overlay user's $caps 818 $this->allcaps = array(); 819 foreach ( (array) $this->roles as $role ) { 820 $the_role = $wp_roles->get_role( $role ); 821 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities ); 822 } 823 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps ); 824 825 return $this->allcaps; 826 } 827 828 /** 829 * Add role to user. 830 * 831 * Updates the user's meta data option with capabilities and roles. 832 * 833 * @since 2.0.0 834 * @access public 835 * 836 * @param string $role Role name. 837 */ 838 public function add_role( $role ) { 839 $this->caps[$role] = true; 840 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 841 $this->get_role_caps(); 842 $this->update_user_level_from_caps(); 843 844 /** 845 * Fires immediately after the user has been given a new role. 846 * 847 * @since 4.3.0 848 * 849 * @param int $user_id The user ID. 850 * @param string $role The new role. 851 */ 852 do_action( 'add_user_role', $this->ID, $role ); 853 } 854 855 /** 856 * Remove role from user. 857 * 858 * @since 2.0.0 859 * @access public 860 * 861 * @param string $role Role name. 862 */ 863 public function remove_role( $role ) { 864 if ( !in_array($role, $this->roles) ) 865 return; 866 unset( $this->caps[$role] ); 867 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 868 $this->get_role_caps(); 869 $this->update_user_level_from_caps(); 870 871 /** 872 * Fires immediately after a role as been removed from a user. 873 * 874 * @since 4.3.0 875 * 876 * @param int $user_id The user ID. 877 * @param string $role The removed role. 878 */ 879 do_action( 'remove_user_role', $this->ID, $role ); 880 } 881 882 /** 883 * Set the role of the user. 884 * 885 * This will remove the previous roles of the user and assign the user the 886 * new one. You can set the role to an empty string and it will remove all 887 * of the roles from the user. 888 * 889 * @since 2.0.0 890 * @access public 891 * 892 * @param string $role Role name. 893 */ 894 public function set_role( $role ) { 895 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) ) 896 return; 897 898 foreach ( (array) $this->roles as $oldrole ) 899 unset( $this->caps[$oldrole] ); 900 901 $old_roles = $this->roles; 902 if ( !empty( $role ) ) { 903 $this->caps[$role] = true; 904 $this->roles = array( $role => true ); 905 } else { 906 $this->roles = false; 907 } 908 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 909 $this->get_role_caps(); 910 $this->update_user_level_from_caps(); 911 912 /** 913 * Fires after the user's role has changed. 914 * 915 * @since 2.9.0 916 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles. 917 * 918 * @param int $user_id The user ID. 919 * @param string $role The new role. 920 * @param array $old_roles An array of the user's previous roles. 921 */ 922 do_action( 'set_user_role', $this->ID, $role, $old_roles ); 923 } 924 925 /** 926 * Choose the maximum level the user has. 927 * 928 * Will compare the level from the $item parameter against the $max 929 * parameter. If the item is incorrect, then just the $max parameter value 930 * will be returned. 931 * 932 * Used to get the max level based on the capabilities the user has. This 933 * is also based on roles, so if the user is assigned the Administrator role 934 * then the capability 'level_10' will exist and the user will get that 935 * value. 936 * 937 * @since 2.0.0 938 * @access public 939 * 940 * @param int $max Max level of user. 941 * @param string $item Level capability name. 942 * @return int Max Level. 943 */ 944 public function level_reduction( $max, $item ) { 945 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) { 946 $level = intval( $matches[1] ); 947 return max( $max, $level ); 948 } else { 949 return $max; 950 } 951 } 952 953 /** 954 * Update the maximum user level for the user. 955 * 956 * Updates the 'user_level' user metadata (includes prefix that is the 957 * database table prefix) with the maximum user level. Gets the value from 958 * the all of the capabilities that the user has. 959 * 960 * @since 2.0.0 961 * @access public 962 * 963 * @global wpdb $wpdb 964 */ 965 public function update_user_level_from_caps() { 966 global $wpdb; 967 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 ); 968 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level ); 969 } 970 971 /** 972 * Add capability and grant or deny access to capability. 973 * 974 * @since 2.0.0 975 * @access public 976 * 977 * @param string $cap Capability name. 978 * @param bool $grant Whether to grant capability to user. 979 */ 980 public function add_cap( $cap, $grant = true ) { 981 $this->caps[$cap] = $grant; 982 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 983 $this->get_role_caps(); 984 $this->update_user_level_from_caps(); 985 } 986 987 /** 988 * Remove capability from user. 989 * 990 * @since 2.0.0 991 * @access public 992 * 993 * @param string $cap Capability name. 994 */ 995 public function remove_cap( $cap ) { 996 if ( ! isset( $this->caps[ $cap ] ) ) { 997 return; 998 } 999 unset( $this->caps[ $cap ] ); 1000 update_user_meta( $this->ID, $this->cap_key, $this->caps ); 1001 $this->get_role_caps(); 1002 $this->update_user_level_from_caps(); 1003 } 1004 1005 /** 1006 * Remove all of the capabilities of the user. 1007 * 1008 * @since 2.1.0 1009 * @access public 1010 * 1011 * @global wpdb $wpdb 1012 */ 1013 public function remove_all_caps() { 1014 global $wpdb; 1015 $this->caps = array(); 1016 delete_user_meta( $this->ID, $this->cap_key ); 1017 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' ); 1018 $this->get_role_caps(); 1019 } 1020 1021 /** 1022 * Whether user has capability or role name. 1023 * 1024 * This is useful for looking up whether the user has a specific role 1025 * assigned to the user. The second optional parameter can also be used to 1026 * check for capabilities against a specific object, such as a post or user. 1027 * 1028 * @since 2.0.0 1029 * @access public 1030 * 1031 * @param string|int $cap Capability or role name to search. 1032 * @return bool True, if user has capability; false, if user does not have capability. 1033 */ 1034 public function has_cap( $cap ) { 1035 if ( is_numeric( $cap ) ) { 1036 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') ); 1037 $cap = $this->translate_level_to_cap( $cap ); 1038 } 1039 1040 $args = array_slice( func_get_args(), 1 ); 1041 $args = array_merge( array( $cap, $this->ID ), $args ); 1042 $caps = call_user_func_array( 'map_meta_cap', $args ); 1043 1044 // Multisite super admin has all caps by definition, Unless specifically denied. 1045 if ( is_multisite() && is_super_admin( $this->ID ) ) { 1046 if ( in_array('do_not_allow', $caps) ) 1047 return false; 1048 return true; 1049 } 1050 1051 /** 1052 * Dynamically filter a user's capabilities. 1053 * 1054 * @since 2.0.0 1055 * @since 3.7.0 Added the user object. 1056 * 1057 * @param array $allcaps An array of all the user's capabilities. 1058 * @param array $caps Actual capabilities for meta capability. 1059 * @param array $args Optional parameters passed to has_cap(), typically object ID. 1060 * @param WP_User $user The user object. 1061 */ 1062 // Must have ALL requested caps 1063 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this ); 1064 $capabilities['exist'] = true; // Everyone is allowed to exist 1065 foreach ( (array) $caps as $cap ) { 1066 if ( empty( $capabilities[ $cap ] ) ) 1067 return false; 1068 } 1069 1070 return true; 1071 } 1072 1073 /** 1074 * Convert numeric level to level capability name. 1075 * 1076 * Prepends 'level_' to level number. 1077 * 1078 * @since 2.0.0 1079 * @access public 1080 * 1081 * @param int $level Level number, 1 to 10. 1082 * @return string 1083 */ 1084 public function translate_level_to_cap( $level ) { 1085 return 'level_' . $level; 1086 } 1087 1088 /** 1089 * Set the blog to operate on. Defaults to the current blog. 1090 * 1091 * @since 3.0.0 1092 * 1093 * @global wpdb $wpdb 1094 * 1095 * @param int $blog_id Optional Blog ID, defaults to current blog. 1096 */ 1097 public function for_blog( $blog_id = '' ) { 1098 global $wpdb; 1099 if ( ! empty( $blog_id ) ) 1100 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; 1101 else 1102 $cap_key = ''; 1103 $this->_init_caps( $cap_key ); 1104 } 1105 } 1106 1107 /** 1108 * Map meta capabilities to primitive capabilities. 1109 * 1110 * This does not actually compare whether the user ID has the actual capability, 1111 * just what the capability or capabilities are. Meta capability list value can 1112 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post', 1113 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'. 1114 * 1115 * @since 2.0.0 1116 * 1117 * @param string $cap Capability name. 1118 * @param int $user_id User ID. 1119 * @return array Actual capabilities for meta capability. 1120 */ 1121 function map_meta_cap( $cap, $user_id ) { 1122 $args = array_slice( func_get_args(), 2 ); 1123 $caps = array(); 1124 1125 switch ( $cap ) { 1126 case 'remove_user': 1127 $caps[] = 'remove_users'; 1128 break; 1129 case 'promote_user': 1130 $caps[] = 'promote_users'; 1131 break; 1132 case 'edit_user': 1133 case 'edit_users': 1134 // Allow user to edit itself 1135 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] ) 1136 break; 1137 1138 // If multisite these caps are allowed only for super admins. 1139 if ( is_multisite() && !is_super_admin( $user_id ) ) 1140 $caps[] = 'do_not_allow'; 1141 else 1142 $caps[] = 'edit_users'; // edit_user maps to edit_users. 1143 break; 1144 case 'delete_post': 1145 case 'delete_page': 1146 $post = get_post( $args[0] ); 1147 1148 if ( 'revision' == $post->post_type ) { 1149 $post = get_post( $post->post_parent ); 1150 } 1151 1152 $post_type = get_post_type_object( $post->post_type ); 1153 1154 if ( ! $post_type->map_meta_cap ) { 1155 $caps[] = $post_type->cap->$cap; 1156 // Prior to 3.1 we would re-call map_meta_cap here. 1157 if ( 'delete_post' == $cap ) 1158 $cap = $post_type->cap->$cap; 1159 break; 1160 } 1161 1162 // If the post author is set and the user is the author... 1163 if ( $post->post_author && $user_id == $post->post_author ) { 1164 // If the post is published... 1165 if ( 'publish' == $post->post_status ) { 1166 $caps[] = $post_type->cap->delete_published_posts; 1167 } elseif ( 'trash' == $post->post_status ) { 1168 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) { 1169 $caps[] = $post_type->cap->delete_published_posts; 1170 } 1171 } else { 1172 // If the post is draft... 1173 $caps[] = $post_type->cap->delete_posts; 1174 } 1175 } else { 1176 // The user is trying to edit someone else's post. 1177 $caps[] = $post_type->cap->delete_others_posts; 1178 // The post is published, extra cap required. 1179 if ( 'publish' == $post->post_status ) { 1180 $caps[] = $post_type->cap->delete_published_posts; 1181 } elseif ( 'private' == $post->post_status ) { 1182 $caps[] = $post_type->cap->delete_private_posts; 1183 } 1184 } 1185 break; 1186 // edit_post breaks down to edit_posts, edit_published_posts, or 1187 // edit_others_posts 1188 case 'edit_post': 1189 case 'edit_page': 1190 $post = get_post( $args[0] ); 1191 if ( empty( $post ) ) { 1192 $caps[] = 'do_not_allow'; 1193 break; 1194 } 1195 1196 if ( 'revision' == $post->post_type ) { 1197 $post = get_post( $post->post_parent ); 1198 } 1199 1200 $post_type = get_post_type_object( $post->post_type ); 1201 1202 if ( ! $post_type->map_meta_cap ) { 1203 $caps[] = $post_type->cap->$cap; 1204 // Prior to 3.1 we would re-call map_meta_cap here. 1205 if ( 'edit_post' == $cap ) 1206 $cap = $post_type->cap->$cap; 1207 break; 1208 } 1209 1210 // If the post author is set and the user is the author... 1211 if ( $post->post_author && $user_id == $post->post_author ) { 1212 // If the post is published... 1213 if ( 'publish' == $post->post_status ) { 1214 $caps[] = $post_type->cap->edit_published_posts; 1215 } elseif ( 'trash' == $post->post_status ) { 1216 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) { 1217 $caps[] = $post_type->cap->edit_published_posts; 1218 } 1219 } else { 1220 // If the post is draft... 1221 $caps[] = $post_type->cap->edit_posts; 1222 } 1223 } else { 1224 // The user is trying to edit someone else's post. 1225 $caps[] = $post_type->cap->edit_others_posts; 1226 // The post is published, extra cap required. 1227 if ( 'publish' == $post->post_status ) { 1228 $caps[] = $post_type->cap->edit_published_posts; 1229 } elseif ( 'private' == $post->post_status ) { 1230 $caps[] = $post_type->cap->edit_private_posts; 1231 } 1232 } 1233 break; 1234 case 'read_post': 1235 case 'read_page': 1236 $post = get_post( $args[0] ); 1237 1238 if ( 'revision' == $post->post_type ) { 1239 $post = get_post( $post->post_parent ); 1240 } 1241 1242 $post_type = get_post_type_object( $post->post_type ); 1243 1244 if ( ! $post_type->map_meta_cap ) { 1245 $caps[] = $post_type->cap->$cap; 1246 // Prior to 3.1 we would re-call map_meta_cap here. 1247 if ( 'read_post' == $cap ) 1248 $cap = $post_type->cap->$cap; 1249 break; 1250 } 1251 1252 $status_obj = get_post_status_object( $post->post_status ); 1253 if ( $status_obj->public ) { 1254 $caps[] = $post_type->cap->read; 1255 break; 1256 } 1257 1258 if ( $post->post_author && $user_id == $post->post_author ) { 1259 $caps[] = $post_type->cap->read; 1260 } elseif ( $status_obj->private ) { 1261 $caps[] = $post_type->cap->read_private_posts; 1262 } else { 1263 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); 1264 } 1265 break; 1266 case 'publish_post': 1267 $post = get_post( $args[0] ); 1268 $post_type = get_post_type_object( $post->post_type ); 1269 1270 $caps[] = $post_type->cap->publish_posts; 1271 break; 1272 case 'edit_post_meta': 1273 case 'delete_post_meta': 1274 case 'add_post_meta': 1275 $post = get_post( $args[0] ); 1276 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); 1277 1278 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false; 1279 1280 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) { 1281 /** 1282 * Filter whether the user is allowed to add post meta to a post. 1283 * 1284 * The dynamic portion of the hook name, `$meta_key`, refers to the 1285 * meta key passed to {@see map_meta_cap()}. 1286 * 1287 * @since 3.3.0 1288 * 1289 * @param bool $allowed Whether the user can add the post meta. Default false. 1290 * @param string $meta_key The meta key. 1291 * @param int $post_id Post ID. 1292 * @param int $user_id User ID. 1293 * @param string $cap Capability name. 1294 * @param array $caps User capabilities. 1295 */ 1296 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps ); 1297 if ( ! $allowed ) 1298 $caps[] = $cap; 1299 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) { 1300 $caps[] = $cap; 1301 } 1302 break; 1303 case 'edit_comment': 1304 $comment = get_comment( $args[0] ); 1305 if ( empty( $comment ) ) 1306 break; 1307 $post = get_post( $comment->comment_post_ID ); 1308 1309 /* 1310 * If the post doesn't exist, we have an orphaned comment. 1311 * Fall back to the edit_posts capability, instead. 1312 */ 1313 if ( $post ) { 1314 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); 1315 } else { 1316 $caps = map_meta_cap( 'edit_posts', $user_id ); 1317 } 1318 break; 1319 case 'unfiltered_upload': 1320 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) ) 1321 $caps[] = $cap; 1322 else 1323 $caps[] = 'do_not_allow'; 1324 break; 1325 case 'unfiltered_html' : 1326 // Disallow unfiltered_html for all users, even admins and super admins. 1327 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) 1328 $caps[] = 'do_not_allow'; 1329 elseif ( is_multisite() && ! is_super_admin( $user_id ) ) 1330 $caps[] = 'do_not_allow'; 1331 else 1332 $caps[] = $cap; 1333 break; 1334 case 'edit_files': 1335 case 'edit_plugins': 1336 case 'edit_themes': 1337 // Disallow the file editors. 1338 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ) 1339 $caps[] = 'do_not_allow'; 1340 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) 1341 $caps[] = 'do_not_allow'; 1342 elseif ( is_multisite() && ! is_super_admin( $user_id ) ) 1343 $caps[] = 'do_not_allow'; 1344 else 1345 $caps[] = $cap; 1346 break; 1347 case 'update_plugins': 1348 case 'delete_plugins': 1349 case 'install_plugins': 1350 case 'upload_plugins': 1351 case 'update_themes': 1352 case 'delete_themes': 1353 case 'install_themes': 1354 case 'upload_themes': 1355 case 'update_core': 1356 // Disallow anything that creates, deletes, or updates core, plugin, or theme files. 1357 // Files in uploads are excepted. 1358 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) { 1359 $caps[] = 'do_not_allow'; 1360 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { 1361 $caps[] = 'do_not_allow'; 1362 } elseif ( 'upload_themes' === $cap ) { 1363 $caps[] = 'install_themes'; 1364 } elseif ( 'upload_plugins' === $cap ) { 1365 $caps[] = 'install_plugins'; 1366 } else { 1367 $caps[] = $cap; 1368 } 1369 break; 1370 case 'activate_plugins': 1371 $caps[] = $cap; 1372 if ( is_multisite() ) { 1373 // update_, install_, and delete_ are handled above with is_super_admin(). 1374 $menu_perms = get_site_option( 'menu_items', array() ); 1375 if ( empty( $menu_perms['plugins'] ) ) 1376 $caps[] = 'manage_network_plugins'; 1377 } 1378 break; 1379 case 'delete_user': 1380 case 'delete_users': 1381 // If multisite only super admins can delete users. 1382 if ( is_multisite() && ! is_super_admin( $user_id ) ) 1383 $caps[] = 'do_not_allow'; 1384 else 1385 $caps[] = 'delete_users'; // delete_user maps to delete_users. 1386 break; 1387 case 'create_users': 1388 if ( !is_multisite() ) 1389 $caps[] = $cap; 1390 elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) ) 1391 $caps[] = $cap; 1392 else 1393 $caps[] = 'do_not_allow'; 1394 break; 1395 case 'manage_links' : 1396 if ( get_option( 'link_manager_enabled' ) ) 1397 $caps[] = $cap; 1398 else 1399 $caps[] = 'do_not_allow'; 1400 break; 1401 case 'customize' : 1402 $caps[] = 'edit_theme_options'; 1403 break; 1404 case 'delete_site': 1405 $caps[] = 'manage_options'; 1406 break; 1407 default: 1408 // Handle meta capabilities for custom post types. 1409 $post_type_meta_caps = _post_type_meta_capabilities(); 1410 if ( isset( $post_type_meta_caps[ $cap ] ) ) { 1411 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args ); 1412 return call_user_func_array( 'map_meta_cap', $args ); 1413 } 1414 1415 // If no meta caps match, return the original cap. 1416 $caps[] = $cap; 1417 } 1418 1419 /** 1420 * Filter a user's capabilities depending on specific context and/or privilege. 1421 * 1422 * @since 2.8.0 1423 * 1424 * @param array $caps Returns the user's actual capabilities. 1425 * @param string $cap Capability name. 1426 * @param int $user_id The user ID. 1427 * @param array $args Adds the context to the cap. Typically the object ID. 1428 */ 1429 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args ); 1430 } 1431 1432 /** 1433 * Whether current user has capability or role. 1434 * 1435 * @since 2.0.0 1436 * 1437 * @param string $capability Capability or role name. 1438 * @return bool 1439 */ 1440 function current_user_can( $capability ) { 1441 $current_user = wp_get_current_user(); 1442 1443 if ( empty( $current_user ) ) 1444 return false; 1445 1446 $args = array_slice( func_get_args(), 1 ); 1447 $args = array_merge( array( $capability ), $args ); 1448 1449 return call_user_func_array( array( $current_user, 'has_cap' ), $args ); 1450 } 1451 1452 /** 1453 * Whether current user has a capability or role for a given blog. 1454 * 1455 * @since 3.0.0 1456 * 1457 * @param int $blog_id Blog ID 1458 * @param string $capability Capability or role name. 1459 * @return bool 1460 */ 1461 function current_user_can_for_blog( $blog_id, $capability ) { 1462 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false; 1463 1464 $current_user = wp_get_current_user(); 1465 1466 if ( empty( $current_user ) ) { 1467 if ( $switched ) { 1468 restore_current_blog(); 1469 } 1470 return false; 1471 } 1472 1473 $args = array_slice( func_get_args(), 2 ); 1474 $args = array_merge( array( $capability ), $args ); 1475 1476 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args ); 1477 1478 if ( $switched ) { 1479 restore_current_blog(); 1480 } 1481 1482 return $can; 1483 } 1484 1485 /** 1486 * Whether author of supplied post has capability or role. 1487 * 1488 * @since 2.9.0 1489 * 1490 * @param int|object $post Post ID or post object. 1491 * @param string $capability Capability or role name. 1492 * @return bool 1493 */ 1494 function author_can( $post, $capability ) { 1495 if ( !$post = get_post($post) ) 1496 return false; 1497 1498 $author = get_userdata( $post->post_author ); 1499 1500 if ( ! $author ) 1501 return false; 1502 1503 $args = array_slice( func_get_args(), 2 ); 1504 $args = array_merge( array( $capability ), $args ); 1505 1506 return call_user_func_array( array( $author, 'has_cap' ), $args ); 1507 } 1508 1509 /** 1510 * Whether a particular user has capability or role. 1511 * 1512 * @since 3.1.0 1513 * 1514 * @param int|object $user User ID or object. 1515 * @param string $capability Capability or role name. 1516 * @return bool 1517 */ 1518 function user_can( $user, $capability ) { 1519 if ( ! is_object( $user ) ) 1520 $user = get_userdata( $user ); 1521 1522 if ( ! $user || ! $user->exists() ) 1523 return false; 1524 1525 $args = array_slice( func_get_args(), 2 ); 1526 $args = array_merge( array( $capability ), $args ); 1527 1528 return call_user_func_array( array( $user, 'has_cap' ), $args ); 1529 } 1530 1531 /** 1532 * Retrieves the global WP_Roles instance and instantiates it if necessary. 1533 * 1534 * @since 4.3.0 1535 * 1536 * @global WP_Roles $wp_roles WP_Roles global instance. 1537 * 1538 * @return WP_Roles WP_Roles global instance if not already instantiated. 1539 */ 1540 function wp_roles() { 1541 global $wp_roles; 1542 1543 if ( ! isset( $wp_roles ) ) { 1544 $wp_roles = new WP_Roles(); 1545 } 1546 return $wp_roles; 1547 } 1548 1549 /** 1550 * Retrieve role object. 1551 * 1552 * @since 2.0.0 1553 * 1554 * @param string $role Role name. 1555 * @return WP_Role|null WP_Role object if found, null if the role does not exist. 1556 */ 1557 function get_role( $role ) { 1558 return wp_roles()->get_role( $role ); 1559 } 1560 1561 /** 1562 * Add role, if it does not exist. 1563 * 1564 * @since 2.0.0 1565 * 1566 * @param string $role Role name. 1567 * @param string $display_name Display name for role. 1568 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false ); 1569 * @return WP_Role|null WP_Role object if role is added, null if already exists. 1570 */ 1571 function add_role( $role, $display_name, $capabilities = array() ) { 1572 return wp_roles()->add_role( $role, $display_name, $capabilities ); 1573 } 1574 1575 /** 1576 * Remove role, if it exists. 1577 * 1578 * @since 2.0.0 1579 * 1580 * @param string $role Role name. 1581 */ 1582 function remove_role( $role ) { 1583 wp_roles()->remove_role( $role ); 1584 } 1585 1586 /** 1587 * Retrieve a list of super admins. 1588 * 1589 * @since 3.0.0 1590 * 1591 * @global array $super_admins 1592 * 1593 * @return array List of super admin logins 1594 */ 1595 function get_super_admins() { 1596 global $super_admins; 1597 1598 if ( isset($super_admins) ) 1599 return $super_admins; 1600 else 1601 return get_site_option( 'site_admins', array('admin') ); 1602 } 1603 1604 /** 1605 * Determine if user is a site admin. 1606 * 1607 * @since 3.0.0 1608 * 1609 * @param int $user_id (Optional) The ID of a user. Defaults to the current user. 1610 * @return bool True if the user is a site admin. 1611 */ 1612 function is_super_admin( $user_id = false ) { 1613 if ( ! $user_id || $user_id == get_current_user_id() ) 1614 $user = wp_get_current_user(); 1615 else 1616 $user = get_userdata( $user_id ); 1617 1618 if ( ! $user || ! $user->exists() ) 1619 return false; 1620 1621 if ( is_multisite() ) { 1622 $super_admins = get_super_admins(); 1623 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) ) 1624 return true; 1625 } else { 1626 if ( $user->has_cap('delete_users') ) 1627 return true; 1628 } 1629 1630 return false; 1631 } 9 require_once( ABSPATH . WPINC . '/class-wp-roles.php' ); 10 require_once( ABSPATH . WPINC . '/class-wp-role.php' ); 11 require_once( ABSPATH . WPINC . '/class-wp-user.php' ); 12 require_once( ABSPATH . WPINC . '/capabilities-functions.php' ); -
trunk/src/wp-includes/class-wp-role.php
r33746 r33752 1 1 <?php 2 /**3 * WordPress Roles and Capabilities.4 *5 * @package WordPress6 * @subpackage User7 */8 9 /**10 * WordPress User Roles.11 *12 * The role option is simple, the structure is organized by role name that store13 * the name in value of the 'name' key. The capabilities are stored as an array14 * in the value of the 'capability' key.15 *16 * array (17 * 'rolename' => array (18 * 'name' => 'rolename',19 * 'capabilities' => array()20 * )21 * )22 *23 * @since 2.0.024 * @package WordPress25 * @subpackage User26 */27 class WP_Roles {28 /**29 * List of roles and capabilities.30 *31 * @since 2.0.032 * @access public33 * @var array34 */35 public $roles;36 37 /**38 * List of the role objects.39 *40 * @since 2.0.041 * @access public42 * @var array43 */44 public $role_objects = array();45 46 /**47 * List of role names.48 *49 * @since 2.0.050 * @access public51 * @var array52 */53 public $role_names = array();54 55 /**56 * Option name for storing role list.57 *58 * @since 2.0.059 * @access public60 * @var string61 */62 public $role_key;63 64 /**65 * Whether to use the database for retrieval and storage.66 *67 * @since 2.1.068 * @access public69 * @var bool70 */71 public $use_db = true;72 73 /**74 * Constructor75 *76 * @since 2.0.077 */78 public function __construct() {79 $this->_init();80 }81 82 /**83 * Make private/protected methods readable for backwards compatibility.84 *85 * @since 4.0.086 * @access public87 *88 * @param callable $name Method to call.89 * @param array $arguments Arguments to pass when calling.90 * @return mixed|false Return value of the callback, false otherwise.91 */92 public function __call( $name, $arguments ) {93 if ( '_init' === $name ) {94 return call_user_func_array( array( $this, $name ), $arguments );95 }96 return false;97 }98 99 /**100 * Set up the object properties.101 *102 * The role key is set to the current prefix for the $wpdb object with103 * 'user_roles' appended. If the $wp_user_roles global is set, then it will104 * be used and the role option will not be updated or used.105 *106 * @since 2.1.0107 * @access protected108 *109 * @global wpdb $wpdb WordPress database abstraction object.110 * @global array $wp_user_roles Used to set the 'roles' property value.111 */112 protected function _init() {113 global $wpdb, $wp_user_roles;114 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';115 if ( ! empty( $wp_user_roles ) ) {116 $this->roles = $wp_user_roles;117 $this->use_db = false;118 } else {119 $this->roles = get_option( $this->role_key );120 }121 122 if ( empty( $this->roles ) )123 return;124 125 $this->role_objects = array();126 $this->role_names = array();127 foreach ( array_keys( $this->roles ) as $role ) {128 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );129 $this->role_names[$role] = $this->roles[$role]['name'];130 }131 }132 133 /**134 * Reinitialize the object135 *136 * Recreates the role objects. This is typically called only by switch_to_blog()137 * after switching wpdb to a new blog ID.138 *139 * @since 3.5.0140 * @access public141 *142 * @global wpdb $wpdb143 */144 public function reinit() {145 // There is no need to reinit if using the wp_user_roles global.146 if ( ! $this->use_db )147 return;148 149 global $wpdb;150 151 // Duplicated from _init() to avoid an extra function call.152 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';153 $this->roles = get_option( $this->role_key );154 if ( empty( $this->roles ) )155 return;156 157 $this->role_objects = array();158 $this->role_names = array();159 foreach ( array_keys( $this->roles ) as $role ) {160 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );161 $this->role_names[$role] = $this->roles[$role]['name'];162 }163 }164 165 /**166 * Add role name with capabilities to list.167 *168 * Updates the list of roles, if the role doesn't already exist.169 *170 * The capabilities are defined in the following format `array( 'read' => true );`171 * To explicitly deny a role a capability you set the value for that capability to false.172 *173 * @since 2.0.0174 * @access public175 *176 * @param string $role Role name.177 * @param string $display_name Role display name.178 * @param array $capabilities List of role capabilities in the above format.179 * @return WP_Role|void WP_Role object, if role is added.180 */181 public function add_role( $role, $display_name, $capabilities = array() ) {182 if ( isset( $this->roles[$role] ) )183 return;184 185 $this->roles[$role] = array(186 'name' => $display_name,187 'capabilities' => $capabilities188 );189 if ( $this->use_db )190 update_option( $this->role_key, $this->roles );191 $this->role_objects[$role] = new WP_Role( $role, $capabilities );192 $this->role_names[$role] = $display_name;193 return $this->role_objects[$role];194 }195 196 /**197 * Remove role by name.198 *199 * @since 2.0.0200 * @access public201 *202 * @param string $role Role name.203 */204 public function remove_role( $role ) {205 if ( ! isset( $this->role_objects[$role] ) )206 return;207 208 unset( $this->role_objects[$role] );209 unset( $this->role_names[$role] );210 unset( $this->roles[$role] );211 212 if ( $this->use_db )213 update_option( $this->role_key, $this->roles );214 215 if ( get_option( 'default_role' ) == $role )216 update_option( 'default_role', 'subscriber' );217 }218 219 /**220 * Add capability to role.221 *222 * @since 2.0.0223 * @access public224 *225 * @param string $role Role name.226 * @param string $cap Capability name.227 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.228 */229 public function add_cap( $role, $cap, $grant = true ) {230 if ( ! isset( $this->roles[$role] ) )231 return;232 233 $this->roles[$role]['capabilities'][$cap] = $grant;234 if ( $this->use_db )235 update_option( $this->role_key, $this->roles );236 }237 238 /**239 * Remove capability from role.240 *241 * @since 2.0.0242 * @access public243 *244 * @param string $role Role name.245 * @param string $cap Capability name.246 */247 public function remove_cap( $role, $cap ) {248 if ( ! isset( $this->roles[$role] ) )249 return;250 251 unset( $this->roles[$role]['capabilities'][$cap] );252 if ( $this->use_db )253 update_option( $this->role_key, $this->roles );254 }255 256 /**257 * Retrieve role object by name.258 *259 * @since 2.0.0260 * @access public261 *262 * @param string $role Role name.263 * @return WP_Role|null WP_Role object if found, null if the role does not exist.264 */265 public function get_role( $role ) {266 if ( isset( $this->role_objects[$role] ) )267 return $this->role_objects[$role];268 else269 return null;270 }271 272 /**273 * Retrieve list of role names.274 *275 * @since 2.0.0276 * @access public277 *278 * @return array List of role names.279 */280 public function get_names() {281 return $this->role_names;282 }283 284 /**285 * Whether role name is currently in the list of available roles.286 *287 * @since 2.0.0288 * @access public289 *290 * @param string $role Role name to look up.291 * @return bool292 */293 public function is_role( $role ) {294 return isset( $this->role_names[$role] );295 }296 }297 298 2 /** 299 3 * WordPress Role class. … … 403 107 404 108 } 405 406 /**407 * WordPress User class.408 *409 * @since 2.0.0410 * @package WordPress411 * @subpackage User412 *413 * @property string $nickname414 * @property string $user_description415 * @property string $user_firstname416 * @property string $user_lastname417 * @property string $user_login418 * @property string $user_pass419 * @property string $user_nicename420 * @property string $user_email421 * @property string $user_url422 * @property string $user_registered423 * @property string $user_activation_key424 * @property string $user_status425 * @property string $display_name426 * @property string $spam427 * @property string $deleted428 */429 class WP_User {430 /**431 * User data container.432 *433 * @since 2.0.0434 * @var object435 */436 public $data;437 438 /**439 * The user's ID.440 *441 * @since 2.1.0442 * @access public443 * @var int444 */445 public $ID = 0;446 447 /**448 * The individual capabilities the user has been given.449 *450 * @since 2.0.0451 * @access public452 * @var array453 */454 public $caps = array();455 456 /**457 * User metadata option name.458 *459 * @since 2.0.0460 * @access public461 * @var string462 */463 public $cap_key;464 465 /**466 * The roles the user is part of.467 *468 * @since 2.0.0469 * @access public470 * @var array471 */472 public $roles = array();473 474 /**475 * All capabilities the user has, including individual and role based.476 *477 * @since 2.0.0478 * @access public479 * @var array480 */481 public $allcaps = array();482 483 /**484 * The filter context applied to user data fields.485 *486 * @since 2.9.0487 * @access private488 * @var string489 */490 var $filter = null;491 492 /**493 * @static494 * @access private495 * @var array496 */497 private static $back_compat_keys;498 499 /**500 * Constructor501 *502 * Retrieves the userdata and passes it to {@link WP_User::init()}.503 *504 * @since 2.0.0505 * @access public506 *507 * @global wpdb $wpdb508 *509 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.510 * @param string $name Optional. User's username511 * @param int $blog_id Optional Blog ID, defaults to current blog.512 */513 public function __construct( $id = 0, $name = '', $blog_id = '' ) {514 if ( ! isset( self::$back_compat_keys ) ) {515 $prefix = $GLOBALS['wpdb']->prefix;516 self::$back_compat_keys = array(517 'user_firstname' => 'first_name',518 'user_lastname' => 'last_name',519 'user_description' => 'description',520 'user_level' => $prefix . 'user_level',521 $prefix . 'usersettings' => $prefix . 'user-settings',522 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',523 );524 }525 526 if ( $id instanceof WP_User ) {527 $this->init( $id->data, $blog_id );528 return;529 } elseif ( is_object( $id ) ) {530 $this->init( $id, $blog_id );531 return;532 }533 534 if ( ! empty( $id ) && ! is_numeric( $id ) ) {535 $name = $id;536 $id = 0;537 }538 539 if ( $id ) {540 $data = self::get_data_by( 'id', $id );541 } else {542 $data = self::get_data_by( 'login', $name );543 }544 545 if ( $data ) {546 $this->init( $data, $blog_id );547 } else {548 $this->data = new stdClass;549 }550 }551 552 /**553 * Sets up object properties, including capabilities.554 *555 * @param object $data User DB row object556 * @param int $blog_id Optional. The blog id to initialize for557 */558 public function init( $data, $blog_id = '' ) {559 $this->data = $data;560 $this->ID = (int) $data->ID;561 562 $this->for_blog( $blog_id );563 }564 565 /**566 * Return only the main user fields567 *568 * @since 3.3.0569 *570 * @static571 *572 * @global wpdb $wpdb573 *574 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'575 * @param string|int $value The field value576 * @return object|false Raw user object577 */578 public static function get_data_by( $field, $value ) {579 global $wpdb;580 581 if ( 'id' == $field ) {582 // Make sure the value is numeric to avoid casting objects, for example,583 // to int 1.584 if ( ! is_numeric( $value ) )585 return false;586 $value = intval( $value );587 if ( $value < 1 )588 return false;589 } else {590 $value = trim( $value );591 }592 593 if ( !$value )594 return false;595 596 switch ( $field ) {597 case 'id':598 $user_id = $value;599 $db_field = 'ID';600 break;601 case 'slug':602 $user_id = wp_cache_get($value, 'userslugs');603 $db_field = 'user_nicename';604 break;605 case 'email':606 $user_id = wp_cache_get($value, 'useremail');607 $db_field = 'user_email';608 break;609 case 'login':610 $value = sanitize_user( $value );611 $user_id = wp_cache_get($value, 'userlogins');612 $db_field = 'user_login';613 break;614 default:615 return false;616 }617 618 if ( false !== $user_id ) {619 if ( $user = wp_cache_get( $user_id, 'users' ) )620 return $user;621 }622 623 if ( !$user = $wpdb->get_row( $wpdb->prepare(624 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value625 ) ) )626 return false;627 628 update_user_caches( $user );629 630 return $user;631 }632 633 /**634 * Makes private/protected methods readable for backwards compatibility.635 *636 * @since 4.3.0637 * @access public638 *639 * @param callable $name Method to call.640 * @param array $arguments Arguments to pass when calling.641 * @return mixed|false Return value of the callback, false otherwise.642 */643 public function __call( $name, $arguments ) {644 if ( '_init_caps' === $name ) {645 return call_user_func_array( array( $this, $name ), $arguments );646 }647 return false;648 }649 650 /**651 * Magic method for checking the existence of a certain custom field652 *653 * @since 3.3.0654 * @param string $key655 * @return bool656 */657 public function __isset( $key ) {658 if ( 'id' == $key ) {659 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );660 $key = 'ID';661 }662 663 if ( isset( $this->data->$key ) )664 return true;665 666 if ( isset( self::$back_compat_keys[ $key ] ) )667 $key = self::$back_compat_keys[ $key ];668 669 return metadata_exists( 'user', $this->ID, $key );670 }671 672 /**673 * Magic method for accessing custom fields674 *675 * @since 3.3.0676 * @param string $key677 * @return mixed678 */679 public function __get( $key ) {680 if ( 'id' == $key ) {681 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );682 return $this->ID;683 }684 685 if ( isset( $this->data->$key ) ) {686 $value = $this->data->$key;687 } else {688 if ( isset( self::$back_compat_keys[ $key ] ) )689 $key = self::$back_compat_keys[ $key ];690 $value = get_user_meta( $this->ID, $key, true );691 }692 693 if ( $this->filter ) {694 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );695 }696 697 return $value;698 }699 700 /**701 * Magic method for setting custom fields702 *703 * @since 3.3.0704 */705 public function __set( $key, $value ) {706 if ( 'id' == $key ) {707 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );708 $this->ID = $value;709 return;710 }711 712 $this->data->$key = $value;713 }714 715 /**716 * Determine whether the user exists in the database.717 *718 * @since 3.4.0719 * @access public720 *721 * @return bool True if user exists in the database, false if not.722 */723 public function exists() {724 return ! empty( $this->ID );725 }726 727 /**728 * Retrieve the value of a property or meta key.729 *730 * Retrieves from the users and usermeta table.731 *732 * @since 3.3.0733 *734 * @param string $key Property735 * @return mixed736 */737 public function get( $key ) {738 return $this->__get( $key );739 }740 741 /**742 * Determine whether a property or meta key is set743 *744 * Consults the users and usermeta tables.745 *746 * @since 3.3.0747 *748 * @param string $key Property749 * @return bool750 */751 public function has_prop( $key ) {752 return $this->__isset( $key );753 }754 755 /**756 * Return an array representation.757 *758 * @since 3.5.0759 *760 * @return array Array representation.761 */762 public function to_array() {763 return get_object_vars( $this->data );764 }765 766 /**767 * Set up capability object properties.768 *769 * Will set the value for the 'cap_key' property to current database table770 * prefix, followed by 'capabilities'. Will then check to see if the771 * property matching the 'cap_key' exists and is an array. If so, it will be772 * used.773 *774 * @access protected775 * @since 2.1.0776 *777 * @global wpdb $wpdb778 *779 * @param string $cap_key Optional capability key780 */781 protected function _init_caps( $cap_key = '' ) {782 global $wpdb;783 784 if ( empty($cap_key) )785 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';786 else787 $this->cap_key = $cap_key;788 789 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );790 791 if ( ! is_array( $this->caps ) )792 $this->caps = array();793 794 $this->get_role_caps();795 }796 797 /**798 * Retrieve all of the role capabilities and merge with individual capabilities.799 *800 * All of the capabilities of the roles the user belongs to are merged with801 * the users individual roles. This also means that the user can be denied802 * specific roles that their role might have, but the specific user isn't803 * granted permission to.804 *805 * @since 2.0.0806 * @access public807 *808 * @return array List of all capabilities for the user.809 */810 public function get_role_caps() {811 $wp_roles = wp_roles();812 813 //Filter out caps that are not role names and assign to $this->roles814 if ( is_array( $this->caps ) )815 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );816 817 //Build $allcaps from role caps, overlay user's $caps818 $this->allcaps = array();819 foreach ( (array) $this->roles as $role ) {820 $the_role = $wp_roles->get_role( $role );821 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );822 }823 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );824 825 return $this->allcaps;826 }827 828 /**829 * Add role to user.830 *831 * Updates the user's meta data option with capabilities and roles.832 *833 * @since 2.0.0834 * @access public835 *836 * @param string $role Role name.837 */838 public function add_role( $role ) {839 $this->caps[$role] = true;840 update_user_meta( $this->ID, $this->cap_key, $this->caps );841 $this->get_role_caps();842 $this->update_user_level_from_caps();843 844 /**845 * Fires immediately after the user has been given a new role.846 *847 * @since 4.3.0848 *849 * @param int $user_id The user ID.850 * @param string $role The new role.851 */852 do_action( 'add_user_role', $this->ID, $role );853 }854 855 /**856 * Remove role from user.857 *858 * @since 2.0.0859 * @access public860 *861 * @param string $role Role name.862 */863 public function remove_role( $role ) {864 if ( !in_array($role, $this->roles) )865 return;866 unset( $this->caps[$role] );867 update_user_meta( $this->ID, $this->cap_key, $this->caps );868 $this->get_role_caps();869 $this->update_user_level_from_caps();870 871 /**872 * Fires immediately after a role as been removed from a user.873 *874 * @since 4.3.0875 *876 * @param int $user_id The user ID.877 * @param string $role The removed role.878 */879 do_action( 'remove_user_role', $this->ID, $role );880 }881 882 /**883 * Set the role of the user.884 *885 * This will remove the previous roles of the user and assign the user the886 * new one. You can set the role to an empty string and it will remove all887 * of the roles from the user.888 *889 * @since 2.0.0890 * @access public891 *892 * @param string $role Role name.893 */894 public function set_role( $role ) {895 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )896 return;897 898 foreach ( (array) $this->roles as $oldrole )899 unset( $this->caps[$oldrole] );900 901 $old_roles = $this->roles;902 if ( !empty( $role ) ) {903 $this->caps[$role] = true;904 $this->roles = array( $role => true );905 } else {906 $this->roles = false;907 }908 update_user_meta( $this->ID, $this->cap_key, $this->caps );909 $this->get_role_caps();910 $this->update_user_level_from_caps();911 912 /**913 * Fires after the user's role has changed.914 *915 * @since 2.9.0916 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.917 *918 * @param int $user_id The user ID.919 * @param string $role The new role.920 * @param array $old_roles An array of the user's previous roles.921 */922 do_action( 'set_user_role', $this->ID, $role, $old_roles );923 }924 925 /**926 * Choose the maximum level the user has.927 *928 * Will compare the level from the $item parameter against the $max929 * parameter. If the item is incorrect, then just the $max parameter value930 * will be returned.931 *932 * Used to get the max level based on the capabilities the user has. This933 * is also based on roles, so if the user is assigned the Administrator role934 * then the capability 'level_10' will exist and the user will get that935 * value.936 *937 * @since 2.0.0938 * @access public939 *940 * @param int $max Max level of user.941 * @param string $item Level capability name.942 * @return int Max Level.943 */944 public function level_reduction( $max, $item ) {945 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {946 $level = intval( $matches[1] );947 return max( $max, $level );948 } else {949 return $max;950 }951 }952 953 /**954 * Update the maximum user level for the user.955 *956 * Updates the 'user_level' user metadata (includes prefix that is the957 * database table prefix) with the maximum user level. Gets the value from958 * the all of the capabilities that the user has.959 *960 * @since 2.0.0961 * @access public962 *963 * @global wpdb $wpdb964 */965 public function update_user_level_from_caps() {966 global $wpdb;967 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );968 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );969 }970 971 /**972 * Add capability and grant or deny access to capability.973 *974 * @since 2.0.0975 * @access public976 *977 * @param string $cap Capability name.978 * @param bool $grant Whether to grant capability to user.979 */980 public function add_cap( $cap, $grant = true ) {981 $this->caps[$cap] = $grant;982 update_user_meta( $this->ID, $this->cap_key, $this->caps );983 $this->get_role_caps();984 $this->update_user_level_from_caps();985 }986 987 /**988 * Remove capability from user.989 *990 * @since 2.0.0991 * @access public992 *993 * @param string $cap Capability name.994 */995 public function remove_cap( $cap ) {996 if ( ! isset( $this->caps[ $cap ] ) ) {997 return;998 }999 unset( $this->caps[ $cap ] );1000 update_user_meta( $this->ID, $this->cap_key, $this->caps );1001 $this->get_role_caps();1002 $this->update_user_level_from_caps();1003 }1004 1005 /**1006 * Remove all of the capabilities of the user.1007 *1008 * @since 2.1.01009 * @access public1010 *1011 * @global wpdb $wpdb1012 */1013 public function remove_all_caps() {1014 global $wpdb;1015 $this->caps = array();1016 delete_user_meta( $this->ID, $this->cap_key );1017 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );1018 $this->get_role_caps();1019 }1020 1021 /**1022 * Whether user has capability or role name.1023 *1024 * This is useful for looking up whether the user has a specific role1025 * assigned to the user. The second optional parameter can also be used to1026 * check for capabilities against a specific object, such as a post or user.1027 *1028 * @since 2.0.01029 * @access public1030 *1031 * @param string|int $cap Capability or role name to search.1032 * @return bool True, if user has capability; false, if user does not have capability.1033 */1034 public function has_cap( $cap ) {1035 if ( is_numeric( $cap ) ) {1036 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );1037 $cap = $this->translate_level_to_cap( $cap );1038 }1039 1040 $args = array_slice( func_get_args(), 1 );1041 $args = array_merge( array( $cap, $this->ID ), $args );1042 $caps = call_user_func_array( 'map_meta_cap', $args );1043 1044 // Multisite super admin has all caps by definition, Unless specifically denied.1045 if ( is_multisite() && is_super_admin( $this->ID ) ) {1046 if ( in_array('do_not_allow', $caps) )1047 return false;1048 return true;1049 }1050 1051 /**1052 * Dynamically filter a user's capabilities.1053 *1054 * @since 2.0.01055 * @since 3.7.0 Added the user object.1056 *1057 * @param array $allcaps An array of all the user's capabilities.1058 * @param array $caps Actual capabilities for meta capability.1059 * @param array $args Optional parameters passed to has_cap(), typically object ID.1060 * @param WP_User $user The user object.1061 */1062 // Must have ALL requested caps1063 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );1064 $capabilities['exist'] = true; // Everyone is allowed to exist1065 foreach ( (array) $caps as $cap ) {1066 if ( empty( $capabilities[ $cap ] ) )1067 return false;1068 }1069 1070 return true;1071 }1072 1073 /**1074 * Convert numeric level to level capability name.1075 *1076 * Prepends 'level_' to level number.1077 *1078 * @since 2.0.01079 * @access public1080 *1081 * @param int $level Level number, 1 to 10.1082 * @return string1083 */1084 public function translate_level_to_cap( $level ) {1085 return 'level_' . $level;1086 }1087 1088 /**1089 * Set the blog to operate on. Defaults to the current blog.1090 *1091 * @since 3.0.01092 *1093 * @global wpdb $wpdb1094 *1095 * @param int $blog_id Optional Blog ID, defaults to current blog.1096 */1097 public function for_blog( $blog_id = '' ) {1098 global $wpdb;1099 if ( ! empty( $blog_id ) )1100 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';1101 else1102 $cap_key = '';1103 $this->_init_caps( $cap_key );1104 }1105 }1106 1107 /**1108 * Map meta capabilities to primitive capabilities.1109 *1110 * This does not actually compare whether the user ID has the actual capability,1111 * just what the capability or capabilities are. Meta capability list value can1112 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',1113 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.1114 *1115 * @since 2.0.01116 *1117 * @param string $cap Capability name.1118 * @param int $user_id User ID.1119 * @return array Actual capabilities for meta capability.1120 */1121 function map_meta_cap( $cap, $user_id ) {1122 $args = array_slice( func_get_args(), 2 );1123 $caps = array();1124 1125 switch ( $cap ) {1126 case 'remove_user':1127 $caps[] = 'remove_users';1128 break;1129 case 'promote_user':1130 $caps[] = 'promote_users';1131 break;1132 case 'edit_user':1133 case 'edit_users':1134 // Allow user to edit itself1135 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )1136 break;1137 1138 // If multisite these caps are allowed only for super admins.1139 if ( is_multisite() && !is_super_admin( $user_id ) )1140 $caps[] = 'do_not_allow';1141 else1142 $caps[] = 'edit_users'; // edit_user maps to edit_users.1143 break;1144 case 'delete_post':1145 case 'delete_page':1146 $post = get_post( $args[0] );1147 1148 if ( 'revision' == $post->post_type ) {1149 $post = get_post( $post->post_parent );1150 }1151 1152 $post_type = get_post_type_object( $post->post_type );1153 1154 if ( ! $post_type->map_meta_cap ) {1155 $caps[] = $post_type->cap->$cap;1156 // Prior to 3.1 we would re-call map_meta_cap here.1157 if ( 'delete_post' == $cap )1158 $cap = $post_type->cap->$cap;1159 break;1160 }1161 1162 // If the post author is set and the user is the author...1163 if ( $post->post_author && $user_id == $post->post_author ) {1164 // If the post is published...1165 if ( 'publish' == $post->post_status ) {1166 $caps[] = $post_type->cap->delete_published_posts;1167 } elseif ( 'trash' == $post->post_status ) {1168 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {1169 $caps[] = $post_type->cap->delete_published_posts;1170 }1171 } else {1172 // If the post is draft...1173 $caps[] = $post_type->cap->delete_posts;1174 }1175 } else {1176 // The user is trying to edit someone else's post.1177 $caps[] = $post_type->cap->delete_others_posts;1178 // The post is published, extra cap required.1179 if ( 'publish' == $post->post_status ) {1180 $caps[] = $post_type->cap->delete_published_posts;1181 } elseif ( 'private' == $post->post_status ) {1182 $caps[] = $post_type->cap->delete_private_posts;1183 }1184 }1185 break;1186 // edit_post breaks down to edit_posts, edit_published_posts, or1187 // edit_others_posts1188 case 'edit_post':1189 case 'edit_page':1190 $post = get_post( $args[0] );1191 if ( empty( $post ) ) {1192 $caps[] = 'do_not_allow';1193 break;1194 }1195 1196 if ( 'revision' == $post->post_type ) {1197 $post = get_post( $post->post_parent );1198 }1199 1200 $post_type = get_post_type_object( $post->post_type );1201 1202 if ( ! $post_type->map_meta_cap ) {1203 $caps[] = $post_type->cap->$cap;1204 // Prior to 3.1 we would re-call map_meta_cap here.1205 if ( 'edit_post' == $cap )1206 $cap = $post_type->cap->$cap;1207 break;1208 }1209 1210 // If the post author is set and the user is the author...1211 if ( $post->post_author && $user_id == $post->post_author ) {1212 // If the post is published...1213 if ( 'publish' == $post->post_status ) {1214 $caps[] = $post_type->cap->edit_published_posts;1215 } elseif ( 'trash' == $post->post_status ) {1216 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {1217 $caps[] = $post_type->cap->edit_published_posts;1218 }1219 } else {1220 // If the post is draft...1221 $caps[] = $post_type->cap->edit_posts;1222 }1223 } else {1224 // The user is trying to edit someone else's post.1225 $caps[] = $post_type->cap->edit_others_posts;1226 // The post is published, extra cap required.1227 if ( 'publish' == $post->post_status ) {1228 $caps[] = $post_type->cap->edit_published_posts;1229 } elseif ( 'private' == $post->post_status ) {1230 $caps[] = $post_type->cap->edit_private_posts;1231 }1232 }1233 break;1234 case 'read_post':1235 case 'read_page':1236 $post = get_post( $args[0] );1237 1238 if ( 'revision' == $post->post_type ) {1239 $post = get_post( $post->post_parent );1240 }1241 1242 $post_type = get_post_type_object( $post->post_type );1243 1244 if ( ! $post_type->map_meta_cap ) {1245 $caps[] = $post_type->cap->$cap;1246 // Prior to 3.1 we would re-call map_meta_cap here.1247 if ( 'read_post' == $cap )1248 $cap = $post_type->cap->$cap;1249 break;1250 }1251 1252 $status_obj = get_post_status_object( $post->post_status );1253 if ( $status_obj->public ) {1254 $caps[] = $post_type->cap->read;1255 break;1256 }1257 1258 if ( $post->post_author && $user_id == $post->post_author ) {1259 $caps[] = $post_type->cap->read;1260 } elseif ( $status_obj->private ) {1261 $caps[] = $post_type->cap->read_private_posts;1262 } else {1263 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );1264 }1265 break;1266 case 'publish_post':1267 $post = get_post( $args[0] );1268 $post_type = get_post_type_object( $post->post_type );1269 1270 $caps[] = $post_type->cap->publish_posts;1271 break;1272 case 'edit_post_meta':1273 case 'delete_post_meta':1274 case 'add_post_meta':1275 $post = get_post( $args[0] );1276 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );1277 1278 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;1279 1280 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {1281 /**1282 * Filter whether the user is allowed to add post meta to a post.1283 *1284 * The dynamic portion of the hook name, `$meta_key`, refers to the1285 * meta key passed to {@see map_meta_cap()}.1286 *1287 * @since 3.3.01288 *1289 * @param bool $allowed Whether the user can add the post meta. Default false.1290 * @param string $meta_key The meta key.1291 * @param int $post_id Post ID.1292 * @param int $user_id User ID.1293 * @param string $cap Capability name.1294 * @param array $caps User capabilities.1295 */1296 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );1297 if ( ! $allowed )1298 $caps[] = $cap;1299 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {1300 $caps[] = $cap;1301 }1302 break;1303 case 'edit_comment':1304 $comment = get_comment( $args[0] );1305 if ( empty( $comment ) )1306 break;1307 $post = get_post( $comment->comment_post_ID );1308 1309 /*1310 * If the post doesn't exist, we have an orphaned comment.1311 * Fall back to the edit_posts capability, instead.1312 */1313 if ( $post ) {1314 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );1315 } else {1316 $caps = map_meta_cap( 'edit_posts', $user_id );1317 }1318 break;1319 case 'unfiltered_upload':1320 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )1321 $caps[] = $cap;1322 else1323 $caps[] = 'do_not_allow';1324 break;1325 case 'unfiltered_html' :1326 // Disallow unfiltered_html for all users, even admins and super admins.1327 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )1328 $caps[] = 'do_not_allow';1329 elseif ( is_multisite() && ! is_super_admin( $user_id ) )1330 $caps[] = 'do_not_allow';1331 else1332 $caps[] = $cap;1333 break;1334 case 'edit_files':1335 case 'edit_plugins':1336 case 'edit_themes':1337 // Disallow the file editors.1338 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )1339 $caps[] = 'do_not_allow';1340 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )1341 $caps[] = 'do_not_allow';1342 elseif ( is_multisite() && ! is_super_admin( $user_id ) )1343 $caps[] = 'do_not_allow';1344 else1345 $caps[] = $cap;1346 break;1347 case 'update_plugins':1348 case 'delete_plugins':1349 case 'install_plugins':1350 case 'upload_plugins':1351 case 'update_themes':1352 case 'delete_themes':1353 case 'install_themes':1354 case 'upload_themes':1355 case 'update_core':1356 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.1357 // Files in uploads are excepted.1358 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {1359 $caps[] = 'do_not_allow';1360 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {1361 $caps[] = 'do_not_allow';1362 } elseif ( 'upload_themes' === $cap ) {1363 $caps[] = 'install_themes';1364 } elseif ( 'upload_plugins' === $cap ) {1365 $caps[] = 'install_plugins';1366 } else {1367 $caps[] = $cap;1368 }1369 break;1370 case 'activate_plugins':1371 $caps[] = $cap;1372 if ( is_multisite() ) {1373 // update_, install_, and delete_ are handled above with is_super_admin().1374 $menu_perms = get_site_option( 'menu_items', array() );1375 if ( empty( $menu_perms['plugins'] ) )1376 $caps[] = 'manage_network_plugins';1377 }1378 break;1379 case 'delete_user':1380 case 'delete_users':1381 // If multisite only super admins can delete users.1382 if ( is_multisite() && ! is_super_admin( $user_id ) )1383 $caps[] = 'do_not_allow';1384 else1385 $caps[] = 'delete_users'; // delete_user maps to delete_users.1386 break;1387 case 'create_users':1388 if ( !is_multisite() )1389 $caps[] = $cap;1390 elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) )1391 $caps[] = $cap;1392 else1393 $caps[] = 'do_not_allow';1394 break;1395 case 'manage_links' :1396 if ( get_option( 'link_manager_enabled' ) )1397 $caps[] = $cap;1398 else1399 $caps[] = 'do_not_allow';1400 break;1401 case 'customize' :1402 $caps[] = 'edit_theme_options';1403 break;1404 case 'delete_site':1405 $caps[] = 'manage_options';1406 break;1407 default:1408 // Handle meta capabilities for custom post types.1409 $post_type_meta_caps = _post_type_meta_capabilities();1410 if ( isset( $post_type_meta_caps[ $cap ] ) ) {1411 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );1412 return call_user_func_array( 'map_meta_cap', $args );1413 }1414 1415 // If no meta caps match, return the original cap.1416 $caps[] = $cap;1417 }1418 1419 /**1420 * Filter a user's capabilities depending on specific context and/or privilege.1421 *1422 * @since 2.8.01423 *1424 * @param array $caps Returns the user's actual capabilities.1425 * @param string $cap Capability name.1426 * @param int $user_id The user ID.1427 * @param array $args Adds the context to the cap. Typically the object ID.1428 */1429 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );1430 }1431 1432 /**1433 * Whether current user has capability or role.1434 *1435 * @since 2.0.01436 *1437 * @param string $capability Capability or role name.1438 * @return bool1439 */1440 function current_user_can( $capability ) {1441 $current_user = wp_get_current_user();1442 1443 if ( empty( $current_user ) )1444 return false;1445 1446 $args = array_slice( func_get_args(), 1 );1447 $args = array_merge( array( $capability ), $args );1448 1449 return call_user_func_array( array( $current_user, 'has_cap' ), $args );1450 }1451 1452 /**1453 * Whether current user has a capability or role for a given blog.1454 *1455 * @since 3.0.01456 *1457 * @param int $blog_id Blog ID1458 * @param string $capability Capability or role name.1459 * @return bool1460 */1461 function current_user_can_for_blog( $blog_id, $capability ) {1462 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;1463 1464 $current_user = wp_get_current_user();1465 1466 if ( empty( $current_user ) ) {1467 if ( $switched ) {1468 restore_current_blog();1469 }1470 return false;1471 }1472 1473 $args = array_slice( func_get_args(), 2 );1474 $args = array_merge( array( $capability ), $args );1475 1476 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );1477 1478 if ( $switched ) {1479 restore_current_blog();1480 }1481 1482 return $can;1483 }1484 1485 /**1486 * Whether author of supplied post has capability or role.1487 *1488 * @since 2.9.01489 *1490 * @param int|object $post Post ID or post object.1491 * @param string $capability Capability or role name.1492 * @return bool1493 */1494 function author_can( $post, $capability ) {1495 if ( !$post = get_post($post) )1496 return false;1497 1498 $author = get_userdata( $post->post_author );1499 1500 if ( ! $author )1501 return false;1502 1503 $args = array_slice( func_get_args(), 2 );1504 $args = array_merge( array( $capability ), $args );1505 1506 return call_user_func_array( array( $author, 'has_cap' ), $args );1507 }1508 1509 /**1510 * Whether a particular user has capability or role.1511 *1512 * @since 3.1.01513 *1514 * @param int|object $user User ID or object.1515 * @param string $capability Capability or role name.1516 * @return bool1517 */1518 function user_can( $user, $capability ) {1519 if ( ! is_object( $user ) )1520 $user = get_userdata( $user );1521 1522 if ( ! $user || ! $user->exists() )1523 return false;1524 1525 $args = array_slice( func_get_args(), 2 );1526 $args = array_merge( array( $capability ), $args );1527 1528 return call_user_func_array( array( $user, 'has_cap' ), $args );1529 }1530 1531 /**1532 * Retrieves the global WP_Roles instance and instantiates it if necessary.1533 *1534 * @since 4.3.01535 *1536 * @global WP_Roles $wp_roles WP_Roles global instance.1537 *1538 * @return WP_Roles WP_Roles global instance if not already instantiated.1539 */1540 function wp_roles() {1541 global $wp_roles;1542 1543 if ( ! isset( $wp_roles ) ) {1544 $wp_roles = new WP_Roles();1545 }1546 return $wp_roles;1547 }1548 1549 /**1550 * Retrieve role object.1551 *1552 * @since 2.0.01553 *1554 * @param string $role Role name.1555 * @return WP_Role|null WP_Role object if found, null if the role does not exist.1556 */1557 function get_role( $role ) {1558 return wp_roles()->get_role( $role );1559 }1560 1561 /**1562 * Add role, if it does not exist.1563 *1564 * @since 2.0.01565 *1566 * @param string $role Role name.1567 * @param string $display_name Display name for role.1568 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );1569 * @return WP_Role|null WP_Role object if role is added, null if already exists.1570 */1571 function add_role( $role, $display_name, $capabilities = array() ) {1572 return wp_roles()->add_role( $role, $display_name, $capabilities );1573 }1574 1575 /**1576 * Remove role, if it exists.1577 *1578 * @since 2.0.01579 *1580 * @param string $role Role name.1581 */1582 function remove_role( $role ) {1583 wp_roles()->remove_role( $role );1584 }1585 1586 /**1587 * Retrieve a list of super admins.1588 *1589 * @since 3.0.01590 *1591 * @global array $super_admins1592 *1593 * @return array List of super admin logins1594 */1595 function get_super_admins() {1596 global $super_admins;1597 1598 if ( isset($super_admins) )1599 return $super_admins;1600 else1601 return get_site_option( 'site_admins', array('admin') );1602 }1603 1604 /**1605 * Determine if user is a site admin.1606 *1607 * @since 3.0.01608 *1609 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.1610 * @return bool True if the user is a site admin.1611 */1612 function is_super_admin( $user_id = false ) {1613 if ( ! $user_id || $user_id == get_current_user_id() )1614 $user = wp_get_current_user();1615 else1616 $user = get_userdata( $user_id );1617 1618 if ( ! $user || ! $user->exists() )1619 return false;1620 1621 if ( is_multisite() ) {1622 $super_admins = get_super_admins();1623 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )1624 return true;1625 } else {1626 if ( $user->has_cap('delete_users') )1627 return true;1628 }1629 1630 return false;1631 } -
trunk/src/wp-includes/class-wp-roles.php
r33746 r33752 1 1 <?php 2 /**3 * WordPress Roles and Capabilities.4 *5 * @package WordPress6 * @subpackage User7 */8 9 2 /** 10 3 * WordPress User Roles. … … 295 288 } 296 289 } 297 298 /**299 * WordPress Role class.300 *301 * @since 2.0.0302 * @package WordPress303 * @subpackage User304 */305 class WP_Role {306 /**307 * Role name.308 *309 * @since 2.0.0310 * @access public311 * @var string312 */313 public $name;314 315 /**316 * List of capabilities the role contains.317 *318 * @since 2.0.0319 * @access public320 * @var array321 */322 public $capabilities;323 324 /**325 * Constructor - Set up object properties.326 *327 * The list of capabilities, must have the key as the name of the capability328 * and the value a boolean of whether it is granted to the role.329 *330 * @since 2.0.0331 * @access public332 *333 * @param string $role Role name.334 * @param array $capabilities List of capabilities.335 */336 public function __construct( $role, $capabilities ) {337 $this->name = $role;338 $this->capabilities = $capabilities;339 }340 341 /**342 * Assign role a capability.343 *344 * @since 2.0.0345 * @access public346 *347 * @param string $cap Capability name.348 * @param bool $grant Whether role has capability privilege.349 */350 public function add_cap( $cap, $grant = true ) {351 $this->capabilities[$cap] = $grant;352 wp_roles()->add_cap( $this->name, $cap, $grant );353 }354 355 /**356 * Remove capability from role.357 *358 * This is a container for {@link WP_Roles::remove_cap()} to remove the359 * capability from the role. That is to say, that {@link360 * WP_Roles::remove_cap()} implements the functionality, but it also makes361 * sense to use this class, because you don't need to enter the role name.362 *363 * @since 2.0.0364 * @access public365 *366 * @param string $cap Capability name.367 */368 public function remove_cap( $cap ) {369 unset( $this->capabilities[$cap] );370 wp_roles()->remove_cap( $this->name, $cap );371 }372 373 /**374 * Whether role has capability.375 *376 * The capabilities is passed through the 'role_has_cap' filter. The first377 * parameter for the hook is the list of capabilities the class has378 * assigned. The second parameter is the capability name to look for. The379 * third and final parameter for the hook is the role name.380 *381 * @since 2.0.0382 * @access public383 *384 * @param string $cap Capability name.385 * @return bool True, if user has capability. False, if doesn't have capability.386 */387 public function has_cap( $cap ) {388 /**389 * Filter which capabilities a role has.390 *391 * @since 2.0.0392 *393 * @param array $capabilities Array of role capabilities.394 * @param string $cap Capability name.395 * @param string $name Role name.396 */397 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );398 if ( !empty( $capabilities[$cap] ) )399 return $capabilities[$cap];400 else401 return false;402 }403 404 }405 406 /**407 * WordPress User class.408 *409 * @since 2.0.0410 * @package WordPress411 * @subpackage User412 *413 * @property string $nickname414 * @property string $user_description415 * @property string $user_firstname416 * @property string $user_lastname417 * @property string $user_login418 * @property string $user_pass419 * @property string $user_nicename420 * @property string $user_email421 * @property string $user_url422 * @property string $user_registered423 * @property string $user_activation_key424 * @property string $user_status425 * @property string $display_name426 * @property string $spam427 * @property string $deleted428 */429 class WP_User {430 /**431 * User data container.432 *433 * @since 2.0.0434 * @var object435 */436 public $data;437 438 /**439 * The user's ID.440 *441 * @since 2.1.0442 * @access public443 * @var int444 */445 public $ID = 0;446 447 /**448 * The individual capabilities the user has been given.449 *450 * @since 2.0.0451 * @access public452 * @var array453 */454 public $caps = array();455 456 /**457 * User metadata option name.458 *459 * @since 2.0.0460 * @access public461 * @var string462 */463 public $cap_key;464 465 /**466 * The roles the user is part of.467 *468 * @since 2.0.0469 * @access public470 * @var array471 */472 public $roles = array();473 474 /**475 * All capabilities the user has, including individual and role based.476 *477 * @since 2.0.0478 * @access public479 * @var array480 */481 public $allcaps = array();482 483 /**484 * The filter context applied to user data fields.485 *486 * @since 2.9.0487 * @access private488 * @var string489 */490 var $filter = null;491 492 /**493 * @static494 * @access private495 * @var array496 */497 private static $back_compat_keys;498 499 /**500 * Constructor501 *502 * Retrieves the userdata and passes it to {@link WP_User::init()}.503 *504 * @since 2.0.0505 * @access public506 *507 * @global wpdb $wpdb508 *509 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.510 * @param string $name Optional. User's username511 * @param int $blog_id Optional Blog ID, defaults to current blog.512 */513 public function __construct( $id = 0, $name = '', $blog_id = '' ) {514 if ( ! isset( self::$back_compat_keys ) ) {515 $prefix = $GLOBALS['wpdb']->prefix;516 self::$back_compat_keys = array(517 'user_firstname' => 'first_name',518 'user_lastname' => 'last_name',519 'user_description' => 'description',520 'user_level' => $prefix . 'user_level',521 $prefix . 'usersettings' => $prefix . 'user-settings',522 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',523 );524 }525 526 if ( $id instanceof WP_User ) {527 $this->init( $id->data, $blog_id );528 return;529 } elseif ( is_object( $id ) ) {530 $this->init( $id, $blog_id );531 return;532 }533 534 if ( ! empty( $id ) && ! is_numeric( $id ) ) {535 $name = $id;536 $id = 0;537 }538 539 if ( $id ) {540 $data = self::get_data_by( 'id', $id );541 } else {542 $data = self::get_data_by( 'login', $name );543 }544 545 if ( $data ) {546 $this->init( $data, $blog_id );547 } else {548 $this->data = new stdClass;549 }550 }551 552 /**553 * Sets up object properties, including capabilities.554 *555 * @param object $data User DB row object556 * @param int $blog_id Optional. The blog id to initialize for557 */558 public function init( $data, $blog_id = '' ) {559 $this->data = $data;560 $this->ID = (int) $data->ID;561 562 $this->for_blog( $blog_id );563 }564 565 /**566 * Return only the main user fields567 *568 * @since 3.3.0569 *570 * @static571 *572 * @global wpdb $wpdb573 *574 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'575 * @param string|int $value The field value576 * @return object|false Raw user object577 */578 public static function get_data_by( $field, $value ) {579 global $wpdb;580 581 if ( 'id' == $field ) {582 // Make sure the value is numeric to avoid casting objects, for example,583 // to int 1.584 if ( ! is_numeric( $value ) )585 return false;586 $value = intval( $value );587 if ( $value < 1 )588 return false;589 } else {590 $value = trim( $value );591 }592 593 if ( !$value )594 return false;595 596 switch ( $field ) {597 case 'id':598 $user_id = $value;599 $db_field = 'ID';600 break;601 case 'slug':602 $user_id = wp_cache_get($value, 'userslugs');603 $db_field = 'user_nicename';604 break;605 case 'email':606 $user_id = wp_cache_get($value, 'useremail');607 $db_field = 'user_email';608 break;609 case 'login':610 $value = sanitize_user( $value );611 $user_id = wp_cache_get($value, 'userlogins');612 $db_field = 'user_login';613 break;614 default:615 return false;616 }617 618 if ( false !== $user_id ) {619 if ( $user = wp_cache_get( $user_id, 'users' ) )620 return $user;621 }622 623 if ( !$user = $wpdb->get_row( $wpdb->prepare(624 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value625 ) ) )626 return false;627 628 update_user_caches( $user );629 630 return $user;631 }632 633 /**634 * Makes private/protected methods readable for backwards compatibility.635 *636 * @since 4.3.0637 * @access public638 *639 * @param callable $name Method to call.640 * @param array $arguments Arguments to pass when calling.641 * @return mixed|false Return value of the callback, false otherwise.642 */643 public function __call( $name, $arguments ) {644 if ( '_init_caps' === $name ) {645 return call_user_func_array( array( $this, $name ), $arguments );646 }647 return false;648 }649 650 /**651 * Magic method for checking the existence of a certain custom field652 *653 * @since 3.3.0654 * @param string $key655 * @return bool656 */657 public function __isset( $key ) {658 if ( 'id' == $key ) {659 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );660 $key = 'ID';661 }662 663 if ( isset( $this->data->$key ) )664 return true;665 666 if ( isset( self::$back_compat_keys[ $key ] ) )667 $key = self::$back_compat_keys[ $key ];668 669 return metadata_exists( 'user', $this->ID, $key );670 }671 672 /**673 * Magic method for accessing custom fields674 *675 * @since 3.3.0676 * @param string $key677 * @return mixed678 */679 public function __get( $key ) {680 if ( 'id' == $key ) {681 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );682 return $this->ID;683 }684 685 if ( isset( $this->data->$key ) ) {686 $value = $this->data->$key;687 } else {688 if ( isset( self::$back_compat_keys[ $key ] ) )689 $key = self::$back_compat_keys[ $key ];690 $value = get_user_meta( $this->ID, $key, true );691 }692 693 if ( $this->filter ) {694 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );695 }696 697 return $value;698 }699 700 /**701 * Magic method for setting custom fields702 *703 * @since 3.3.0704 */705 public function __set( $key, $value ) {706 if ( 'id' == $key ) {707 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );708 $this->ID = $value;709 return;710 }711 712 $this->data->$key = $value;713 }714 715 /**716 * Determine whether the user exists in the database.717 *718 * @since 3.4.0719 * @access public720 *721 * @return bool True if user exists in the database, false if not.722 */723 public function exists() {724 return ! empty( $this->ID );725 }726 727 /**728 * Retrieve the value of a property or meta key.729 *730 * Retrieves from the users and usermeta table.731 *732 * @since 3.3.0733 *734 * @param string $key Property735 * @return mixed736 */737 public function get( $key ) {738 return $this->__get( $key );739 }740 741 /**742 * Determine whether a property or meta key is set743 *744 * Consults the users and usermeta tables.745 *746 * @since 3.3.0747 *748 * @param string $key Property749 * @return bool750 */751 public function has_prop( $key ) {752 return $this->__isset( $key );753 }754 755 /**756 * Return an array representation.757 *758 * @since 3.5.0759 *760 * @return array Array representation.761 */762 public function to_array() {763 return get_object_vars( $this->data );764 }765 766 /**767 * Set up capability object properties.768 *769 * Will set the value for the 'cap_key' property to current database table770 * prefix, followed by 'capabilities'. Will then check to see if the771 * property matching the 'cap_key' exists and is an array. If so, it will be772 * used.773 *774 * @access protected775 * @since 2.1.0776 *777 * @global wpdb $wpdb778 *779 * @param string $cap_key Optional capability key780 */781 protected function _init_caps( $cap_key = '' ) {782 global $wpdb;783 784 if ( empty($cap_key) )785 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';786 else787 $this->cap_key = $cap_key;788 789 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );790 791 if ( ! is_array( $this->caps ) )792 $this->caps = array();793 794 $this->get_role_caps();795 }796 797 /**798 * Retrieve all of the role capabilities and merge with individual capabilities.799 *800 * All of the capabilities of the roles the user belongs to are merged with801 * the users individual roles. This also means that the user can be denied802 * specific roles that their role might have, but the specific user isn't803 * granted permission to.804 *805 * @since 2.0.0806 * @access public807 *808 * @return array List of all capabilities for the user.809 */810 public function get_role_caps() {811 $wp_roles = wp_roles();812 813 //Filter out caps that are not role names and assign to $this->roles814 if ( is_array( $this->caps ) )815 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );816 817 //Build $allcaps from role caps, overlay user's $caps818 $this->allcaps = array();819 foreach ( (array) $this->roles as $role ) {820 $the_role = $wp_roles->get_role( $role );821 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );822 }823 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );824 825 return $this->allcaps;826 }827 828 /**829 * Add role to user.830 *831 * Updates the user's meta data option with capabilities and roles.832 *833 * @since 2.0.0834 * @access public835 *836 * @param string $role Role name.837 */838 public function add_role( $role ) {839 $this->caps[$role] = true;840 update_user_meta( $this->ID, $this->cap_key, $this->caps );841 $this->get_role_caps();842 $this->update_user_level_from_caps();843 844 /**845 * Fires immediately after the user has been given a new role.846 *847 * @since 4.3.0848 *849 * @param int $user_id The user ID.850 * @param string $role The new role.851 */852 do_action( 'add_user_role', $this->ID, $role );853 }854 855 /**856 * Remove role from user.857 *858 * @since 2.0.0859 * @access public860 *861 * @param string $role Role name.862 */863 public function remove_role( $role ) {864 if ( !in_array($role, $this->roles) )865 return;866 unset( $this->caps[$role] );867 update_user_meta( $this->ID, $this->cap_key, $this->caps );868 $this->get_role_caps();869 $this->update_user_level_from_caps();870 871 /**872 * Fires immediately after a role as been removed from a user.873 *874 * @since 4.3.0875 *876 * @param int $user_id The user ID.877 * @param string $role The removed role.878 */879 do_action( 'remove_user_role', $this->ID, $role );880 }881 882 /**883 * Set the role of the user.884 *885 * This will remove the previous roles of the user and assign the user the886 * new one. You can set the role to an empty string and it will remove all887 * of the roles from the user.888 *889 * @since 2.0.0890 * @access public891 *892 * @param string $role Role name.893 */894 public function set_role( $role ) {895 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )896 return;897 898 foreach ( (array) $this->roles as $oldrole )899 unset( $this->caps[$oldrole] );900 901 $old_roles = $this->roles;902 if ( !empty( $role ) ) {903 $this->caps[$role] = true;904 $this->roles = array( $role => true );905 } else {906 $this->roles = false;907 }908 update_user_meta( $this->ID, $this->cap_key, $this->caps );909 $this->get_role_caps();910 $this->update_user_level_from_caps();911 912 /**913 * Fires after the user's role has changed.914 *915 * @since 2.9.0916 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.917 *918 * @param int $user_id The user ID.919 * @param string $role The new role.920 * @param array $old_roles An array of the user's previous roles.921 */922 do_action( 'set_user_role', $this->ID, $role, $old_roles );923 }924 925 /**926 * Choose the maximum level the user has.927 *928 * Will compare the level from the $item parameter against the $max929 * parameter. If the item is incorrect, then just the $max parameter value930 * will be returned.931 *932 * Used to get the max level based on the capabilities the user has. This933 * is also based on roles, so if the user is assigned the Administrator role934 * then the capability 'level_10' will exist and the user will get that935 * value.936 *937 * @since 2.0.0938 * @access public939 *940 * @param int $max Max level of user.941 * @param string $item Level capability name.942 * @return int Max Level.943 */944 public function level_reduction( $max, $item ) {945 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {946 $level = intval( $matches[1] );947 return max( $max, $level );948 } else {949 return $max;950 }951 }952 953 /**954 * Update the maximum user level for the user.955 *956 * Updates the 'user_level' user metadata (includes prefix that is the957 * database table prefix) with the maximum user level. Gets the value from958 * the all of the capabilities that the user has.959 *960 * @since 2.0.0961 * @access public962 *963 * @global wpdb $wpdb964 */965 public function update_user_level_from_caps() {966 global $wpdb;967 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );968 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );969 }970 971 /**972 * Add capability and grant or deny access to capability.973 *974 * @since 2.0.0975 * @access public976 *977 * @param string $cap Capability name.978 * @param bool $grant Whether to grant capability to user.979 */980 public function add_cap( $cap, $grant = true ) {981 $this->caps[$cap] = $grant;982 update_user_meta( $this->ID, $this->cap_key, $this->caps );983 $this->get_role_caps();984 $this->update_user_level_from_caps();985 }986 987 /**988 * Remove capability from user.989 *990 * @since 2.0.0991 * @access public992 *993 * @param string $cap Capability name.994 */995 public function remove_cap( $cap ) {996 if ( ! isset( $this->caps[ $cap ] ) ) {997 return;998 }999 unset( $this->caps[ $cap ] );1000 update_user_meta( $this->ID, $this->cap_key, $this->caps );1001 $this->get_role_caps();1002 $this->update_user_level_from_caps();1003 }1004 1005 /**1006 * Remove all of the capabilities of the user.1007 *1008 * @since 2.1.01009 * @access public1010 *1011 * @global wpdb $wpdb1012 */1013 public function remove_all_caps() {1014 global $wpdb;1015 $this->caps = array();1016 delete_user_meta( $this->ID, $this->cap_key );1017 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );1018 $this->get_role_caps();1019 }1020 1021 /**1022 * Whether user has capability or role name.1023 *1024 * This is useful for looking up whether the user has a specific role1025 * assigned to the user. The second optional parameter can also be used to1026 * check for capabilities against a specific object, such as a post or user.1027 *1028 * @since 2.0.01029 * @access public1030 *1031 * @param string|int $cap Capability or role name to search.1032 * @return bool True, if user has capability; false, if user does not have capability.1033 */1034 public function has_cap( $cap ) {1035 if ( is_numeric( $cap ) ) {1036 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );1037 $cap = $this->translate_level_to_cap( $cap );1038 }1039 1040 $args = array_slice( func_get_args(), 1 );1041 $args = array_merge( array( $cap, $this->ID ), $args );1042 $caps = call_user_func_array( 'map_meta_cap', $args );1043 1044 // Multisite super admin has all caps by definition, Unless specifically denied.1045 if ( is_multisite() && is_super_admin( $this->ID ) ) {1046 if ( in_array('do_not_allow', $caps) )1047 return false;1048 return true;1049 }1050 1051 /**1052 * Dynamically filter a user's capabilities.1053 *1054 * @since 2.0.01055 * @since 3.7.0 Added the user object.1056 *1057 * @param array $allcaps An array of all the user's capabilities.1058 * @param array $caps Actual capabilities for meta capability.1059 * @param array $args Optional parameters passed to has_cap(), typically object ID.1060 * @param WP_User $user The user object.1061 */1062 // Must have ALL requested caps1063 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );1064 $capabilities['exist'] = true; // Everyone is allowed to exist1065 foreach ( (array) $caps as $cap ) {1066 if ( empty( $capabilities[ $cap ] ) )1067 return false;1068 }1069 1070 return true;1071 }1072 1073 /**1074 * Convert numeric level to level capability name.1075 *1076 * Prepends 'level_' to level number.1077 *1078 * @since 2.0.01079 * @access public1080 *1081 * @param int $level Level number, 1 to 10.1082 * @return string1083 */1084 public function translate_level_to_cap( $level ) {1085 return 'level_' . $level;1086 }1087 1088 /**1089 * Set the blog to operate on. Defaults to the current blog.1090 *1091 * @since 3.0.01092 *1093 * @global wpdb $wpdb1094 *1095 * @param int $blog_id Optional Blog ID, defaults to current blog.1096 */1097 public function for_blog( $blog_id = '' ) {1098 global $wpdb;1099 if ( ! empty( $blog_id ) )1100 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';1101 else1102 $cap_key = '';1103 $this->_init_caps( $cap_key );1104 }1105 }1106 1107 /**1108 * Map meta capabilities to primitive capabilities.1109 *1110 * This does not actually compare whether the user ID has the actual capability,1111 * just what the capability or capabilities are. Meta capability list value can1112 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',1113 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.1114 *1115 * @since 2.0.01116 *1117 * @param string $cap Capability name.1118 * @param int $user_id User ID.1119 * @return array Actual capabilities for meta capability.1120 */1121 function map_meta_cap( $cap, $user_id ) {1122 $args = array_slice( func_get_args(), 2 );1123 $caps = array();1124 1125 switch ( $cap ) {1126 case 'remove_user':1127 $caps[] = 'remove_users';1128 break;1129 case 'promote_user':1130 $caps[] = 'promote_users';1131 break;1132 case 'edit_user':1133 case 'edit_users':1134 // Allow user to edit itself1135 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )1136 break;1137 1138 // If multisite these caps are allowed only for super admins.1139 if ( is_multisite() && !is_super_admin( $user_id ) )1140 $caps[] = 'do_not_allow';1141 else1142 $caps[] = 'edit_users'; // edit_user maps to edit_users.1143 break;1144 case 'delete_post':1145 case 'delete_page':1146 $post = get_post( $args[0] );1147 1148 if ( 'revision' == $post->post_type ) {1149 $post = get_post( $post->post_parent );1150 }1151 1152 $post_type = get_post_type_object( $post->post_type );1153 1154 if ( ! $post_type->map_meta_cap ) {1155 $caps[] = $post_type->cap->$cap;1156 // Prior to 3.1 we would re-call map_meta_cap here.1157 if ( 'delete_post' == $cap )1158 $cap = $post_type->cap->$cap;1159 break;1160 }1161 1162 // If the post author is set and the user is the author...1163 if ( $post->post_author && $user_id == $post->post_author ) {1164 // If the post is published...1165 if ( 'publish' == $post->post_status ) {1166 $caps[] = $post_type->cap->delete_published_posts;1167 } elseif ( 'trash' == $post->post_status ) {1168 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {1169 $caps[] = $post_type->cap->delete_published_posts;1170 }1171 } else {1172 // If the post is draft...1173 $caps[] = $post_type->cap->delete_posts;1174 }1175 } else {1176 // The user is trying to edit someone else's post.1177 $caps[] = $post_type->cap->delete_others_posts;1178 // The post is published, extra cap required.1179 if ( 'publish' == $post->post_status ) {1180 $caps[] = $post_type->cap->delete_published_posts;1181 } elseif ( 'private' == $post->post_status ) {1182 $caps[] = $post_type->cap->delete_private_posts;1183 }1184 }1185 break;1186 // edit_post breaks down to edit_posts, edit_published_posts, or1187 // edit_others_posts1188 case 'edit_post':1189 case 'edit_page':1190 $post = get_post( $args[0] );1191 if ( empty( $post ) ) {1192 $caps[] = 'do_not_allow';1193 break;1194 }1195 1196 if ( 'revision' == $post->post_type ) {1197 $post = get_post( $post->post_parent );1198 }1199 1200 $post_type = get_post_type_object( $post->post_type );1201 1202 if ( ! $post_type->map_meta_cap ) {1203 $caps[] = $post_type->cap->$cap;1204 // Prior to 3.1 we would re-call map_meta_cap here.1205 if ( 'edit_post' == $cap )1206 $cap = $post_type->cap->$cap;1207 break;1208 }1209 1210 // If the post author is set and the user is the author...1211 if ( $post->post_author && $user_id == $post->post_author ) {1212 // If the post is published...1213 if ( 'publish' == $post->post_status ) {1214 $caps[] = $post_type->cap->edit_published_posts;1215 } elseif ( 'trash' == $post->post_status ) {1216 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {1217 $caps[] = $post_type->cap->edit_published_posts;1218 }1219 } else {1220 // If the post is draft...1221 $caps[] = $post_type->cap->edit_posts;1222 }1223 } else {1224 // The user is trying to edit someone else's post.1225 $caps[] = $post_type->cap->edit_others_posts;1226 // The post is published, extra cap required.1227 if ( 'publish' == $post->post_status ) {1228 $caps[] = $post_type->cap->edit_published_posts;1229 } elseif ( 'private' == $post->post_status ) {1230 $caps[] = $post_type->cap->edit_private_posts;1231 }1232 }1233 break;1234 case 'read_post':1235 case 'read_page':1236 $post = get_post( $args[0] );1237 1238 if ( 'revision' == $post->post_type ) {1239 $post = get_post( $post->post_parent );1240 }1241 1242 $post_type = get_post_type_object( $post->post_type );1243 1244 if ( ! $post_type->map_meta_cap ) {1245 $caps[] = $post_type->cap->$cap;1246 // Prior to 3.1 we would re-call map_meta_cap here.1247 if ( 'read_post' == $cap )1248 $cap = $post_type->cap->$cap;1249 break;1250 }1251 1252 $status_obj = get_post_status_object( $post->post_status );1253 if ( $status_obj->public ) {1254 $caps[] = $post_type->cap->read;1255 break;1256 }1257 1258 if ( $post->post_author && $user_id == $post->post_author ) {1259 $caps[] = $post_type->cap->read;1260 } elseif ( $status_obj->private ) {1261 $caps[] = $post_type->cap->read_private_posts;1262 } else {1263 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );1264 }1265 break;1266 case 'publish_post':1267 $post = get_post( $args[0] );1268 $post_type = get_post_type_object( $post->post_type );1269 1270 $caps[] = $post_type->cap->publish_posts;1271 break;1272 case 'edit_post_meta':1273 case 'delete_post_meta':1274 case 'add_post_meta':1275 $post = get_post( $args[0] );1276 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );1277 1278 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;1279 1280 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {1281 /**1282 * Filter whether the user is allowed to add post meta to a post.1283 *1284 * The dynamic portion of the hook name, `$meta_key`, refers to the1285 * meta key passed to {@see map_meta_cap()}.1286 *1287 * @since 3.3.01288 *1289 * @param bool $allowed Whether the user can add the post meta. Default false.1290 * @param string $meta_key The meta key.1291 * @param int $post_id Post ID.1292 * @param int $user_id User ID.1293 * @param string $cap Capability name.1294 * @param array $caps User capabilities.1295 */1296 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );1297 if ( ! $allowed )1298 $caps[] = $cap;1299 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {1300 $caps[] = $cap;1301 }1302 break;1303 case 'edit_comment':1304 $comment = get_comment( $args[0] );1305 if ( empty( $comment ) )1306 break;1307 $post = get_post( $comment->comment_post_ID );1308 1309 /*1310 * If the post doesn't exist, we have an orphaned comment.1311 * Fall back to the edit_posts capability, instead.1312 */1313 if ( $post ) {1314 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );1315 } else {1316 $caps = map_meta_cap( 'edit_posts', $user_id );1317 }1318 break;1319 case 'unfiltered_upload':1320 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )1321 $caps[] = $cap;1322 else1323 $caps[] = 'do_not_allow';1324 break;1325 case 'unfiltered_html' :1326 // Disallow unfiltered_html for all users, even admins and super admins.1327 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )1328 $caps[] = 'do_not_allow';1329 elseif ( is_multisite() && ! is_super_admin( $user_id ) )1330 $caps[] = 'do_not_allow';1331 else1332 $caps[] = $cap;1333 break;1334 case 'edit_files':1335 case 'edit_plugins':1336 case 'edit_themes':1337 // Disallow the file editors.1338 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )1339 $caps[] = 'do_not_allow';1340 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )1341 $caps[] = 'do_not_allow';1342 elseif ( is_multisite() && ! is_super_admin( $user_id ) )1343 $caps[] = 'do_not_allow';1344 else1345 $caps[] = $cap;1346 break;1347 case 'update_plugins':1348 case 'delete_plugins':1349 case 'install_plugins':1350 case 'upload_plugins':1351 case 'update_themes':1352 case 'delete_themes':1353 case 'install_themes':1354 case 'upload_themes':1355 case 'update_core':1356 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.1357 // Files in uploads are excepted.1358 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {1359 $caps[] = 'do_not_allow';1360 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {1361 $caps[] = 'do_not_allow';1362 } elseif ( 'upload_themes' === $cap ) {1363 $caps[] = 'install_themes';1364 } elseif ( 'upload_plugins' === $cap ) {1365 $caps[] = 'install_plugins';1366 } else {1367 $caps[] = $cap;1368 }1369 break;1370 case 'activate_plugins':1371 $caps[] = $cap;1372 if ( is_multisite() ) {1373 // update_, install_, and delete_ are handled above with is_super_admin().1374 $menu_perms = get_site_option( 'menu_items', array() );1375 if ( empty( $menu_perms['plugins'] ) )1376 $caps[] = 'manage_network_plugins';1377 }1378 break;1379 case 'delete_user':1380 case 'delete_users':1381 // If multisite only super admins can delete users.1382 if ( is_multisite() && ! is_super_admin( $user_id ) )1383 $caps[] = 'do_not_allow';1384 else1385 $caps[] = 'delete_users'; // delete_user maps to delete_users.1386 break;1387 case 'create_users':1388 if ( !is_multisite() )1389 $caps[] = $cap;1390 elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) )1391 $caps[] = $cap;1392 else1393 $caps[] = 'do_not_allow';1394 break;1395 case 'manage_links' :1396 if ( get_option( 'link_manager_enabled' ) )1397 $caps[] = $cap;1398 else1399 $caps[] = 'do_not_allow';1400 break;1401 case 'customize' :1402 $caps[] = 'edit_theme_options';1403 break;1404 case 'delete_site':1405 $caps[] = 'manage_options';1406 break;1407 default:1408 // Handle meta capabilities for custom post types.1409 $post_type_meta_caps = _post_type_meta_capabilities();1410 if ( isset( $post_type_meta_caps[ $cap ] ) ) {1411 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );1412 return call_user_func_array( 'map_meta_cap', $args );1413 }1414 1415 // If no meta caps match, return the original cap.1416 $caps[] = $cap;1417 }1418 1419 /**1420 * Filter a user's capabilities depending on specific context and/or privilege.1421 *1422 * @since 2.8.01423 *1424 * @param array $caps Returns the user's actual capabilities.1425 * @param string $cap Capability name.1426 * @param int $user_id The user ID.1427 * @param array $args Adds the context to the cap. Typically the object ID.1428 */1429 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );1430 }1431 1432 /**1433 * Whether current user has capability or role.1434 *1435 * @since 2.0.01436 *1437 * @param string $capability Capability or role name.1438 * @return bool1439 */1440 function current_user_can( $capability ) {1441 $current_user = wp_get_current_user();1442 1443 if ( empty( $current_user ) )1444 return false;1445 1446 $args = array_slice( func_get_args(), 1 );1447 $args = array_merge( array( $capability ), $args );1448 1449 return call_user_func_array( array( $current_user, 'has_cap' ), $args );1450 }1451 1452 /**1453 * Whether current user has a capability or role for a given blog.1454 *1455 * @since 3.0.01456 *1457 * @param int $blog_id Blog ID1458 * @param string $capability Capability or role name.1459 * @return bool1460 */1461 function current_user_can_for_blog( $blog_id, $capability ) {1462 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;1463 1464 $current_user = wp_get_current_user();1465 1466 if ( empty( $current_user ) ) {1467 if ( $switched ) {1468 restore_current_blog();1469 }1470 return false;1471 }1472 1473 $args = array_slice( func_get_args(), 2 );1474 $args = array_merge( array( $capability ), $args );1475 1476 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );1477 1478 if ( $switched ) {1479 restore_current_blog();1480 }1481 1482 return $can;1483 }1484 1485 /**1486 * Whether author of supplied post has capability or role.1487 *1488 * @since 2.9.01489 *1490 * @param int|object $post Post ID or post object.1491 * @param string $capability Capability or role name.1492 * @return bool1493 */1494 function author_can( $post, $capability ) {1495 if ( !$post = get_post($post) )1496 return false;1497 1498 $author = get_userdata( $post->post_author );1499 1500 if ( ! $author )1501 return false;1502 1503 $args = array_slice( func_get_args(), 2 );1504 $args = array_merge( array( $capability ), $args );1505 1506 return call_user_func_array( array( $author, 'has_cap' ), $args );1507 }1508 1509 /**1510 * Whether a particular user has capability or role.1511 *1512 * @since 3.1.01513 *1514 * @param int|object $user User ID or object.1515 * @param string $capability Capability or role name.1516 * @return bool1517 */1518 function user_can( $user, $capability ) {1519 if ( ! is_object( $user ) )1520 $user = get_userdata( $user );1521 1522 if ( ! $user || ! $user->exists() )1523 return false;1524 1525 $args = array_slice( func_get_args(), 2 );1526 $args = array_merge( array( $capability ), $args );1527 1528 return call_user_func_array( array( $user, 'has_cap' ), $args );1529 }1530 1531 /**1532 * Retrieves the global WP_Roles instance and instantiates it if necessary.1533 *1534 * @since 4.3.01535 *1536 * @global WP_Roles $wp_roles WP_Roles global instance.1537 *1538 * @return WP_Roles WP_Roles global instance if not already instantiated.1539 */1540 function wp_roles() {1541 global $wp_roles;1542 1543 if ( ! isset( $wp_roles ) ) {1544 $wp_roles = new WP_Roles();1545 }1546 return $wp_roles;1547 }1548 1549 /**1550 * Retrieve role object.1551 *1552 * @since 2.0.01553 *1554 * @param string $role Role name.1555 * @return WP_Role|null WP_Role object if found, null if the role does not exist.1556 */1557 function get_role( $role ) {1558 return wp_roles()->get_role( $role );1559 }1560 1561 /**1562 * Add role, if it does not exist.1563 *1564 * @since 2.0.01565 *1566 * @param string $role Role name.1567 * @param string $display_name Display name for role.1568 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );1569 * @return WP_Role|null WP_Role object if role is added, null if already exists.1570 */1571 function add_role( $role, $display_name, $capabilities = array() ) {1572 return wp_roles()->add_role( $role, $display_name, $capabilities );1573 }1574 1575 /**1576 * Remove role, if it exists.1577 *1578 * @since 2.0.01579 *1580 * @param string $role Role name.1581 */1582 function remove_role( $role ) {1583 wp_roles()->remove_role( $role );1584 }1585 1586 /**1587 * Retrieve a list of super admins.1588 *1589 * @since 3.0.01590 *1591 * @global array $super_admins1592 *1593 * @return array List of super admin logins1594 */1595 function get_super_admins() {1596 global $super_admins;1597 1598 if ( isset($super_admins) )1599 return $super_admins;1600 else1601 return get_site_option( 'site_admins', array('admin') );1602 }1603 1604 /**1605 * Determine if user is a site admin.1606 *1607 * @since 3.0.01608 *1609 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.1610 * @return bool True if the user is a site admin.1611 */1612 function is_super_admin( $user_id = false ) {1613 if ( ! $user_id || $user_id == get_current_user_id() )1614 $user = wp_get_current_user();1615 else1616 $user = get_userdata( $user_id );1617 1618 if ( ! $user || ! $user->exists() )1619 return false;1620 1621 if ( is_multisite() ) {1622 $super_admins = get_super_admins();1623 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )1624 return true;1625 } else {1626 if ( $user->has_cap('delete_users') )1627 return true;1628 }1629 1630 return false;1631 } -
trunk/src/wp-includes/class-wp-user.php
r33746 r33752 1 1 <?php 2 /**3 * WordPress Roles and Capabilities.4 *5 * @package WordPress6 * @subpackage User7 */8 9 /**10 * WordPress User Roles.11 *12 * The role option is simple, the structure is organized by role name that store13 * the name in value of the 'name' key. The capabilities are stored as an array14 * in the value of the 'capability' key.15 *16 * array (17 * 'rolename' => array (18 * 'name' => 'rolename',19 * 'capabilities' => array()20 * )21 * )22 *23 * @since 2.0.024 * @package WordPress25 * @subpackage User26 */27 class WP_Roles {28 /**29 * List of roles and capabilities.30 *31 * @since 2.0.032 * @access public33 * @var array34 */35 public $roles;36 37 /**38 * List of the role objects.39 *40 * @since 2.0.041 * @access public42 * @var array43 */44 public $role_objects = array();45 46 /**47 * List of role names.48 *49 * @since 2.0.050 * @access public51 * @var array52 */53 public $role_names = array();54 55 /**56 * Option name for storing role list.57 *58 * @since 2.0.059 * @access public60 * @var string61 */62 public $role_key;63 64 /**65 * Whether to use the database for retrieval and storage.66 *67 * @since 2.1.068 * @access public69 * @var bool70 */71 public $use_db = true;72 73 /**74 * Constructor75 *76 * @since 2.0.077 */78 public function __construct() {79 $this->_init();80 }81 82 /**83 * Make private/protected methods readable for backwards compatibility.84 *85 * @since 4.0.086 * @access public87 *88 * @param callable $name Method to call.89 * @param array $arguments Arguments to pass when calling.90 * @return mixed|false Return value of the callback, false otherwise.91 */92 public function __call( $name, $arguments ) {93 if ( '_init' === $name ) {94 return call_user_func_array( array( $this, $name ), $arguments );95 }96 return false;97 }98 99 /**100 * Set up the object properties.101 *102 * The role key is set to the current prefix for the $wpdb object with103 * 'user_roles' appended. If the $wp_user_roles global is set, then it will104 * be used and the role option will not be updated or used.105 *106 * @since 2.1.0107 * @access protected108 *109 * @global wpdb $wpdb WordPress database abstraction object.110 * @global array $wp_user_roles Used to set the 'roles' property value.111 */112 protected function _init() {113 global $wpdb, $wp_user_roles;114 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';115 if ( ! empty( $wp_user_roles ) ) {116 $this->roles = $wp_user_roles;117 $this->use_db = false;118 } else {119 $this->roles = get_option( $this->role_key );120 }121 122 if ( empty( $this->roles ) )123 return;124 125 $this->role_objects = array();126 $this->role_names = array();127 foreach ( array_keys( $this->roles ) as $role ) {128 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );129 $this->role_names[$role] = $this->roles[$role]['name'];130 }131 }132 133 /**134 * Reinitialize the object135 *136 * Recreates the role objects. This is typically called only by switch_to_blog()137 * after switching wpdb to a new blog ID.138 *139 * @since 3.5.0140 * @access public141 *142 * @global wpdb $wpdb143 */144 public function reinit() {145 // There is no need to reinit if using the wp_user_roles global.146 if ( ! $this->use_db )147 return;148 149 global $wpdb;150 151 // Duplicated from _init() to avoid an extra function call.152 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';153 $this->roles = get_option( $this->role_key );154 if ( empty( $this->roles ) )155 return;156 157 $this->role_objects = array();158 $this->role_names = array();159 foreach ( array_keys( $this->roles ) as $role ) {160 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );161 $this->role_names[$role] = $this->roles[$role]['name'];162 }163 }164 165 /**166 * Add role name with capabilities to list.167 *168 * Updates the list of roles, if the role doesn't already exist.169 *170 * The capabilities are defined in the following format `array( 'read' => true );`171 * To explicitly deny a role a capability you set the value for that capability to false.172 *173 * @since 2.0.0174 * @access public175 *176 * @param string $role Role name.177 * @param string $display_name Role display name.178 * @param array $capabilities List of role capabilities in the above format.179 * @return WP_Role|void WP_Role object, if role is added.180 */181 public function add_role( $role, $display_name, $capabilities = array() ) {182 if ( isset( $this->roles[$role] ) )183 return;184 185 $this->roles[$role] = array(186 'name' => $display_name,187 'capabilities' => $capabilities188 );189 if ( $this->use_db )190 update_option( $this->role_key, $this->roles );191 $this->role_objects[$role] = new WP_Role( $role, $capabilities );192 $this->role_names[$role] = $display_name;193 return $this->role_objects[$role];194 }195 196 /**197 * Remove role by name.198 *199 * @since 2.0.0200 * @access public201 *202 * @param string $role Role name.203 */204 public function remove_role( $role ) {205 if ( ! isset( $this->role_objects[$role] ) )206 return;207 208 unset( $this->role_objects[$role] );209 unset( $this->role_names[$role] );210 unset( $this->roles[$role] );211 212 if ( $this->use_db )213 update_option( $this->role_key, $this->roles );214 215 if ( get_option( 'default_role' ) == $role )216 update_option( 'default_role', 'subscriber' );217 }218 219 /**220 * Add capability to role.221 *222 * @since 2.0.0223 * @access public224 *225 * @param string $role Role name.226 * @param string $cap Capability name.227 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.228 */229 public function add_cap( $role, $cap, $grant = true ) {230 if ( ! isset( $this->roles[$role] ) )231 return;232 233 $this->roles[$role]['capabilities'][$cap] = $grant;234 if ( $this->use_db )235 update_option( $this->role_key, $this->roles );236 }237 238 /**239 * Remove capability from role.240 *241 * @since 2.0.0242 * @access public243 *244 * @param string $role Role name.245 * @param string $cap Capability name.246 */247 public function remove_cap( $role, $cap ) {248 if ( ! isset( $this->roles[$role] ) )249 return;250 251 unset( $this->roles[$role]['capabilities'][$cap] );252 if ( $this->use_db )253 update_option( $this->role_key, $this->roles );254 }255 256 /**257 * Retrieve role object by name.258 *259 * @since 2.0.0260 * @access public261 *262 * @param string $role Role name.263 * @return WP_Role|null WP_Role object if found, null if the role does not exist.264 */265 public function get_role( $role ) {266 if ( isset( $this->role_objects[$role] ) )267 return $this->role_objects[$role];268 else269 return null;270 }271 272 /**273 * Retrieve list of role names.274 *275 * @since 2.0.0276 * @access public277 *278 * @return array List of role names.279 */280 public function get_names() {281 return $this->role_names;282 }283 284 /**285 * Whether role name is currently in the list of available roles.286 *287 * @since 2.0.0288 * @access public289 *290 * @param string $role Role name to look up.291 * @return bool292 */293 public function is_role( $role ) {294 return isset( $this->role_names[$role] );295 }296 }297 298 /**299 * WordPress Role class.300 *301 * @since 2.0.0302 * @package WordPress303 * @subpackage User304 */305 class WP_Role {306 /**307 * Role name.308 *309 * @since 2.0.0310 * @access public311 * @var string312 */313 public $name;314 315 /**316 * List of capabilities the role contains.317 *318 * @since 2.0.0319 * @access public320 * @var array321 */322 public $capabilities;323 324 /**325 * Constructor - Set up object properties.326 *327 * The list of capabilities, must have the key as the name of the capability328 * and the value a boolean of whether it is granted to the role.329 *330 * @since 2.0.0331 * @access public332 *333 * @param string $role Role name.334 * @param array $capabilities List of capabilities.335 */336 public function __construct( $role, $capabilities ) {337 $this->name = $role;338 $this->capabilities = $capabilities;339 }340 341 /**342 * Assign role a capability.343 *344 * @since 2.0.0345 * @access public346 *347 * @param string $cap Capability name.348 * @param bool $grant Whether role has capability privilege.349 */350 public function add_cap( $cap, $grant = true ) {351 $this->capabilities[$cap] = $grant;352 wp_roles()->add_cap( $this->name, $cap, $grant );353 }354 355 /**356 * Remove capability from role.357 *358 * This is a container for {@link WP_Roles::remove_cap()} to remove the359 * capability from the role. That is to say, that {@link360 * WP_Roles::remove_cap()} implements the functionality, but it also makes361 * sense to use this class, because you don't need to enter the role name.362 *363 * @since 2.0.0364 * @access public365 *366 * @param string $cap Capability name.367 */368 public function remove_cap( $cap ) {369 unset( $this->capabilities[$cap] );370 wp_roles()->remove_cap( $this->name, $cap );371 }372 373 /**374 * Whether role has capability.375 *376 * The capabilities is passed through the 'role_has_cap' filter. The first377 * parameter for the hook is the list of capabilities the class has378 * assigned. The second parameter is the capability name to look for. The379 * third and final parameter for the hook is the role name.380 *381 * @since 2.0.0382 * @access public383 *384 * @param string $cap Capability name.385 * @return bool True, if user has capability. False, if doesn't have capability.386 */387 public function has_cap( $cap ) {388 /**389 * Filter which capabilities a role has.390 *391 * @since 2.0.0392 *393 * @param array $capabilities Array of role capabilities.394 * @param string $cap Capability name.395 * @param string $name Role name.396 */397 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );398 if ( !empty( $capabilities[$cap] ) )399 return $capabilities[$cap];400 else401 return false;402 }403 404 }405 406 2 /** 407 3 * WordPress User class. … … 1104 700 } 1105 701 } 1106 1107 /**1108 * Map meta capabilities to primitive capabilities.1109 *1110 * This does not actually compare whether the user ID has the actual capability,1111 * just what the capability or capabilities are. Meta capability list value can1112 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',1113 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.1114 *1115 * @since 2.0.01116 *1117 * @param string $cap Capability name.1118 * @param int $user_id User ID.1119 * @return array Actual capabilities for meta capability.1120 */1121 function map_meta_cap( $cap, $user_id ) {1122 $args = array_slice( func_get_args(), 2 );1123 $caps = array();1124 1125 switch ( $cap ) {1126 case 'remove_user':1127 $caps[] = 'remove_users';1128 break;1129 case 'promote_user':1130 $caps[] = 'promote_users';1131 break;1132 case 'edit_user':1133 case 'edit_users':1134 // Allow user to edit itself1135 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )1136 break;1137 1138 // If multisite these caps are allowed only for super admins.1139 if ( is_multisite() && !is_super_admin( $user_id ) )1140 $caps[] = 'do_not_allow';1141 else1142 $caps[] = 'edit_users'; // edit_user maps to edit_users.1143 break;1144 case 'delete_post':1145 case 'delete_page':1146 $post = get_post( $args[0] );1147 1148 if ( 'revision' == $post->post_type ) {1149 $post = get_post( $post->post_parent );1150 }1151 1152 $post_type = get_post_type_object( $post->post_type );1153 1154 if ( ! $post_type->map_meta_cap ) {1155 $caps[] = $post_type->cap->$cap;1156 // Prior to 3.1 we would re-call map_meta_cap here.1157 if ( 'delete_post' == $cap )1158 $cap = $post_type->cap->$cap;1159 break;1160 }1161 1162 // If the post author is set and the user is the author...1163 if ( $post->post_author && $user_id == $post->post_author ) {1164 // If the post is published...1165 if ( 'publish' == $post->post_status ) {1166 $caps[] = $post_type->cap->delete_published_posts;1167 } elseif ( 'trash' == $post->post_status ) {1168 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {1169 $caps[] = $post_type->cap->delete_published_posts;1170 }1171 } else {1172 // If the post is draft...1173 $caps[] = $post_type->cap->delete_posts;1174 }1175 } else {1176 // The user is trying to edit someone else's post.1177 $caps[] = $post_type->cap->delete_others_posts;1178 // The post is published, extra cap required.1179 if ( 'publish' == $post->post_status ) {1180 $caps[] = $post_type->cap->delete_published_posts;1181 } elseif ( 'private' == $post->post_status ) {1182 $caps[] = $post_type->cap->delete_private_posts;1183 }1184 }1185 break;1186 // edit_post breaks down to edit_posts, edit_published_posts, or1187 // edit_others_posts1188 case 'edit_post':1189