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 public $role_objects = array();
55 public $role_names = array();
58 * Option name for storing role list.
67 * Whether to use the database for retrieval and storage.
73 public $use_db = true;
80 public function __construct() {
85 * Make private/protected methods readable for backwards compatibility.
90 * @param callable $name Method to call.
91 * @param array $arguments Arguments to pass when calling.
92 * @return mixed|bool Return value of the callback, false otherwise.
94 public function __call( $name, $arguments ) {
95 return call_user_func_array( array( $this, $name ), $arguments );
99 * Set up the object properties.
101 * The role key is set to the current prefix for the $wpdb object with
102 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
103 * be used and the role option will not be updated or used.
107 * @uses $wpdb Used to get the database prefix.
108 * @global array $wp_user_roles Used to set the 'roles' property value.
110 protected function _init() {
111 global $wpdb, $wp_user_roles;
112 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
113 if ( ! empty( $wp_user_roles ) ) {
114 $this->roles = $wp_user_roles;
115 $this->use_db = false;
117 $this->roles = get_option( $this->role_key );
120 if ( empty( $this->roles ) )
123 $this->role_objects = array();
124 $this->role_names = array();
125 foreach ( array_keys( $this->roles ) as $role ) {
126 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
127 $this->role_names[$role] = $this->roles[$role]['name'];
132 * Reinitialize the object
134 * Recreates the role objects. This is typically called only by switch_to_blog()
135 * after switching wpdb to a new blog ID.
140 public function reinit() {
141 // There is no need to reinit if using the wp_user_roles global.
142 if ( ! $this->use_db )
147 // Duplicated from _init() to avoid an extra function call.
148 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
149 $this->roles = get_option( $this->role_key );
150 if ( empty( $this->roles ) )
153 $this->role_objects = array();
154 $this->role_names = array();
155 foreach ( array_keys( $this->roles ) as $role ) {
156 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
157 $this->role_names[$role] = $this->roles[$role]['name'];
162 * Add role name with capabilities to list.
164 * Updates the list of roles, if the role doesn't already exist.
166 * The capabilities are defined in the following format `array( 'read' => true );`
167 * To explicitly deny a role a capability you set the value for that capability to false.
172 * @param string $role Role name.
173 * @param string $display_name Role display name.
174 * @param array $capabilities List of role capabilities in the above format.
175 * @return WP_Role|null WP_Role object if role is added, null if already exists.
177 public function add_role( $role, $display_name, $capabilities = array() ) {
178 if ( isset( $this->roles[$role] ) )
181 $this->roles[$role] = array(
182 'name' => $display_name,
183 'capabilities' => $capabilities
186 update_option( $this->role_key, $this->roles );
187 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
188 $this->role_names[$role] = $display_name;
189 return $this->role_objects[$role];
193 * Remove role by name.
198 * @param string $role Role name.
200 public function remove_role( $role ) {
201 if ( ! isset( $this->role_objects[$role] ) )
204 unset( $this->role_objects[$role] );
205 unset( $this->role_names[$role] );
206 unset( $this->roles[$role] );
209 update_option( $this->role_key, $this->roles );
211 if ( get_option( 'default_role' ) == $role )
212 update_option( 'default_role', 'subscriber' );
216 * Add capability to role.
221 * @param string $role Role name.
222 * @param string $cap Capability name.
223 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
225 public function add_cap( $role, $cap, $grant = true ) {
226 if ( ! isset( $this->roles[$role] ) )
229 $this->roles[$role]['capabilities'][$cap] = $grant;
231 update_option( $this->role_key, $this->roles );
235 * Remove capability from role.
240 * @param string $role Role name.
241 * @param string $cap Capability name.
243 public function remove_cap( $role, $cap ) {
244 if ( ! isset( $this->roles[$role] ) )
247 unset( $this->roles[$role]['capabilities'][$cap] );
249 update_option( $this->role_key, $this->roles );
253 * Retrieve role object by name.
258 * @param string $role Role name.
259 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
261 public function get_role( $role ) {
262 if ( isset( $this->role_objects[$role] ) )
263 return $this->role_objects[$role];
269 * Retrieve list of role names.
274 * @return array List of role names.
276 public function get_names() {
277 return $this->role_names;
281 * Whether role name is currently in the list of available roles.
286 * @param string $role Role name to look up.
289 public function is_role( $role ) {
290 return isset( $this->role_names[$role] );
295 * WordPress Role class.
312 * List of capabilities the role contains.
318 public $capabilities;
321 * Constructor - Set up object properties.
323 * The list of capabilities, must have the key as the name of the capability
324 * and the value a boolean of whether it is granted to the role.
329 * @param string $role Role name.
330 * @param array $capabilities List of capabilities.
332 public function __construct( $role, $capabilities ) {
334 $this->capabilities = $capabilities;
338 * Assign role a capability.
340 * @see WP_Roles::add_cap() Method uses implementation for role.
344 * @param string $cap Capability name.
345 * @param bool $grant Whether role has capability privilege.
347 public function add_cap( $cap, $grant = true ) {
350 if ( ! isset( $wp_roles ) )
351 $wp_roles = new WP_Roles();
353 $this->capabilities[$cap] = $grant;
354 $wp_roles->add_cap( $this->name, $cap, $grant );
358 * Remove capability from role.
360 * This is a container for {@link WP_Roles::remove_cap()} to remove the
361 * capability from the role. That is to say, that {@link
362 * WP_Roles::remove_cap()} implements the functionality, but it also makes
363 * sense to use this class, because you don't need to enter the role name.
368 * @param string $cap Capability name.
370 public function remove_cap( $cap ) {
373 if ( ! isset( $wp_roles ) )
374 $wp_roles = new WP_Roles();
376 unset( $this->capabilities[$cap] );
377 $wp_roles->remove_cap( $this->name, $cap );
381 * Whether role has capability.
383 * The capabilities is passed through the 'role_has_cap' filter. The first
384 * parameter for the hook is the list of capabilities the class has
385 * assigned. The second parameter is the capability name to look for. The
386 * third and final parameter for the hook is the role name.
391 * @param string $cap Capability name.
392 * @return bool True, if user has capability. False, if doesn't have capability.
394 public function has_cap( $cap ) {
396 * Filter which capabilities a role has.
400 * @param array $capabilities Array of role capabilities.
401 * @param string $cap Capability name.
402 * @param string $name Role name.
404 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
405 if ( !empty( $capabilities[$cap] ) )
406 return $capabilities[$cap];
414 * WordPress User class.
422 * User data container.
440 * The individual capabilities the user has been given.
446 public $caps = array();
449 * User metadata option name.
458 * The roles the user is part of.
464 public $roles = array();
467 * All capabilities the user has, including individual and role based.
473 public $allcaps = array();
476 * The filter context applied to user data fields.
484 private static $back_compat_keys;
489 * Retrieves the userdata and passes it to {@link WP_User::init()}.
494 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
495 * @param string $name Optional. User's username
496 * @param int $blog_id Optional Blog ID, defaults to current blog.
499 public function __construct( $id = 0, $name = '', $blog_id = '' ) {
500 if ( ! isset( self::$back_compat_keys ) ) {
501 $prefix = $GLOBALS['wpdb']->prefix;
502 self::$back_compat_keys = array(
503 'user_firstname' => 'first_name',
504 'user_lastname' => 'last_name',
505 'user_description' => 'description',
506 'user_level' => $prefix . 'user_level',
507 $prefix . 'usersettings' => $prefix . 'user-settings',
508 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
512 if ( is_a( $id, 'WP_User' ) ) {
513 $this->init( $id->data, $blog_id );
515 } elseif ( is_object( $id ) ) {
516 $this->init( $id, $blog_id );
520 if ( ! empty( $id ) && ! is_numeric( $id ) ) {
526 $data = self::get_data_by( 'id', $id );
528 $data = self::get_data_by( 'login', $name );
531 $this->init( $data, $blog_id );
535 * Sets up object properties, including capabilities.
537 * @param object $data User DB row object
538 * @param int $blog_id Optional. The blog id to initialize for
540 public function init( $data, $blog_id = '' ) {
542 $this->ID = (int) $data->ID;
544 $this->for_blog( $blog_id );
548 * Return only the main user fields
552 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
553 * @param string|int $value The field value
554 * @return object Raw user object
556 public static function get_data_by( $field, $value ) {
559 if ( 'id' == $field ) {
560 // Make sure the value is numeric to avoid casting objects, for example,
562 if ( ! is_numeric( $value ) )
564 $value = intval( $value );
568 $value = trim( $value );
580 $user_id = wp_cache_get($value, 'userslugs');
581 $db_field = 'user_nicename';
584 $user_id = wp_cache_get($value, 'useremail');
585 $db_field = 'user_email';
588 $value = sanitize_user( $value );
589 $user_id = wp_cache_get($value, 'userlogins');
590 $db_field = 'user_login';
596 if ( false !== $user_id ) {
597 if ( $user = wp_cache_get( $user_id, 'users' ) )
601 if ( !$user = $wpdb->get_row( $wpdb->prepare(
602 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
606 update_user_caches( $user );
612 * Magic method for checking the existence of a certain custom field
616 public function __isset( $key ) {
617 if ( 'id' == $key ) {
618 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
622 if ( isset( $this->data->$key ) )
625 if ( isset( self::$back_compat_keys[ $key ] ) )
626 $key = self::$back_compat_keys[ $key ];
628 return metadata_exists( 'user', $this->ID, $key );
632 * Magic method for accessing custom fields
636 public function __get( $key ) {
637 if ( 'id' == $key ) {
638 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
642 if ( isset( $this->data->$key ) ) {
643 $value = $this->data->$key;
645 if ( isset( self::$back_compat_keys[ $key ] ) )
646 $key = self::$back_compat_keys[ $key ];
647 $value = get_user_meta( $this->ID, $key, true );
650 if ( $this->filter ) {
651 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
658 * Magic method for setting custom fields
662 public function __set( $key, $value ) {
663 if ( 'id' == $key ) {
664 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
669 $this->data->$key = $value;
673 * Determine whether the user exists in the database.
678 * @return bool True if user exists in the database, false if not.
680 public function exists() {
681 return ! empty( $this->ID );
685 * Retrieve the value of a property or meta key.
687 * Retrieves from the users and usermeta table.
691 * @param string $key Property
693 public function get( $key ) {
694 return $this->__get( $key );
698 * Determine whether a property or meta key is set
700 * Consults the users and usermeta tables.
704 * @param string $key Property
706 public function has_prop( $key ) {
707 return $this->__isset( $key );
711 * Return an array representation.
715 * @return array Array representation.
717 public function to_array() {
718 return get_object_vars( $this->data );
722 * Set up capability object properties.
724 * Will set the value for the 'cap_key' property to current database table
725 * prefix, followed by 'capabilities'. Will then check to see if the
726 * property matching the 'cap_key' exists and is an array. If so, it will be
732 * @param string $cap_key Optional capability key
734 function _init_caps( $cap_key = '' ) {
737 if ( empty($cap_key) )
738 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
740 $this->cap_key = $cap_key;
742 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
744 if ( ! is_array( $this->caps ) )
745 $this->caps = array();
747 $this->get_role_caps();
751 * Retrieve all of the role capabilities and merge with individual capabilities.
753 * All of the capabilities of the roles the user belongs to are merged with
754 * the users individual roles. This also means that the user can be denied
755 * specific roles that their role might have, but the specific user isn't
756 * granted permission to.
762 * @return array List of all capabilities for the user.
764 public function get_role_caps() {
767 if ( ! isset( $wp_roles ) )
768 $wp_roles = new WP_Roles();
770 //Filter out caps that are not role names and assign to $this->roles
771 if ( is_array( $this->caps ) )
772 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
774 //Build $allcaps from role caps, overlay user's $caps
775 $this->allcaps = array();
776 foreach ( (array) $this->roles as $role ) {
777 $the_role = $wp_roles->get_role( $role );
778 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
780 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
782 return $this->allcaps;
788 * Updates the user's meta data option with capabilities and roles.
793 * @param string $role Role name.
795 public function add_role( $role ) {
796 $this->caps[$role] = true;
797 update_user_meta( $this->ID, $this->cap_key, $this->caps );
798 $this->get_role_caps();
799 $this->update_user_level_from_caps();
803 * Remove role from user.
808 * @param string $role Role name.
810 public function remove_role( $role ) {
811 if ( !in_array($role, $this->roles) )
813 unset( $this->caps[$role] );
814 update_user_meta( $this->ID, $this->cap_key, $this->caps );
815 $this->get_role_caps();
816 $this->update_user_level_from_caps();
820 * Set the role of the user.
822 * This will remove the previous roles of the user and assign the user the
823 * new one. You can set the role to an empty string and it will remove all
824 * of the roles from the user.
829 * @param string $role Role name.
831 public function set_role( $role ) {
832 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
835 foreach ( (array) $this->roles as $oldrole )
836 unset( $this->caps[$oldrole] );
838 $old_roles = $this->roles;
839 if ( !empty( $role ) ) {
840 $this->caps[$role] = true;
841 $this->roles = array( $role => true );
843 $this->roles = false;
845 update_user_meta( $this->ID, $this->cap_key, $this->caps );
846 $this->get_role_caps();
847 $this->update_user_level_from_caps();
850 * Fires after the user's role has changed.
853 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
855 * @param int $user_id The user ID.
856 * @param string $role The new role.
857 * @param array $old_roles An array of the user's previous roles.
859 do_action( 'set_user_role', $this->ID, $role, $old_roles );
863 * Choose the maximum level the user has.
865 * Will compare the level from the $item parameter against the $max
866 * parameter. If the item is incorrect, then just the $max parameter value
869 * Used to get the max level based on the capabilities the user has. This
870 * is also based on roles, so if the user is assigned the Administrator role
871 * then the capability 'level_10' will exist and the user will get that
877 * @param int $max Max level of user.
878 * @param string $item Level capability name.
879 * @return int Max Level.
881 public function level_reduction( $max, $item ) {
882 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
883 $level = intval( $matches[1] );
884 return max( $max, $level );
891 * Update the maximum user level for the user.
893 * Updates the 'user_level' user metadata (includes prefix that is the
894 * database table prefix) with the maximum user level. Gets the value from
895 * the all of the capabilities that the user has.
900 public function update_user_level_from_caps() {
902 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
903 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
907 * Add capability and grant or deny access to capability.
912 * @param string $cap Capability name.
913 * @param bool $grant Whether to grant capability to user.
915 public function add_cap( $cap, $grant = true ) {
916 $this->caps[$cap] = $grant;
917 update_user_meta( $this->ID, $this->cap_key, $this->caps );
921 * Remove capability from user.
926 * @param string $cap Capability name.
928 public function remove_cap( $cap ) {
929 if ( ! isset( $this->caps[$cap] ) )
931 unset( $this->caps[$cap] );
932 update_user_meta( $this->ID, $this->cap_key, $this->caps );
936 * Remove all of the capabilities of the user.
941 public function remove_all_caps() {
943 $this->caps = array();
944 delete_user_meta( $this->ID, $this->cap_key );
945 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
946 $this->get_role_caps();
950 * Whether user has capability or role name.
952 * This is useful for looking up whether the user has a specific role
953 * assigned to the user. The second optional parameter can also be used to
954 * check for capabilities against a specific object, such as a post or user.
959 * @param string|int $cap Capability or role name to search.
960 * @return bool True, if user has capability; false, if user does not have capability.
962 public function has_cap( $cap ) {
963 if ( is_numeric( $cap ) ) {
964 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
965 $cap = $this->translate_level_to_cap( $cap );
968 $args = array_slice( func_get_args(), 1 );
969 $args = array_merge( array( $cap, $this->ID ), $args );
970 $caps = call_user_func_array( 'map_meta_cap', $args );
972 // Multisite super admin has all caps by definition, Unless specifically denied.
973 if ( is_multisite() && is_super_admin( $this->ID ) ) {
974 if ( in_array('do_not_allow', $caps) )
980 * Dynamically filter a user's capabilities.
983 * @since 3.7.0 Added the user object.
985 * @param array $allcaps An array of all the role's capabilities.
986 * @param array $caps Actual capabilities for meta capability.
987 * @param array $args Optional parameters passed to has_cap(), typically object ID.
988 * @param WP_User $user The user object.
990 // Must have ALL requested caps
991 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
992 $capabilities['exist'] = true; // Everyone is allowed to exist
993 foreach ( (array) $caps as $cap ) {
994 if ( empty( $capabilities[ $cap ] ) )
1002 * Convert numeric level to level capability name.
1004 * Prepends 'level_' to level number.
1009 * @param int $level Level number, 1 to 10.
1012 public function translate_level_to_cap( $level ) {
1013 return 'level_' . $level;
1017 * Set the blog to operate on. Defaults to the current blog.
1021 * @param int $blog_id Optional Blog ID, defaults to current blog.
1023 public function for_blog( $blog_id = '' ) {
1025 if ( ! empty( $blog_id ) )
1026 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
1029 $this->_init_caps( $cap_key );
1034 * Map meta capabilities to primitive capabilities.
1036 * This does not actually compare whether the user ID has the actual capability,
1037 * just what the capability or capabilities are. Meta capability list value can
1038 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
1039 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
1043 * @param string $cap Capability name.
1044 * @param int $user_id User ID.
1045 * @return array Actual capabilities for meta capability.
1047 function map_meta_cap( $cap, $user_id ) {
1048 $args = array_slice( func_get_args(), 2 );
1053 $caps[] = 'remove_users';
1055 case 'promote_user':
1056 $caps[] = 'promote_users';
1060 // Allow user to edit itself
1061 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
1064 // If multisite these caps are allowed only for super admins.
1065 if ( is_multisite() && !is_super_admin( $user_id ) )
1066 $caps[] = 'do_not_allow';
1068 $caps[] = 'edit_users'; // edit_user maps to edit_users.
1072 $post = get_post( $args[0] );
1074 if ( 'revision' == $post->post_type ) {
1075 $post = get_post( $post->post_parent );
1078 $post_type = get_post_type_object( $post->post_type );
1080 if ( ! $post_type->map_meta_cap ) {
1081 $caps[] = $post_type->cap->$cap;
1082 // Prior to 3.1 we would re-call map_meta_cap here.
1083 if ( 'delete_post' == $cap )
1084 $cap = $post_type->cap->$cap;
1088 // If the post author is set and the user is the author...
1089 if ( $post->post_author && $user_id == $post->post_author ) {
1090 // If the post is published...
1091 if ( 'publish' == $post->post_status ) {
1092 $caps[] = $post_type->cap->delete_published_posts;
1093 } elseif ( 'trash' == $post->post_status ) {
1094 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1095 $caps[] = $post_type->cap->delete_published_posts;
1098 // If the post is draft...
1099 $caps[] = $post_type->cap->delete_posts;
1102 // The user is trying to edit someone else's post.
1103 $caps[] = $post_type->cap->delete_others_posts;
1104 // The post is published, extra cap required.
1105 if ( 'publish' == $post->post_status ) {
1106 $caps[] = $post_type->cap->delete_published_posts;
1107 } elseif ( 'private' == $post->post_status ) {
1108 $caps[] = $post_type->cap->delete_private_posts;
1112 // edit_post breaks down to edit_posts, edit_published_posts, or
1113 // edit_others_posts
1116 $post = get_post( $args[0] );
1117 if ( empty( $post ) )
1120 if ( 'revision' == $post->post_type ) {
1121 $post = get_post( $post->post_parent );
1124 $post_type = get_post_type_object( $post->post_type );
1126 if ( ! $post_type->map_meta_cap ) {
1127 $caps[] = $post_type->cap->$cap;
1128 // Prior to 3.1 we would re-call map_meta_cap here.
1129 if ( 'edit_post' == $cap )
1130 $cap = $post_type->cap->$cap;
1134 // If the post author is set and the user is the author...
1135 if ( $post->post_author && $user_id == $post->post_author ) {
1136 // If the post is published...
1137 if ( 'publish' == $post->post_status ) {
1138 $caps[] = $post_type->cap->edit_published_posts;
1139 } elseif ( 'trash' == $post->post_status ) {
1140 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1141 $caps[] = $post_type->cap->edit_published_posts;
1144 // If the post is draft...
1145 $caps[] = $post_type->cap->edit_posts;
1148 // The user is trying to edit someone else's post.
1149 $caps[] = $post_type->cap->edit_others_posts;
1150 // The post is published, extra cap required.
1151 if ( 'publish' == $post->post_status ) {
1152 $caps[] = $post_type->cap->edit_published_posts;
1153 } elseif ( 'private' == $post->post_status ) {
1154 $caps[] = $post_type->cap->edit_private_posts;
1160 $post = get_post( $args[0] );
1162 if ( 'revision' == $post->post_type ) {
1163 $post = get_post( $post->post_parent );
1166 $post_type = get_post_type_object( $post->post_type );
1168 if ( ! $post_type->map_meta_cap ) {
1169 $caps[] = $post_type->cap->$cap;
1170 // Prior to 3.1 we would re-call map_meta_cap here.
1171 if ( 'read_post' == $cap )
1172 $cap = $post_type->cap->$cap;
1176 $status_obj = get_post_status_object( $post->post_status );
1177 if ( $status_obj->public ) {
1178 $caps[] = $post_type->cap->read;
1182 if ( $post->post_author && $user_id == $post->post_author ) {
1183 $caps[] = $post_type->cap->read;
1184 } elseif ( $status_obj->private ) {
1185 $caps[] = $post_type->cap->read_private_posts;
1187 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1190 case 'publish_post':
1191 $post = get_post( $args[0] );
1192 $post_type = get_post_type_object( $post->post_type );
1194 $caps[] = $post_type->cap->publish_posts;
1196 case 'edit_post_meta':
1197 case 'delete_post_meta':
1198 case 'add_post_meta':
1199 $post = get_post( $args[0] );
1200 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1202 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1204 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1206 * Filter whether the user is allowed to add post meta to a post.
1208 * The dynamic portion of the hook name, $meta_key, refers to the
1209 * meta key passed to map_meta_cap().
1213 * @param bool $allowed Whether the user can add the post meta. Default false.
1214 * @param string $meta_key The meta key.
1215 * @param int $post_id Post ID.
1216 * @param int $user_id User ID.
1217 * @param string $cap Capability name.
1218 * @param array $caps User capabilities.
1220 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1223 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1227 case 'edit_comment':
1228 $comment = get_comment( $args[0] );
1229 if ( empty( $comment ) )
1231 $post = get_post( $comment->comment_post_ID );
1232 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1234 case 'unfiltered_upload':
1235 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
1238 $caps[] = 'do_not_allow';
1240 case 'unfiltered_html' :
1241 // Disallow unfiltered_html for all users, even admins and super admins.
1242 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
1243 $caps[] = 'do_not_allow';
1244 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1245 $caps[] = 'do_not_allow';
1250 case 'edit_plugins':
1252 // Disallow the file editors.
1253 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
1254 $caps[] = 'do_not_allow';
1255 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1256 $caps[] = 'do_not_allow';
1257 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1258 $caps[] = 'do_not_allow';
1262 case 'update_plugins':
1263 case 'delete_plugins':
1264 case 'install_plugins':
1265 case 'upload_plugins':
1266 case 'update_themes':
1267 case 'delete_themes':
1268 case 'install_themes':
1269 case 'upload_themes':
1271 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
1272 // Files in uploads are excepted.
1273 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
1274 $caps[] = 'do_not_allow';
1275 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
1276 $caps[] = 'do_not_allow';
1277 } elseif ( 'upload_themes' === $cap ) {
1278 $caps[] = 'install_themes';
1279 } elseif ( 'upload_plugins' === $cap ) {
1280 $caps[] = 'install_plugins';
1285 case 'activate_plugins':
1287 if ( is_multisite() ) {
1288 // update_, install_, and delete_ are handled above with is_super_admin().
1289 $menu_perms = get_site_option( 'menu_items', array() );
1290 if ( empty( $menu_perms['plugins'] ) )
1291 $caps[] = 'manage_network_plugins';
1295 case 'delete_users':
1296 // If multisite only super admins can delete users.
1297 if ( is_multisite() && ! is_super_admin( $user_id ) )
1298 $caps[] = 'do_not_allow';
1300 $caps[] = 'delete_users'; // delete_user maps to delete_users.
1302 case 'create_users':
1303 if ( !is_multisite() )
1305 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1308 $caps[] = 'do_not_allow';
1310 case 'manage_links' :
1311 if ( get_option( 'link_manager_enabled' ) )
1314 $caps[] = 'do_not_allow';
1317 $caps[] = 'edit_theme_options';
1320 // Handle meta capabilities for custom post types.
1321 $post_type_meta_caps = _post_type_meta_capabilities();
1322 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1323 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1324 return call_user_func_array( 'map_meta_cap', $args );
1327 // If no meta caps match, return the original cap.
1332 * Filter a user's capabilities depending on specific context and/or privilege.
1336 * @param array $caps Returns the user's actual capabilities.
1337 * @param string $cap Capability name.
1338 * @param int $user_id The user ID.
1339 * @param array $args Adds the context to the cap. Typically the object ID.
1341 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
1345 * Whether current user has capability or role.
1349 * @param string $capability Capability or role name.
1352 function current_user_can( $capability ) {
1353 $current_user = wp_get_current_user();
1355 if ( empty( $current_user ) )
1358 $args = array_slice( func_get_args(), 1 );
1359 $args = array_merge( array( $capability ), $args );
1361 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
1365 * Whether current user has a capability or role for a given blog.
1369 * @param int $blog_id Blog ID
1370 * @param string $capability Capability or role name.
1373 function current_user_can_for_blog( $blog_id, $capability ) {
1374 if ( is_multisite() )
1375 switch_to_blog( $blog_id );
1377 $current_user = wp_get_current_user();
1379 if ( empty( $current_user ) )
1382 $args = array_slice( func_get_args(), 2 );
1383 $args = array_merge( array( $capability ), $args );
1385 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
1387 if ( is_multisite() )
1388 restore_current_blog();
1394 * Whether author of supplied post has capability or role.
1398 * @param int|object $post Post ID or post object.
1399 * @param string $capability Capability or role name.
1402 function author_can( $post, $capability ) {
1403 if ( !$post = get_post($post) )
1406 $author = get_userdata( $post->post_author );
1411 $args = array_slice( func_get_args(), 2 );
1412 $args = array_merge( array( $capability ), $args );
1414 return call_user_func_array( array( $author, 'has_cap' ), $args );
1418 * Whether a particular user has capability or role.
1422 * @param int|object $user User ID or object.
1423 * @param string $capability Capability or role name.
1426 function user_can( $user, $capability ) {
1427 if ( ! is_object( $user ) )
1428 $user = get_userdata( $user );
1430 if ( ! $user || ! $user->exists() )
1433 $args = array_slice( func_get_args(), 2 );
1434 $args = array_merge( array( $capability ), $args );
1436 return call_user_func_array( array( $user, 'has_cap' ), $args );
1440 * Retrieve role object.
1442 * @see WP_Roles::get_role() Uses method to retrieve role object.
1445 * @param string $role Role name.
1446 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
1448 function get_role( $role ) {
1451 if ( ! isset( $wp_roles ) )
1452 $wp_roles = new WP_Roles();
1454 return $wp_roles->get_role( $role );
1458 * Add role, if it does not exist.
1460 * @see WP_Roles::add_role() Uses method to add role.
1463 * @param string $role Role name.
1464 * @param string $display_name Display name for role.
1465 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1466 * @return WP_Role|null WP_Role object if role is added, null if already exists.
1468 function add_role( $role, $display_name, $capabilities = array() ) {
1471 if ( ! isset( $wp_roles ) )
1472 $wp_roles = new WP_Roles();
1474 return $wp_roles->add_role( $role, $display_name, $capabilities );
1478 * Remove role, if it exists.
1480 * @see WP_Roles::remove_role() Uses method to remove role.
1483 * @param string $role Role name.
1485 function remove_role( $role ) {
1488 if ( ! isset( $wp_roles ) )
1489 $wp_roles = new WP_Roles();
1491 $wp_roles->remove_role( $role );
1495 * Retrieve a list of super admins.
1499 * @uses $super_admins Super admins global variable, if set.
1501 * @return array List of super admin logins
1503 function get_super_admins() {
1504 global $super_admins;
1506 if ( isset($super_admins) )
1507 return $super_admins;
1509 return get_site_option( 'site_admins', array('admin') );
1513 * Determine if user is a site admin.
1517 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1518 * @return bool True if the user is a site admin.
1520 function is_super_admin( $user_id = false ) {
1521 if ( ! $user_id || $user_id == get_current_user_id() )
1522 $user = wp_get_current_user();
1524 $user = get_userdata( $user_id );
1526 if ( ! $user || ! $user->exists() )
1529 if ( is_multisite() ) {
1530 $super_admins = get_super_admins();
1531 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1534 if ( $user->has_cap('delete_users') )