3 * WordPress Roles and Capabilities.
10 * WordPress User Roles.
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.
18 * 'rolename' => array (
19 * 'name' => 'rolename',
20 * 'capabilities' => array()
31 * List of roles and capabilities.
40 * List of the role objects.
46 var $role_objects = array();
55 var $role_names = array();
58 * Option name for storing role list.
67 * Whether to use the database for retrieval and storage.
80 function __construct() {
85 * Set up the object properties.
87 * The role key is set to the current prefix for the $wpdb object with
88 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
89 * be used and the role option will not be updated or used.
93 * @uses $wpdb Used to get the database prefix.
94 * @global array $wp_user_roles Used to set the 'roles' property value.
97 global $wpdb, $wp_user_roles;
98 $this->role_key = $wpdb->prefix . 'user_roles';
99 if ( ! empty( $wp_user_roles ) ) {
100 $this->roles = $wp_user_roles;
101 $this->use_db = false;
103 $this->roles = get_option( $this->role_key );
106 if ( empty( $this->roles ) )
109 $this->role_objects = array();
110 $this->role_names = array();
111 foreach ( (array) $this->roles as $role => $data ) {
112 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
113 $this->role_names[$role] = $this->roles[$role]['name'];
118 * Add role name with capabilities to list.
120 * Updates the list of roles, if the role doesn't already exist.
122 * The capabilities are defined in the following format `array( 'read' => true );`
123 * To explicitly deny a role a capability you set the value for that capability to false.
128 * @param string $role Role name.
129 * @param string $display_name Role display name.
130 * @param array $capabilities List of role capabilities in the above format.
131 * @return null|WP_Role WP_Role object if role is added, null if already exists.
133 function add_role( $role, $display_name, $capabilities = array() ) {
134 if ( isset( $this->roles[$role] ) )
137 $this->roles[$role] = array(
138 'name' => $display_name,
139 'capabilities' => $capabilities
142 update_option( $this->role_key, $this->roles );
143 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
144 $this->role_names[$role] = $display_name;
145 return $this->role_objects[$role];
149 * Remove role by name.
154 * @param string $role Role name.
156 function remove_role( $role ) {
157 if ( ! isset( $this->role_objects[$role] ) )
160 unset( $this->role_objects[$role] );
161 unset( $this->role_names[$role] );
162 unset( $this->roles[$role] );
165 update_option( $this->role_key, $this->roles );
169 * Add capability to role.
174 * @param string $role Role name.
175 * @param string $cap Capability name.
176 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
178 function add_cap( $role, $cap, $grant = true ) {
179 $this->roles[$role]['capabilities'][$cap] = $grant;
181 update_option( $this->role_key, $this->roles );
185 * Remove capability from role.
190 * @param string $role Role name.
191 * @param string $cap Capability name.
193 function remove_cap( $role, $cap ) {
194 unset( $this->roles[$role]['capabilities'][$cap] );
196 update_option( $this->role_key, $this->roles );
200 * Retrieve role object by name.
205 * @param string $role Role name.
206 * @return object|null Null, if role does not exist. WP_Role object, if found.
208 function get_role( $role ) {
209 if ( isset( $this->role_objects[$role] ) )
210 return $this->role_objects[$role];
216 * Retrieve list of role names.
221 * @return array List of role names.
223 function get_names() {
224 return $this->role_names;
228 * Whether role name is currently in the list of available roles.
233 * @param string $role Role name to look up.
236 function is_role( $role )
238 return isset( $this->role_names[$role] );
243 * WordPress Role class.
260 * List of capabilities the role contains.
269 * Constructor - Set up object properties.
271 * The list of capabilities, must have the key as the name of the capability
272 * and the value a boolean of whether it is granted to the role.
277 * @param string $role Role name.
278 * @param array $capabilities List of capabilities.
280 function __construct( $role, $capabilities ) {
282 $this->capabilities = $capabilities;
286 * Assign role a capability.
288 * @see WP_Roles::add_cap() Method uses implementation for role.
292 * @param string $cap Capability name.
293 * @param bool $grant Whether role has capability privilege.
295 function add_cap( $cap, $grant = true ) {
298 if ( ! isset( $wp_roles ) )
299 $wp_roles = new WP_Roles();
301 $this->capabilities[$cap] = $grant;
302 $wp_roles->add_cap( $this->name, $cap, $grant );
306 * Remove capability from role.
308 * This is a container for {@link WP_Roles::remove_cap()} to remove the
309 * capability from the role. That is to say, that {@link
310 * WP_Roles::remove_cap()} implements the functionality, but it also makes
311 * sense to use this class, because you don't need to enter the role name.
316 * @param string $cap Capability name.
318 function remove_cap( $cap ) {
321 if ( ! isset( $wp_roles ) )
322 $wp_roles = new WP_Roles();
324 unset( $this->capabilities[$cap] );
325 $wp_roles->remove_cap( $this->name, $cap );
329 * Whether role has capability.
331 * The capabilities is passed through the 'role_has_cap' filter. The first
332 * parameter for the hook is the list of capabilities the class has
333 * assigned. The second parameter is the capability name to look for. The
334 * third and final parameter for the hook is the role name.
339 * @param string $cap Capability name.
340 * @return bool True, if user has capability. False, if doesn't have capability.
342 function has_cap( $cap ) {
343 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
344 if ( !empty( $capabilities[$cap] ) )
345 return $capabilities[$cap];
353 * WordPress User class.
361 * User data container.
379 * The individual capabilities the user has been given.
388 * User metadata option name.
397 * The roles the user is part of.
403 var $roles = array();
406 * All capabilities the user has, including individual and role based.
412 var $allcaps = array();
415 * The filter context applied to user data fields.
423 private static $back_compat_keys;
428 * Retrieves the userdata and passes it to {@link WP_User::init()}.
433 * @param int|string $id User's ID
434 * @param string $name Optional. User's username
435 * @param int $blog_id Optional Blog ID, defaults to current blog.
438 function __construct( $id = 0, $name = '', $blog_id = '' ) {
439 if ( ! isset( self::$back_compat_keys ) ) {
440 $prefix = $GLOBALS['wpdb']->prefix;
441 self::$back_compat_keys = array(
442 'user_firstname' => 'first_name',
443 'user_lastname' => 'last_name',
444 'user_description' => 'description',
445 'user_level' => $prefix . 'user_level',
446 $prefix . 'usersettings' => $prefix . 'user-settings',
447 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
451 if ( ! empty( $id ) && ! is_numeric( $id ) ) {
457 $data = self::get_data_by( 'id', $id );
459 $data = self::get_data_by( 'login', $name );
462 $this->init( $data, $blog_id );
466 * Sets up object properties, including capabilities.
468 * @param object $data User DB row object
469 * @param int $blog_id Optional. The blog id to initialize for
471 function init( $data, $blog_id = '' ) {
473 $this->ID = (int) $data->ID;
475 $this->for_blog( $blog_id );
479 * Return only the main user fields
483 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
484 * @param string|int $value The field value
485 * @return object Raw user object
487 static function get_data_by( $field, $value ) {
490 if ( 'id' == $field ) {
491 // Make sure the value is numeric to avoid casting objects, for example,
493 if ( ! is_numeric( $value ) )
495 $value = absint( $value );
497 $value = trim( $value );
509 $user_id = wp_cache_get($value, 'userslugs');
510 $db_field = 'user_nicename';
513 $user_id = wp_cache_get($value, 'useremail');
514 $db_field = 'user_email';
517 $value = sanitize_user( $value );
518 $user_id = wp_cache_get($value, 'userlogins');
519 $db_field = 'user_login';
525 if ( false !== $user_id ) {
526 if ( $user = wp_cache_get( $user_id, 'users' ) )
530 if ( !$user = $wpdb->get_row( $wpdb->prepare(
531 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
535 update_user_caches( $user );
541 * Magic method for checking the existence of a certain custom field
545 function __isset( $key ) {
546 if ( 'id' == $key ) {
547 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
551 if ( isset( $this->data->$key ) )
554 if ( isset( self::$back_compat_keys[ $key ] ) )
555 $key = self::$back_compat_keys[ $key ];
557 return metadata_exists( 'user', $this->ID, $key );
561 * Magic method for accessing custom fields
565 function __get( $key ) {
566 if ( 'id' == $key ) {
567 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
571 if ( isset( $this->data->$key ) ) {
572 $value = $this->data->$key;
574 if ( isset( self::$back_compat_keys[ $key ] ) )
575 $key = self::$back_compat_keys[ $key ];
576 $value = get_user_meta( $this->ID, $key, true );
579 if ( $this->filter ) {
580 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
587 * Magic method for setting custom fields
591 function __set( $key, $value ) {
592 if ( 'id' == $key ) {
593 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
598 $this->data->$key = $value;
602 * Determine whether the user exists in the database.
607 * @return bool True if user exists in the database, false if not.
610 return ! empty( $this->ID );
614 * Retrieve the value of a property or meta key.
616 * Retrieves from the users and usermeta table.
620 * @param string $key Property
622 function get( $key ) {
623 return $this->__get( $key );
627 * Determine whether a property or meta key is set
629 * Consults the users and usermeta tables.
633 * @param string $key Property
635 function has_prop( $key ) {
636 return $this->__isset( $key );
640 * Set up capability object properties.
642 * Will set the value for the 'cap_key' property to current database table
643 * prefix, followed by 'capabilities'. Will then check to see if the
644 * property matching the 'cap_key' exists and is an array. If so, it will be
650 * @param string $cap_key Optional capability key
652 function _init_caps( $cap_key = '' ) {
655 if ( empty($cap_key) )
656 $this->cap_key = $wpdb->prefix . 'capabilities';
658 $this->cap_key = $cap_key;
660 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
662 if ( ! is_array( $this->caps ) )
663 $this->caps = array();
665 $this->get_role_caps();
669 * Retrieve all of the role capabilities and merge with individual capabilities.
671 * All of the capabilities of the roles the user belongs to are merged with
672 * the users individual roles. This also means that the user can be denied
673 * specific roles that their role might have, but the specific user isn't
674 * granted permission to.
680 function get_role_caps() {
683 if ( ! isset( $wp_roles ) )
684 $wp_roles = new WP_Roles();
686 //Filter out caps that are not role names and assign to $this->roles
687 if ( is_array( $this->caps ) )
688 $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
690 //Build $allcaps from role caps, overlay user's $caps
691 $this->allcaps = array();
692 foreach ( (array) $this->roles as $role ) {
693 $the_role = $wp_roles->get_role( $role );
694 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
696 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
702 * Updates the user's meta data option with capabilities and roles.
707 * @param string $role Role name.
709 function add_role( $role ) {
710 $this->caps[$role] = true;
711 update_user_meta( $this->ID, $this->cap_key, $this->caps );
712 $this->get_role_caps();
713 $this->update_user_level_from_caps();
717 * Remove role from user.
722 * @param string $role Role name.
724 function remove_role( $role ) {
725 if ( !in_array($role, $this->roles) )
727 unset( $this->caps[$role] );
728 update_user_meta( $this->ID, $this->cap_key, $this->caps );
729 $this->get_role_caps();
730 $this->update_user_level_from_caps();
734 * Set the role of the user.
736 * This will remove the previous roles of the user and assign the user the
737 * new one. You can set the role to an empty string and it will remove all
738 * of the roles from the user.
743 * @param string $role Role name.
745 function set_role( $role ) {
746 foreach ( (array) $this->roles as $oldrole )
747 unset( $this->caps[$oldrole] );
749 if ( 1 == count( $this->roles ) && $role == $this->roles[0] )
752 if ( !empty( $role ) ) {
753 $this->caps[$role] = true;
754 $this->roles = array( $role => true );
756 $this->roles = false;
758 update_user_meta( $this->ID, $this->cap_key, $this->caps );
759 $this->get_role_caps();
760 $this->update_user_level_from_caps();
761 do_action( 'set_user_role', $this->ID, $role );
765 * Choose the maximum level the user has.
767 * Will compare the level from the $item parameter against the $max
768 * parameter. If the item is incorrect, then just the $max parameter value
771 * Used to get the max level based on the capabilities the user has. This
772 * is also based on roles, so if the user is assigned the Administrator role
773 * then the capability 'level_10' will exist and the user will get that
779 * @param int $max Max level of user.
780 * @param string $item Level capability name.
781 * @return int Max Level.
783 function level_reduction( $max, $item ) {
784 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
785 $level = intval( $matches[1] );
786 return max( $max, $level );
793 * Update the maximum user level for the user.
795 * Updates the 'user_level' user metadata (includes prefix that is the
796 * database table prefix) with the maximum user level. Gets the value from
797 * the all of the capabilities that the user has.
802 function update_user_level_from_caps() {
804 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
805 update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
809 * Add capability and grant or deny access to capability.
814 * @param string $cap Capability name.
815 * @param bool $grant Whether to grant capability to user.
817 function add_cap( $cap, $grant = true ) {
818 $this->caps[$cap] = $grant;
819 update_user_meta( $this->ID, $this->cap_key, $this->caps );
823 * Remove capability from user.
828 * @param string $cap Capability name.
830 function remove_cap( $cap ) {
831 if ( empty( $this->caps[$cap] ) )
833 unset( $this->caps[$cap] );
834 update_user_meta( $this->ID, $this->cap_key, $this->caps );
838 * Remove all of the capabilities of the user.
843 function remove_all_caps() {
845 $this->caps = array();
846 delete_user_meta( $this->ID, $this->cap_key );
847 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
848 $this->get_role_caps();
852 * Whether user has capability or role name.
854 * This is useful for looking up whether the user has a specific role
855 * assigned to the user. The second optional parameter can also be used to
856 * check for capabilities against a specific object, such as a post or user.
861 * @param string|int $cap Capability or role name to search.
862 * @return bool True, if user has capability; false, if user does not have capability.
864 function has_cap( $cap ) {
865 if ( is_numeric( $cap ) ) {
866 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
867 $cap = $this->translate_level_to_cap( $cap );
870 $args = array_slice( func_get_args(), 1 );
871 $args = array_merge( array( $cap, $this->ID ), $args );
872 $caps = call_user_func_array( 'map_meta_cap', $args );
874 // Multisite super admin has all caps by definition, Unless specifically denied.
875 if ( is_multisite() && is_super_admin( $this->ID ) ) {
876 if ( in_array('do_not_allow', $caps) )
881 // Must have ALL requested caps
882 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
883 $capabilities['exist'] = true; // Everyone is allowed to exist
884 foreach ( (array) $caps as $cap ) {
885 if ( empty( $capabilities[ $cap ] ) )
893 * Convert numeric level to level capability name.
895 * Prepends 'level_' to level number.
900 * @param int $level Level number, 1 to 10.
903 function translate_level_to_cap( $level ) {
904 return 'level_' . $level;
908 * Set the blog to operate on. Defaults to the current blog.
912 * @param int $blog_id Optional Blog ID, defaults to current blog.
914 function for_blog( $blog_id = '' ) {
916 if ( ! empty( $blog_id ) )
917 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
920 $this->_init_caps( $cap_key );
925 * Map meta capabilities to primitive capabilities.
927 * This does not actually compare whether the user ID has the actual capability,
928 * just what the capability or capabilities are. Meta capability list value can
929 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
930 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
934 * @param string $cap Capability name.
935 * @param int $user_id User ID.
936 * @return array Actual capabilities for meta capability.
938 function map_meta_cap( $cap, $user_id ) {
939 $args = array_slice( func_get_args(), 2 );
944 $caps[] = 'remove_users';
947 $caps[] = 'promote_users';
950 // Allow user to edit itself
951 if ( isset( $args[0] ) && $user_id == $args[0] )
955 // If multisite these caps are allowed only for super admins.
956 if ( is_multisite() && !is_super_admin( $user_id ) )
957 $caps[] = 'do_not_allow';
959 $caps[] = 'edit_users'; // Explicit due to primitive fall through
963 $author_data = get_userdata( $user_id );
964 $post = get_post( $args[0] );
966 if ( 'revision' == $post->post_type ) {
967 $post = get_post( $post->post_parent );
970 $post_type = get_post_type_object( $post->post_type );
972 if ( ! $post_type->map_meta_cap ) {
973 $caps[] = $post_type->cap->$cap;
974 // Prior to 3.1 we would re-call map_meta_cap here.
975 if ( 'delete_post' == $cap )
976 $cap = $post_type->cap->$cap;
980 if ( '' != $post->post_author ) {
981 $post_author_data = get_userdata( $post->post_author );
983 // No author set yet, so default to current user for cap checks.
984 $post_author_data = $author_data;
987 // If the user is the author...
988 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
989 // If the post is published...
990 if ( 'publish' == $post->post_status ) {
991 $caps[] = $post_type->cap->delete_published_posts;
992 } elseif ( 'trash' == $post->post_status ) {
993 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
994 $caps[] = $post_type->cap->delete_published_posts;
996 // If the post is draft...
997 $caps[] = $post_type->cap->delete_posts;
1000 // The user is trying to edit someone else's post.
1001 $caps[] = $post_type->cap->delete_others_posts;
1002 // The post is published, extra cap required.
1003 if ( 'publish' == $post->post_status )
1004 $caps[] = $post_type->cap->delete_published_posts;
1005 elseif ( 'private' == $post->post_status )
1006 $caps[] = $post_type->cap->delete_private_posts;
1009 // edit_post breaks down to edit_posts, edit_published_posts, or
1010 // edit_others_posts
1013 $author_data = get_userdata( $user_id );
1014 $post = get_post( $args[0] );
1016 if ( 'revision' == $post->post_type ) {
1017 $post = get_post( $post->post_parent );
1020 $post_type = get_post_type_object( $post->post_type );
1022 if ( ! $post_type->map_meta_cap ) {
1023 $caps[] = $post_type->cap->$cap;
1024 // Prior to 3.1 we would re-call map_meta_cap here.
1025 if ( 'edit_post' == $cap )
1026 $cap = $post_type->cap->$cap;
1030 if ( '' != $post->post_author ) {
1031 $post_author_data = get_userdata( $post->post_author );
1033 // No author set yet, so default to current user for cap checks.
1034 $post_author_data = $author_data;
1037 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
1038 // If the user is the author...
1039 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
1040 // If the post is published...
1041 if ( 'publish' == $post->post_status ) {
1042 $caps[] = $post_type->cap->edit_published_posts;
1043 } elseif ( 'trash' == $post->post_status ) {
1044 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
1045 $caps[] = $post_type->cap->edit_published_posts;
1047 // If the post is draft...
1048 $caps[] = $post_type->cap->edit_posts;
1051 // The user is trying to edit someone else's post.
1052 $caps[] = $post_type->cap->edit_others_posts;
1053 // The post is published, extra cap required.
1054 if ( 'publish' == $post->post_status )
1055 $caps[] = $post_type->cap->edit_published_posts;
1056 elseif ( 'private' == $post->post_status )
1057 $caps[] = $post_type->cap->edit_private_posts;
1062 $author_data = get_userdata( $user_id );
1063 $post = get_post( $args[0] );
1065 if ( 'revision' == $post->post_type ) {
1066 $post = get_post( $post->post_parent );
1069 $post_type = get_post_type_object( $post->post_type );
1071 if ( ! $post_type->map_meta_cap ) {
1072 $caps[] = $post_type->cap->$cap;
1073 // Prior to 3.1 we would re-call map_meta_cap here.
1074 if ( 'read_post' == $cap )
1075 $cap = $post_type->cap->$cap;
1079 $status_obj = get_post_status_object( $post->post_status );
1080 if ( $status_obj->public ) {
1081 $caps[] = $post_type->cap->read;
1085 if ( '' != $post->post_author ) {
1086 $post_author_data = get_userdata( $post->post_author );
1088 // No author set yet, so default to current user for cap checks.
1089 $post_author_data = $author_data;
1092 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
1093 $caps[] = $post_type->cap->read;
1094 elseif ( $status_obj->private )
1095 $caps[] = $post_type->cap->read_private_posts;
1097 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1099 case 'edit_post_meta':
1100 case 'delete_post_meta':
1101 case 'add_post_meta':
1102 $post = get_post( $args[0] );
1103 $post_type_object = get_post_type_object( $post->post_type );
1104 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
1106 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1108 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1109 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1112 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1116 case 'edit_comment':
1117 $comment = get_comment( $args[0] );
1118 $post = get_post( $comment->comment_post_ID );
1119 $post_type_object = get_post_type_object( $post->post_type );
1121 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
1123 case 'unfiltered_upload':
1124 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
1127 $caps[] = 'do_not_allow';
1129 case 'unfiltered_html' :
1130 // Disallow unfiltered_html for all users, even admins and super admins.
1131 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
1132 $caps[] = 'do_not_allow';
1133 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1134 $caps[] = 'do_not_allow';
1139 case 'edit_plugins':
1141 if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
1142 $caps[] = 'do_not_allow';
1145 // Fall through if not DISALLOW_FILE_EDIT.
1146 case 'update_plugins':
1147 case 'delete_plugins':
1148 case 'install_plugins':
1149 case 'update_themes':
1150 case 'delete_themes':
1151 case 'install_themes':
1153 // Disallow anything that creates, deletes, or edits core, plugin, or theme files.
1154 // Files in uploads are excepted.
1155 if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
1156 $caps[] = 'do_not_allow';
1159 // Fall through if not DISALLOW_FILE_MODS.
1161 case 'delete_users':
1162 // If multisite these caps are allowed only for super admins.
1163 if ( is_multisite() && !is_super_admin( $user_id ) ) {
1164 $caps[] = 'do_not_allow';
1166 if ( 'delete_user' == $cap )
1167 $cap = 'delete_users';
1171 case 'create_users':
1172 if ( !is_multisite() )
1174 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1177 $caps[] = 'do_not_allow';
1180 // Handle meta capabilities for custom post types.
1181 $post_type_meta_caps = _post_type_meta_capabilities();
1182 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1183 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1184 return call_user_func_array( 'map_meta_cap', $args );
1187 // If no meta caps match, return the original cap.
1191 return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
1195 * Whether current user has capability or role.
1199 * @param string $capability Capability or role name.
1202 function current_user_can( $capability ) {
1203 $current_user = wp_get_current_user();
1205 if ( empty( $current_user ) )
1208 $args = array_slice( func_get_args(), 1 );
1209 $args = array_merge( array( $capability ), $args );
1211 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
1215 * Whether current user has a capability or role for a given blog.
1219 * @param int $blog_id Blog ID
1220 * @param string $capability Capability or role name.
1223 function current_user_can_for_blog( $blog_id, $capability ) {
1224 $current_user = wp_get_current_user();
1226 if ( empty( $current_user ) )
1229 // Create new object to avoid stomping the global current_user.
1230 $user = new WP_User( $current_user->ID) ;
1232 // Set the blog id. @todo add blog id arg to WP_User constructor?
1233 $user->for_blog( $blog_id );
1235 $args = array_slice( func_get_args(), 2 );
1236 $args = array_merge( array( $capability ), $args );
1238 return call_user_func_array( array( &$user, 'has_cap' ), $args );
1242 * Whether author of supplied post has capability or role.
1246 * @param int|object $post Post ID or post object.
1247 * @param string $capability Capability or role name.
1250 function author_can( $post, $capability ) {
1251 if ( !$post = get_post($post) )
1254 $author = new WP_User( $post->post_author );
1256 if ( empty( $author->ID ) )
1259 $args = array_slice( func_get_args(), 2 );
1260 $args = array_merge( array( $capability ), $args );
1262 return call_user_func_array( array( &$author, 'has_cap' ), $args );
1266 * Whether a particular user has capability or role.
1270 * @param int|object $user User ID or object.
1271 * @param string $capability Capability or role name.
1274 function user_can( $user, $capability ) {
1275 if ( ! is_object( $user ) )
1276 $user = new WP_User( $user );
1278 if ( ! $user || ! $user->exists() )
1281 $args = array_slice( func_get_args(), 2 );
1282 $args = array_merge( array( $capability ), $args );
1284 return call_user_func_array( array( &$user, 'has_cap' ), $args );
1288 * Retrieve role object.
1290 * @see WP_Roles::get_role() Uses method to retrieve role object.
1293 * @param string $role Role name.
1296 function get_role( $role ) {
1299 if ( ! isset( $wp_roles ) )
1300 $wp_roles = new WP_Roles();
1302 return $wp_roles->get_role( $role );
1306 * Add role, if it does not exist.
1308 * @see WP_Roles::add_role() Uses method to add role.
1311 * @param string $role Role name.
1312 * @param string $display_name Display name for role.
1313 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1314 * @return null|WP_Role WP_Role object if role is added, null if already exists.
1316 function add_role( $role, $display_name, $capabilities = array() ) {
1319 if ( ! isset( $wp_roles ) )
1320 $wp_roles = new WP_Roles();
1322 return $wp_roles->add_role( $role, $display_name, $capabilities );
1326 * Remove role, if it exists.
1328 * @see WP_Roles::remove_role() Uses method to remove role.
1331 * @param string $role Role name.
1334 function remove_role( $role ) {
1337 if ( ! isset( $wp_roles ) )
1338 $wp_roles = new WP_Roles();
1340 return $wp_roles->remove_role( $role );
1344 * Retrieve a list of super admins.
1348 * @uses $super_admins Super admins global variable, if set.
1350 * @return array List of super admin logins
1352 function get_super_admins() {
1353 global $super_admins;
1355 if ( isset($super_admins) )
1356 return $super_admins;
1358 return get_site_option( 'site_admins', array('admin') );
1362 * Determine if user is a site admin.
1366 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1367 * @return bool True if the user is a site admin.
1369 function is_super_admin( $user_id = false ) {
1371 $user = new WP_User( $user_id );
1373 $user = wp_get_current_user();
1375 if ( ! $user->exists() )
1378 if ( is_multisite() ) {
1379 $super_admins = get_super_admins();
1380 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1383 if ( $user->has_cap('delete_users') )