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->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->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 );
199 * Add capability to role.
204 * @param string $role Role name.
205 * @param string $cap Capability name.
206 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
208 function add_cap( $role, $cap, $grant = true ) {
209 if ( ! isset( $this->roles[$role] ) )
212 $this->roles[$role]['capabilities'][$cap] = $grant;
214 update_option( $this->role_key, $this->roles );
218 * Remove capability from role.
223 * @param string $role Role name.
224 * @param string $cap Capability name.
226 function remove_cap( $role, $cap ) {
227 if ( ! isset( $this->roles[$role] ) )
230 unset( $this->roles[$role]['capabilities'][$cap] );
232 update_option( $this->role_key, $this->roles );
236 * Retrieve role object by name.
241 * @param string $role Role name.
242 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
244 function get_role( $role ) {
245 if ( isset( $this->role_objects[$role] ) )
246 return $this->role_objects[$role];
252 * Retrieve list of role names.
257 * @return array List of role names.
259 function get_names() {
260 return $this->role_names;
264 * Whether role name is currently in the list of available roles.
269 * @param string $role Role name to look up.
272 function is_role( $role ) {
273 return isset( $this->role_names[$role] );
278 * WordPress Role class.
295 * List of capabilities the role contains.
304 * Constructor - Set up object properties.
306 * The list of capabilities, must have the key as the name of the capability
307 * and the value a boolean of whether it is granted to the role.
312 * @param string $role Role name.
313 * @param array $capabilities List of capabilities.
315 function __construct( $role, $capabilities ) {
317 $this->capabilities = $capabilities;
321 * Assign role a capability.
323 * @see WP_Roles::add_cap() Method uses implementation for role.
327 * @param string $cap Capability name.
328 * @param bool $grant Whether role has capability privilege.
330 function add_cap( $cap, $grant = true ) {
333 if ( ! isset( $wp_roles ) )
334 $wp_roles = new WP_Roles();
336 $this->capabilities[$cap] = $grant;
337 $wp_roles->add_cap( $this->name, $cap, $grant );
341 * Remove capability from role.
343 * This is a container for {@link WP_Roles::remove_cap()} to remove the
344 * capability from the role. That is to say, that {@link
345 * WP_Roles::remove_cap()} implements the functionality, but it also makes
346 * sense to use this class, because you don't need to enter the role name.
351 * @param string $cap Capability name.
353 function remove_cap( $cap ) {
356 if ( ! isset( $wp_roles ) )
357 $wp_roles = new WP_Roles();
359 unset( $this->capabilities[$cap] );
360 $wp_roles->remove_cap( $this->name, $cap );
364 * Whether role has capability.
366 * The capabilities is passed through the 'role_has_cap' filter. The first
367 * parameter for the hook is the list of capabilities the class has
368 * assigned. The second parameter is the capability name to look for. The
369 * third and final parameter for the hook is the role name.
374 * @param string $cap Capability name.
375 * @return bool True, if user has capability. False, if doesn't have capability.
377 function has_cap( $cap ) {
378 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
379 if ( !empty( $capabilities[$cap] ) )
380 return $capabilities[$cap];
388 * WordPress User class.
396 * User data container.
414 * The individual capabilities the user has been given.
423 * User metadata option name.
432 * The roles the user is part of.
438 var $roles = array();
441 * All capabilities the user has, including individual and role based.
447 var $allcaps = array();
450 * The filter context applied to user data fields.
458 private static $back_compat_keys;
463 * Retrieves the userdata and passes it to {@link WP_User::init()}.
468 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
469 * @param string $name Optional. User's username
470 * @param int $blog_id Optional Blog ID, defaults to current blog.
473 function __construct( $id = 0, $name = '', $blog_id = '' ) {
474 if ( ! isset( self::$back_compat_keys ) ) {
475 $prefix = $GLOBALS['wpdb']->prefix;
476 self::$back_compat_keys = array(
477 'user_firstname' => 'first_name',
478 'user_lastname' => 'last_name',
479 'user_description' => 'description',
480 'user_level' => $prefix . 'user_level',
481 $prefix . 'usersettings' => $prefix . 'user-settings',
482 $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
486 if ( is_a( $id, 'WP_User' ) ) {
487 $this->init( $id->data, $blog_id );
489 } elseif ( is_object( $id ) ) {
490 $this->init( $id, $blog_id );
494 if ( ! empty( $id ) && ! is_numeric( $id ) ) {
500 $data = self::get_data_by( 'id', $id );
502 $data = self::get_data_by( 'login', $name );
505 $this->init( $data, $blog_id );
509 * Sets up object properties, including capabilities.
511 * @param object $data User DB row object
512 * @param int $blog_id Optional. The blog id to initialize for
514 function init( $data, $blog_id = '' ) {
516 $this->ID = (int) $data->ID;
518 $this->for_blog( $blog_id );
522 * Return only the main user fields
526 * @param string $field The field to query against: 'id', 'slug', 'email' or 'login'
527 * @param string|int $value The field value
528 * @return object Raw user object
530 static function get_data_by( $field, $value ) {
533 if ( 'id' == $field ) {
534 // Make sure the value is numeric to avoid casting objects, for example,
536 if ( ! is_numeric( $value ) )
538 $value = intval( $value );
542 $value = trim( $value );
554 $user_id = wp_cache_get($value, 'userslugs');
555 $db_field = 'user_nicename';
558 $user_id = wp_cache_get($value, 'useremail');
559 $db_field = 'user_email';
562 $value = sanitize_user( $value );
563 $user_id = wp_cache_get($value, 'userlogins');
564 $db_field = 'user_login';
570 if ( false !== $user_id ) {
571 if ( $user = wp_cache_get( $user_id, 'users' ) )
575 if ( !$user = $wpdb->get_row( $wpdb->prepare(
576 "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
580 update_user_caches( $user );
586 * Magic method for checking the existence of a certain custom field
590 function __isset( $key ) {
591 if ( 'id' == $key ) {
592 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
596 if ( isset( $this->data->$key ) )
599 if ( isset( self::$back_compat_keys[ $key ] ) )
600 $key = self::$back_compat_keys[ $key ];
602 return metadata_exists( 'user', $this->ID, $key );
606 * Magic method for accessing custom fields
610 function __get( $key ) {
611 if ( 'id' == $key ) {
612 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
616 if ( isset( $this->data->$key ) ) {
617 $value = $this->data->$key;
619 if ( isset( self::$back_compat_keys[ $key ] ) )
620 $key = self::$back_compat_keys[ $key ];
621 $value = get_user_meta( $this->ID, $key, true );
624 if ( $this->filter ) {
625 $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
632 * Magic method for setting custom fields
636 function __set( $key, $value ) {
637 if ( 'id' == $key ) {
638 _deprecated_argument( 'WP_User->id', '2.1', __( 'Use <code>WP_User->ID</code> instead.' ) );
643 $this->data->$key = $value;
647 * Determine whether the user exists in the database.
652 * @return bool True if user exists in the database, false if not.
655 return ! empty( $this->ID );
659 * Retrieve the value of a property or meta key.
661 * Retrieves from the users and usermeta table.
665 * @param string $key Property
667 function get( $key ) {
668 return $this->__get( $key );
672 * Determine whether a property or meta key is set
674 * Consults the users and usermeta tables.
678 * @param string $key Property
680 function has_prop( $key ) {
681 return $this->__isset( $key );
685 * Return an array representation.
689 * @return array Array representation.
691 function to_array() {
692 return get_object_vars( $this->data );
696 * Set up capability object properties.
698 * Will set the value for the 'cap_key' property to current database table
699 * prefix, followed by 'capabilities'. Will then check to see if the
700 * property matching the 'cap_key' exists and is an array. If so, it will be
706 * @param string $cap_key Optional capability key
708 function _init_caps( $cap_key = '' ) {
711 if ( empty($cap_key) )
712 $this->cap_key = $wpdb->prefix . 'capabilities';
714 $this->cap_key = $cap_key;
716 $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
718 if ( ! is_array( $this->caps ) )
719 $this->caps = array();
721 $this->get_role_caps();
725 * Retrieve all of the role capabilities and merge with individual capabilities.
727 * All of the capabilities of the roles the user belongs to are merged with
728 * the users individual roles. This also means that the user can be denied
729 * specific roles that their role might have, but the specific user isn't
730 * granted permission to.
736 function get_role_caps() {
739 if ( ! isset( $wp_roles ) )
740 $wp_roles = new WP_Roles();
742 //Filter out caps that are not role names and assign to $this->roles
743 if ( is_array( $this->caps ) )
744 $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
746 //Build $allcaps from role caps, overlay user's $caps
747 $this->allcaps = array();
748 foreach ( (array) $this->roles as $role ) {
749 $the_role = $wp_roles->get_role( $role );
750 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
752 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
758 * Updates the user's meta data option with capabilities and roles.
763 * @param string $role Role name.
765 function add_role( $role ) {
766 $this->caps[$role] = true;
767 update_user_meta( $this->ID, $this->cap_key, $this->caps );
768 $this->get_role_caps();
769 $this->update_user_level_from_caps();
773 * Remove role from user.
778 * @param string $role Role name.
780 function remove_role( $role ) {
781 if ( !in_array($role, $this->roles) )
783 unset( $this->caps[$role] );
784 update_user_meta( $this->ID, $this->cap_key, $this->caps );
785 $this->get_role_caps();
786 $this->update_user_level_from_caps();
790 * Set the role of the user.
792 * This will remove the previous roles of the user and assign the user the
793 * new one. You can set the role to an empty string and it will remove all
794 * of the roles from the user.
799 * @param string $role Role name.
801 function set_role( $role ) {
802 if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
805 foreach ( (array) $this->roles as $oldrole )
806 unset( $this->caps[$oldrole] );
808 $old_roles = $this->roles;
809 if ( !empty( $role ) ) {
810 $this->caps[$role] = true;
811 $this->roles = array( $role => true );
813 $this->roles = false;
815 update_user_meta( $this->ID, $this->cap_key, $this->caps );
816 $this->get_role_caps();
817 $this->update_user_level_from_caps();
818 do_action( 'set_user_role', $this->ID, $role, $old_roles );
822 * Choose the maximum level the user has.
824 * Will compare the level from the $item parameter against the $max
825 * parameter. If the item is incorrect, then just the $max parameter value
828 * Used to get the max level based on the capabilities the user has. This
829 * is also based on roles, so if the user is assigned the Administrator role
830 * then the capability 'level_10' will exist and the user will get that
836 * @param int $max Max level of user.
837 * @param string $item Level capability name.
838 * @return int Max Level.
840 function level_reduction( $max, $item ) {
841 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
842 $level = intval( $matches[1] );
843 return max( $max, $level );
850 * Update the maximum user level for the user.
852 * Updates the 'user_level' user metadata (includes prefix that is the
853 * database table prefix) with the maximum user level. Gets the value from
854 * the all of the capabilities that the user has.
859 function update_user_level_from_caps() {
861 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
862 update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
866 * Add capability and grant or deny access to capability.
871 * @param string $cap Capability name.
872 * @param bool $grant Whether to grant capability to user.
874 function add_cap( $cap, $grant = true ) {
875 $this->caps[$cap] = $grant;
876 update_user_meta( $this->ID, $this->cap_key, $this->caps );
880 * Remove capability from user.
885 * @param string $cap Capability name.
887 function remove_cap( $cap ) {
888 if ( ! isset( $this->caps[$cap] ) )
890 unset( $this->caps[$cap] );
891 update_user_meta( $this->ID, $this->cap_key, $this->caps );
895 * Remove all of the capabilities of the user.
900 function remove_all_caps() {
902 $this->caps = array();
903 delete_user_meta( $this->ID, $this->cap_key );
904 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
905 $this->get_role_caps();
909 * Whether user has capability or role name.
911 * This is useful for looking up whether the user has a specific role
912 * assigned to the user. The second optional parameter can also be used to
913 * check for capabilities against a specific object, such as a post or user.
918 * @param string|int $cap Capability or role name to search.
919 * @return bool True, if user has capability; false, if user does not have capability.
921 function has_cap( $cap ) {
922 if ( is_numeric( $cap ) ) {
923 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
924 $cap = $this->translate_level_to_cap( $cap );
927 $args = array_slice( func_get_args(), 1 );
928 $args = array_merge( array( $cap, $this->ID ), $args );
929 $caps = call_user_func_array( 'map_meta_cap', $args );
931 // Multisite super admin has all caps by definition, Unless specifically denied.
932 if ( is_multisite() && is_super_admin( $this->ID ) ) {
933 if ( in_array('do_not_allow', $caps) )
938 // Must have ALL requested caps
939 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
940 $capabilities['exist'] = true; // Everyone is allowed to exist
941 foreach ( (array) $caps as $cap ) {
942 if ( empty( $capabilities[ $cap ] ) )
950 * Convert numeric level to level capability name.
952 * Prepends 'level_' to level number.
957 * @param int $level Level number, 1 to 10.
960 function translate_level_to_cap( $level ) {
961 return 'level_' . $level;
965 * Set the blog to operate on. Defaults to the current blog.
969 * @param int $blog_id Optional Blog ID, defaults to current blog.
971 function for_blog( $blog_id = '' ) {
973 if ( ! empty( $blog_id ) )
974 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
977 $this->_init_caps( $cap_key );
982 * Map meta capabilities to primitive capabilities.
984 * This does not actually compare whether the user ID has the actual capability,
985 * just what the capability or capabilities are. Meta capability list value can
986 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
987 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
991 * @param string $cap Capability name.
992 * @param int $user_id User ID.
993 * @return array Actual capabilities for meta capability.
995 function map_meta_cap( $cap, $user_id ) {
996 $args = array_slice( func_get_args(), 2 );
1001 $caps[] = 'remove_users';
1003 case 'promote_user':
1004 $caps[] = 'promote_users';
1008 // Allow user to edit itself
1009 if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
1012 // If multisite these caps are allowed only for super admins.
1013 if ( is_multisite() && !is_super_admin( $user_id ) )
1014 $caps[] = 'do_not_allow';
1016 $caps[] = 'edit_users'; // edit_user maps to edit_users.
1020 $post = get_post( $args[0] );
1022 if ( 'revision' == $post->post_type ) {
1023 $post = get_post( $post->post_parent );
1026 $post_type = get_post_type_object( $post->post_type );
1028 if ( ! $post_type->map_meta_cap ) {
1029 $caps[] = $post_type->cap->$cap;
1030 // Prior to 3.1 we would re-call map_meta_cap here.
1031 if ( 'delete_post' == $cap )
1032 $cap = $post_type->cap->$cap;
1036 $post_author_id = $post->post_author;
1038 // If no author set yet, default to current user for cap checks.
1039 if ( ! $post_author_id )
1040 $post_author_id = $user_id;
1042 $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
1044 // If the user is the author...
1045 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
1046 // If the post is published...
1047 if ( 'publish' == $post->post_status ) {
1048 $caps[] = $post_type->cap->delete_published_posts;
1049 } elseif ( 'trash' == $post->post_status ) {
1050 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
1051 $caps[] = $post_type->cap->delete_published_posts;
1053 // If the post is draft...
1054 $caps[] = $post_type->cap->delete_posts;
1057 // The user is trying to edit someone else's post.
1058 $caps[] = $post_type->cap->delete_others_posts;
1059 // The post is published, extra cap required.
1060 if ( 'publish' == $post->post_status )
1061 $caps[] = $post_type->cap->delete_published_posts;
1062 elseif ( 'private' == $post->post_status )
1063 $caps[] = $post_type->cap->delete_private_posts;
1066 // edit_post breaks down to edit_posts, edit_published_posts, or
1067 // edit_others_posts
1070 $post = get_post( $args[0] );
1072 if ( 'revision' == $post->post_type ) {
1073 $post = get_post( $post->post_parent );
1076 $post_type = get_post_type_object( $post->post_type );
1078 if ( ! $post_type->map_meta_cap ) {
1079 $caps[] = $post_type->cap->$cap;
1080 // Prior to 3.1 we would re-call map_meta_cap here.
1081 if ( 'edit_post' == $cap )
1082 $cap = $post_type->cap->$cap;
1086 $post_author_id = $post->post_author;
1088 // If no author set yet, default to current user for cap checks.
1089 if ( ! $post_author_id )
1090 $post_author_id = $user_id;
1092 $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
1094 // If the user is the author...
1095 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
1096 // If the post is published...
1097 if ( 'publish' == $post->post_status ) {
1098 $caps[] = $post_type->cap->edit_published_posts;
1099 } elseif ( 'trash' == $post->post_status ) {
1100 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
1101 $caps[] = $post_type->cap->edit_published_posts;
1103 // If the post is draft...
1104 $caps[] = $post_type->cap->edit_posts;
1107 // The user is trying to edit someone else's post.
1108 $caps[] = $post_type->cap->edit_others_posts;
1109 // The post is published, extra cap required.
1110 if ( 'publish' == $post->post_status )
1111 $caps[] = $post_type->cap->edit_published_posts;
1112 elseif ( 'private' == $post->post_status )
1113 $caps[] = $post_type->cap->edit_private_posts;
1118 $post = get_post( $args[0] );
1120 if ( 'revision' == $post->post_type ) {
1121 $post = get_post( $post->post_parent );
1124 $post_type = get_post_type_object( $post->post_type );
1126 if ( ! $post_type->map_meta_cap ) {
1127 $caps[] = $post_type->cap->$cap;
1128 // Prior to 3.1 we would re-call map_meta_cap here.
1129 if ( 'read_post' == $cap )
1130 $cap = $post_type->cap->$cap;
1134 $status_obj = get_post_status_object( $post->post_status );
1135 if ( $status_obj->public ) {
1136 $caps[] = $post_type->cap->read;
1140 $post_author_id = $post->post_author;
1142 // If no author set yet, default to current user for cap checks.
1143 if ( ! $post_author_id )
1144 $post_author_id = $user_id;
1146 $post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
1148 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
1149 $caps[] = $post_type->cap->read;
1150 elseif ( $status_obj->private )
1151 $caps[] = $post_type->cap->read_private_posts;
1153 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1155 case 'publish_post':
1156 $post = get_post( $args[0] );
1157 $post_type = get_post_type_object( $post->post_type );
1159 $caps[] = $post_type->cap->publish_posts;
1161 case 'edit_post_meta':
1162 case 'delete_post_meta':
1163 case 'add_post_meta':
1164 $post = get_post( $args[0] );
1165 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1167 $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false;
1169 if ( $meta_key && has_filter( "auth_post_meta_{$meta_key}" ) ) {
1170 $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps );
1173 } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) {
1177 case 'edit_comment':
1178 $comment = get_comment( $args[0] );
1179 $post = get_post( $comment->comment_post_ID );
1180 $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
1182 case 'unfiltered_upload':
1183 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
1186 $caps[] = 'do_not_allow';
1188 case 'unfiltered_html' :
1189 // Disallow unfiltered_html for all users, even admins and super admins.
1190 if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
1191 $caps[] = 'do_not_allow';
1192 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1193 $caps[] = 'do_not_allow';
1198 case 'edit_plugins':
1200 // Disallow the file editors.
1201 if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
1202 $caps[] = 'do_not_allow';
1203 elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1204 $caps[] = 'do_not_allow';
1205 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1206 $caps[] = 'do_not_allow';
1210 case 'update_plugins':
1211 case 'delete_plugins':
1212 case 'install_plugins':
1213 case 'update_themes':
1214 case 'delete_themes':
1215 case 'install_themes':
1217 // Disallow anything that creates, deletes, or updates core, plugin, or theme files.
1218 // Files in uploads are excepted.
1219 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1220 $caps[] = 'do_not_allow';
1221 elseif ( is_multisite() && ! is_super_admin( $user_id ) )
1222 $caps[] = 'do_not_allow';
1226 case 'activate_plugins':
1228 if ( is_multisite() ) {
1229 // update_, install_, and delete_ are handled above with is_super_admin().
1230 $menu_perms = get_site_option( 'menu_items', array() );
1231 if ( empty( $menu_perms['plugins'] ) )
1232 $caps[] = 'manage_network_plugins';
1236 case 'delete_users':
1237 // If multisite only super admins can delete users.
1238 if ( is_multisite() && ! is_super_admin( $user_id ) )
1239 $caps[] = 'do_not_allow';
1241 $caps[] = 'delete_users'; // delete_user maps to delete_users.
1243 case 'create_users':
1244 if ( !is_multisite() )
1246 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1249 $caps[] = 'do_not_allow';
1251 case 'manage_links' :
1252 if ( get_option( 'link_manager_enabled' ) )
1255 $caps[] = 'do_not_allow';
1258 // Handle meta capabilities for custom post types.
1259 $post_type_meta_caps = _post_type_meta_capabilities();
1260 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1261 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1262 return call_user_func_array( 'map_meta_cap', $args );
1265 // If no meta caps match, return the original cap.
1269 return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
1273 * Whether current user has capability or role.
1277 * @param string $capability Capability or role name.
1280 function current_user_can( $capability ) {
1281 $current_user = wp_get_current_user();
1283 if ( empty( $current_user ) )
1286 $args = array_slice( func_get_args(), 1 );
1287 $args = array_merge( array( $capability ), $args );
1289 return call_user_func_array( array( $current_user, 'has_cap' ), $args );
1293 * Whether current user has a capability or role for a given blog.
1297 * @param int $blog_id Blog ID
1298 * @param string $capability Capability or role name.
1301 function current_user_can_for_blog( $blog_id, $capability ) {
1302 if ( is_multisite() )
1303 switch_to_blog( $blog_id );
1305 $current_user = wp_get_current_user();
1307 if ( empty( $current_user ) )
1310 $args = array_slice( func_get_args(), 2 );
1311 $args = array_merge( array( $capability ), $args );
1313 $can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
1315 if ( is_multisite() )
1316 restore_current_blog();
1322 * Whether author of supplied post has capability or role.
1326 * @param int|object $post Post ID or post object.
1327 * @param string $capability Capability or role name.
1330 function author_can( $post, $capability ) {
1331 if ( !$post = get_post($post) )
1334 $author = get_userdata( $post->post_author );
1339 $args = array_slice( func_get_args(), 2 );
1340 $args = array_merge( array( $capability ), $args );
1342 return call_user_func_array( array( $author, 'has_cap' ), $args );
1346 * Whether a particular user has capability or role.
1350 * @param int|object $user User ID or object.
1351 * @param string $capability Capability or role name.
1354 function user_can( $user, $capability ) {
1355 if ( ! is_object( $user ) )
1356 $user = get_userdata( $user );
1358 if ( ! $user || ! $user->exists() )
1361 $args = array_slice( func_get_args(), 2 );
1362 $args = array_merge( array( $capability ), $args );
1364 return call_user_func_array( array( $user, 'has_cap' ), $args );
1368 * Retrieve role object.
1370 * @see WP_Roles::get_role() Uses method to retrieve role object.
1373 * @param string $role Role name.
1374 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
1376 function get_role( $role ) {
1379 if ( ! isset( $wp_roles ) )
1380 $wp_roles = new WP_Roles();
1382 return $wp_roles->get_role( $role );
1386 * Add role, if it does not exist.
1388 * @see WP_Roles::add_role() Uses method to add role.
1391 * @param string $role Role name.
1392 * @param string $display_name Display name for role.
1393 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1394 * @return WP_Role|null WP_Role object if role is added, null if already exists.
1396 function add_role( $role, $display_name, $capabilities = array() ) {
1399 if ( ! isset( $wp_roles ) )
1400 $wp_roles = new WP_Roles();
1402 return $wp_roles->add_role( $role, $display_name, $capabilities );
1406 * Remove role, if it exists.
1408 * @see WP_Roles::remove_role() Uses method to remove role.
1411 * @param string $role Role name.
1414 function remove_role( $role ) {
1417 if ( ! isset( $wp_roles ) )
1418 $wp_roles = new WP_Roles();
1420 return $wp_roles->remove_role( $role );
1424 * Retrieve a list of super admins.
1428 * @uses $super_admins Super admins global variable, if set.
1430 * @return array List of super admin logins
1432 function get_super_admins() {
1433 global $super_admins;
1435 if ( isset($super_admins) )
1436 return $super_admins;
1438 return get_site_option( 'site_admins', array('admin') );
1442 * Determine if user is a site admin.
1446 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1447 * @return bool True if the user is a site admin.
1449 function is_super_admin( $user_id = false ) {
1450 if ( ! $user_id || $user_id == get_current_user_id() )
1451 $user = wp_get_current_user();
1453 $user = get_userdata( $user_id );
1455 if ( ! $user || ! $user->exists() )
1458 if ( is_multisite() ) {
1459 $super_admins = get_super_admins();
1460 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1463 if ( $user->has_cap('delete_users') )