3 * WordPress Roles and Capabilities.
10 * WordPress User Roles.
12 * The role option is simple, the structure is organized by role name that store
13 * the name in value of the 'name' key. The capabilities are stored as an array
14 * in the value of the 'capability' key.
18 * 'rolename' => array (
19 * 'name' => 'rolename',
20 * 'capabilities' => array()
31 * List of roles and capabilities.
40 * List of the role objects.
46 var $role_objects = array();
55 var $role_names = array();
58 * Option name for storing role list.
67 * Whether to use the database for retrieval and storage.
80 function __construct() {
85 * Set up the object properties.
87 * The role key is set to the current prefix for the $wpdb object with
88 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
89 * be used and the role option will not be updated or used.
93 * @uses $wpdb Used to get the database prefix.
94 * @global array $wp_user_roles Used to set the 'roles' property value.
97 global $wpdb, $wp_user_roles;
98 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
99 if ( ! empty( $wp_user_roles ) ) {
100 $this->roles = $wp_user_roles;
101 $this->use_db = false;
103 $this->roles = get_option( $this->role_key );
106 if ( empty( $this->roles ) )
109 $this->role_objects = array();
110 $this->role_names = array();
111 foreach ( array_keys( $this->roles ) as $role ) {
112 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
113 $this->role_names[$role] = $this->roles[$role]['name'];
118 * Reinitialize the object
120 * Recreates the role objects. This is typically called only by switch_to_blog()
121 * after switching wpdb to a new blog ID.
127 // There is no need to reinit if using the wp_user_roles global.
128 if ( ! $this->use_db )
131 global $wpdb, $wp_user_roles;
133 // Duplicated from _init() to avoid an extra function call.
134 $this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
135 $this->roles = get_option( $this->role_key );
136 if ( empty( $this->roles ) )
139 $this->role_objects = array();
140 $this->role_names = array();
141 foreach ( array_keys( $this->roles ) as $role ) {
142 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
143 $this->role_names[$role] = $this->roles[$role]['name'];
148 * Add role name with capabilities to list.
150 * Updates the list of roles, if the role doesn't already exist.
152 * The capabilities are defined in the following format `array( 'read' => true );`
153 * To explicitly deny a role a capability you set the value for that capability to false.
158 * @param string $role Role name.
159 * @param string $display_name Role display name.
160 * @param array $capabilities List of role capabilities in the above format.
161 * @return WP_Role|null WP_Role object if role is added, null if already exists.
163 function add_role( $role, $display_name, $capabilities = array() ) {
164 if ( isset( $this->roles[$role] ) )
167 $this->roles[$role] = array(
168 'name' => $display_name,
169 'capabilities' => $capabilities
172 update_option( $this->role_key, $this->roles );
173 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
174 $this->role_names[$role] = $display_name;
175 return $this->role_objects[$role];
179 * Remove role by name.
184 * @param string $role Role name.
186 function remove_role( $role ) {
187 if ( ! isset( $this->role_objects[$role] ) )
190 unset( $this->role_objects[$role] );
191 unset( $this->role_names[$role] );
192 unset( $this->roles[$role] );
195 update_option( $this->role_key, $this->roles );
197 if ( get_option( 'default_role' ) == $role )
198 update_option( 'default_role', 'subscriber' );
202 * Add capability to role.
207 * @param string $role Role name.
208 * @param string $cap Capability name.
209 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
211 function add_cap( $role, $cap, $grant = true ) {
212 if ( ! isset( $this->roles[$role] ) )
215 $this->roles[$role]['capabilities'][$cap] = $grant;
217 update_option( $this->role_key, $this->roles );
221 * Remove capability from role.
226 * @param string $role Role name.
227 * @param string $cap Capability name.
229 function remove_cap( $role, $cap ) {
230 if ( ! isset( $this->roles[$role] ) )
233 unset( $this->roles[$role]['capabilities'][$cap] );
235 update_option( $this->role_key, $this->roles );
239 * Retrieve role object by name.
244 * @param string $role Role name.
245 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
247 function get_role( $role ) {
248 if ( isset( $this->role_objects[$role] ) )
249 return $this->role_objects[$role];
255 * Retrieve list of role names.
260 * @return array List of role names.
262 function get_names() {
263 return $this->role_names;
267 * Whether role name is currently in the list of available roles.
272 * @param string $role Role name to look up.
275 function is_role( $role ) {
276 return isset( $this->role_names[$role] );
281 * WordPress Role class.
298 * List of capabilities the role contains.
307 * Constructor - Set up object properties.
309 * The list of capabilities, must have the key as the name of the capability
310 * and the value a boolean of whether it is granted to the role.
315 * @param string $role Role name.
316 * @param array $capabilities List of capabilities.
318 function __construct( $role, $capabilities ) {
320 $this->capabilities = $capabilities;
324 * Assign role a capability.
326 * @see WP_Roles::add_cap() Method uses implementation for role.
330 * @param string $cap Capability name.
331 * @param bool $grant Whether role has capability privilege.
333 function add_cap( $cap, $grant = true ) {
336 if ( ! isset( $wp_roles ) )
337 $wp_roles = new WP_Roles();
339 $this->capabilities[$cap] = $grant;
340 $wp_roles->add_cap( $this->name, $cap, $grant );
344 * Remove capability from role.
346 * This is a container for {@link WP_Roles::remove_cap()} to remove the
347 * capability from the role. That is to say, that {@link
348 * WP_Roles::remove_cap()} implements the functionality, but it also makes
349 * sense to use this class, because you don't need to enter the role name.
354 * @param string $cap Capability name.
356 function remove_cap( $cap ) {
359 if ( ! isset( $wp_roles ) )
360 $wp_roles = new WP_Roles();
362 unset( $this->capabilities[$cap] );
363 $wp_roles->remove_cap( $this->name, $cap );
367 * Whether role has capability.
369 * The capabilities is passed through the 'role_has_cap' filter. The first
370 * parameter for the hook is the list of capabilities the class has
371 * assigned. The second parameter is the capability name to look for. The
372 * third and final parameter for the hook is the role name.
377 * @param string $cap Capability name.
378 * @return bool True, if user has capability. False, if doesn't have capability.
380 function has_cap( $cap ) {
382 * Filter which capabilities a role has.
386 * @param array $capabilities Array of role capabilities.
387 * @param string $cap Capability name.
388 * @param string $name Role name.
390 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
391 if ( !empty( $capabilities[$cap] ) )
392 return $capabilities[$cap];
400 * WordPress User class.
408 * User data container.
426 * The individual capabilities the user has been given.
435 * User metadata option name.
444 * The roles the user is part of.
450 var $roles = array();
453 * All capabilities the user has, including individual and role based.
459 var $allcaps = array();
462 * The filter context applied to user data fields.
470 private static $back_compat_keys;
475 * Retrieves the userdata and passes it to {@link WP_User::init()}.
480 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
481 * @param string $name Optional. User's username
482 * @param int $blog_id Optional Blog ID, defaults to current blog.
485 function __construct( $id = 0, $name = '', $blog_id = '' ) {
486 if ( ! isset( self::$back_compat_keys ) ) {
487 $prefix = $GLOBALS['wpdb']->prefix;
488 self::$back_compat_keys = array(
489 'user_firstname' => 'first_name',
490 'user_lastname' => 'last_name',
491 'user_description' => 'description',
492 'user_level' => $prefix . 'user_level',
493 $prefix . 'usersettings' => $prefix . 'user-settings',
494 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
498 if ( is_a( $id, 'WP_User' ) ) {
499 $this->init( $id->data, $blog_id );
501 } elseif ( is_object( $id ) ) {
502 $this->init( $id, $blog_id );
506 if ( ! empty( $id ) && ! is_numeric( $id ) ) {
512 $data = self::get_data_by( 'id', $id );
514 $data = self::get_data_by( 'login', $name );
517 $this->init( $data, $blog_id );
521 * Sets up object properties, including capabilities.
523 * @param object $data User DB row object
524 * @param int $blog_id Optional. The blog id to initialize for
526 function init( $data, $blog_id = '' ) {
528 $this->ID = (int) $data->ID;
530 $this->for_blog( $blog_id );
534 * Return only the main user fields
538 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
539 * @param string|int $value The field value
540 * @return object Raw user object
542 static function get_data_by( $field, $value ) {
545 if ( 'id' == $field ) {
546 // Make sure the value is numeric to avoid casting objects, for example,
548 if ( ! is_numeric( $value ) )
550 $value = intval( $value );
554 $value = trim( $value );
566 $user_id = wp_cache_get($value, 'userslugs');
567 $db_field = 'user_nicename';
570 $user_id = wp_cache_get($value, 'useremail');
571 $db_field = 'user_email';
574 $value = sanitize_user( $value );
575 $user_id = wp_cache_get($value, 'userlogins');
576 $db_field = 'user_login';
582 if ( false !== $user_id ) {
583 if ( $user = wp_cache_get( $user_id, 'users' ) )
587 if ( !$user = $wpdb->get_row( $wpdb->prepare(
588 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
592 update_user_caches( $user );
598 * Magic method for checking the existence of a certain custom field
602 function __isset( $key ) {
603 if ( 'id' == $key ) {
604 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
608 if ( isset( $this->data->$key ) )
611 if ( isset( self::$back_compat_keys[ $key ] ) )
612 $key = self::$back_compat_keys[ $key ];
614 return metadata_exists( 'user', $this->ID, $key );
618 * Magic method for accessing custom fields
622 function __get( $key ) {
623 if ( 'id' == $key ) {
624 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
628 if ( isset( $this->data->$key ) ) {
629 $value = $this->data->$key;
631 if ( isset( self::$back_compat_keys[ $key ] ) )
632 $key = self::$back_compat_keys[ $key ];
633 $value = get_user_meta( $this->ID, $key, true );
636 if ( $this->filter ) {
637 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
644 * Magic method for setting custom fields
648 function __set( $key, $value ) {
649 if ( 'id' == $key ) {
650 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
655 $this->data->$key = $value;
659 * Determine whether the user exists in the database.
664 * @return bool True if user exists in the database, false if not.
667 return ! empty( $this->ID );
671 * Retrieve the value of a property or meta key.
673 * Retrieves from the users and usermeta table.
677 * @param string $key Property
679 function get( $key ) {
680 return $this->__get( $key );
684 * Determine whether a property or meta key is set
686 * Consults the users and usermeta tables.
690 * @param string $key Property
692 function has_prop( $key ) {
693 return $this->__isset( $key );
697 * Return an array representation.
701 * @return array Array representation.
703 function to_array() {
704 return get_object_vars( $this->data );
708 * Set up capability object properties.
710 * Will set the value for the 'cap_key' property to current database table
711 * prefix, followed by 'capabilities'. Will then check to see if the
712 * property matching the 'cap_key' exists and is an array. If so, it will be
718 * @param string $cap_key Optional capability key
720 function _init_caps( $cap_key = '' ) {
723 if ( empty($cap_key) )
724 $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
726 $this->cap_key = $cap_key;
728 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
730 if ( ! is_array( $this->caps ) )
731 $this->caps = array();
733 $this->get_role_caps();
737 * Retrieve all of the role capabilities and merge with individual capabilities.
739 * All of the capabilities of the roles the user belongs to are merged with
740 * the users individual roles. This also means that the user can be denied
741 * specific roles that their role might have, but the specific user isn't
742 * granted permission to.
748 * @return array List of all capabilities for the user.
750 function get_role_caps() {
753 if ( ! isset( $wp_roles ) )
754 $wp_roles = new WP_Roles();
756 //Filter out caps that are not role names and assign to $this->roles
757 if ( is_array( $this->caps ) )
758 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
760 //Build $allcaps from role caps, overlay user's $caps
761 $this->allcaps = array();
762 foreach ( (array) $this->roles as $role ) {
763 $the_role = $wp_roles->get_role( $role );
764 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
766 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
768 return $this->allcaps;
774 * Updates the user's meta data option with capabilities and roles.
779 * @param string $role Role name.
781 function add_role( $role ) {
782 $this->caps[$role] = true;
783 update_user_meta( $this->ID, $this->cap_key, $this->caps );
784 $this->get_role_caps();
785 $this->update_user_level_from_caps();
789 * Remove role from user.
794 * @param string $role Role name.
796 function remove_role( $role ) {
797 if ( !in_array($role, $this->roles) )
799 unset( $this->caps[$role] );
800 update_user_meta( $this->ID, $this->cap_key, $this->caps );
801 $this->get_role_caps();
802 $this->update_user_level_from_caps();
806 * Set the role of the user.
808 * This will remove the previous roles of the user and assign the user the
809 * new one. You can set the role to an empty string and it will remove all
810 * of the roles from the user.
815 * @param string $role Role name.
817 function set_role( $role ) {
818 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
821 foreach ( (array) $this->roles as $oldrole )
822 unset( $this->caps[$oldrole] );
824 $old_roles = $this->roles;
825 if ( !empty( $role ) ) {
826 $this->caps[$role] = true;
827 $this->roles = array( $role => true );
829 $this->roles = false;
831 update_user_meta( $this->ID, $this->cap_key, $this->caps );
832 $this->get_role_caps();
833 $this->update_user_level_from_caps();
836 * Fires after the user's role has changed.
839 * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
841 * @param int $user_id The user ID.
842 * @param string $role The new role.
843 * @param array $old_roles An array of the user's previous roles.
845 do_action( 'set_user_role', $this->ID, $role, $old_roles );
849 * Choose the maximum level the user has.
851 * Will compare the level from the $item parameter against the $max
852 * parameter. If the item is incorrect, then just the $max parameter value
855 * Used to get the max level based on the capabilities the user has. This
856 * is also based on roles, so if the user is assigned the Administrator role
857 * then the capability 'level_10' will exist and the user will get that
863 * @param int $max Max level of user.
864 * @param string $item Level capability name.
865 * @return int Max Level.
867 function level_reduction( $max, $item ) {
868 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
869 $level = intval( $matches[1] );
870 return max( $max, $level );
877 * Update the maximum user level for the user.
879 * Updates the 'user_level' user metadata (includes prefix that is the
880 * database table prefix) with the maximum user level. Gets the value from
881 * the all of the capabilities that the user has.
886 function update_user_level_from_caps() {
888 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
889 update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
893 * Add capability and grant or deny access to capability.
898 * @param string $cap Capability name.
899 * @param bool $grant Whether to grant capability to user.
901 function add_cap( $cap, $grant = true ) {
902 $this->caps[$cap] = $grant;
903 update_user_meta( $this->ID, $this->cap_key, $this->caps );
907 * Remove capability from user.
912 * @param string $cap Capability name.
914 function remove_cap( $cap ) {
915 if ( ! isset( $this->caps[$cap] ) )
917 unset( $this->caps[$cap] );
918 update_user_meta( $this->ID, $this->cap_key, $this->caps );
922 * Remove all of the capabilities of the user.
927 function remove_all_caps() {
929 $this->caps = array();
930 delete_user_meta( $this->ID, $this->cap_key );
931 delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
932 $this->get_role_caps();
936 * Whether user has capability or role name.
938 * This is useful for looking up whether the user has a specific role
939 * assigned to the user. The second optional parameter can also be used to
940 * check for capabilities against a specific object, such as a post or user.
945 * @param string|int $cap Capability or role name to search.
946 * @return bool True, if user has capability; false, if user does not have capability.
948 function has_cap( $cap ) {
949 if ( is_numeric( $cap ) ) {
950 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
951 $cap = $this->translate_level_to_cap( $cap );
954 $args = array_slice( func_get_args(), 1 );
955 $args = array_merge( array( $cap, $this->ID ), $args );
956 $caps = call_user_func_array( 'map_meta_cap', $args );
958 // Multisite super admin has all caps by definition, Unless specifically denied.
959 if ( is_multisite() && is_super_admin( $this->ID ) ) {
960 if ( in_array('do_not_allow', $caps) )
966 * Dynamically filter a user's capabilities.
969 * @since 3.7.0 Added the user object.
971 * @param array $allcaps An array of all the role's capabilities.
972 * @param array $caps Actual capabilities for meta capability.
973 * @param array $args Optional parameters passed to has_cap(), typically object ID.
974 * @param WP_User $user The user object.
976 // Must have ALL requested caps
977 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
978 $capabilities['exist'] = true; // Everyone is allowed to exist
979 foreach ( (array) $caps as $cap ) {
980 if ( empty( $capabilities[ $cap ] ) )
988 * Convert numeric level to level capability name.
990 * Prepends 'level_' to level number.
995 * @param int $level Level number, 1 to 10.
998 function translate_level_to_cap( $level ) {
999 return 'level_' . $level;
1003 * Set the blog to operate on. Defaults to the current blog.
1007 * @param int $blog_id Optional Blog ID, defaults to current blog.
1009 function for_blog( $blog_id = '' ) {
1011 if ( ! empty( $blog_id ) )
1012 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
1015 $this->_init_caps( $cap_key );
1020 * Map meta capabilities to primitive capabilities.
1022 * This does not actually compare whether the user ID has the actual capability,
1023 * just what the capability or capabilities are. Meta capability list value can
1024 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
1025 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
1029 * @param string $cap Capability name.
1030 * @param int $user_id User ID.
1031 * @return array Actual capabilities for meta capability.
1033 function map_meta_cap( $cap, $user_id ) {
1034 $args = array_slice( func_get_args(), 2 );
1039 $caps[] = 'remove_users';
1041 case 'promote_user':
1042 $caps[] = 'promote_users';
1046 // Allow user to edit itself
1047 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
1050 // If multisite these caps are allowed only for super admins.
1051 if ( is_multisite() && !is_super_admin( $user_id ) )
1052 $caps[] = 'do_not_allow';
1054 $caps[] = 'edit_users'; // edit_user maps to edit_users.
1058 $post = get_post( $args[0] );
1060 if ( 'revision' == $post->post_type ) {
1061 $post = get_post( $post->post_parent );
1064 $post_type = get_post_type_object( $post->post_type );
1066 if ( ! $post_type->map_meta_cap ) {
1067 $caps[] = $post_type->cap->$cap;
1068 // Prior to 3.1 we would re-call map_meta_cap here.
1069 if ( 'delete_post' == $cap )
1070 $cap = $post_type->cap->$cap;
1074 // If the post author is set and the user is the author...
1075 if ( $post->post_author && $user_id == $post->post_author ) {
1076 // If the post is published...
1077 if ( 'publish' == $post->post_status ) {
1078 $caps[] = $post_type->cap->delete_published_posts;
1079 } elseif ( 'trash' == $post->post_status ) {
1080 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1081 $caps[] = $post_type->cap->delete_published_posts;
1084 // If the post is draft...
1085 $caps[] = $post_type->cap->delete_posts;
1088 // The user is trying to edit someone else's post.
1089 $caps[] = $post_type->cap->delete_others_posts;
1090 // The post is published, extra cap required.
1091 if ( 'publish' == $post->post_status ) {
1092 $caps[] = $post_type->cap->delete_published_posts;
1093 } elseif ( 'private' == $post->post_status ) {
1094 $caps[] = $post_type->cap->delete_private_posts;
1098 // edit_post breaks down to edit_posts, edit_published_posts, or
1099 // edit_others_posts
1102 $post = get_post( $args[0] );
1103 if ( empty( $post ) )
1106 if ( 'revision' == $post->post_type ) {
1107 $post = get_post( $post->post_parent );
1110 $post_type = get_post_type_object( $post->post_type );
1112 if ( ! $post_type->map_meta_cap ) {
1113 $caps[] = $post_type->cap->$cap;
1114 // Prior to 3.1 we would re-call map_meta_cap here.
1115 if ( 'edit_post' == $cap )
1116 $cap = $post_type->cap->$cap;
1120 // If the post author is set and the user is the author...
1121 if ( $post->post_author && $user_id == $post->post_author ) {
1122 // If the post is published...
1123 if ( 'publish' == $post->post_status ) {
1124 $caps[] = $post_type->cap->edit_published_posts;
1125 } elseif ( 'trash' == $post->post_status ) {
1126 if ( 'publish' == get_post_meta( $post->ID, '_wp_trash_meta_status', true ) ) {
1127 $caps[] = $post_type->cap->edit_published_posts;
1130 // If the post is draft...
1131 $caps[] = $post_type->cap->edit_posts;
1134 // The user is trying to edit someone else's post.
1135 $caps[] = $post_type->cap->edit_others_posts;
1136 // The post is published, extra cap required.
1137 if ( 'publish' == $post->post_status ) {
1138 $caps[] = $post_type->cap->edit_published_posts;
1139 } elseif ( 'private' == $post->post_status ) {
1140 $caps[] = $post_type->cap->edit_private_posts;
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 ( 'read_post' == $cap )
1158 $cap = $post_type->cap->$cap;
1162 $status_obj = get_post_status_object( $post->post_status );
1163 if ( $status_obj->public ) {
1164 $caps[] = $post_type->cap->read;
1168 if ( $post->post_author && $user_id == $post->post_author ) {
1169 $caps[] = $post_type->cap->read;
1170 } elseif ( $status_obj->private ) {
1171 $caps[] = $post_type->cap->read_private_posts;
1173 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1176 case 'publish_post':
1177 $post = get_post( $args[0] );
1178 $post_type = get_post_type_object( $post->post_type );
1180 $caps[] = $post_type->cap->publish_posts;
1182 case 'edit_post_meta':
1183 case 'delete_post_meta':
1184 case 'add_post_meta':
1185 $post = get_post( $args[0] );
1186 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1188 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1190 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1192 * Filter whether the user is allowed to add post meta to a post.
1194 * The dynamic portion of the hook name, $meta_key, refers to the
1195 * meta key passed to map_meta_cap().
1199 * @param bool $allowed Whether the user can add the post meta. Default false.
1200 * @param string $meta_key The meta key.
1201 * @param int $post_id Post ID.
1202 * @param int $user_id User ID.
1203 * @param string $cap Capability name.
1204 * @param array $caps User capabilities.
1206 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1209 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1213 case 'edit_comment':
1214 $comment = get_comment( $args[0] );
1215 if ( empty( $comment ) )
1217 $post = get_post( $comment->comment_post_ID );
1218 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1220 case 'unfiltered_upload':
1221 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
1224 $caps[] = 'do_not_allow';
1226 case 'unfiltered_html' :
1227 // Disallow unfiltered_html for all users, even admins and super admins.
1228 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
1229 $caps[] = 'do_not_allow';
1230 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1231 $caps[] = 'do_not_allow';
1236 case 'edit_plugins':
1238 // Disallow the file editors.
1239 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
1240 $caps[] = 'do_not_allow';
1241 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1242 $caps[] = 'do_not_allow';
1243 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1244 $caps[] = 'do_not_allow';
1248 case 'update_plugins':
1249 case 'delete_plugins':
1250 case 'install_plugins':
1251 case 'update_themes':
1252 case 'delete_themes':
1253 case 'install_themes':
1255 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
1256 // Files in uploads are excepted.
1257 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1258 $caps[] = 'do_not_allow';
1259 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1260 $caps[] = 'do_not_allow';
1264 case 'activate_plugins':
1266 if ( is_multisite() ) {
1267 // update_, install_, and delete_ are handled above with is_super_admin().
1268 $menu_perms = get_site_option( 'menu_items', array() );
1269 if ( empty( $menu_perms['plugins'] ) )
1270 $caps[] = 'manage_network_plugins';
1274 case 'delete_users':
1275 // If multisite only super admins can delete users.
1276 if ( is_multisite() && ! is_super_admin( $user_id ) )
1277 $caps[] = 'do_not_allow';
1279 $caps[] = 'delete_users'; // delete_user maps to delete_users.
1281 case 'create_users':
1282 if ( !is_multisite() )
1284 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1287 $caps[] = 'do_not_allow';
1289 case 'manage_links' :
1290 if ( get_option( 'link_manager_enabled' ) )
1293 $caps[] = 'do_not_allow';
1296 // Handle meta capabilities for custom post types.
1297 $post_type_meta_caps = _post_type_meta_capabilities();
1298 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1299 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1300 return call_user_func_array( 'map_meta_cap', $args );
1303 // If no meta caps match, return the original cap.
1308 * Filter a user's capabilities depending on specific context and/or privilege.
1312 * @param array $caps Returns the user's actual capabilities.
1313 * @param string $cap Capability name.
1314 * @param int $user_id The user ID.
1315 * @param array $args Adds the context to the cap. Typically the object ID.
1317 return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );
1321 * Whether current user has capability or role.
1325 * @param string $capability Capability or role name.
1328 function current_user_can( $capability ) {
1329 $current_user = wp_get_current_user();
1331 if ( empty( $current_user ) )
1334 $args = array_slice( func_get_args(), 1 );
1335 $args = array_merge( array( $capability ), $args );
1337 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
1341 * Whether current user has a capability or role for a given blog.
1345 * @param int $blog_id Blog ID
1346 * @param string $capability Capability or role name.
1349 function current_user_can_for_blog( $blog_id, $capability ) {
1350 if ( is_multisite() )
1351 switch_to_blog( $blog_id );
1353 $current_user = wp_get_current_user();
1355 if ( empty( $current_user ) )
1358 $args = array_slice( func_get_args(), 2 );
1359 $args = array_merge( array( $capability ), $args );
1361 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
1363 if ( is_multisite() )
1364 restore_current_blog();
1370 * Whether author of supplied post has capability or role.
1374 * @param int|object $post Post ID or post object.
1375 * @param string $capability Capability or role name.
1378 function author_can( $post, $capability ) {
1379 if ( !$post = get_post($post) )
1382 $author = get_userdata( $post->post_author );
1387 $args = array_slice( func_get_args(), 2 );
1388 $args = array_merge( array( $capability ), $args );
1390 return call_user_func_array( array( $author, 'has_cap' ), $args );
1394 * Whether a particular user has capability or role.
1398 * @param int|object $user User ID or object.
1399 * @param string $capability Capability or role name.
1402 function user_can( $user, $capability ) {
1403 if ( ! is_object( $user ) )
1404 $user = get_userdata( $user );
1406 if ( ! $user || ! $user->exists() )
1409 $args = array_slice( func_get_args(), 2 );
1410 $args = array_merge( array( $capability ), $args );
1412 return call_user_func_array( array( $user, 'has_cap' ), $args );
1416 * Retrieve role object.
1418 * @see WP_Roles::get_role() Uses method to retrieve role object.
1421 * @param string $role Role name.
1422 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
1424 function get_role( $role ) {
1427 if ( ! isset( $wp_roles ) )
1428 $wp_roles = new WP_Roles();
1430 return $wp_roles->get_role( $role );
1434 * Add role, if it does not exist.
1436 * @see WP_Roles::add_role() Uses method to add role.
1439 * @param string $role Role name.
1440 * @param string $display_name Display name for role.
1441 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1442 * @return WP_Role|null WP_Role object if role is added, null if already exists.
1444 function add_role( $role, $display_name, $capabilities = array() ) {
1447 if ( ! isset( $wp_roles ) )
1448 $wp_roles = new WP_Roles();
1450 return $wp_roles->add_role( $role, $display_name, $capabilities );
1454 * Remove role, if it exists.
1456 * @see WP_Roles::remove_role() Uses method to remove role.
1459 * @param string $role Role name.
1461 function remove_role( $role ) {
1464 if ( ! isset( $wp_roles ) )
1465 $wp_roles = new WP_Roles();
1467 $wp_roles->remove_role( $role );
1471 * Retrieve a list of super admins.
1475 * @uses $super_admins Super admins global variable, if set.
1477 * @return array List of super admin logins
1479 function get_super_admins() {
1480 global $super_admins;
1482 if ( isset($super_admins) )
1483 return $super_admins;
1485 return get_site_option( 'site_admins', array('admin') );
1489 * Determine if user is a site admin.
1493 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1494 * @return bool True if the user is a site admin.
1496 function is_super_admin( $user_id = false ) {
1497 if ( ! $user_id || $user_id == get_current_user_id() )
1498 $user = wp_get_current_user();
1500 $user = get_userdata( $user_id );
1502 if ( ! $user || ! $user->exists() )
1505 if ( is_multisite() ) {
1506 $super_admins = get_super_admins();
1507 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1510 if ( $user->has_cap('delete_users') )