Changeset 33752 for trunk/src/wp-includes/capabilities-functions.php
- Timestamp:
- 08/26/2015 04:57:48 AM (10 years ago)
- File:
-
- 1 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 /**
Note: See TracChangeset
for help on using the changeset viewer.