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|false Return value of the callback, false otherwise.
92 public function __call( $name, $arguments ) {
93 if ( '_init' === $name ) {
94 return call_user_func_array( array( $this, $name ), $arguments );
100 * Set up the object properties.
102 * The role key is set to the current prefix for the $wpdb object with
103 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
104 * be used and the role option will not be updated or used.
109 * @global wpdb $wpdb WordPress database abstraction object.
110 * @global array $wp_user_roles Used to set the 'roles' property value.
112 protected function _init() {
113 global $wpdb, $wp_user_roles;
114 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
115 if ( ! empty( $wp_user_roles ) ) {
116 $this->roles = $wp_user_roles;
117 $this->use_db = false;
119 $this->roles = get_option( $this->role_key );
122 if ( empty( $this->roles ) )
125 $this->role_objects = array();
126 $this->role_names = array();
127 foreach ( array_keys( $this->roles ) as $role ) {
128 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
129 $this->role_names[$role] = $this->roles[$role]['name'];
134 * Reinitialize the object
136 * Recreates the role objects. This is typically called only by switch_to_blog()
137 * after switching wpdb to a new blog ID.
144 public function reinit() {
145 // There is no need to reinit if using the wp_user_roles global.
146 if ( ! $this->use_db )
151 // Duplicated from _init() to avoid an extra function call.
152 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
153 $this->roles = get_option( $this->role_key );
154 if ( empty( $this->roles ) )
157 $this->role_objects = array();
158 $this->role_names = array();
159 foreach ( array_keys( $this->roles ) as $role ) {
160 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
161 $this->role_names[$role] = $this->roles[$role]['name'];
166 * Add role name with capabilities to list.
168 * Updates the list of roles, if the role doesn't already exist.
170 * The capabilities are defined in the following format `array( 'read' => true );`
171 * To explicitly deny a role a capability you set the value for that capability to false.
176 * @param string $role Role name.
177 * @param string $display_name Role display name.
178 * @param array $capabilities List of role capabilities in the above format.
179 * @return WP_Role|void WP_Role object, if role is added.
181 public function add_role( $role, $display_name, $capabilities = array() ) {
182 if ( isset( $this->roles[$role] ) )
185 $this->roles[$role] = array(
186 'name' => $display_name,
187 'capabilities' => $capabilities
190 update_option( $this->role_key, $this->roles );
191 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
192 $this->role_names[$role] = $display_name;
193 return $this->role_objects[$role];
197 * Remove role by name.
202 * @param string $role Role name.
204 public function remove_role( $role ) {
205 if ( ! isset( $this->role_objects[$role] ) )
208 unset( $this->role_objects[$role] );
209 unset( $this->role_names[$role] );
210 unset( $this->roles[$role] );
213 update_option( $this->role_key, $this->roles );
215 if ( get_option( 'default_role' ) == $role )
216 update_option( 'default_role', 'subscriber' );
220 * Add capability to role.
225 * @param string $role Role name.
226 * @param string $cap Capability name.
227 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
229 public function add_cap( $role, $cap, $grant = true ) {
230 if ( ! isset( $this->roles[$role] ) )
233 $this->roles[$role]['capabilities'][$cap] = $grant;
235 update_option( $this->role_key, $this->roles );
239 * Remove capability from role.
244 * @param string $role Role name.
245 * @param string $cap Capability name.
247 public function remove_cap( $role, $cap ) {
248 if ( ! isset( $this->roles[$role] ) )
251 unset( $this->roles[$role]['capabilities'][$cap] );
253 update_option( $this->role_key, $this->roles );
257 * Retrieve role object by name.
262 * @param string $role Role name.
263 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
265 public function get_role( $role ) {
266 if ( isset( $this->role_objects[$role] ) )
267 return $this->role_objects[$role];
273 * Retrieve list of role names.
278 * @return array List of role names.
280 public function get_names() {
281 return $this->role_names;
285 * Whether role name is currently in the list of available roles.
290 * @param string $role Role name to look up.
293 public function is_role( $role ) {
294 return isset( $this->role_names[$role] );
299 * WordPress Role class.
316 * List of capabilities the role contains.
322 public $capabilities;
325 * Constructor - Set up object properties.
327 * The list of capabilities, must have the key as the name of the capability
328 * and the value a boolean of whether it is granted to the role.
333 * @param string $role Role name.
334 * @param array $capabilities List of capabilities.
336 public function __construct( $role, $capabilities ) {
338 $this->capabilities = $capabilities;
342 * Assign role a capability.
347 * @param string $cap Capability name.
348 * @param bool $grant Whether role has capability privilege.
350 public function add_cap( $cap, $grant = true ) {
351 $this->capabilities[$cap] = $grant;
352 wp_roles()->add_cap( $this->name, $cap, $grant );
356 * Remove capability from role.
358 * This is a container for {@link WP_Roles::remove_cap()} to remove the
359 * capability from the role. That is to say, that {@link
360 * WP_Roles::remove_cap()} implements the functionality, but it also makes
361 * sense to use this class, because you don't need to enter the role name.
366 * @param string $cap Capability name.
368 public function remove_cap( $cap ) {
369 unset( $this->capabilities[$cap] );
370 wp_roles()->remove_cap( $this->name, $cap );
374 * Whether role has capability.
376 * The capabilities is passed through the 'role_has_cap' filter. The first
377 * parameter for the hook is the list of capabilities the class has
378 * assigned. The second parameter is the capability name to look for. The
379 * third and final parameter for the hook is the role name.
384 * @param string $cap Capability name.
385 * @return bool True, if user has capability. False, if doesn't have capability.
387 public function has_cap( $cap ) {
389 * Filter which capabilities a role has.
393 * @param array $capabilities Array of role capabilities.
394 * @param string $cap Capability name.
395 * @param string $name Role name.
397 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
398 if ( !empty( $capabilities[$cap] ) )
399 return $capabilities[$cap];
407 * WordPress User class.
413 * @property string $nickname
414 * @property string $user_description
415 * @property string $user_firstname
416 * @property string $user_lastname
417 * @property string $user_login
418 * @property string $user_pass
419 * @property string $user_nicename
420 * @property string $user_email
421 * @property string $user_url
422 * @property string $user_registered
423 * @property string $user_activation_key
424 * @property string $user_status
425 * @property string $display_name
426 * @property string $spam
427 * @property string $deleted
431 * User data container.
448 * The individual capabilities the user has been given.
454 public $caps = array();
457 * User metadata option name.
466 * The roles the user is part of.
472 public $roles = array();
475 * All capabilities the user has, including individual and role based.
481 public $allcaps = array();
484 * The filter context applied to user data fields.
497 private static $back_compat_keys;
502 * Retrieves the userdata and passes it to {@link WP_User::init()}.
509 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
510 * @param string $name Optional. User's username
511 * @param int $blog_id Optional Blog ID, defaults to current blog.
513 public function __construct( $id = 0, $name = '', $blog_id = '' ) {
514 if ( ! isset( self::$back_compat_keys ) ) {
515 $prefix = $GLOBALS['wpdb']->prefix;
516 self::$back_compat_keys = array(
517 'user_firstname' => 'first_name',
518 'user_lastname' => 'last_name',
519 'user_description' => 'description',
520 'user_level' => $prefix . 'user_level',
521 $prefix . 'usersettings' => $prefix . 'user-settings',
522 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
526 if ( $id instanceof WP_User ) {
527 $this->init( $id->data, $blog_id );
529 } elseif ( is_object( $id ) ) {
530 $this->init( $id, $blog_id );
534 if ( ! empty( $id ) && ! is_numeric( $id ) ) {
540 $data = self::get_data_by( 'id', $id );
542 $data = self::get_data_by( 'login', $name );
546 $this->init( $data, $blog_id );
548 $this->data = new stdClass;
553 * Sets up object properties, including capabilities.
555 * @param object $data User DB row object
556 * @param int $blog_id Optional. The blog id to initialize for
558 public function init( $data, $blog_id = '' ) {
560 $this->ID = (int) $data->ID;
562 $this->for_blog( $blog_id );
566 * Return only the main user fields
574 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
575 * @param string|int $value The field value
576 * @return object|false Raw user object
578 public static function get_data_by( $field, $value ) {
581 if ( 'id' == $field ) {
582 // Make sure the value is numeric to avoid casting objects, for example,
584 if ( ! is_numeric( $value ) )
586 $value = intval( $value );
590 $value = trim( $value );
602 $user_id = wp_cache_get($value, 'userslugs');
603 $db_field = 'user_nicename';
606 $user_id = wp_cache_get($value, 'useremail');
607 $db_field = 'user_email';
610 $value = sanitize_user( $value );
611 $user_id = wp_cache_get($value, 'userlogins');
612 $db_field = 'user_login';
618 if ( false !== $user_id ) {
619 if ( $user = wp_cache_get( $user_id, 'users' ) )
623 if ( !$user = $wpdb->get_row( $wpdb->prepare(
624 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
628 update_user_caches( $user );
634 * Makes private/protected methods readable for backwards compatibility.
639 * @param callable $name Method to call.
640 * @param array $arguments Arguments to pass when calling.
641 * @return mixed|false Return value of the callback, false otherwise.
643 public function __call( $name, $arguments ) {
644 if ( '_init_caps' === $name ) {
645 return call_user_func_array( array( $this, $name ), $arguments );
651 * Magic method for checking the existence of a certain custom field
657 public function __isset( $key ) {
658 if ( 'id' == $key ) {
659 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
663 if ( isset( $this->data->$key ) )
666 if ( isset( self::$back_compat_keys[ $key ] ) )
667 $key = self::$back_compat_keys[ $key ];
669 return metadata_exists( 'user', $this->ID, $key );
673 * Magic method for accessing custom fields
679 public function __get( $key ) {
680 if ( 'id' == $key ) {
681 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
685 if ( isset( $this->data->$key ) ) {
686 $value = $this->data->$key;
688 if ( isset( self::$back_compat_keys[ $key ] ) )
689 $key = self::$back_compat_keys[ $key ];
690 $value = get_user_meta( $this->ID, $key, true );
693 if ( $this->filter ) {
694 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
701 * Magic method for setting custom fields
705 public function __set( $key, $value ) {
706 if ( 'id' == $key ) {
707 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
712 $this->data->$key = $value;
716 * Determine whether the user exists in the database.
721 * @return bool True if user exists in the database, false if not.
723 public function exists() {
724 return ! empty( $this->ID );
728 * Retrieve the value of a property or meta key.
730 * Retrieves from the users and usermeta table.
734 * @param string $key Property
737 public function get( $key ) {
738 return $this->__get( $key );
742 * Determine whether a property or meta key is set
744 * Consults the users and usermeta tables.
748 * @param string $key Property
751 public function has_prop( $key ) {
752 return $this->__isset( $key );
756 * Return an array representation.
760 * @return array Array representation.
762 public function to_array() {
763 return get_object_vars( $this->data );
767 * Set up capability object properties.
769 * Will set the value for the 'cap_key' property to current database table
770 * prefix, followed by 'capabilities'. Will then check to see if the
771 * property matching the 'cap_key' exists and is an array. If so, it will be
779 * @param string $cap_key Optional capability key
781 protected function _init_caps( $cap_key = '' ) {
784 if ( empty($cap_key) )
785 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
787 $this->cap_key = $cap_key;
789 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
791 if ( ! is_array( $this->caps ) )
792 $this->caps = array();
794 $this->get_role_caps();
798 * Retrieve all of the role capabilities and merge with individual capabilities.
800 * All of the capabilities of the roles the user belongs to are merged with
801 * the users individual roles. This also means that the user can be denied
802 * specific roles that their role might have, but the specific user isn't
803 * granted permission to.
808 * @return array List of all capabilities for the user.
810 public function get_role_caps() {
811 $wp_roles = wp_roles();
813 //Filter out caps that are not role names and assign to $this->roles
814 if ( is_array( $this->caps ) )
815 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
817 //Build $allcaps from role caps, overlay user's $caps
818 $this->allcaps = array();
819 foreach ( (array) $this->roles as $role ) {
820 $the_role = $wp_roles->get_role( $role );
821 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
823 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
825 return $this->allcaps;
831 * Updates the user's meta data option with capabilities and roles.
836 * @param string $role Role name.
838 public function add_role( $role ) {
839 $this->caps[$role] = true;
840 update_user_meta( $this->ID, $this->cap_key, $this->caps );
841 $this->get_role_caps();
842 $this->update_user_level_from_caps();
845 * Fires immediately after the user has been given a new role.
849 * @param int $user_id The user ID.
850 * @param string $role The new role.
852 do_action( 'add_user_role', $this->ID, $role );
856 * Remove role from user.
861 * @param string $role Role name.
863 public function remove_role( $role ) {
864 if ( !in_array($role, $this->roles) )
866 unset( $this->caps[$role] );
867 update_user_meta( $this->ID, $this->cap_key, $this->caps );
868 $this->get_role_caps();
869 $this->update_user_level_from_caps();
872 * Fires immediately after a role as been removed from a user.
876 * @param int $user_id The user ID.
877 * @param string $role The removed role.
879 do_action( 'remove_user_role', $this->ID, $role );
883 * Set the role of the user.
885 * This will remove the previous roles of the user and assign the user the
886 * new one. You can set the role to an empty string and it will remove all
887 * of the roles from the user.
892 * @param string $role Role name.
894 public function set_role( $role ) {
895 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
898 foreach ( (array) $this->roles as $oldrole )
899 unset( $this->caps[$oldrole] );
901 $old_roles = $this->roles;
902 if ( !empty( $role ) ) {
903 $this->caps[$role] = true;
904 $this->roles = array( $role => true );
906 $this->roles = false;
908 update_user_meta( $this->ID, $this->cap_key, $this->caps );
909 $this->get_role_caps();
910 $this->update_user_level_from_caps();
913 * Fires after the user's role has changed.
916 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
918 * @param int $user_id The user ID.
919 * @param string $role The new role.
920 * @param array $old_roles An array of the user's previous roles.
922 do_action( 'set_user_role', $this->ID, $role, $old_roles );
926 * Choose the maximum level the user has.
928 * Will compare the level from the $item parameter against the $max
929 * parameter. If the item is incorrect, then just the $max parameter value
932 * Used to get the max level based on the capabilities the user has. This
933 * is also based on roles, so if the user is assigned the Administrator role
934 * then the capability 'level_10' will exist and the user will get that
940 * @param int $max Max level of user.
941 * @param string $item Level capability name.
942 * @return int Max Level.
944 public function level_reduction( $max, $item ) {
945 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
946 $level = intval( $matches[1] );
947 return max( $max, $level );
954 * Update the maximum user level for the user.
956 * Updates the 'user_level' user metadata (includes prefix that is the
957 * database table prefix) with the maximum user level. Gets the value from
958 * the all of the capabilities that the user has.
965 public function update_user_level_from_caps() {
967 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
968 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
972 * Add capability and grant or deny access to capability.
977 * @param string $cap Capability name.
978 * @param bool $grant Whether to grant capability to user.
980 public function add_cap( $cap, $grant = true ) {
981 $this->caps[$cap] = $grant;
982 update_user_meta( $this->ID, $this->cap_key, $this->caps );
983 $this->get_role_caps();
984 $this->update_user_level_from_caps();
988 * Remove capability from user.
993 * @param string $cap Capability name.
995 public function remove_cap( $cap ) {
996 if ( ! isset( $this->caps[ $cap ] ) ) {
999 unset( $this->caps[ $cap ] );
1000 update_user_meta( $this->ID, $this->cap_key, $this->caps );
1001 $this->get_role_caps();
1002 $this->update_user_level_from_caps();
1006 * Remove all of the capabilities of the user.
1011 * @global wpdb $wpdb
1013 public function remove_all_caps() {
1015 $this->caps = array();
1016 delete_user_meta( $this->ID, $this->cap_key );
1017 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
1018 $this->get_role_caps();
1022 * Whether user has capability or role name.
1024 * This is useful for looking up whether the user has a specific role
1025 * assigned to the user. The second optional parameter can also be used to
1026 * check for capabilities against a specific object, such as a post or user.
1031 * @param string|int $cap Capability or role name to search.
1032 * @return bool True, if user has capability; false, if user does not have capability.
1034 public function has_cap( $cap ) {
1035 if ( is_numeric( $cap ) ) {
1036 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
1037 $cap = $this->translate_level_to_cap( $cap );
1040 $args = array_slice( func_get_args(), 1 );
1041 $args = array_merge( array( $cap, $this->ID ), $args );
1042 $caps = call_user_func_array( 'map_meta_cap', $args );
1044 // Multisite super admin has all caps by definition, Unless specifically denied.
1045 if ( is_multisite() && is_super_admin( $this->ID ) ) {
1046 if ( in_array('do_not_allow', $caps) )
1052 * Dynamically filter a user's capabilities.
1055 * @since 3.7.0 Added the user object.
1057 * @param array $allcaps An array of all the user's capabilities.
1058 * @param array $caps Actual capabilities for meta capability.
1059 * @param array $args Optional parameters passed to has_cap(), typically object ID.
1060 * @param WP_User $user The user object.
1062 // Must have ALL requested caps
1063 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
1064 $capabilities['exist'] = true; // Everyone is allowed to exist
1065 foreach ( (array) $caps as $cap ) {
1066 if ( empty( $capabilities[ $cap ] ) )
1074 * Convert numeric level to level capability name.
1076 * Prepends 'level_' to level number.
1081 * @param int $level Level number, 1 to 10.
1084 public function translate_level_to_cap( $level ) {
1085 return 'level_' . $level;
1089 * Set the blog to operate on. Defaults to the current blog.
1093 * @global wpdb $wpdb
1095 * @param int $blog_id Optional Blog ID, defaults to current blog.
1097 public function for_blog( $blog_id = '' ) {
1099 if ( ! empty( $blog_id ) )
1100 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
1103 $this->_init_caps( $cap_key );
1108 * Map meta capabilities to primitive capabilities.
1110 * This does not actually compare whether the user ID has the actual capability,
1111 * just what the capability or capabilities are. Meta capability list value can
1112 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
1113 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
1117 * @param string $cap Capability name.
1118 * @param int $user_id User ID.
1119 * @return array Actual capabilities for meta capability.
1121 function map_meta_cap( $cap, $user_id ) {
1122 $args = array_slice( func_get_args(), 2 );
1127 $caps[] = 'remove_users';
1129 case 'promote_user':
1130 $caps[] = 'promote_users';
1134 // Allow user to edit itself
1135 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
1138 // If multisite these caps are allowed only for super admins.
1139 if ( is_multisite() && !is_super_admin( $user_id ) )
1140 $caps[] = 'do_not_allow';
1142 $caps[] = 'edit_users'; // edit_user maps to edit_users.
1146 $post = get_post( $args[0] );
1148 if ( 'revision' == $post->post_type ) {
1149 $post = get_post( $post->post_parent );
1152 $post_type = get_post_type_object( $post->post_type );
1154 if ( ! $post_type->map_meta_cap ) {
1155 $caps[] = $post_type->cap->$cap;
1156 // Prior to 3.1 we would re-call map_meta_cap here.
1157 if ( 'delete_post' == $cap )
1158 $cap = $post_type->cap->$cap;
1162 // If the post author is set and the user is the author...
1163 if ( $post->post_author && $user_id == $post->post_author ) {
1164 // If the post is published...
1165 if ( 'publish' == $post->post_status ) {
1166 $caps[] = $post_type->cap->delete_published_posts;
1167 } elseif ( 'trash' == $post->post_status ) {
1168 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1169 $caps[] = $post_type->cap->delete_published_posts;
1172 // If the post is draft...
1173 $caps[] = $post_type->cap->delete_posts;
1176 // The user is trying to edit someone else's post.
1177 $caps[] = $post_type->cap->delete_others_posts;
1178 // The post is published, extra cap required.
1179 if ( 'publish' == $post->post_status ) {
1180 $caps[] = $post_type->cap->delete_published_posts;
1181 } elseif ( 'private' == $post->post_status ) {
1182 $caps[] = $post_type->cap->delete_private_posts;
1186 // edit_post breaks down to edit_posts, edit_published_posts, or
1187 // edit_others_posts
1190 $post = get_post( $args[0] );
1191 if ( empty( $post ) ) {
1192 $caps[] = 'do_not_allow';
1196 if ( 'revision' == $post->post_type ) {
1197 $post = get_post( $post->post_parent );
1200 $post_type = get_post_type_object( $post->post_type );
1202 if ( ! $post_type->map_meta_cap ) {
1203 $caps[] = $post_type->cap->$cap;
1204 // Prior to 3.1 we would re-call map_meta_cap here.
1205 if ( 'edit_post' == $cap )
1206 $cap = $post_type->cap->$cap;
1210 // If the post author is set and the user is the author...
1211 if ( $post->post_author && $user_id == $post->post_author ) {
1212 // If the post is published...
1213 if ( 'publish' == $post->post_status ) {
1214 $caps[] = $post_type->cap->edit_published_posts;
1215 } elseif ( 'trash' == $post->post_status ) {
1216 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1217 $caps[] = $post_type->cap->edit_published_posts;
1220 // If the post is draft...
1221 $caps[] = $post_type->cap->edit_posts;
1224 // The user is trying to edit someone else's post.
1225 $caps[] = $post_type->cap->edit_others_posts;
1226 // The post is published, extra cap required.
1227 if ( 'publish' == $post->post_status ) {
1228 $caps[] = $post_type->cap->edit_published_posts;
1229 } elseif ( 'private' == $post->post_status ) {
1230 $caps[] = $post_type->cap->edit_private_posts;
1236 $post = get_post( $args[0] );
1238 if ( 'revision' == $post->post_type ) {
1239 $post = get_post( $post->post_parent );
1242 $post_type = get_post_type_object( $post->post_type );
1244 if ( ! $post_type->map_meta_cap ) {
1245 $caps[] = $post_type->cap->$cap;
1246 // Prior to 3.1 we would re-call map_meta_cap here.
1247 if ( 'read_post' == $cap )
1248 $cap = $post_type->cap->$cap;
1252 $status_obj = get_post_status_object( $post->post_status );
1253 if ( $status_obj->public ) {
1254 $caps[] = $post_type->cap->read;
1258 if ( $post->post_author && $user_id == $post->post_author ) {
1259 $caps[] = $post_type->cap->read;
1260 } elseif ( $status_obj->private ) {
1261 $caps[] = $post_type->cap->read_private_posts;
1263 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1266 case 'publish_post':
1267 $post = get_post( $args[0] );
1268 $post_type = get_post_type_object( $post->post_type );
1270 $caps[] = $post_type->cap->publish_posts;
1272 case 'edit_post_meta':
1273 case 'delete_post_meta':
1274 case 'add_post_meta':
1275 $post = get_post( $args[0] );
1276 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1278 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1280 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1282 * Filter whether the user is allowed to add post meta to a post.
1284 * The dynamic portion of the hook name, `$meta_key`, refers to the
1285 * meta key passed to {@see map_meta_cap()}.
1289 * @param bool $allowed Whether the user can add the post meta. Default false.
1290 * @param string $meta_key The meta key.
1291 * @param int $post_id Post ID.
1292 * @param int $user_id User ID.
1293 * @param string $cap Capability name.
1294 * @param array $caps User capabilities.
1296 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1299 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1303 case 'edit_comment':
1304 $comment = get_comment( $args[0] );
1305 if ( empty( $comment ) )
1307 $post = get_post( $comment->comment_post_ID );
1310 * If the post doesn't exist, we have an orphaned comment.
1311 * Fall back to the edit_posts capability, instead.
1314 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1316 $caps = map_meta_cap( 'edit_posts', $user_id );
1319 case 'unfiltered_upload':
1320 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
1323 $caps[] = 'do_not_allow';
1325 case 'unfiltered_html' :
1326 // Disallow unfiltered_html for all users, even admins and super admins.
1327 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
1328 $caps[] = 'do_not_allow';
1329 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1330 $caps[] = 'do_not_allow';
1335 case 'edit_plugins':
1337 // Disallow the file editors.
1338 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
1339 $caps[] = 'do_not_allow';
1340 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1341 $caps[] = 'do_not_allow';
1342 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1343 $caps[] = 'do_not_allow';
1347 case 'update_plugins':
1348 case 'delete_plugins':
1349 case 'install_plugins':
1350 case 'upload_plugins':
1351 case 'update_themes':
1352 case 'delete_themes':
1353 case 'install_themes':
1354 case 'upload_themes':
1356 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
1357 // Files in uploads are excepted.
1358 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
1359 $caps[] = 'do_not_allow';
1360 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
1361 $caps[] = 'do_not_allow';
1362 } elseif ( 'upload_themes' === $cap ) {
1363 $caps[] = 'install_themes';
1364 } elseif ( 'upload_plugins' === $cap ) {
1365 $caps[] = 'install_plugins';
1370 case 'activate_plugins':
1372 if ( is_multisite() ) {
1373 // update_, install_, and delete_ are handled above with is_super_admin().
1374 $menu_perms = get_site_option( 'menu_items', array() );
1375 if ( empty( $menu_perms['plugins'] ) )
1376 $caps[] = 'manage_network_plugins';
1380 case 'delete_users':
1381 // If multisite only super admins can delete users.
1382 if ( is_multisite() && ! is_super_admin( $user_id ) )
1383 $caps[] = 'do_not_allow';
1385 $caps[] = 'delete_users'; // delete_user maps to delete_users.
1387 case 'create_users':
1388 if ( !is_multisite() )
1390 elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) )
1393 $caps[] = 'do_not_allow';
1395 case 'manage_links' :
1396 if ( get_option( 'link_manager_enabled' ) )
1399 $caps[] = 'do_not_allow';
1402 $caps[] = 'edit_theme_options';
1405 $caps[] = 'manage_options';
1408 // Handle meta capabilities for custom post types.
1409 $post_type_meta_caps = _post_type_meta_capabilities();
1410 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1411 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1412 return call_user_func_array( 'map_meta_cap', $args );
1415 // If no meta caps match, return the original cap.
1420 * Filter a user's capabilities depending on specific context and/or privilege.
1424 * @param array $caps Returns the user's actual capabilities.
1425 * @param string $cap Capability name.
1426 * @param int $user_id The user ID.
1427 * @param array $args Adds the context to the cap. Typically the object ID.
1429 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
1433 * Whether current user has capability or role.
1437 * @param string $capability Capability or role name.
1440 function current_user_can( $capability ) {
1441 $current_user = wp_get_current_user();
1443 if ( empty( $current_user ) )
1446 $args = array_slice( func_get_args(), 1 );
1447 $args = array_merge( array( $capability ), $args );
1449 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
1453 * Whether current user has a capability or role for a given blog.
1457 * @param int $blog_id Blog ID
1458 * @param string $capability Capability or role name.
1461 function current_user_can_for_blog( $blog_id, $capability ) {
1462 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
1464 $current_user = wp_get_current_user();
1466 if ( empty( $current_user ) ) {
1468 restore_current_blog();
1473 $args = array_slice( func_get_args(), 2 );
1474 $args = array_merge( array( $capability ), $args );
1476 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
1479 restore_current_blog();
1486 * Whether author of supplied post has capability or role.
1490 * @param int|object $post Post ID or post object.
1491 * @param string $capability Capability or role name.
1494 function author_can( $post, $capability ) {
1495 if ( !$post = get_post($post) )
1498 $author = get_userdata( $post->post_author );
1503 $args = array_slice( func_get_args(), 2 );
1504 $args = array_merge( array( $capability ), $args );
1506 return call_user_func_array( array( $author, 'has_cap' ), $args );
1510 * Whether a particular user has capability or role.
1514 * @param int|object $user User ID or object.
1515 * @param string $capability Capability or role name.
1518 function user_can( $user, $capability ) {
1519 if ( ! is_object( $user ) )
1520 $user = get_userdata( $user );
1522 if ( ! $user || ! $user->exists() )
1525 $args = array_slice( func_get_args(), 2 );
1526 $args = array_merge( array( $capability ), $args );
1528 return call_user_func_array( array( $user, 'has_cap' ), $args );
1532 * Retrieves the global WP_Roles instance and instantiates it if necessary.
1536 * @global WP_Roles $wp_roles WP_Roles global instance.
1538 * @return WP_Roles WP_Roles global instance if not already instantiated.
1540 function wp_roles() {
1543 if ( ! isset( $wp_roles ) ) {
1544 $wp_roles = new WP_Roles();
1550 * Retrieve role object.
1554 * @param string $role Role name.
1555 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
1557 function get_role( $role ) {
1558 return wp_roles()->get_role( $role );
1562 * Add role, if it does not exist.
1566 * @param string $role Role name.
1567 * @param string $display_name Display name for role.
1568 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1569 * @return WP_Role|null WP_Role object if role is added, null if already exists.
1571 function add_role( $role, $display_name, $capabilities = array() ) {
1572 return wp_roles()->add_role( $role, $display_name, $capabilities );
1576 * Remove role, if it exists.
1580 * @param string $role Role name.
1582 function remove_role( $role ) {
1583 wp_roles()->remove_role( $role );
1587 * Retrieve a list of super admins.
1591 * @global array $super_admins
1593 * @return array List of super admin logins
1595 function get_super_admins() {
1596 global $super_admins;
1598 if ( isset($super_admins) )
1599 return $super_admins;
1601 return get_site_option( 'site_admins', array('admin') );
1605 * Determine if user is a site admin.
1609 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1610 * @return bool True if the user is a site admin.
1612 function is_super_admin( $user_id = false ) {
1613 if ( ! $user_id || $user_id == get_current_user_id() )
1614 $user = wp_get_current_user();
1616 $user = get_userdata( $user_id );
1618 if ( ! $user || ! $user->exists() )
1621 if ( is_multisite() ) {
1622 $super_admins = get_super_admins();
1623 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1626 if ( $user->has_cap('delete_users') )