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 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.
142 public function reinit() {
143 // There is no need to reinit if using the wp_user_roles global.
144 if ( ! $this->use_db )
149 // Duplicated from _init() to avoid an extra function call.
150 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
151 $this->roles = get_option( $this->role_key );
152 if ( empty( $this->roles ) )
155 $this->role_objects = array();
156 $this->role_names = array();
157 foreach ( array_keys( $this->roles ) as $role ) {
158 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
159 $this->role_names[$role] = $this->roles[$role]['name'];
164 * Add role name with capabilities to list.
166 * Updates the list of roles, if the role doesn't already exist.
168 * The capabilities are defined in the following format `array( 'read' => true );`
169 * To explicitly deny a role a capability you set the value for that capability to false.
174 * @param string $role Role name.
175 * @param string $display_name Role display name.
176 * @param array $capabilities List of role capabilities in the above format.
177 * @return WP_Role|null WP_Role object if role is added, null if already exists.
179 public function add_role( $role, $display_name, $capabilities = array() ) {
180 if ( isset( $this->roles[$role] ) )
183 $this->roles[$role] = array(
184 'name' => $display_name,
185 'capabilities' => $capabilities
188 update_option( $this->role_key, $this->roles );
189 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
190 $this->role_names[$role] = $display_name;
191 return $this->role_objects[$role];
195 * Remove role by name.
200 * @param string $role Role name.
202 public function remove_role( $role ) {
203 if ( ! isset( $this->role_objects[$role] ) )
206 unset( $this->role_objects[$role] );
207 unset( $this->role_names[$role] );
208 unset( $this->roles[$role] );
211 update_option( $this->role_key, $this->roles );
213 if ( get_option( 'default_role' ) == $role )
214 update_option( 'default_role', 'subscriber' );
218 * Add capability to role.
223 * @param string $role Role name.
224 * @param string $cap Capability name.
225 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
227 public function add_cap( $role, $cap, $grant = true ) {
228 if ( ! isset( $this->roles[$role] ) )
231 $this->roles[$role]['capabilities'][$cap] = $grant;
233 update_option( $this->role_key, $this->roles );
237 * Remove capability from role.
242 * @param string $role Role name.
243 * @param string $cap Capability name.
245 public function remove_cap( $role, $cap ) {
246 if ( ! isset( $this->roles[$role] ) )
249 unset( $this->roles[$role]['capabilities'][$cap] );
251 update_option( $this->role_key, $this->roles );
255 * Retrieve role object by name.
260 * @param string $role Role name.
261 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
263 public function get_role( $role ) {
264 if ( isset( $this->role_objects[$role] ) )
265 return $this->role_objects[$role];
271 * Retrieve list of role names.
276 * @return array List of role names.
278 public function get_names() {
279 return $this->role_names;
283 * Whether role name is currently in the list of available roles.
288 * @param string $role Role name to look up.
291 public function is_role( $role ) {
292 return isset( $this->role_names[$role] );
297 * WordPress Role class.
314 * List of capabilities the role contains.
320 public $capabilities;
323 * Constructor - Set up object properties.
325 * The list of capabilities, must have the key as the name of the capability
326 * and the value a boolean of whether it is granted to the role.
331 * @param string $role Role name.
332 * @param array $capabilities List of capabilities.
334 public function __construct( $role, $capabilities ) {
336 $this->capabilities = $capabilities;
340 * Assign role a capability.
342 * @see WP_Roles::add_cap() Method uses implementation for role.
346 * @param string $cap Capability name.
347 * @param bool $grant Whether role has capability privilege.
349 public function add_cap( $cap, $grant = true ) {
352 if ( ! isset( $wp_roles ) )
353 $wp_roles = new WP_Roles();
355 $this->capabilities[$cap] = $grant;
356 $wp_roles->add_cap( $this->name, $cap, $grant );
360 * Remove capability from role.
362 * This is a container for {@link WP_Roles::remove_cap()} to remove the
363 * capability from the role. That is to say, that {@link
364 * WP_Roles::remove_cap()} implements the functionality, but it also makes
365 * sense to use this class, because you don't need to enter the role name.
370 * @param string $cap Capability name.
372 public function remove_cap( $cap ) {
375 if ( ! isset( $wp_roles ) )
376 $wp_roles = new WP_Roles();
378 unset( $this->capabilities[$cap] );
379 $wp_roles->remove_cap( $this->name, $cap );
383 * Whether role has capability.
385 * The capabilities is passed through the 'role_has_cap' filter. The first
386 * parameter for the hook is the list of capabilities the class has
387 * assigned. The second parameter is the capability name to look for. The
388 * third and final parameter for the hook is the role name.
393 * @param string $cap Capability name.
394 * @return bool True, if user has capability. False, if doesn't have capability.
396 public function has_cap( $cap ) {
398 * Filter which capabilities a role has.
402 * @param array $capabilities Array of role capabilities.
403 * @param string $cap Capability name.
404 * @param string $name Role name.
406 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
407 if ( !empty( $capabilities[$cap] ) )
408 return $capabilities[$cap];
416 * WordPress User class.
422 * @property string $nickname
423 * @property string $user_description
424 * @property string $user_firstname
425 * @property string $user_lastname
426 * @property string $user_login
427 * @property string $user_pass
428 * @property string $user_nicename
429 * @property string $user_email
430 * @property string $user_url
431 * @property string $user_registered
432 * @property string $user_activation_key
433 * @property string $user_status
434 * @property string $display_name
435 * @property string $spam
436 * @property string $deleted
440 * User data container.
457 * The individual capabilities the user has been given.
463 public $caps = array();
466 * User metadata option name.
475 * The roles the user is part of.
481 public $roles = array();
484 * All capabilities the user has, including individual and role based.
490 public $allcaps = array();
493 * The filter context applied to user data fields.
501 private static $back_compat_keys;
506 * Retrieves the userdata and passes it to {@link WP_User::init()}.
511 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
512 * @param string $name Optional. User's username
513 * @param int $blog_id Optional Blog ID, defaults to current blog.
515 public function __construct( $id = 0, $name = '', $blog_id = '' ) {
516 if ( ! isset( self::$back_compat_keys ) ) {
517 $prefix = $GLOBALS['wpdb']->prefix;
518 self::$back_compat_keys = array(
519 'user_firstname' => 'first_name',
520 'user_lastname' => 'last_name',
521 'user_description' => 'description',
522 'user_level' => $prefix . 'user_level',
523 $prefix . 'usersettings' => $prefix . 'user-settings',
524 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
528 if ( $id instanceof WP_User ) {
529 $this->init( $id->data, $blog_id );
531 } elseif ( is_object( $id ) ) {
532 $this->init( $id, $blog_id );
536 if ( ! empty( $id ) && ! is_numeric( $id ) ) {
542 $data = self::get_data_by( 'id', $id );
544 $data = self::get_data_by( 'login', $name );
548 $this->init( $data, $blog_id );
550 $this->data = new stdClass;
555 * Sets up object properties, including capabilities.
557 * @param object $data User DB row object
558 * @param int $blog_id Optional. The blog id to initialize for
560 public function init( $data, $blog_id = '' ) {
562 $this->ID = (int) $data->ID;
564 $this->for_blog( $blog_id );
568 * Return only the main user fields
572 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
573 * @param string|int $value The field value
574 * @return object|false Raw user object
576 public static function get_data_by( $field, $value ) {
579 if ( 'id' == $field ) {
580 // Make sure the value is numeric to avoid casting objects, for example,
582 if ( ! is_numeric( $value ) )
584 $value = intval( $value );
588 $value = trim( $value );
600 $user_id = wp_cache_get($value, 'userslugs');
601 $db_field = 'user_nicename';
604 $user_id = wp_cache_get($value, 'useremail');
605 $db_field = 'user_email';
608 $value = sanitize_user( $value );
609 $user_id = wp_cache_get($value, 'userlogins');
610 $db_field = 'user_login';
616 if ( false !== $user_id ) {
617 if ( $user = wp_cache_get( $user_id, 'users' ) )
621 if ( !$user = $wpdb->get_row( $wpdb->prepare(
622 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
626 update_user_caches( $user );
632 * Magic method for checking the existence of a certain custom field
638 public function __isset( $key ) {
639 if ( 'id' == $key ) {
640 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
644 if ( isset( $this->data->$key ) )
647 if ( isset( self::$back_compat_keys[ $key ] ) )
648 $key = self::$back_compat_keys[ $key ];
650 return metadata_exists( 'user', $this->ID, $key );
654 * Magic method for accessing custom fields
660 public function __get( $key ) {
661 if ( 'id' == $key ) {
662 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
666 if ( isset( $this->data->$key ) ) {
667 $value = $this->data->$key;
669 if ( isset( self::$back_compat_keys[ $key ] ) )
670 $key = self::$back_compat_keys[ $key ];
671 $value = get_user_meta( $this->ID, $key, true );
674 if ( $this->filter ) {
675 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
682 * Magic method for setting custom fields
686 public function __set( $key, $value ) {
687 if ( 'id' == $key ) {
688 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
693 $this->data->$key = $value;
697 * Determine whether the user exists in the database.
702 * @return bool True if user exists in the database, false if not.
704 public function exists() {
705 return ! empty( $this->ID );
709 * Retrieve the value of a property or meta key.
711 * Retrieves from the users and usermeta table.
715 * @param string $key Property
717 public function get( $key ) {
718 return $this->__get( $key );
722 * Determine whether a property or meta key is set
724 * Consults the users and usermeta tables.
728 * @param string $key Property
730 public function has_prop( $key ) {
731 return $this->__isset( $key );
735 * Return an array representation.
739 * @return array Array representation.
741 public function to_array() {
742 return get_object_vars( $this->data );
746 * Set up capability object properties.
748 * Will set the value for the 'cap_key' property to current database table
749 * prefix, followed by 'capabilities'. Will then check to see if the
750 * property matching the 'cap_key' exists and is an array. If so, it will be
756 * @param string $cap_key Optional capability key
758 function _init_caps( $cap_key = '' ) {
761 if ( empty($cap_key) )
762 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
764 $this->cap_key = $cap_key;
766 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
768 if ( ! is_array( $this->caps ) )
769 $this->caps = array();
771 $this->get_role_caps();
775 * Retrieve all of the role capabilities and merge with individual capabilities.
777 * All of the capabilities of the roles the user belongs to are merged with
778 * the users individual roles. This also means that the user can be denied
779 * specific roles that their role might have, but the specific user isn't
780 * granted permission to.
786 * @return array List of all capabilities for the user.
788 public function get_role_caps() {
791 if ( ! isset( $wp_roles ) )
792 $wp_roles = new WP_Roles();
794 //Filter out caps that are not role names and assign to $this->roles
795 if ( is_array( $this->caps ) )
796 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
798 //Build $allcaps from role caps, overlay user's $caps
799 $this->allcaps = array();
800 foreach ( (array) $this->roles as $role ) {
801 $the_role = $wp_roles->get_role( $role );
802 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
804 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
806 return $this->allcaps;
812 * Updates the user's meta data option with capabilities and roles.
817 * @param string $role Role name.
819 public function add_role( $role ) {
820 $this->caps[$role] = true;
821 update_user_meta( $this->ID, $this->cap_key, $this->caps );
822 $this->get_role_caps();
823 $this->update_user_level_from_caps();
827 * Remove role from user.
832 * @param string $role Role name.
834 public function remove_role( $role ) {
835 if ( !in_array($role, $this->roles) )
837 unset( $this->caps[$role] );
838 update_user_meta( $this->ID, $this->cap_key, $this->caps );
839 $this->get_role_caps();
840 $this->update_user_level_from_caps();
844 * Set the role of the user.
846 * This will remove the previous roles of the user and assign the user the
847 * new one. You can set the role to an empty string and it will remove all
848 * of the roles from the user.
853 * @param string $role Role name.
855 public function set_role( $role ) {
856 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
859 foreach ( (array) $this->roles as $oldrole )
860 unset( $this->caps[$oldrole] );
862 $old_roles = $this->roles;
863 if ( !empty( $role ) ) {
864 $this->caps[$role] = true;
865 $this->roles = array( $role => true );
867 $this->roles = false;
869 update_user_meta( $this->ID, $this->cap_key, $this->caps );
870 $this->get_role_caps();
871 $this->update_user_level_from_caps();
874 * Fires after the user's role has changed.
877 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
879 * @param int $user_id The user ID.
880 * @param string $role The new role.
881 * @param array $old_roles An array of the user's previous roles.
883 do_action( 'set_user_role', $this->ID, $role, $old_roles );
887 * Choose the maximum level the user has.
889 * Will compare the level from the $item parameter against the $max
890 * parameter. If the item is incorrect, then just the $max parameter value
893 * Used to get the max level based on the capabilities the user has. This
894 * is also based on roles, so if the user is assigned the Administrator role
895 * then the capability 'level_10' will exist and the user will get that
901 * @param int $max Max level of user.
902 * @param string $item Level capability name.
903 * @return int Max Level.
905 public function level_reduction( $max, $item ) {
906 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
907 $level = intval( $matches[1] );
908 return max( $max, $level );
915 * Update the maximum user level for the user.
917 * Updates the 'user_level' user metadata (includes prefix that is the
918 * database table prefix) with the maximum user level. Gets the value from
919 * the all of the capabilities that the user has.
924 public function update_user_level_from_caps() {
926 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
927 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
931 * Add capability and grant or deny access to capability.
936 * @param string $cap Capability name.
937 * @param bool $grant Whether to grant capability to user.
939 public function add_cap( $cap, $grant = true ) {
940 $this->caps[$cap] = $grant;
941 update_user_meta( $this->ID, $this->cap_key, $this->caps );
942 $this->get_role_caps();
943 $this->update_user_level_from_caps();
947 * Remove capability from user.
952 * @param string $cap Capability name.
954 public function remove_cap( $cap ) {
955 if ( ! isset( $this->caps[ $cap ] ) ) {
958 unset( $this->caps[ $cap ] );
959 update_user_meta( $this->ID, $this->cap_key, $this->caps );
960 $this->get_role_caps();
961 $this->update_user_level_from_caps();
965 * Remove all of the capabilities of the user.
970 public function remove_all_caps() {
972 $this->caps = array();
973 delete_user_meta( $this->ID, $this->cap_key );
974 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
975 $this->get_role_caps();
979 * Whether user has capability or role name.
981 * This is useful for looking up whether the user has a specific role
982 * assigned to the user. The second optional parameter can also be used to
983 * check for capabilities against a specific object, such as a post or user.
988 * @param string|int $cap Capability or role name to search.
989 * @return bool True, if user has capability; false, if user does not have capability.
991 public function has_cap( $cap ) {
992 if ( is_numeric( $cap ) ) {
993 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
994 $cap = $this->translate_level_to_cap( $cap );
997 $args = array_slice( func_get_args(), 1 );
998 $args = array_merge( array( $cap, $this->ID ), $args );
999 $caps = call_user_func_array( 'map_meta_cap', $args );
1001 // Multisite super admin has all caps by definition, Unless specifically denied.
1002 if ( is_multisite() && is_super_admin( $this->ID ) ) {
1003 if ( in_array('do_not_allow', $caps) )
1009 * Dynamically filter a user's capabilities.
1012 * @since 3.7.0 Added the user object.
1014 * @param array $allcaps An array of all the user's capabilities.
1015 * @param array $caps Actual capabilities for meta capability.
1016 * @param array $args Optional parameters passed to has_cap(), typically object ID.
1017 * @param WP_User $user The user object.
1019 // Must have ALL requested caps
1020 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
1021 $capabilities['exist'] = true; // Everyone is allowed to exist
1022 foreach ( (array) $caps as $cap ) {
1023 if ( empty( $capabilities[ $cap ] ) )
1031 * Convert numeric level to level capability name.
1033 * Prepends 'level_' to level number.
1038 * @param int $level Level number, 1 to 10.
1041 public function translate_level_to_cap( $level ) {
1042 return 'level_' . $level;
1046 * Set the blog to operate on. Defaults to the current blog.
1050 * @param int $blog_id Optional Blog ID, defaults to current blog.
1052 public function for_blog( $blog_id = '' ) {
1054 if ( ! empty( $blog_id ) )
1055 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
1058 $this->_init_caps( $cap_key );
1063 * Map meta capabilities to primitive capabilities.
1065 * This does not actually compare whether the user ID has the actual capability,
1066 * just what the capability or capabilities are. Meta capability list value can
1067 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
1068 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
1072 * @param string $cap Capability name.
1073 * @param int $user_id User ID.
1074 * @return array Actual capabilities for meta capability.
1076 function map_meta_cap( $cap, $user_id ) {
1077 $args = array_slice( func_get_args(), 2 );
1082 $caps[] = 'remove_users';
1084 case 'promote_user':
1085 $caps[] = 'promote_users';
1089 // Allow user to edit itself
1090 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
1093 // If multisite these caps are allowed only for super admins.
1094 if ( is_multisite() && !is_super_admin( $user_id ) )
1095 $caps[] = 'do_not_allow';
1097 $caps[] = 'edit_users'; // edit_user maps to edit_users.
1101 $post = get_post( $args[0] );
1103 if ( 'revision' == $post->post_type ) {
1104 $post = get_post( $post->post_parent );
1107 $post_type = get_post_type_object( $post->post_type );
1109 if ( ! $post_type->map_meta_cap ) {
1110 $caps[] = $post_type->cap->$cap;
1111 // Prior to 3.1 we would re-call map_meta_cap here.
1112 if ( 'delete_post' == $cap )
1113 $cap = $post_type->cap->$cap;
1117 // If the post author is set and the user is the author...
1118 if ( $post->post_author && $user_id == $post->post_author ) {
1119 // If the post is published...
1120 if ( 'publish' == $post->post_status ) {
1121 $caps[] = $post_type->cap->delete_published_posts;
1122 } elseif ( 'trash' == $post->post_status ) {
1123 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1124 $caps[] = $post_type->cap->delete_published_posts;
1127 // If the post is draft...
1128 $caps[] = $post_type->cap->delete_posts;
1131 // The user is trying to edit someone else's post.
1132 $caps[] = $post_type->cap->delete_others_posts;
1133 // The post is published, extra cap required.
1134 if ( 'publish' == $post->post_status ) {
1135 $caps[] = $post_type->cap->delete_published_posts;
1136 } elseif ( 'private' == $post->post_status ) {
1137 $caps[] = $post_type->cap->delete_private_posts;
1141 // edit_post breaks down to edit_posts, edit_published_posts, or
1142 // edit_others_posts
1145 $post = get_post( $args[0] );
1146 if ( empty( $post ) ) {
1147 $caps[] = 'do_not_allow';
1151 if ( 'revision' == $post->post_type ) {
1152 $post = get_post( $post->post_parent );
1155 $post_type = get_post_type_object( $post->post_type );
1157 if ( ! $post_type->map_meta_cap ) {
1158 $caps[] = $post_type->cap->$cap;
1159 // Prior to 3.1 we would re-call map_meta_cap here.
1160 if ( 'edit_post' == $cap )
1161 $cap = $post_type->cap->$cap;
1165 // If the post author is set and the user is the author...
1166 if ( $post->post_author && $user_id == $post->post_author ) {
1167 // If the post is published...
1168 if ( 'publish' == $post->post_status ) {
1169 $caps[] = $post_type->cap->edit_published_posts;
1170 } elseif ( 'trash' == $post->post_status ) {
1171 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1172 $caps[] = $post_type->cap->edit_published_posts;
1175 // If the post is draft...
1176 $caps[] = $post_type->cap->edit_posts;
1179 // The user is trying to edit someone else's post.
1180 $caps[] = $post_type->cap->edit_others_posts;
1181 // The post is published, extra cap required.
1182 if ( 'publish' == $post->post_status ) {
1183 $caps[] = $post_type->cap->edit_published_posts;
1184 } elseif ( 'private' == $post->post_status ) {
1185 $caps[] = $post_type->cap->edit_private_posts;
1191 $post = get_post( $args[0] );
1193 if ( 'revision' == $post->post_type ) {
1194 $post = get_post( $post->post_parent );
1197 $post_type = get_post_type_object( $post->post_type );
1199 if ( ! $post_type->map_meta_cap ) {
1200 $caps[] = $post_type->cap->$cap;
1201 // Prior to 3.1 we would re-call map_meta_cap here.
1202 if ( 'read_post' == $cap )
1203 $cap = $post_type->cap->$cap;
1207 $status_obj = get_post_status_object( $post->post_status );
1208 if ( $status_obj->public ) {
1209 $caps[] = $post_type->cap->read;
1213 if ( $post->post_author && $user_id == $post->post_author ) {
1214 $caps[] = $post_type->cap->read;
1215 } elseif ( $status_obj->private ) {
1216 $caps[] = $post_type->cap->read_private_posts;
1218 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1221 case 'publish_post':
1222 $post = get_post( $args[0] );
1223 $post_type = get_post_type_object( $post->post_type );
1225 $caps[] = $post_type->cap->publish_posts;
1227 case 'edit_post_meta':
1228 case 'delete_post_meta':
1229 case 'add_post_meta':
1230 $post = get_post( $args[0] );
1231 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1233 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1235 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1237 * Filter whether the user is allowed to add post meta to a post.
1239 * The dynamic portion of the hook name, `$meta_key`, refers to the
1240 * meta key passed to {@see map_meta_cap()}.
1244 * @param bool $allowed Whether the user can add the post meta. Default false.
1245 * @param string $meta_key The meta key.
1246 * @param int $post_id Post ID.
1247 * @param int $user_id User ID.
1248 * @param string $cap Capability name.
1249 * @param array $caps User capabilities.
1251 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1254 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1258 case 'edit_comment':
1259 $comment = get_comment( $args[0] );
1260 if ( empty( $comment ) )
1262 $post = get_post( $comment->comment_post_ID );
1265 * If the post doesn't exist, we have an orphaned comment.
1266 * Fall back to the edit_posts capability, instead.
1269 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1271 $caps = map_meta_cap( 'edit_posts', $user_id );
1274 case 'unfiltered_upload':
1275 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
1278 $caps[] = 'do_not_allow';
1280 case 'unfiltered_html' :
1281 // Disallow unfiltered_html for all users, even admins and super admins.
1282 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
1283 $caps[] = 'do_not_allow';
1284 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1285 $caps[] = 'do_not_allow';
1290 case 'edit_plugins':
1292 // Disallow the file editors.
1293 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
1294 $caps[] = 'do_not_allow';
1295 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1296 $caps[] = 'do_not_allow';
1297 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1298 $caps[] = 'do_not_allow';
1302 case 'update_plugins':
1303 case 'delete_plugins':
1304 case 'install_plugins':
1305 case 'upload_plugins':
1306 case 'update_themes':
1307 case 'delete_themes':
1308 case 'install_themes':
1309 case 'upload_themes':
1311 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
1312 // Files in uploads are excepted.
1313 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) {
1314 $caps[] = 'do_not_allow';
1315 } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) {
1316 $caps[] = 'do_not_allow';
1317 } elseif ( 'upload_themes' === $cap ) {
1318 $caps[] = 'install_themes';
1319 } elseif ( 'upload_plugins' === $cap ) {
1320 $caps[] = 'install_plugins';
1325 case 'activate_plugins':
1327 if ( is_multisite() ) {
1328 // update_, install_, and delete_ are handled above with is_super_admin().
1329 $menu_perms = get_site_option( 'menu_items', array() );
1330 if ( empty( $menu_perms['plugins'] ) )
1331 $caps[] = 'manage_network_plugins';
1335 case 'delete_users':
1336 // If multisite only super admins can delete users.
1337 if ( is_multisite() && ! is_super_admin( $user_id ) )
1338 $caps[] = 'do_not_allow';
1340 $caps[] = 'delete_users'; // delete_user maps to delete_users.
1342 case 'create_users':
1343 if ( !is_multisite() )
1345 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1348 $caps[] = 'do_not_allow';
1350 case 'manage_links' :
1351 if ( get_option( 'link_manager_enabled' ) )
1354 $caps[] = 'do_not_allow';
1357 $caps[] = 'edit_theme_options';
1360 $caps[] = 'manage_options';
1363 // Handle meta capabilities for custom post types.
1364 $post_type_meta_caps = _post_type_meta_capabilities();
1365 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1366 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1367 return call_user_func_array( 'map_meta_cap', $args );
1370 // If no meta caps match, return the original cap.
1375 * Filter a user's capabilities depending on specific context and/or privilege.
1379 * @param array $caps Returns the user's actual capabilities.
1380 * @param string $cap Capability name.
1381 * @param int $user_id The user ID.
1382 * @param array $args Adds the context to the cap. Typically the object ID.
1384 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
1388 * Whether current user has capability or role.
1392 * @param string $capability Capability or role name.
1395 function current_user_can( $capability ) {
1396 $current_user = wp_get_current_user();
1398 if ( empty( $current_user ) )
1401 $args = array_slice( func_get_args(), 1 );
1402 $args = array_merge( array( $capability ), $args );
1404 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
1408 * Whether current user has a capability or role for a given blog.
1412 * @param int $blog_id Blog ID
1413 * @param string $capability Capability or role name.
1416 function current_user_can_for_blog( $blog_id, $capability ) {
1417 $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
1419 $current_user = wp_get_current_user();
1421 if ( empty( $current_user ) ) {
1423 restore_current_blog();
1428 $args = array_slice( func_get_args(), 2 );
1429 $args = array_merge( array( $capability ), $args );
1431 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
1434 restore_current_blog();
1441 * Whether author of supplied post has capability or role.
1445 * @param int|object $post Post ID or post object.
1446 * @param string $capability Capability or role name.
1449 function author_can( $post, $capability ) {
1450 if ( !$post = get_post($post) )
1453 $author = get_userdata( $post->post_author );
1458 $args = array_slice( func_get_args(), 2 );
1459 $args = array_merge( array( $capability ), $args );
1461 return call_user_func_array( array( $author, 'has_cap' ), $args );
1465 * Whether a particular user has capability or role.
1469 * @param int|object $user User ID or object.
1470 * @param string $capability Capability or role name.
1473 function user_can( $user, $capability ) {
1474 if ( ! is_object( $user ) )
1475 $user = get_userdata( $user );
1477 if ( ! $user || ! $user->exists() )
1480 $args = array_slice( func_get_args(), 2 );
1481 $args = array_merge( array( $capability ), $args );
1483 return call_user_func_array( array( $user, 'has_cap' ), $args );
1487 * Retrieve role object.
1489 * @see WP_Roles::get_role() Uses method to retrieve role object.
1492 * @param string $role Role name.
1493 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
1495 function get_role( $role ) {
1498 if ( ! isset( $wp_roles ) )
1499 $wp_roles = new WP_Roles();
1501 return $wp_roles->get_role( $role );
1505 * Add role, if it does not exist.
1507 * @see WP_Roles::add_role() Uses method to add role.
1510 * @param string $role Role name.
1511 * @param string $display_name Display name for role.
1512 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1513 * @return WP_Role|null WP_Role object if role is added, null if already exists.
1515 function add_role( $role, $display_name, $capabilities = array() ) {
1518 if ( ! isset( $wp_roles ) )
1519 $wp_roles = new WP_Roles();
1521 return $wp_roles->add_role( $role, $display_name, $capabilities );
1525 * Remove role, if it exists.
1527 * @see WP_Roles::remove_role() Uses method to remove role.
1530 * @param string $role Role name.
1532 function remove_role( $role ) {
1535 if ( ! isset( $wp_roles ) )
1536 $wp_roles = new WP_Roles();
1538 $wp_roles->remove_role( $role );
1542 * Retrieve a list of super admins.
1546 * @uses $super_admins Super admins global variable, if set.
1548 * @return array List of super admin logins
1550 function get_super_admins() {
1551 global $super_admins;
1553 if ( isset($super_admins) )
1554 return $super_admins;
1556 return get_site_option( 'site_admins', array('admin') );
1560 * Determine if user is a site admin.
1564 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1565 * @return bool True if the user is a site admin.
1567 function is_super_admin( $user_id = false ) {
1568 if ( ! $user_id || $user_id == get_current_user_id() )
1569 $user = wp_get_current_user();
1571 $user = get_userdata( $user_id );
1573 if ( ! $user || ! $user->exists() )
1576 if ( is_multisite() ) {
1577 $super_admins = get_super_admins();
1578 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1581 if ( $user->has_cap('delete_users') )