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) $this->roles as $role => $data ) {
112 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
113 $this->role_names[$role] = $this->roles[$role]['name'];
118 * Add role name with capabilities to list.
120 * Updates the list of roles, if the role doesn't already exist.
122 * The capabilities are defined in the following format `array( 'read' => true );`
123 * To explicitly deny a role a capability you set the value for that capability to false.
128 * @param string $role Role name.
129 * @param string $display_name Role display name.
130 * @param array $capabilities List of role capabilities in the above format.
131 * @return null|WP_Role WP_Role object if role is added, null if already exists.
133 function add_role( $role, $display_name, $capabilities = array() ) {
134 if ( isset( $this->roles[$role] ) )
137 $this->roles[$role] = array(
138 'name' => $display_name,
139 'capabilities' => $capabilities
142 update_option( $this->role_key, $this->roles );
143 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
144 $this->role_names[$role] = $display_name;
145 return $this->role_objects[$role];
149 * Remove role by name.
154 * @param string $role Role name.
156 function remove_role( $role ) {
157 if ( ! isset( $this->role_objects[$role] ) )
160 unset( $this->role_objects[$role] );
161 unset( $this->role_names[$role] );
162 unset( $this->roles[$role] );
165 update_option( $this->role_key, $this->roles );
169 * Add capability to role.
174 * @param string $role Role name.
175 * @param string $cap Capability name.
176 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
178 function add_cap( $role, $cap, $grant = true ) {
179 $this->roles[$role]['capabilities'][$cap] = $grant;
181 update_option( $this->role_key, $this->roles );
185 * Remove capability from role.
190 * @param string $role Role name.
191 * @param string $cap Capability name.
193 function remove_cap( $role, $cap ) {
194 unset( $this->roles[$role]['capabilities'][$cap] );
196 update_option( $this->role_key, $this->roles );
200 * Retrieve role object by name.
205 * @param string $role Role name.
206 * @return object|null Null, if role does not exist. WP_Role object, if found.
208 function &get_role( $role ) {
209 if ( isset( $this->role_objects[$role] ) )
210 return $this->role_objects[$role];
216 * Retrieve list of role names.
221 * @return array List of role names.
223 function get_names() {
224 return $this->role_names;
228 * Whether role name is currently in the list of available roles.
233 * @param string $role Role name to look up.
236 function is_role( $role )
238 return isset( $this->role_names[$role] );
243 * WordPress Role class.
260 * List of capabilities the role contains.
269 * Constructor - Set up object properties.
271 * The list of capabilities, must have the key as the name of the capability
272 * and the value a boolean of whether it is granted to the role.
277 * @param string $role Role name.
278 * @param array $capabilities List of capabilities.
280 function __construct( $role, $capabilities ) {
282 $this->capabilities = $capabilities;
286 * Assign role a capability.
288 * @see WP_Roles::add_cap() Method uses implementation for role.
292 * @param string $cap Capability name.
293 * @param bool $grant Whether role has capability privilege.
295 function add_cap( $cap, $grant = true ) {
298 if ( ! isset( $wp_roles ) )
299 $wp_roles = new WP_Roles();
301 $this->capabilities[$cap] = $grant;
302 $wp_roles->add_cap( $this->name, $cap, $grant );
306 * Remove capability from role.
308 * This is a container for {@link WP_Roles::remove_cap()} to remove the
309 * capability from the role. That is to say, that {@link
310 * WP_Roles::remove_cap()} implements the functionality, but it also makes
311 * sense to use this class, because you don't need to enter the role name.
316 * @param string $cap Capability name.
318 function remove_cap( $cap ) {
321 if ( ! isset( $wp_roles ) )
322 $wp_roles = new WP_Roles();
324 unset( $this->capabilities[$cap] );
325 $wp_roles->remove_cap( $this->name, $cap );
329 * Whether role has capability.
331 * The capabilities is passed through the 'role_has_cap' filter. The first
332 * parameter for the hook is the list of capabilities the class has
333 * assigned. The second parameter is the capability name to look for. The
334 * third and final parameter for the hook is the role name.
339 * @param string $cap Capability name.
340 * @return bool True, if user has capability. False, if doesn't have capability.
342 function has_cap( $cap ) {
343 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
344 if ( !empty( $capabilities[$cap] ) )
345 return $capabilities[$cap];
353 * WordPress User class.
361 * User data container.
363 * This will be set as properties of the object.
381 * The deprecated user's ID.
385 * @deprecated Use WP_User::$ID
392 * The individual capabilities the user has been given.
401 * User metadata option name.
410 * The roles the user is part of.
416 var $roles = array();
419 * All capabilities the user has, including individual and role based.
425 var $allcaps = array();
428 * First name of the user.
430 * Created to prevent notices.
436 var $first_name = '';
439 * Last name of the user.
441 * Created to prevent notices.
450 * The filter context applied to user data fields.
459 * Constructor - Sets up the object properties.
461 * Retrieves the userdata and then assigns all of the data keys to direct
462 * properties of the object. Calls {@link WP_User::_init_caps()} after
463 * setting up the object's user data properties.
468 * @param int|string $id User's ID or username
469 * @param int $name Optional. User's username
470 * @param int $blog_id Optional Blog ID, defaults to current blog.
473 function __construct( $id, $name = '', $blog_id = '' ) {
475 if ( empty( $id ) && empty( $name ) )
478 if ( ! is_numeric( $id ) ) {
483 if ( ! empty( $id ) )
484 $this->data = get_userdata( $id );
486 $this->data = get_userdatabylogin( $name );
488 if ( empty( $this->data->ID ) )
491 foreach ( get_object_vars( $this->data ) as $key => $value ) {
492 $this->{$key} = $value;
495 $this->id = $this->ID;
496 $this->for_blog( $blog_id );
500 * Set up capability object properties.
502 * Will set the value for the 'cap_key' property to current database table
503 * prefix, followed by 'capabilities'. Will then check to see if the
504 * property matching the 'cap_key' exists and is an array. If so, it will be
509 * @param string $cap_key Optional capability key
512 function _init_caps( $cap_key = '' ) {
514 if ( empty($cap_key) )
515 $this->cap_key = $wpdb->prefix . 'capabilities';
517 $this->cap_key = $cap_key;
518 $this->caps = &$this->{$this->cap_key};
519 if ( ! is_array( $this->caps ) )
520 $this->caps = array();
521 $this->get_role_caps();
525 * Retrieve all of the role capabilities and merge with individual capabilities.
527 * All of the capabilities of the roles the user belongs to are merged with
528 * the users individual roles. This also means that the user can be denied
529 * specific roles that their role might have, but the specific user isn't
530 * granted permission to.
536 function get_role_caps() {
539 if ( ! isset( $wp_roles ) )
540 $wp_roles = new WP_Roles();
542 //Filter out caps that are not role names and assign to $this->roles
543 if ( is_array( $this->caps ) )
544 $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
546 //Build $allcaps from role caps, overlay user's $caps
547 $this->allcaps = array();
548 foreach ( (array) $this->roles as $role ) {
549 $the_role =& $wp_roles->get_role( $role );
550 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
552 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
558 * Updates the user's meta data option with capabilities and roles.
563 * @param string $role Role name.
565 function add_role( $role ) {
566 $this->caps[$role] = true;
567 update_user_meta( $this->ID, $this->cap_key, $this->caps );
568 $this->get_role_caps();
569 $this->update_user_level_from_caps();
573 * Remove role from user.
578 * @param string $role Role name.
580 function remove_role( $role ) {
581 if ( !in_array($role, $this->roles) )
583 unset( $this->caps[$role] );
584 update_user_meta( $this->ID, $this->cap_key, $this->caps );
585 $this->get_role_caps();
586 $this->update_user_level_from_caps();
590 * Set the role of the user.
592 * This will remove the previous roles of the user and assign the user the
593 * new one. You can set the role to an empty string and it will remove all
594 * of the roles from the user.
599 * @param string $role Role name.
601 function set_role( $role ) {
602 foreach ( (array) $this->roles as $oldrole )
603 unset( $this->caps[$oldrole] );
605 if ( 1 == count( $this->roles ) && $role == $this->roles[0] )
608 if ( !empty( $role ) ) {
609 $this->caps[$role] = true;
610 $this->roles = array( $role => true );
612 $this->roles = false;
614 update_user_meta( $this->ID, $this->cap_key, $this->caps );
615 $this->get_role_caps();
616 $this->update_user_level_from_caps();
617 do_action( 'set_user_role', $this->ID, $role );
621 * Choose the maximum level the user has.
623 * Will compare the level from the $item parameter against the $max
624 * parameter. If the item is incorrect, then just the $max parameter value
627 * Used to get the max level based on the capabilities the user has. This
628 * is also based on roles, so if the user is assigned the Administrator role
629 * then the capability 'level_10' will exist and the user will get that
635 * @param int $max Max level of user.
636 * @param string $item Level capability name.
637 * @return int Max Level.
639 function level_reduction( $max, $item ) {
640 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
641 $level = intval( $matches[1] );
642 return max( $max, $level );
649 * Update the maximum user level for the user.
651 * Updates the 'user_level' user metadata (includes prefix that is the
652 * database table prefix) with the maximum user level. Gets the value from
653 * the all of the capabilities that the user has.
658 function update_user_level_from_caps() {
660 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
661 update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
665 * Add capability and grant or deny access to capability.
670 * @param string $cap Capability name.
671 * @param bool $grant Whether to grant capability to user.
673 function add_cap( $cap, $grant = true ) {
674 $this->caps[$cap] = $grant;
675 update_user_meta( $this->ID, $this->cap_key, $this->caps );
679 * Remove capability from user.
684 * @param string $cap Capability name.
686 function remove_cap( $cap ) {
687 if ( empty( $this->caps[$cap] ) )
689 unset( $this->caps[$cap] );
690 update_user_meta( $this->ID, $this->cap_key, $this->caps );
694 * Remove all of the capabilities of the user.
699 function remove_all_caps() {
701 $this->caps = array();
702 delete_user_meta( $this->ID, $this->cap_key );
703 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
704 $this->get_role_caps();
708 * Whether user has capability or role name.
710 * This is useful for looking up whether the user has a specific role
711 * assigned to the user. The second optional parameter can also be used to
712 * check for capabilities against a specfic post.
717 * @param string|int $cap Capability or role name to search.
718 * @param int $post_id Optional. Post ID to check capability against specific post.
719 * @return bool True, if user has capability; false, if user does not have capability.
721 function has_cap( $cap ) {
722 if ( is_numeric( $cap ) ) {
723 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
724 $cap = $this->translate_level_to_cap( $cap );
727 $args = array_slice( func_get_args(), 1 );
728 $args = array_merge( array( $cap, $this->ID ), $args );
729 $caps = call_user_func_array( 'map_meta_cap', $args );
731 // Multisite super admin has all caps by definition, Unless specifically denied.
732 if ( is_multisite() && is_super_admin( $this->ID ) ) {
733 if ( in_array('do_not_allow', $caps) )
738 // Must have ALL requested caps
739 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
740 $capabilities['exist'] = true; // Everyone is allowed to exist
741 foreach ( (array) $caps as $cap ) {
742 //echo "Checking cap $cap<br />";
743 if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
751 * Convert numeric level to level capability name.
753 * Prepends 'level_' to level number.
758 * @param int $level Level number, 1 to 10.
761 function translate_level_to_cap( $level ) {
762 return 'level_' . $level;
766 * Set the blog to operate on. Defaults to the current blog.
770 * @param int $blog_id Optional Blog ID, defaults to current blog.
772 function for_blog( $blog_id = '' ) {
774 if ( ! empty( $blog_id ) )
775 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
778 $this->_init_caps( $cap_key );
783 * Map meta capabilities to primitive capabilities.
785 * This does not actually compare whether the user ID has the actual capability,
786 * just what the capability or capabilities are. Meta capability list value can
787 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
788 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
792 * @param string $cap Capability name.
793 * @param int $user_id User ID.
794 * @return array Actual capabilities for meta capability.
796 function map_meta_cap( $cap, $user_id ) {
797 $args = array_slice( func_get_args(), 2 );
802 $caps[] = 'remove_users';
805 $caps[] = 'promote_users';
808 // Allow user to edit itself
809 if ( isset( $args[0] ) && $user_id == $args[0] )
813 // If multisite these caps are allowed only for super admins.
814 if ( is_multisite() && !is_super_admin( $user_id ) )
815 $caps[] = 'do_not_allow';
817 $caps[] = 'edit_users'; // Explicit due to primitive fall through
821 $author_data = get_userdata( $user_id );
822 $post = get_post( $args[0] );
824 if ( 'revision' == $post->post_type ) {
825 $post = get_post( $post->post_parent );
828 $post_type = get_post_type_object( $post->post_type );
830 if ( ! $post_type->map_meta_cap ) {
831 $caps[] = $post_type->cap->$cap;
832 // Prior to 3.1 we would re-call map_meta_cap here.
833 if ( 'delete_post' == $cap )
834 $cap = $post_type->cap->$cap;
838 if ( '' != $post->post_author ) {
839 $post_author_data = get_userdata( $post->post_author );
841 // No author set yet, so default to current user for cap checks.
842 $post_author_data = $author_data;
845 // If the user is the author...
846 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
847 // If the post is published...
848 if ( 'publish' == $post->post_status ) {
849 $caps[] = $post_type->cap->delete_published_posts;
850 } elseif ( 'trash' == $post->post_status ) {
851 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
852 $caps[] = $post_type->cap->delete_published_posts;
854 // If the post is draft...
855 $caps[] = $post_type->cap->delete_posts;
858 // The user is trying to edit someone else's post.
859 $caps[] = $post_type->cap->delete_others_posts;
860 // The post is published, extra cap required.
861 if ( 'publish' == $post->post_status )
862 $caps[] = $post_type->cap->delete_published_posts;
863 elseif ( 'private' == $post->post_status )
864 $caps[] = $post_type->cap->delete_private_posts;
867 // edit_post breaks down to edit_posts, edit_published_posts, or
871 $author_data = get_userdata( $user_id );
872 $post = get_post( $args[0] );
874 if ( 'revision' == $post->post_type ) {
875 $post = get_post( $post->post_parent );
878 $post_type = get_post_type_object( $post->post_type );
880 if ( ! $post_type->map_meta_cap ) {
881 $caps[] = $post_type->cap->$cap;
882 // Prior to 3.1 we would re-call map_meta_cap here.
883 if ( 'edit_post' == $cap )
884 $cap = $post_type->cap->$cap;
888 if ( '' != $post->post_author ) {
889 $post_author_data = get_userdata( $post->post_author );
891 // No author set yet, so default to current user for cap checks.
892 $post_author_data = $author_data;
895 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
896 // If the user is the author...
897 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
898 // If the post is published...
899 if ( 'publish' == $post->post_status ) {
900 $caps[] = $post_type->cap->edit_published_posts;
901 } elseif ( 'trash' == $post->post_status ) {
902 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
903 $caps[] = $post_type->cap->edit_published_posts;
905 // If the post is draft...
906 $caps[] = $post_type->cap->edit_posts;
909 // The user is trying to edit someone else's post.
910 $caps[] = $post_type->cap->edit_others_posts;
911 // The post is published, extra cap required.
912 if ( 'publish' == $post->post_status )
913 $caps[] = $post_type->cap->edit_published_posts;
914 elseif ( 'private' == $post->post_status )
915 $caps[] = $post_type->cap->edit_private_posts;
920 $author_data = get_userdata( $user_id );
921 $post = get_post( $args[0] );
923 if ( 'revision' == $post->post_type ) {
924 $post = get_post( $post->post_parent );
927 $post_type = get_post_type_object( $post->post_type );
929 if ( ! $post_type->map_meta_cap ) {
930 $caps[] = $post_type->cap->$cap;
931 // Prior to 3.1 we would re-call map_meta_cap here.
932 if ( 'read_post' == $cap )
933 $cap = $post_type->cap->$cap;
937 if ( 'private' != $post->post_status ) {
938 $caps[] = $post_type->cap->read;
942 if ( '' != $post->post_author ) {
943 $post_author_data = get_userdata( $post->post_author );
945 // No author set yet, so default to current user for cap checks.
946 $post_author_data = $author_data;
949 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
950 $caps[] = $post_type->cap->read;
952 $caps[] = $post_type->cap->read_private_posts;
955 $comment = get_comment( $args[0] );
956 $post = get_post( $comment->comment_post_ID );
957 $post_type_object = get_post_type_object( $post->post_type );
959 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
961 case 'unfiltered_upload':
962 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
965 $caps[] = 'do_not_allow';
970 if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
971 $caps[] = 'do_not_allow';
974 // Fall through if not DISALLOW_FILE_EDIT.
975 case 'update_plugins':
976 case 'delete_plugins':
977 case 'install_plugins':
978 case 'update_themes':
979 case 'delete_themes':
980 case 'install_themes':
982 // Disallow anything that creates, deletes, or edits core, plugin, or theme files.
983 // Files in uploads are excepted.
984 if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
985 $caps[] = 'do_not_allow';
988 // Fall through if not DISALLOW_FILE_MODS.
989 case 'unfiltered_html':
990 // Disallow unfiltered_html for all users, even admins and super admins.
991 if ( defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML ) {
992 $caps[] = 'do_not_allow';
995 // Fall through if not DISALLOW_UNFILTERED_HTML
998 // If multisite these caps are allowed only for super admins.
999 if ( is_multisite() && !is_super_admin( $user_id ) ) {
1000 $caps[] = 'do_not_allow';
1002 if ( 'delete_user' == $cap )
1003 $cap = 'delete_users';
1007 case 'create_users':
1008 if ( !is_multisite() )
1010 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1013 $caps[] = 'do_not_allow';
1016 // Handle meta capabilities for custom post types.
1017 $post_type_meta_caps = _post_type_meta_capabilities();
1018 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1019 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1020 return call_user_func_array( 'map_meta_cap', $args );
1023 // If no meta caps match, return the original cap.
1027 return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
1031 * Whether current user has capability or role.
1035 * @param string $capability Capability or role name.
1038 function current_user_can( $capability ) {
1039 $current_user = wp_get_current_user();
1041 if ( empty( $current_user ) )
1044 $args = array_slice( func_get_args(), 1 );
1045 $args = array_merge( array( $capability ), $args );
1047 return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
1051 * Whether current user has a capability or role for a given blog.
1055 * @param int $blog_id Blog ID
1056 * @param string $capability Capability or role name.
1059 function current_user_can_for_blog( $blog_id, $capability ) {
1060 $current_user = wp_get_current_user();
1062 if ( empty( $current_user ) )
1065 // Create new object to avoid stomping the global current_user.
1066 $user = new WP_User( $current_user->id) ;
1068 // Set the blog id. @todo add blog id arg to WP_User constructor?
1069 $user->for_blog( $blog_id );
1071 $args = array_slice( func_get_args(), 2 );
1072 $args = array_merge( array( $capability ), $args );
1074 return call_user_func_array( array( &$user, 'has_cap' ), $args );
1078 * Whether author of supplied post has capability or role.
1082 * @param int|object $post Post ID or post object.
1083 * @param string $capability Capability or role name.
1086 function author_can( $post, $capability ) {
1087 if ( !$post = get_post($post) )
1090 $author = new WP_User( $post->post_author );
1092 if ( empty( $author->ID ) )
1095 $args = array_slice( func_get_args(), 2 );
1096 $args = array_merge( array( $capability ), $args );
1098 return call_user_func_array( array( &$author, 'has_cap' ), $args );
1102 * Whether a particular user has capability or role.
1106 * @param int|object $user User ID or object.
1107 * @param string $capability Capability or role name.
1110 function user_can( $user, $capability ) {
1111 if ( ! is_object( $user ) )
1112 $user = new WP_User( $user );
1114 if ( ! $user || ! $user->ID )
1117 $args = array_slice( func_get_args(), 2 );
1118 $args = array_merge( array( $capability ), $args );
1120 return call_user_func_array( array( &$user, 'has_cap' ), $args );
1124 * Retrieve role object.
1126 * @see WP_Roles::get_role() Uses method to retrieve role object.
1129 * @param string $role Role name.
1132 function get_role( $role ) {
1135 if ( ! isset( $wp_roles ) )
1136 $wp_roles = new WP_Roles();
1138 return $wp_roles->get_role( $role );
1142 * Add role, if it does not exist.
1144 * @see WP_Roles::add_role() Uses method to add role.
1147 * @param string $role Role name.
1148 * @param string $display_name Display name for role.
1149 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1150 * @return null|WP_Role WP_Role object if role is added, null if already exists.
1152 function add_role( $role, $display_name, $capabilities = array() ) {
1155 if ( ! isset( $wp_roles ) )
1156 $wp_roles = new WP_Roles();
1158 return $wp_roles->add_role( $role, $display_name, $capabilities );
1162 * Remove role, if it exists.
1164 * @see WP_Roles::remove_role() Uses method to remove role.
1167 * @param string $role Role name.
1170 function remove_role( $role ) {
1173 if ( ! isset( $wp_roles ) )
1174 $wp_roles = new WP_Roles();
1176 return $wp_roles->remove_role( $role );
1180 * Retrieve a list of super admins.
1184 * @uses $super_admins Super admins global variable, if set.
1186 * @return array List of super admin logins
1188 function get_super_admins() {
1189 global $super_admins;
1191 if ( isset($super_admins) )
1192 return $super_admins;
1194 return get_site_option( 'site_admins', array('admin') );
1198 * Determine if user is a site admin.
1202 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1203 * @return bool True if the user is a site admin.
1205 function is_super_admin( $user_id = false ) {
1207 $user = new WP_User( $user_id );
1209 $user = wp_get_current_user();
1211 if ( empty( $user->id ) )
1214 if ( is_multisite() ) {
1215 $super_admins = get_super_admins();
1216 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1219 if ( $user->has_cap('delete_users') )