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.
17 * 'rolename' => array (
18 * 'name' => 'rolename',
19 * 'capabilities' => array()
29 * List of roles and capabilities.
38 * List of the role objects.
44 public $role_objects = array();
53 public $role_names = array();
56 * Option name for storing role list.
65 * Whether to use the database for retrieval and storage.
71 public $use_db = true;
78 public function __construct() {
83 * Make private/protected methods readable for backwards compatibility.
88 * @param callable $name Method to call.
89 * @param array $arguments Arguments to pass when calling.
90 * @return mixed|bool Return value of the callback, false otherwise.
92 public function __call( $name, $arguments ) {
93 return call_user_func_array( array( $this, $name ), $arguments );
97 * Set up the object properties.
99 * The role key is set to the current prefix for the $wpdb object with
100 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
101 * be used and the role option will not be updated or used.
106 * @global wpdb $wpdb WordPress database abstraction object.
107 * @global array $wp_user_roles Used to set the 'roles' property value.
109 protected function _init() {
110 global $wpdb, $wp_user_roles;
111 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
112 if ( ! empty( $wp_user_roles ) ) {
113 $this->roles = $wp_user_roles;
114 $this->use_db = false;
116 $this->roles = get_option( $this->role_key );
119 if ( empty( $this->roles ) )
122 $this->role_objects = array();
123 $this->role_names = array();
124 foreach ( array_keys( $this->roles ) as $role ) {
125 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
126 $this->role_names[$role] = $this->roles[$role]['name'];
131 * Reinitialize the object
133 * Recreates the role objects. This is typically called only by switch_to_blog()
134 * after switching wpdb to a new blog ID.
139 public function reinit() {
140 // There is no need to reinit if using the wp_user_roles global.
141 if ( ! $this->use_db )
146 // Duplicated from _init() to avoid an extra function call.
147 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
148 $this->roles = get_option( $this->role_key );
149 if ( empty( $this->roles ) )
152 $this->role_objects = array();
153 $this->role_names = array();
154 foreach ( array_keys( $this->roles ) as $role ) {
155 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
156 $this->role_names[$role] = $this->roles[$role]['name'];
161 * Add role name with capabilities to list.
163 * Updates the list of roles, if the role doesn't already exist.
165 * The capabilities are defined in the following format `array( 'read' => true );`
166 * To explicitly deny a role a capability you set the value for that capability to false.
171 * @param string $role Role name.
172 * @param string $display_name Role display name.
173 * @param array $capabilities List of role capabilities in the above format.
174 * @return WP_Role|null WP_Role object if role is added, null if already exists.
176 public function add_role( $role, $display_name, $capabilities = array() ) {
177 if ( isset( $this->roles[$role] ) )
180 $this->roles[$role] = array(
181 'name' => $display_name,
182 'capabilities' => $capabilities
185 update_option( $this->role_key, $this->roles );
186 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
187 $this->role_names[$role] = $display_name;
188 return $this->role_objects[$role];
192 * Remove role by name.
197 * @param string $role Role name.
199 public function remove_role( $role ) {
200 if ( ! isset( $this->role_objects[$role] ) )
203 unset( $this->role_objects[$role] );
204 unset( $this->role_names[$role] );
205 unset( $this->roles[$role] );
208 update_option( $this->role_key, $this->roles );
210 if ( get_option( 'default_role' ) == $role )
211 update_option( 'default_role', 'subscriber' );
215 * Add capability to role.
220 * @param string $role Role name.
221 * @param string $cap Capability name.
222 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
224 public function add_cap( $role, $cap, $grant = true ) {
225 if ( ! isset( $this->roles[$role] ) )
228 $this->roles[$role]['capabilities'][$cap] = $grant;
230 update_option( $this->role_key, $this->roles );
234 * Remove capability from role.
239 * @param string $role Role name.
240 * @param string $cap Capability name.
242 public function remove_cap( $role, $cap ) {
243 if ( ! isset( $this->roles[$role] ) )
246 unset( $this->roles[$role]['capabilities'][$cap] );
248 update_option( $this->role_key, $this->roles );
252 * Retrieve role object by name.
257 * @param string $role Role name.
258 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
260 public function get_role( $role ) {
261 if ( isset( $this->role_objects[$role] ) )
262 return $this->role_objects[$role];
268 * Retrieve list of role names.
273 * @return array List of role names.
275 public function get_names() {
276 return $this->role_names;
280 * Whether role name is currently in the list of available roles.
285 * @param string $role Role name to look up.
288 public function is_role( $role ) {
289 return isset( $this->role_names[$role] );
294 * WordPress Role class.
311 * List of capabilities the role contains.
317 public $capabilities;
320 * Constructor - Set up object properties.
322 * The list of capabilities, must have the key as the name of the capability
323 * and the value a boolean of whether it is granted to the role.
328 * @param string $role Role name.
329 * @param array $capabilities List of capabilities.
331 public function __construct( $role, $capabilities ) {
333 $this->capabilities = $capabilities;
337 * Assign role a capability.
339 * @see WP_Roles::add_cap() Method uses implementation for role.
343 * @param string $cap Capability name.
344 * @param bool $grant Whether role has capability privilege.
346 public function add_cap( $cap, $grant = true ) {
349 if ( ! isset( $wp_roles ) )
350 $wp_roles = new WP_Roles();
352 $this->capabilities[$cap] = $grant;
353 $wp_roles->add_cap( $this->name, $cap, $grant );
357 * Remove capability from role.
359 * This is a container for {@link WP_Roles::remove_cap()} to remove the
360 * capability from the role. That is to say, that {@link
361 * WP_Roles::remove_cap()} implements the functionality, but it also makes
362 * sense to use this class, because you don't need to enter the role name.
367 * @param string $cap Capability name.
369 public function remove_cap( $cap ) {
372 if ( ! isset( $wp_roles ) )
373 $wp_roles = new WP_Roles();
375 unset( $this->capabilities[$cap] );
376 $wp_roles->remove_cap( $this->name, $cap );
380 * Whether role has capability.
382 * The capabilities is passed through the 'role_has_cap' filter. The first
383 * parameter for the hook is the list of capabilities the class has
384 * assigned. The second parameter is the capability name to look for. The
385 * third and final parameter for the hook is the role name.
390 * @param string $cap Capability name.
391 * @return bool True, if user has capability. False, if doesn't have capability.
393 public function has_cap( $cap ) {
395 * Filter which capabilities a role has.
399 * @param array $capabilities Array of role capabilities.
400 * @param string $cap Capability name.
401 * @param string $name Role name.
403 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
404 if ( !empty( $capabilities[$cap] ) )
405 return $capabilities[$cap];
413 * WordPress User class.
421 * User data container.
439 * The individual capabilities the user has been given.
445 public $caps = array();
448 * User metadata option name.
457 * The roles the user is part of.
463 public $roles = array();
466 * All capabilities the user has, including individual and role based.
472 public $allcaps = array();
475 * The filter context applied to user data fields.
483 private static $back_compat_keys;
488 * Retrieves the userdata and passes it to {@link WP_User::init()}.
493 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
494 * @param string $name Optional. User's username
495 * @param int $blog_id Optional Blog ID, defaults to current blog.
498 public function __construct( $id = 0, $name = '', $blog_id = '' ) {
499 if ( ! isset( self::$back_compat_keys ) ) {
500 $prefix = $GLOBALS['wpdb']->prefix;
501 self::$back_compat_keys = array(
502 'user_firstname' => 'first_name',
503 'user_lastname' => 'last_name',
504 'user_description' => 'description',
505 'user_level' => $prefix . 'user_level',
506 $prefix . 'usersettings' => $prefix . 'user-settings',
507 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
511 if ( is_a( $id, 'WP_User' ) ) {
512 $this->init( $id->data, $blog_id );
514 } elseif ( is_object( $id ) ) {
515 $this->init( $id, $blog_id );
519 if ( ! empty( $id ) && ! is_numeric( $id ) ) {
525 $data = self::get_data_by( 'id', $id );
527 $data = self::get_data_by( 'login', $name );
530 $this->init( $data, $blog_id );
534 * Sets up object properties, including capabilities.
536 * @param object $data User DB row object
537 * @param int $blog_id Optional. The blog id to initialize for
539 public function init( $data, $blog_id = '' ) {
541 $this->ID = (int) $data->ID;
543 $this->for_blog( $blog_id );
547 * Return only the main user fields
551 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
552 * @param string|int $value The field value
553 * @return object Raw user object
555 public static function get_data_by( $field, $value ) {
558 if ( 'id' == $field ) {
559 // Make sure the value is numeric to avoid casting objects, for example,
561 if ( ! is_numeric( $value ) )
563 $value = intval( $value );
567 $value = trim( $value );
579 $user_id = wp_cache_get($value, 'userslugs');
580 $db_field = 'user_nicename';
583 $user_id = wp_cache_get($value, 'useremail');
584 $db_field = 'user_email';
587 $value = sanitize_user( $value );
588 $user_id = wp_cache_get($value, 'userlogins');
589 $db_field = 'user_login';
595 if ( false !== $user_id ) {
596 if ( $user = wp_cache_get( $user_id, 'users' ) )
600 if ( !$user = $wpdb->get_row( $wpdb->prepare(
601 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
605 update_user_caches( $user );
611 * Magic method for checking the existence of a certain custom field
617 public function __isset( $key ) {
618 if ( 'id' == $key ) {
619 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
623 if ( isset( $this->data->$key ) )
626 if ( isset( self::$back_compat_keys[ $key ] ) )
627 $key = self::$back_compat_keys[ $key ];
629 return metadata_exists( 'user', $this->ID, $key );
633 * Magic method for accessing custom fields
639 public function __get( $key ) {
640 if ( 'id' == $key ) {
641 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
645 if ( isset( $this->data->$key ) ) {
646 $value = $this->data->$key;
648 if ( isset( self::$back_compat_keys[ $key ] ) )
649 $key = self::$back_compat_keys[ $key ];
650 $value = get_user_meta( $this->ID, $key, true );
653 if ( $this->filter ) {
654 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
661 * Magic method for setting custom fields
665 public function __set( $key, $value ) {
666 if ( 'id' == $key ) {
667 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
672 $this->data->$key = $value;
676 * Determine whether the user exists in the database.
681 * @return bool True if user exists in the database, false if not.
683 public function exists() {
684 return ! empty( $this->ID );
688 * Retrieve the value of a property or meta key.
690 * Retrieves from the users and usermeta table.
694 * @param string $key Property
696 public function get( $key ) {
697 return $this->__get( $key );
701 * Determine whether a property or meta key is set
703 * Consults the users and usermeta tables.
707 * @param string $key Property
709 public function has_prop( $key ) {
710 return $this->__isset( $key );
714 * Return an array representation.
718 * @return array Array representation.
720 public function to_array() {
721 return get_object_vars( $this->data );
725 * Set up capability object properties.
727 * Will set the value for the 'cap_key' property to current database table
728 * prefix, followed by 'capabilities'. Will then check to see if the
729 * property matching the 'cap_key' exists and is an array. If so, it will be
735 * @param string $cap_key Optional capability key
737 function _init_caps( $cap_key = '' ) {
740 if ( empty($cap_key) )
741 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
743 $this->cap_key = $cap_key;
745 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
747 if ( ! is_array( $this->caps ) )
748 $this->caps = array();
750 $this->get_role_caps();
754 * Retrieve all of the role capabilities and merge with individual capabilities.
756 * All of the capabilities of the roles the user belongs to are merged with
757 * the users individual roles. This also means that the user can be denied
758 * specific roles that their role might have, but the specific user isn't
759 * granted permission to.
765 * @return array List of all capabilities for the user.
767 public function get_role_caps() {
770 if ( ! isset( $wp_roles ) )
771 $wp_roles = new WP_Roles();
773 //Filter out caps that are not role names and assign to $this->roles
774 if ( is_array( $this->caps ) )
775 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
777 //Build $allcaps from role caps, overlay user's $caps
778 $this->allcaps = array();
779 foreach ( (array) $this->roles as $role ) {
780 $the_role = $wp_roles->get_role( $role );
781 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
783 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
785 return $this->allcaps;
791 * Updates the user's meta data option with capabilities and roles.
796 * @param string $role Role name.
798 public function add_role( $role ) {
799 $this->caps[$role] = true;
800 update_user_meta( $this->ID, $this->cap_key, $this->caps );
801 $this->get_role_caps();
802 $this->update_user_level_from_caps();
806 * Remove role from user.
811 * @param string $role Role name.
813 public function remove_role( $role ) {
814 if ( !in_array($role, $this->roles) )
816 unset( $this->caps[$role] );
817 update_user_meta( $this->ID, $this->cap_key, $this->caps );
818 $this->get_role_caps();
819 $this->update_user_level_from_caps();
823 * Set the role of the user.
825 * This will remove the previous roles of the user and assign the user the
826 * new one. You can set the role to an empty string and it will remove all
827 * of the roles from the user.
832 * @param string $role Role name.
834 public function set_role( $role ) {
835 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
838 foreach ( (array) $this->roles as $oldrole )
839 unset( $this->caps[$oldrole] );
841 $old_roles = $this->roles;
842 if ( !empty( $role ) ) {
843 $this->caps[$role] = true;
844 $this->roles = array( $role => true );
846 $this->roles = false;
848 update_user_meta( $this->ID, $this->cap_key, $this->caps );
849 $this->get_role_caps();
850 $this->update_user_level_from_caps();
853 * Fires after the user's role has changed.
856 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
858 * @param int $user_id The user ID.
859 * @param string $role The new role.
860 * @param array $old_roles An array of the user's previous roles.
862 do_action( 'set_user_role', $this->ID, $role, $old_roles );
866 * Choose the maximum level the user has.
868 * Will compare the level from the $item parameter against the $max
869 * parameter. If the item is incorrect, then just the $max parameter value
872 * Used to get the max level based on the capabilities the user has. This
873 * is also based on roles, so if the user is assigned the Administrator role
874 * then the capability 'level_10' will exist and the user will get that
880 * @param int $max Max level of user.
881 * @param string $item Level capability name.
882 * @return int Max Level.
884 public function level_reduction( $max, $item ) {
885 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
886 $level = intval( $matches[1] );
887 return max( $max, $level );
894 * Update the maximum user level for the user.
896 * Updates the 'user_level' user metadata (includes prefix that is the
897 * database table prefix) with the maximum user level. Gets the value from
898 * the all of the capabilities that the user has.
903 public function update_user_level_from_caps() {
905 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
906 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
910 * Add capability and grant or deny access to capability.
915 * @param string $cap Capability name.
916 * @param bool $grant Whether to grant capability to user.
918 public function add_cap( $cap, $grant = true ) {
919 $this->caps[$cap] = $grant;
920 update_user_meta( $this->ID, $this->cap_key, $this->caps );
924 * Remove capability from user.
929 * @param string $cap Capability name.
931 public function remove_cap( $cap ) {
932 if ( ! isset( $this->caps[$cap] ) )
934 unset( $this->caps[$cap] );
935 update_user_meta( $this->ID, $this->cap_key, $this->caps );
939 * Remove all of the capabilities of the user.
944 public function remove_all_caps() {
946 $this->caps = array();
947 delete_user_meta( $this->ID, $this->cap_key );
948 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
949 $this->get_role_caps();
953 * Whether user has capability or role name.
955 * This is useful for looking up whether the user has a specific role
956 * assigned to the user. The second optional parameter can also be used to
957 * check for capabilities against a specific object, such as a post or user.
962 * @param string|int $cap Capability or role name to search.
963 * @return bool True, if user has capability; false, if user does not have capability.
965 public function has_cap( $cap ) {
966 if ( is_numeric( $cap ) ) {
967 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
968 $cap = $this->translate_level_to_cap( $cap );
971 $args = array_slice( func_get_args(), 1 );
972 $args = array_merge( array( $cap, $this->ID ), $args );
973 $caps = call_user_func_array( 'map_meta_cap', $args );
975 // Multisite super admin has all caps by definition, Unless specifically denied.
976 if ( is_multisite() && is_super_admin( $this->ID ) ) {
977 if ( in_array('do_not_allow', $caps) )
983 * Dynamically filter a user's capabilities.
986 * @since 3.7.0 Added the user object.
988 * @param array $allcaps An array of all the role's capabilities.
989 * @param array $caps Actual capabilities for meta capability.
990 * @param array $args Optional parameters passed to has_cap(), typically object ID.
991 * @param WP_User $user The user object.
993 // Must have ALL requested caps
994 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
995 $capabilities['exist'] = true; // Everyone is allowed to exist
996 foreach ( (array) $caps as $cap ) {
997 if ( empty( $capabilities[ $cap ] ) )
1005 * Convert numeric level to level capability name.
1007 * Prepends 'level_' to level number.
1012 * @param int $level Level number, 1 to 10.
1015 public function translate_level_to_cap( $level ) {
1016 return 'level_' . $level;
1020 * Set the blog to operate on. Defaults to the current blog.
1024 * @param int $blog_id Optional Blog ID, defaults to current blog.
1026 public function for_blog( $blog_id = '' ) {
1028 if ( ! empty( $blog_id ) )
1029 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
1032 $this->_init_caps( $cap_key );
1037 * Map meta capabilities to primitive capabilities.
1039 * This does not actually compare whether the user ID has the actual capability,
1040 * just what the capability or capabilities are. Meta capability list value can
1041 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
1042 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
1046 * @param string $cap Capability name.
1047 * @param int $user_id User ID.
1048 * @return array Actual capabilities for meta capability.
1050 function map_meta_cap( $cap, $user_id ) {
1051 $args = array_slice( func_get_args(), 2 );
1056 $caps[] = 'remove_users';
1058 case 'promote_user':
1059 $caps[] = 'promote_users';
1063 // Allow user to edit itself
1064 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
1067 // If multisite these caps are allowed only for super admins.
1068 if ( is_multisite() && !is_super_admin( $user_id ) )
1069 $caps[] = 'do_not_allow';
1071 $caps[] = 'edit_users'; // edit_user maps to edit_users.
1075 $post = get_post( $args[0] );
1077 if ( 'revision' == $post->post_type ) {
1078 $post = get_post( $post->post_parent );
1081 $post_type = get_post_type_object( $post->post_type );
1083 if ( ! $post_type->map_meta_cap ) {
1084 $caps[] = $post_type->cap->$cap;
1085 // Prior to 3.1 we would re-call map_meta_cap here.
1086 if ( 'delete_post' == $cap )
1087 $cap = $post_type->cap->$cap;
1091 // If the post author is set and the user is the author...
1092 if ( $post->post_author && $user_id == $post->post_author ) {
1093 // If the post is published...
1094 if ( 'publish' == $post->post_status ) {
1095 $caps[] = $post_type->cap->delete_published_posts;
1096 } elseif ( 'trash' == $post->post_status ) {
1097 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1098 $caps[] = $post_type->cap->delete_published_posts;
1101 // If the post is draft...
1102 $caps[] = $post_type->cap->delete_posts;
1105 // The user is trying to edit someone else's post.
1106 $caps[] = $post_type->cap->delete_others_posts;
1107 // The post is published, extra cap required.
1108 if ( 'publish' == $post->post_status ) {
1109 $caps[] = $post_type->cap->delete_published_posts;
1110 } elseif ( 'private' == $post->post_status ) {
1111 $caps[] = $post_type->cap->delete_private_posts;
1115 // edit_post breaks down to edit_posts, edit_published_posts, or
1116 // edit_others_posts
1119 $post = get_post( $args[0] );
1120 if ( empty( $post ) )
1123 if ( 'revision' == $post->post_type ) {
1124 $post = get_post( $post->post_parent );
1127 $post_type = get_post_type_object( $post->post_type );
1129 if ( ! $post_type->map_meta_cap ) {
1130 $caps[] = $post_type->cap->$cap;
1131 // Prior to 3.1 we would re-call map_meta_cap here.
1132 if ( 'edit_post' == $cap )
1133 $cap = $post_type->cap->$cap;
1137 // If the post author is set and the user is the author...
1138 if ( $post->post_author && $user_id == $post->post_author ) {
1139 // If the post is published...
1140 if ( 'publish' == $post->post_status ) {
1141 $caps[] = $post_type->cap->edit_published_posts;
1142 } elseif ( 'trash' == $post->post_status ) {
1143 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1144 $caps[] = $post_type->cap->edit_published_posts;
1147 // If the post is draft...
1148 $caps[] = $post_type->cap->edit_posts;
1151 // The user is trying to edit someone else's post.
1152 $caps[] = $post_type->cap->edit_others_posts;
1153 // The post is published, extra cap required.
1154 if ( 'publish' == $post->post_status ) {
1155 $caps[] = $post_type->cap->edit_published_posts;
1156 } elseif ( 'private' == $post->post_status ) {
1157 $caps[] = $post_type->cap->edit_private_posts;
1163 $post = get_post( $args[0] );
1165 if ( 'revision' == $post->post_type ) {
1166 $post = get_post( $post->post_parent );
1169 $post_type = get_post_type_object( $post->post_type );
1171 if ( ! $post_type->map_meta_cap ) {
1172 $caps[] = $post_type->cap->$cap;
1173 // Prior to 3.1 we would re-call map_meta_cap here.
1174 if ( 'read_post' == $cap )
1175 $cap = $post_type->cap->$cap;
1179 $status_obj = get_post_status_object( $post->post_status );
1180 if ( $status_obj->public ) {
1181 $caps[] = $post_type->cap->read;
1185 if ( $post->post_author && $user_id == $post->post_author ) {
1186 $caps[] = $post_type->cap->read;
1187 } elseif ( $status_obj->private ) {
1188 $caps[] = $post_type->cap->read_private_posts;
1190 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1193 case 'publish_post':
1194 $post = get_post( $args[0] );
1195 $post_type = get_post_type_object( $post->post_type );
1197 $caps[] = $post_type->cap->publish_posts;
1199 case 'edit_post_meta':
1200 case 'delete_post_meta':
1201 case 'add_post_meta':
1202 $post = get_post( $args[0] );
1203 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1205 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1207 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1209 * Filter whether the user is allowed to add post meta to a post.
1211 * The dynamic portion of the hook name, `$meta_key`, refers to the
1212 * meta key passed to {@see map_meta_cap()}.
1216 * @param bool $allowed Whether the user can add the post meta. Default false.
1217 * @param string $meta_key The meta key.
1218 * @param int $post_id Post ID.
1219 * @param int $user_id User ID.
1220 * @param string $cap Capability name.
1221 * @param array $caps User capabilities.
1223 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1226 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1230 case 'edit_comment':
1231 $comment = get_comment( $args[0] );
1232 if ( empty( $comment ) )
1234 $post = get_post( $comment->comment_post_ID );
1235 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1237 case 'unfiltered_upload':
1238 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
1241 $caps[] = 'do_not_allow';
1243 case 'unfiltered_html' :
1244 // Disallow unfiltered_html for all users, even admins and super admins.
1245 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
1246 $caps[] = 'do_not_allow';
1247 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1248 $caps[] = 'do_not_allow';
1253 case 'edit_plugins':
1255 // Disallow the file editors.
1256 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
1257 $caps[] = 'do_not_allow';
1258 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1259 $caps[] = 'do_not_allow';
1260 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1261 $caps[] = 'do_not_allow';
1265 case 'update_plugins':
1266 case 'delete_plugins':
1267 case 'install_plugins':
1268 case 'upload_plugins':
1269 case 'update_themes':
1270 case 'delete_themes':
1271 case 'install_themes':
1272 case 'upload_themes':
1274 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
1275 // Files in uploads are excepted.
1276 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
1277 $caps[] = 'do_not_allow';
1278 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
1279 $caps[] = 'do_not_allow';
1280 } elseif ( 'upload_themes' === $cap ) {
1281 $caps[] = 'install_themes';
1282 } elseif ( 'upload_plugins' === $cap ) {
1283 $caps[] = 'install_plugins';
1288 case 'activate_plugins':
1290 if ( is_multisite() ) {
1291 // update_, install_, and delete_ are handled above with is_super_admin().
1292 $menu_perms = get_site_option( 'menu_items', array() );
1293 if ( empty( $menu_perms['plugins'] ) )
1294 $caps[] = 'manage_network_plugins';
1298 case 'delete_users':
1299 // If multisite only super admins can delete users.
1300 if ( is_multisite() && ! is_super_admin( $user_id ) )
1301 $caps[] = 'do_not_allow';
1303 $caps[] = 'delete_users'; // delete_user maps to delete_users.
1305 case 'create_users':
1306 if ( !is_multisite() )
1308 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1311 $caps[] = 'do_not_allow';
1313 case 'manage_links' :
1314 if ( get_option( 'link_manager_enabled' ) )
1317 $caps[] = 'do_not_allow';
1320 $caps[] = 'edit_theme_options';
1323 // Handle meta capabilities for custom post types.
1324 $post_type_meta_caps = _post_type_meta_capabilities();
1325 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1326 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1327 return call_user_func_array( 'map_meta_cap', $args );
1330 // If no meta caps match, return the original cap.
1335 * Filter a user's capabilities depending on specific context and/or privilege.
1339 * @param array $caps Returns the user's actual capabilities.
1340 * @param string $cap Capability name.
1341 * @param int $user_id The user ID.
1342 * @param array $args Adds the context to the cap. Typically the object ID.
1344 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
1348 * Whether current user has capability or role.
1352 * @param string $capability Capability or role name.
1355 function current_user_can( $capability ) {
1356 $current_user = wp_get_current_user();
1358 if ( empty( $current_user ) )
1361 $args = array_slice( func_get_args(), 1 );
1362 $args = array_merge( array( $capability ), $args );
1364 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
1368 * Whether current user has a capability or role for a given blog.
1372 * @param int $blog_id Blog ID
1373 * @param string $capability Capability or role name.
1376 function current_user_can_for_blog( $blog_id, $capability ) {
1377 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
1379 $current_user = wp_get_current_user();
1381 if ( empty( $current_user ) ) {
1383 restore_current_blog();
1388 $args = array_slice( func_get_args(), 2 );
1389 $args = array_merge( array( $capability ), $args );
1391 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
1394 restore_current_blog();
1401 * Whether author of supplied post has capability or role.
1405 * @param int|object $post Post ID or post object.
1406 * @param string $capability Capability or role name.
1409 function author_can( $post, $capability ) {
1410 if ( !$post = get_post($post) )
1413 $author = get_userdata( $post->post_author );
1418 $args = array_slice( func_get_args(), 2 );
1419 $args = array_merge( array( $capability ), $args );
1421 return call_user_func_array( array( $author, 'has_cap' ), $args );
1425 * Whether a particular user has capability or role.
1429 * @param int|object $user User ID or object.
1430 * @param string $capability Capability or role name.
1433 function user_can( $user, $capability ) {
1434 if ( ! is_object( $user ) )
1435 $user = get_userdata( $user );
1437 if ( ! $user || ! $user->exists() )
1440 $args = array_slice( func_get_args(), 2 );
1441 $args = array_merge( array( $capability ), $args );
1443 return call_user_func_array( array( $user, 'has_cap' ), $args );
1447 * Retrieve role object.
1449 * @see WP_Roles::get_role() Uses method to retrieve role object.
1452 * @param string $role Role name.
1453 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
1455 function get_role( $role ) {
1458 if ( ! isset( $wp_roles ) )
1459 $wp_roles = new WP_Roles();
1461 return $wp_roles->get_role( $role );
1465 * Add role, if it does not exist.
1467 * @see WP_Roles::add_role() Uses method to add role.
1470 * @param string $role Role name.
1471 * @param string $display_name Display name for role.
1472 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1473 * @return WP_Role|null WP_Role object if role is added, null if already exists.
1475 function add_role( $role, $display_name, $capabilities = array() ) {
1478 if ( ! isset( $wp_roles ) )
1479 $wp_roles = new WP_Roles();
1481 return $wp_roles->add_role( $role, $display_name, $capabilities );
1485 * Remove role, if it exists.
1487 * @see WP_Roles::remove_role() Uses method to remove role.
1490 * @param string $role Role name.
1492 function remove_role( $role ) {
1495 if ( ! isset( $wp_roles ) )
1496 $wp_roles = new WP_Roles();
1498 $wp_roles->remove_role( $role );
1502 * Retrieve a list of super admins.
1506 * @uses $super_admins Super admins global variable, if set.
1508 * @return array List of super admin logins
1510 function get_super_admins() {
1511 global $super_admins;
1513 if ( isset($super_admins) )
1514 return $super_admins;
1516 return get_site_option( 'site_admins', array('admin') );
1520 * Determine if user is a site admin.
1524 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1525 * @return bool True if the user is a site admin.
1527 function is_super_admin( $user_id = false ) {
1528 if ( ! $user_id || $user_id == get_current_user_id() )
1529 $user = wp_get_current_user();
1531 $user = get_userdata( $user_id );
1533 if ( ! $user || ! $user->exists() )
1536 if ( is_multisite() ) {
1537 $super_admins = get_super_admins();
1538 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1541 if ( $user->has_cap('delete_users') )