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->get_blog_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_keys( $this->roles ) as $role ) {
112 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
113 $this->role_names[$role] = $this->roles[$role]['name'];
118 * Reinitialize the object
120 * Recreates the role objects. This is typically called only by switch_to_blog()
121 * after switching wpdb to a new blog ID.
127 // There is no need to reinit if using the wp_user_roles global.
128 if ( ! $this->use_db )
131 global $wpdb, $wp_user_roles;
133 // Duplicated from _init() to avoid an extra function call.
134 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
135 $this->roles = get_option( $this->role_key );
136 if ( empty( $this->roles ) )
139 $this->role_objects = array();
140 $this->role_names = array();
141 foreach ( array_keys( $this->roles ) as $role ) {
142 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
143 $this->role_names[$role] = $this->roles[$role]['name'];
148 * Add role name with capabilities to list.
150 * Updates the list of roles, if the role doesn't already exist.
152 * The capabilities are defined in the following format `array( 'read' => true );`
153 * To explicitly deny a role a capability you set the value for that capability to false.
158 * @param string $role Role name.
159 * @param string $display_name Role display name.
160 * @param array $capabilities List of role capabilities in the above format.
161 * @return WP_Role|null WP_Role object if role is added, null if already exists.
163 function add_role( $role, $display_name, $capabilities = array() ) {
164 if ( isset( $this->roles[$role] ) )
167 $this->roles[$role] = array(
168 'name' => $display_name,
169 'capabilities' => $capabilities
172 update_option( $this->role_key, $this->roles );
173 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
174 $this->role_names[$role] = $display_name;
175 return $this->role_objects[$role];
179 * Remove role by name.
184 * @param string $role Role name.
186 function remove_role( $role ) {
187 if ( ! isset( $this->role_objects[$role] ) )
190 unset( $this->role_objects[$role] );
191 unset( $this->role_names[$role] );
192 unset( $this->roles[$role] );
195 update_option( $this->role_key, $this->roles );
197 if ( get_option( 'default_role' ) == $role )
198 update_option( 'default_role', 'subscriber' );
202 * Add capability to role.
207 * @param string $role Role name.
208 * @param string $cap Capability name.
209 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
211 function add_cap( $role, $cap, $grant = true ) {
212 if ( ! isset( $this->roles[$role] ) )
215 $this->roles[$role]['capabilities'][$cap] = $grant;
217 update_option( $this->role_key, $this->roles );
221 * Remove capability from role.
226 * @param string $role Role name.
227 * @param string $cap Capability name.
229 function remove_cap( $role, $cap ) {
230 if ( ! isset( $this->roles[$role] ) )
233 unset( $this->roles[$role]['capabilities'][$cap] );
235 update_option( $this->role_key, $this->roles );
239 * Retrieve role object by name.
244 * @param string $role Role name.
245 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
247 function get_role( $role ) {
248 if ( isset( $this->role_objects[$role] ) )
249 return $this->role_objects[$role];
255 * Retrieve list of role names.
260 * @return array List of role names.
262 function get_names() {
263 return $this->role_names;
267 * Whether role name is currently in the list of available roles.
272 * @param string $role Role name to look up.
275 function is_role( $role ) {
276 return isset( $this->role_names[$role] );
281 * WordPress Role class.
298 * List of capabilities the role contains.
307 * Constructor - Set up object properties.
309 * The list of capabilities, must have the key as the name of the capability
310 * and the value a boolean of whether it is granted to the role.
315 * @param string $role Role name.
316 * @param array $capabilities List of capabilities.
318 function __construct( $role, $capabilities ) {
320 $this->capabilities = $capabilities;
324 * Assign role a capability.
326 * @see WP_Roles::add_cap() Method uses implementation for role.
330 * @param string $cap Capability name.
331 * @param bool $grant Whether role has capability privilege.
333 function add_cap( $cap, $grant = true ) {
336 if ( ! isset( $wp_roles ) )
337 $wp_roles = new WP_Roles();
339 $this->capabilities[$cap] = $grant;
340 $wp_roles->add_cap( $this->name, $cap, $grant );
344 * Remove capability from role.
346 * This is a container for {@link WP_Roles::remove_cap()} to remove the
347 * capability from the role. That is to say, that {@link
348 * WP_Roles::remove_cap()} implements the functionality, but it also makes
349 * sense to use this class, because you don't need to enter the role name.
354 * @param string $cap Capability name.
356 function remove_cap( $cap ) {
359 if ( ! isset( $wp_roles ) )
360 $wp_roles = new WP_Roles();
362 unset( $this->capabilities[$cap] );
363 $wp_roles->remove_cap( $this->name, $cap );
367 * Whether role has capability.
369 * The capabilities is passed through the 'role_has_cap' filter. The first
370 * parameter for the hook is the list of capabilities the class has
371 * assigned. The second parameter is the capability name to look for. The
372 * third and final parameter for the hook is the role name.
377 * @param string $cap Capability name.
378 * @return bool True, if user has capability. False, if doesn't have capability.
380 function has_cap( $cap ) {
381 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
382 if ( !empty( $capabilities[$cap] ) )
383 return $capabilities[$cap];
391 * WordPress User class.
399 * User data container.
417 * The individual capabilities the user has been given.
426 * User metadata option name.
435 * The roles the user is part of.
441 var $roles = array();
444 * All capabilities the user has, including individual and role based.
450 var $allcaps = array();
453 * The filter context applied to user data fields.
461 private static $back_compat_keys;
466 * Retrieves the userdata and passes it to {@link WP_User::init()}.
471 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
472 * @param string $name Optional. User's username
473 * @param int $blog_id Optional Blog ID, defaults to current blog.
476 function __construct( $id = 0, $name = '', $blog_id = '' ) {
477 if ( ! isset( self::$back_compat_keys ) ) {
478 $prefix = $GLOBALS['wpdb']->prefix;
479 self::$back_compat_keys = array(
480 'user_firstname' => 'first_name',
481 'user_lastname' => 'last_name',
482 'user_description' => 'description',
483 'user_level' => $prefix . 'user_level',
484 $prefix . 'usersettings' => $prefix . 'user-settings',
485 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
489 if ( is_a( $id, 'WP_User' ) ) {
490 $this->init( $id->data, $blog_id );
492 } elseif ( is_object( $id ) ) {
493 $this->init( $id, $blog_id );
497 if ( ! empty( $id ) && ! is_numeric( $id ) ) {
503 $data = self::get_data_by( 'id', $id );
505 $data = self::get_data_by( 'login', $name );
508 $this->init( $data, $blog_id );
512 * Sets up object properties, including capabilities.
514 * @param object $data User DB row object
515 * @param int $blog_id Optional. The blog id to initialize for
517 function init( $data, $blog_id = '' ) {
519 $this->ID = (int) $data->ID;
521 $this->for_blog( $blog_id );
525 * Return only the main user fields
529 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
530 * @param string|int $value The field value
531 * @return object Raw user object
533 static function get_data_by( $field, $value ) {
536 if ( 'id' == $field ) {
537 // Make sure the value is numeric to avoid casting objects, for example,
539 if ( ! is_numeric( $value ) )
541 $value = intval( $value );
545 $value = trim( $value );
557 $user_id = wp_cache_get($value, 'userslugs');
558 $db_field = 'user_nicename';
561 $user_id = wp_cache_get($value, 'useremail');
562 $db_field = 'user_email';
565 $value = sanitize_user( $value );
566 $user_id = wp_cache_get($value, 'userlogins');
567 $db_field = 'user_login';
573 if ( false !== $user_id ) {
574 if ( $user = wp_cache_get( $user_id, 'users' ) )
578 if ( !$user = $wpdb->get_row( $wpdb->prepare(
579 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
583 update_user_caches( $user );
589 * Magic method for checking the existence of a certain custom field
593 function __isset( $key ) {
594 if ( 'id' == $key ) {
595 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
599 if ( isset( $this->data->$key ) )
602 if ( isset( self::$back_compat_keys[ $key ] ) )
603 $key = self::$back_compat_keys[ $key ];
605 return metadata_exists( 'user', $this->ID, $key );
609 * Magic method for accessing custom fields
613 function __get( $key ) {
614 if ( 'id' == $key ) {
615 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
619 if ( isset( $this->data->$key ) ) {
620 $value = $this->data->$key;
622 if ( isset( self::$back_compat_keys[ $key ] ) )
623 $key = self::$back_compat_keys[ $key ];
624 $value = get_user_meta( $this->ID, $key, true );
627 if ( $this->filter ) {
628 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
635 * Magic method for setting custom fields
639 function __set( $key, $value ) {
640 if ( 'id' == $key ) {
641 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
646 $this->data->$key = $value;
650 * Determine whether the user exists in the database.
655 * @return bool True if user exists in the database, false if not.
658 return ! empty( $this->ID );
662 * Retrieve the value of a property or meta key.
664 * Retrieves from the users and usermeta table.
668 * @param string $key Property
670 function get( $key ) {
671 return $this->__get( $key );
675 * Determine whether a property or meta key is set
677 * Consults the users and usermeta tables.
681 * @param string $key Property
683 function has_prop( $key ) {
684 return $this->__isset( $key );
688 * Return an array representation.
692 * @return array Array representation.
694 function to_array() {
695 return get_object_vars( $this->data );
699 * Set up capability object properties.
701 * Will set the value for the 'cap_key' property to current database table
702 * prefix, followed by 'capabilities'. Will then check to see if the
703 * property matching the 'cap_key' exists and is an array. If so, it will be
709 * @param string $cap_key Optional capability key
711 function _init_caps( $cap_key = '' ) {
714 if ( empty($cap_key) )
715 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
717 $this->cap_key = $cap_key;
719 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
721 if ( ! is_array( $this->caps ) )
722 $this->caps = array();
724 $this->get_role_caps();
728 * Retrieve all of the role capabilities and merge with individual capabilities.
730 * All of the capabilities of the roles the user belongs to are merged with
731 * the users individual roles. This also means that the user can be denied
732 * specific roles that their role might have, but the specific user isn't
733 * granted permission to.
739 * @return array List of all capabilities for the user.
741 function get_role_caps() {
744 if ( ! isset( $wp_roles ) )
745 $wp_roles = new WP_Roles();
747 //Filter out caps that are not role names and assign to $this->roles
748 if ( is_array( $this->caps ) )
749 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
751 //Build $allcaps from role caps, overlay user's $caps
752 $this->allcaps = array();
753 foreach ( (array) $this->roles as $role ) {
754 $the_role = $wp_roles->get_role( $role );
755 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
757 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
759 return $this->allcaps;
765 * Updates the user's meta data option with capabilities and roles.
770 * @param string $role Role name.
772 function add_role( $role ) {
773 $this->caps[$role] = true;
774 update_user_meta( $this->ID, $this->cap_key, $this->caps );
775 $this->get_role_caps();
776 $this->update_user_level_from_caps();
780 * Remove role from user.
785 * @param string $role Role name.
787 function remove_role( $role ) {
788 if ( !in_array($role, $this->roles) )
790 unset( $this->caps[$role] );
791 update_user_meta( $this->ID, $this->cap_key, $this->caps );
792 $this->get_role_caps();
793 $this->update_user_level_from_caps();
797 * Set the role of the user.
799 * This will remove the previous roles of the user and assign the user the
800 * new one. You can set the role to an empty string and it will remove all
801 * of the roles from the user.
806 * @param string $role Role name.
808 function set_role( $role ) {
809 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
812 foreach ( (array) $this->roles as $oldrole )
813 unset( $this->caps[$oldrole] );
815 $old_roles = $this->roles;
816 if ( !empty( $role ) ) {
817 $this->caps[$role] = true;
818 $this->roles = array( $role => true );
820 $this->roles = false;
822 update_user_meta( $this->ID, $this->cap_key, $this->caps );
823 $this->get_role_caps();
824 $this->update_user_level_from_caps();
825 do_action( 'set_user_role', $this->ID, $role, $old_roles );
829 * Choose the maximum level the user has.
831 * Will compare the level from the $item parameter against the $max
832 * parameter. If the item is incorrect, then just the $max parameter value
835 * Used to get the max level based on the capabilities the user has. This
836 * is also based on roles, so if the user is assigned the Administrator role
837 * then the capability 'level_10' will exist and the user will get that
843 * @param int $max Max level of user.
844 * @param string $item Level capability name.
845 * @return int Max Level.
847 function level_reduction( $max, $item ) {
848 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
849 $level = intval( $matches[1] );
850 return max( $max, $level );
857 * Update the maximum user level for the user.
859 * Updates the 'user_level' user metadata (includes prefix that is the
860 * database table prefix) with the maximum user level. Gets the value from
861 * the all of the capabilities that the user has.
866 function update_user_level_from_caps() {
868 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
869 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
873 * Add capability and grant or deny access to capability.
878 * @param string $cap Capability name.
879 * @param bool $grant Whether to grant capability to user.
881 function add_cap( $cap, $grant = true ) {
882 $this->caps[$cap] = $grant;
883 update_user_meta( $this->ID, $this->cap_key, $this->caps );
887 * Remove capability from user.
892 * @param string $cap Capability name.
894 function remove_cap( $cap ) {
895 if ( ! isset( $this->caps[$cap] ) )
897 unset( $this->caps[$cap] );
898 update_user_meta( $this->ID, $this->cap_key, $this->caps );
902 * Remove all of the capabilities of the user.
907 function remove_all_caps() {
909 $this->caps = array();
910 delete_user_meta( $this->ID, $this->cap_key );
911 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
912 $this->get_role_caps();
916 * Whether user has capability or role name.
918 * This is useful for looking up whether the user has a specific role
919 * assigned to the user. The second optional parameter can also be used to
920 * check for capabilities against a specific object, such as a post or user.
925 * @param string|int $cap Capability or role name to search.
926 * @return bool True, if user has capability; false, if user does not have capability.
928 function has_cap( $cap ) {
929 if ( is_numeric( $cap ) ) {
930 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
931 $cap = $this->translate_level_to_cap( $cap );
934 $args = array_slice( func_get_args(), 1 );
935 $args = array_merge( array( $cap, $this->ID ), $args );
936 $caps = call_user_func_array( 'map_meta_cap', $args );
938 // Multisite super admin has all caps by definition, Unless specifically denied.
939 if ( is_multisite() && is_super_admin( $this->ID ) ) {
940 if ( in_array('do_not_allow', $caps) )
945 // Must have ALL requested caps
946 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
947 $capabilities['exist'] = true; // Everyone is allowed to exist
948 foreach ( (array) $caps as $cap ) {
949 if ( empty( $capabilities[ $cap ] ) )
957 * Convert numeric level to level capability name.
959 * Prepends 'level_' to level number.
964 * @param int $level Level number, 1 to 10.
967 function translate_level_to_cap( $level ) {
968 return 'level_' . $level;
972 * Set the blog to operate on. Defaults to the current blog.
976 * @param int $blog_id Optional Blog ID, defaults to current blog.
978 function for_blog( $blog_id = '' ) {
980 if ( ! empty( $blog_id ) )
981 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
984 $this->_init_caps( $cap_key );
989 * Map meta capabilities to primitive capabilities.
991 * This does not actually compare whether the user ID has the actual capability,
992 * just what the capability or capabilities are. Meta capability list value can
993 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
994 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
998 * @param string $cap Capability name.
999 * @param int $user_id User ID.
1000 * @return array Actual capabilities for meta capability.
1002 function map_meta_cap( $cap, $user_id ) {
1003 $args = array_slice( func_get_args(), 2 );
1008 $caps[] = 'remove_users';
1010 case 'promote_user':
1011 $caps[] = 'promote_users';
1015 // Allow user to edit itself
1016 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
1019 // If multisite these caps are allowed only for super admins.
1020 if ( is_multisite() && !is_super_admin( $user_id ) )
1021 $caps[] = 'do_not_allow';
1023 $caps[] = 'edit_users'; // edit_user maps to edit_users.
1027 $post = get_post( $args[0] );
1029 if ( 'revision' == $post->post_type ) {
1030 $post = get_post( $post->post_parent );
1033 $post_type = get_post_type_object( $post->post_type );
1035 if ( ! $post_type->map_meta_cap ) {
1036 $caps[] = $post_type->cap->$cap;
1037 // Prior to 3.1 we would re-call map_meta_cap here.
1038 if ( 'delete_post' == $cap )
1039 $cap = $post_type->cap->$cap;
1043 $post_author_id = $post->post_author;
1045 // If no author set yet, default to current user for cap checks.
1046 if ( ! $post_author_id )
1047 $post_author_id = $user_id;
1049 // If the user is the author...
1050 if ( $user_id == $post_author_id ) {
1051 // If the post is published...
1052 if ( 'publish' == $post->post_status ) {
1053 $caps[] = $post_type->cap->delete_published_posts;
1054 } elseif ( 'trash' == $post->post_status ) {
1055 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
1056 $caps[] = $post_type->cap->delete_published_posts;
1058 // If the post is draft...
1059 $caps[] = $post_type->cap->delete_posts;
1062 // The user is trying to edit someone else's post.
1063 $caps[] = $post_type->cap->delete_others_posts;
1064 // The post is published, extra cap required.
1065 if ( 'publish' == $post->post_status )
1066 $caps[] = $post_type->cap->delete_published_posts;
1067 elseif ( 'private' == $post->post_status )
1068 $caps[] = $post_type->cap->delete_private_posts;
1071 // edit_post breaks down to edit_posts, edit_published_posts, or
1072 // edit_others_posts
1075 $post = get_post( $args[0] );
1076 if ( empty( $post ) )
1079 if ( 'revision' == $post->post_type ) {
1080 $post = get_post( $post->post_parent );
1083 $post_type = get_post_type_object( $post->post_type );
1085 if ( ! $post_type->map_meta_cap ) {
1086 $caps[] = $post_type->cap->$cap;
1087 // Prior to 3.1 we would re-call map_meta_cap here.
1088 if ( 'edit_post' == $cap )
1089 $cap = $post_type->cap->$cap;
1093 $post_author_id = $post->post_author;
1095 // If no author set yet, default to current user for cap checks.
1096 if ( ! $post_author_id )
1097 $post_author_id = $user_id;
1099 // If the user is the author...
1100 if ( $user_id == $post_author_id ) {
1101 // If the post is published...
1102 if ( 'publish' == $post->post_status ) {
1103 $caps[] = $post_type->cap->edit_published_posts;
1104 } elseif ( 'trash' == $post->post_status ) {
1105 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
1106 $caps[] = $post_type->cap->edit_published_posts;
1108 // If the post is draft...
1109 $caps[] = $post_type->cap->edit_posts;
1112 // The user is trying to edit someone else's post.
1113 $caps[] = $post_type->cap->edit_others_posts;
1114 // The post is published, extra cap required.
1115 if ( 'publish' == $post->post_status )
1116 $caps[] = $post_type->cap->edit_published_posts;
1117 elseif ( 'private' == $post->post_status )
1118 $caps[] = $post_type->cap->edit_private_posts;
1123 $post = get_post( $args[0] );
1125 if ( 'revision' == $post->post_type ) {
1126 $post = get_post( $post->post_parent );
1129 $post_type = get_post_type_object( $post->post_type );
1131 if ( ! $post_type->map_meta_cap ) {
1132 $caps[] = $post_type->cap->$cap;
1133 // Prior to 3.1 we would re-call map_meta_cap here.
1134 if ( 'read_post' == $cap )
1135 $cap = $post_type->cap->$cap;
1139 $status_obj = get_post_status_object( $post->post_status );
1140 if ( $status_obj->public ) {
1141 $caps[] = $post_type->cap->read;
1145 $post_author_id = $post->post_author;
1147 // If no author set yet, default to current user for cap checks.
1148 if ( ! $post_author_id )
1149 $post_author_id = $user_id;
1151 if ( $user_id == $post_author_id )
1152 $caps[] = $post_type->cap->read;
1153 elseif ( $status_obj->private )
1154 $caps[] = $post_type->cap->read_private_posts;
1156 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1158 case 'publish_post':
1159 $post = get_post( $args[0] );
1160 $post_type = get_post_type_object( $post->post_type );
1162 $caps[] = $post_type->cap->publish_posts;
1164 case 'edit_post_meta':
1165 case 'delete_post_meta':
1166 case 'add_post_meta':
1167 $post = get_post( $args[0] );
1168 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1170 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1172 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1173 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1176 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1180 case 'edit_comment':
1181 $comment = get_comment( $args[0] );
1182 if ( empty( $comment ) )
1184 $post = get_post( $comment->comment_post_ID );
1185 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1187 case 'unfiltered_upload':
1188 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
1191 $caps[] = 'do_not_allow';
1193 case 'unfiltered_html' :
1194 // Disallow unfiltered_html for all users, even admins and super admins.
1195 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
1196 $caps[] = 'do_not_allow';
1197 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1198 $caps[] = 'do_not_allow';
1203 case 'edit_plugins':
1205 // Disallow the file editors.
1206 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
1207 $caps[] = 'do_not_allow';
1208 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1209 $caps[] = 'do_not_allow';
1210 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1211 $caps[] = 'do_not_allow';
1215 case 'update_plugins':
1216 case 'delete_plugins':
1217 case 'install_plugins':
1218 case 'update_themes':
1219 case 'delete_themes':
1220 case 'install_themes':
1222 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
1223 // Files in uploads are excepted.
1224 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1225 $caps[] = 'do_not_allow';
1226 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1227 $caps[] = 'do_not_allow';
1231 case 'activate_plugins':
1233 if ( is_multisite() ) {
1234 // update_, install_, and delete_ are handled above with is_super_admin().
1235 $menu_perms = get_site_option( 'menu_items', array() );
1236 if ( empty( $menu_perms['plugins'] ) )
1237 $caps[] = 'manage_network_plugins';
1241 case 'delete_users':
1242 // If multisite only super admins can delete users.
1243 if ( is_multisite() && ! is_super_admin( $user_id ) )
1244 $caps[] = 'do_not_allow';
1246 $caps[] = 'delete_users'; // delete_user maps to delete_users.
1248 case 'create_users':
1249 if ( !is_multisite() )
1251 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1254 $caps[] = 'do_not_allow';
1256 case 'manage_links' :
1257 if ( get_option( 'link_manager_enabled' ) )
1260 $caps[] = 'do_not_allow';
1263 // Handle meta capabilities for custom post types.
1264 $post_type_meta_caps = _post_type_meta_capabilities();
1265 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1266 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1267 return call_user_func_array( 'map_meta_cap', $args );
1270 // If no meta caps match, return the original cap.
1274 return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
1278 * Whether current user has capability or role.
1282 * @param string $capability Capability or role name.
1285 function current_user_can( $capability ) {
1286 $current_user = wp_get_current_user();
1288 if ( empty( $current_user ) )
1291 $args = array_slice( func_get_args(), 1 );
1292 $args = array_merge( array( $capability ), $args );
1294 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
1298 * Whether current user has a capability or role for a given blog.
1302 * @param int $blog_id Blog ID
1303 * @param string $capability Capability or role name.
1306 function current_user_can_for_blog( $blog_id, $capability ) {
1307 if ( is_multisite() )
1308 switch_to_blog( $blog_id );
1310 $current_user = wp_get_current_user();
1312 if ( empty( $current_user ) )
1315 $args = array_slice( func_get_args(), 2 );
1316 $args = array_merge( array( $capability ), $args );
1318 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
1320 if ( is_multisite() )
1321 restore_current_blog();
1327 * Whether author of supplied post has capability or role.
1331 * @param int|object $post Post ID or post object.
1332 * @param string $capability Capability or role name.
1335 function author_can( $post, $capability ) {
1336 if ( !$post = get_post($post) )
1339 $author = get_userdata( $post->post_author );
1344 $args = array_slice( func_get_args(), 2 );
1345 $args = array_merge( array( $capability ), $args );
1347 return call_user_func_array( array( $author, 'has_cap' ), $args );
1351 * Whether a particular user has capability or role.
1355 * @param int|object $user User ID or object.
1356 * @param string $capability Capability or role name.
1359 function user_can( $user, $capability ) {
1360 if ( ! is_object( $user ) )
1361 $user = get_userdata( $user );
1363 if ( ! $user || ! $user->exists() )
1366 $args = array_slice( func_get_args(), 2 );
1367 $args = array_merge( array( $capability ), $args );
1369 return call_user_func_array( array( $user, 'has_cap' ), $args );
1373 * Retrieve role object.
1375 * @see WP_Roles::get_role() Uses method to retrieve role object.
1378 * @param string $role Role name.
1379 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
1381 function get_role( $role ) {
1384 if ( ! isset( $wp_roles ) )
1385 $wp_roles = new WP_Roles();
1387 return $wp_roles->get_role( $role );
1391 * Add role, if it does not exist.
1393 * @see WP_Roles::add_role() Uses method to add role.
1396 * @param string $role Role name.
1397 * @param string $display_name Display name for role.
1398 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1399 * @return WP_Role|null WP_Role object if role is added, null if already exists.
1401 function add_role( $role, $display_name, $capabilities = array() ) {
1404 if ( ! isset( $wp_roles ) )
1405 $wp_roles = new WP_Roles();
1407 return $wp_roles->add_role( $role, $display_name, $capabilities );
1411 * Remove role, if it exists.
1413 * @see WP_Roles::remove_role() Uses method to remove role.
1416 * @param string $role Role name.
1418 function remove_role( $role ) {
1421 if ( ! isset( $wp_roles ) )
1422 $wp_roles = new WP_Roles();
1424 $wp_roles->remove_role( $role );
1428 * Retrieve a list of super admins.
1432 * @uses $super_admins Super admins global variable, if set.
1434 * @return array List of super admin logins
1436 function get_super_admins() {
1437 global $super_admins;
1439 if ( isset($super_admins) )
1440 return $super_admins;
1442 return get_site_option( 'site_admins', array('admin') );
1446 * Determine if user is a site admin.
1450 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1451 * @return bool True if the user is a site admin.
1453 function is_super_admin( $user_id = false ) {
1454 if ( ! $user_id || $user_id == get_current_user_id() )
1455 $user = wp_get_current_user();
1457 $user = get_userdata( $user_id );
1459 if ( ! $user || ! $user->exists() )
1462 if ( is_multisite() ) {
1463 $super_admins = get_super_admins();
1464 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1467 if ( $user->has_cap('delete_users') )