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.
76 * PHP4 Constructor - Call {@link WP_Roles::_init()} method.
88 * Set up the object properties.
90 * The role key is set to the current prefix for the $wpdb object with
91 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
92 * be used and the role option will not be updated or used.
96 * @uses $wpdb Used to get the database prefix.
97 * @global array $wp_user_roles Used to set the 'roles' property value.
100 global $wpdb, $wp_user_roles;
101 $this->role_key = $wpdb->prefix . 'user_roles';
102 if ( ! empty( $wp_user_roles ) ) {
103 $this->roles = $wp_user_roles;
104 $this->use_db = false;
106 $this->roles = get_option( $this->role_key );
109 if ( empty( $this->roles ) )
112 $this->role_objects = array();
113 $this->role_names = array();
114 foreach ( (array) $this->roles as $role => $data ) {
115 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
116 $this->role_names[$role] = $this->roles[$role]['name'];
121 * Add role name with capabilities to list.
123 * Updates the list of roles, if the role doesn't already exist.
125 * The capabilities are defined in the following format `array( 'read' => true );`
126 * To explicitly deny a role a capability you set the value for that capability to false.
131 * @param string $role Role name.
132 * @param string $display_name Role display name.
133 * @param array $capabilities List of role capabilities in the above format.
134 * @return null|WP_Role WP_Role object if role is added, null if already exists.
136 function add_role( $role, $display_name, $capabilities = array() ) {
137 if ( isset( $this->roles[$role] ) )
140 $this->roles[$role] = array(
141 'name' => $display_name,
142 'capabilities' => $capabilities
145 update_option( $this->role_key, $this->roles );
146 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
147 $this->role_names[$role] = $display_name;
148 return $this->role_objects[$role];
152 * Remove role by name.
157 * @param string $role Role name.
159 function remove_role( $role ) {
160 if ( ! isset( $this->role_objects[$role] ) )
163 unset( $this->role_objects[$role] );
164 unset( $this->role_names[$role] );
165 unset( $this->roles[$role] );
168 update_option( $this->role_key, $this->roles );
172 * Add capability to role.
177 * @param string $role Role name.
178 * @param string $cap Capability name.
179 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
181 function add_cap( $role, $cap, $grant = true ) {
182 $this->roles[$role]['capabilities'][$cap] = $grant;
184 update_option( $this->role_key, $this->roles );
188 * Remove capability from role.
193 * @param string $role Role name.
194 * @param string $cap Capability name.
196 function remove_cap( $role, $cap ) {
197 unset( $this->roles[$role]['capabilities'][$cap] );
199 update_option( $this->role_key, $this->roles );
203 * Retrieve role object by name.
208 * @param string $role Role name.
209 * @return object|null Null, if role does not exist. WP_Role object, if found.
211 function &get_role( $role ) {
212 if ( isset( $this->role_objects[$role] ) )
213 return $this->role_objects[$role];
219 * Retrieve list of role names.
224 * @return array List of role names.
226 function get_names() {
227 return $this->role_names;
231 * Whether role name is currently in the list of available roles.
236 * @param string $role Role name to look up.
239 function is_role( $role )
241 return isset( $this->role_names[$role] );
246 * WordPress Role class.
263 * List of capabilities the role contains.
272 * PHP4 Constructor - Set up object properties.
274 * The list of capabilities, must have the key as the name of the capability
275 * and the value a boolean of whether it is granted to the role.
280 * @param string $role Role name.
281 * @param array $capabilities List of capabilities.
284 function WP_Role( $role, $capabilities ) {
286 $this->capabilities = $capabilities;
290 * Assign role a capability.
292 * @see WP_Roles::add_cap() Method uses implementation for role.
296 * @param string $cap Capability name.
297 * @param bool $grant Whether role has capability privilege.
299 function add_cap( $cap, $grant = true ) {
302 if ( ! isset( $wp_roles ) )
303 $wp_roles = new WP_Roles();
305 $this->capabilities[$cap] = $grant;
306 $wp_roles->add_cap( $this->name, $cap, $grant );
310 * Remove capability from role.
312 * This is a container for {@link WP_Roles::remove_cap()} to remove the
313 * capability from the role. That is to say, that {@link
314 * WP_Roles::remove_cap()} implements the functionality, but it also makes
315 * sense to use this class, because you don't need to enter the role name.
320 * @param string $cap Capability name.
322 function remove_cap( $cap ) {
325 if ( ! isset( $wp_roles ) )
326 $wp_roles = new WP_Roles();
328 unset( $this->capabilities[$cap] );
329 $wp_roles->remove_cap( $this->name, $cap );
333 * Whether role has capability.
335 * The capabilities is passed through the 'role_has_cap' filter. The first
336 * parameter for the hook is the list of capabilities the class has
337 * assigned. The second parameter is the capability name to look for. The
338 * third and final parameter for the hook is the role name.
343 * @param string $cap Capability name.
344 * @return bool True, if user has capability. False, if doesn't have capability.
346 function has_cap( $cap ) {
347 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
348 if ( !empty( $capabilities[$cap] ) )
349 return $capabilities[$cap];
357 * WordPress User class.
365 * User data container.
367 * This will be set as properties of the object.
385 * The deprecated user's ID.
389 * @deprecated Use WP_User::$ID
396 * The individual capabilities the user has been given.
405 * User metadata option name.
414 * The roles the user is part of.
420 var $roles = array();
423 * All capabilities the user has, including individual and role based.
429 var $allcaps = array();
432 * First name of the user.
434 * Created to prevent notices.
440 var $first_name = '';
443 * Last name of the user.
445 * Created to prevent notices.
454 * The filter context applied to user data fields.
463 * PHP4 Constructor - Sets up the object properties.
465 * Retrieves the userdata and then assigns all of the data keys to direct
466 * properties of the object. Calls {@link WP_User::_init_caps()} after
467 * setting up the object's user data properties.
472 * @param int|string $id User's ID or username
473 * @param int $name Optional. User's username
474 * @param int $blog_id Optional Blog ID, defaults to current blog.
477 function WP_User( $id, $name = '', $blog_id = '' ) {
479 if ( empty( $id ) && empty( $name ) )
482 if ( ! is_numeric( $id ) ) {
487 if ( ! empty( $id ) )
488 $this->data = get_userdata( $id );
490 $this->data = get_userdatabylogin( $name );
492 if ( empty( $this->data->ID ) )
495 foreach ( get_object_vars( $this->data ) as $key => $value ) {
496 $this->{$key} = $value;
499 $this->id = $this->ID;
500 $this->for_blog( $blog_id );
504 * Set up capability object properties.
506 * Will set the value for the 'cap_key' property to current database table
507 * prefix, followed by 'capabilities'. Will then check to see if the
508 * property matching the 'cap_key' exists and is an array. If so, it will be
513 * @param string $cap_key Optional capability key
516 function _init_caps( $cap_key = '' ) {
518 if ( empty($cap_key) )
519 $this->cap_key = $wpdb->prefix . 'capabilities';
521 $this->cap_key = $cap_key;
522 $this->caps = &$this->{$this->cap_key};
523 if ( ! is_array( $this->caps ) )
524 $this->caps = array();
525 $this->get_role_caps();
529 * Retrieve all of the role capabilities and merge with individual capabilities.
531 * All of the capabilities of the roles the user belongs to are merged with
532 * the users individual roles. This also means that the user can be denied
533 * specific roles that their role might have, but the specific user isn't
534 * granted permission to.
540 function get_role_caps() {
543 if ( ! isset( $wp_roles ) )
544 $wp_roles = new WP_Roles();
546 //Filter out caps that are not role names and assign to $this->roles
547 if ( is_array( $this->caps ) )
548 $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
550 //Build $allcaps from role caps, overlay user's $caps
551 $this->allcaps = array();
552 foreach ( (array) $this->roles as $role ) {
553 $the_role =& $wp_roles->get_role( $role );
554 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
556 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
562 * Updates the user's meta data option with capabilities and roles.
567 * @param string $role Role name.
569 function add_role( $role ) {
570 $this->caps[$role] = true;
571 update_user_meta( $this->ID, $this->cap_key, $this->caps );
572 $this->get_role_caps();
573 $this->update_user_level_from_caps();
577 * Remove role from user.
582 * @param string $role Role name.
584 function remove_role( $role ) {
585 if ( !in_array($role, $this->roles) )
587 unset( $this->caps[$role] );
588 update_user_meta( $this->ID, $this->cap_key, $this->caps );
589 $this->get_role_caps();
590 $this->update_user_level_from_caps();
594 * Set the role of the user.
596 * This will remove the previous roles of the user and assign the user the
597 * new one. You can set the role to an empty string and it will remove all
598 * of the roles from the user.
603 * @param string $role Role name.
605 function set_role( $role ) {
606 foreach ( (array) $this->roles as $oldrole )
607 unset( $this->caps[$oldrole] );
609 if ( 1 == count( $this->roles ) && $role == $this->roles[0] )
612 if ( !empty( $role ) ) {
613 $this->caps[$role] = true;
614 $this->roles = array( $role => true );
616 $this->roles = false;
618 update_user_meta( $this->ID, $this->cap_key, $this->caps );
619 $this->get_role_caps();
620 $this->update_user_level_from_caps();
621 do_action( 'set_user_role', $this->ID, $role );
625 * Choose the maximum level the user has.
627 * Will compare the level from the $item parameter against the $max
628 * parameter. If the item is incorrect, then just the $max parameter value
631 * Used to get the max level based on the capabilities the user has. This
632 * is also based on roles, so if the user is assigned the Administrator role
633 * then the capability 'level_10' will exist and the user will get that
639 * @param int $max Max level of user.
640 * @param string $item Level capability name.
641 * @return int Max Level.
643 function level_reduction( $max, $item ) {
644 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
645 $level = intval( $matches[1] );
646 return max( $max, $level );
653 * Update the maximum user level for the user.
655 * Updates the 'user_level' user metadata (includes prefix that is the
656 * database table prefix) with the maximum user level. Gets the value from
657 * the all of the capabilities that the user has.
662 function update_user_level_from_caps() {
664 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
665 update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
669 * Add capability and grant or deny access to capability.
674 * @param string $cap Capability name.
675 * @param bool $grant Whether to grant capability to user.
677 function add_cap( $cap, $grant = true ) {
678 $this->caps[$cap] = $grant;
679 update_user_meta( $this->ID, $this->cap_key, $this->caps );
683 * Remove capability from user.
688 * @param string $cap Capability name.
690 function remove_cap( $cap ) {
691 if ( empty( $this->caps[$cap] ) )
693 unset( $this->caps[$cap] );
694 update_user_meta( $this->ID, $this->cap_key, $this->caps );
698 * Remove all of the capabilities of the user.
703 function remove_all_caps() {
705 $this->caps = array();
706 delete_user_meta( $this->ID, $this->cap_key );
707 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
708 $this->get_role_caps();
712 * Whether user has capability or role name.
714 * This is useful for looking up whether the user has a specific role
715 * assigned to the user. The second optional parameter can also be used to
716 * check for capabilities against a specfic post.
721 * @param string|int $cap Capability or role name to search.
722 * @param int $post_id Optional. Post ID to check capability against specific post.
723 * @return bool True, if user has capability; false, if user does not have capability.
725 function has_cap( $cap ) {
726 if ( is_numeric( $cap ) ) {
727 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
728 $cap = $this->translate_level_to_cap( $cap );
731 $args = array_slice( func_get_args(), 1 );
732 $args = array_merge( array( $cap, $this->ID ), $args );
733 $caps = call_user_func_array( 'map_meta_cap', $args );
735 // Multisite super admin has all caps by definition, Unless specifically denied.
736 if ( is_multisite() && is_super_admin( $this->ID ) ) {
737 if ( in_array('do_not_allow', $caps) )
742 // Must have ALL requested caps
743 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
744 $capabilities['exist'] = true; // Everyone is allowed to exist
745 foreach ( (array) $caps as $cap ) {
746 //echo "Checking cap $cap<br />";
747 if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
755 * Convert numeric level to level capability name.
757 * Prepends 'level_' to level number.
762 * @param int $level Level number, 1 to 10.
765 function translate_level_to_cap( $level ) {
766 return 'level_' . $level;
770 * Set the blog to operate on. Defaults to the current blog.
774 * @param int $blog_id Optional Blog ID, defaults to current blog.
776 function for_blog( $blog_id = '' ) {
778 if ( ! empty( $blog_id ) )
779 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
782 $this->_init_caps( $cap_key );
787 * Map meta capabilities to primitive capabilities.
789 * This does not actually compare whether the user ID has the actual capability,
790 * just what the capability or capabilities are. Meta capability list value can
791 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
792 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
796 * @param string $cap Capability name.
797 * @param int $user_id User ID.
798 * @return array Actual capabilities for meta capability.
800 function map_meta_cap( $cap, $user_id ) {
801 $args = array_slice( func_get_args(), 2 );
806 $caps[] = 'remove_users';
809 $caps[] = 'promote_users';
812 // Allow user to edit itself
813 if ( isset( $args[0] ) && $user_id == $args[0] )
817 // If multisite these caps are allowed only for super admins.
818 if ( is_multisite() && !is_super_admin( $user_id ) )
819 $caps[] = 'do_not_allow';
821 $caps[] = 'edit_users'; // Explicit due to primitive fall through
825 $author_data = get_userdata( $user_id );
826 $post = get_post( $args[0] );
827 $post_type = get_post_type_object( $post->post_type );
829 if ( ! $post_type->map_meta_cap ) {
830 $caps[] = $post_type->cap->$cap;
831 // Prior to 3.1 we would re-call map_meta_cap here.
832 if ( 'delete_post' == $cap )
833 $cap = $post_type->cap->$cap;
837 if ( '' != $post->post_author ) {
838 $post_author_data = get_userdata( $post->post_author );
840 // No author set yet, so default to current user for cap checks.
841 $post_author_data = $author_data;
844 // If the user is the author...
845 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
846 // If the post is published...
847 if ( 'publish' == $post->post_status ) {
848 $caps[] = $post_type->cap->delete_published_posts;
849 } elseif ( 'trash' == $post->post_status ) {
850 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
851 $caps[] = $post_type->cap->delete_published_posts;
853 // If the post is draft...
854 $caps[] = $post_type->cap->delete_posts;
857 // The user is trying to edit someone else's post.
858 $caps[] = $post_type->cap->delete_others_posts;
859 // The post is published, extra cap required.
860 if ( 'publish' == $post->post_status )
861 $caps[] = $post_type->cap->delete_published_posts;
862 elseif ( 'private' == $post->post_status )
863 $caps[] = $post_type->cap->delete_private_posts;
866 // edit_post breaks down to edit_posts, edit_published_posts, or
870 $author_data = get_userdata( $user_id );
871 $post = get_post( $args[0] );
872 $post_type = get_post_type_object( $post->post_type );
874 if ( ! $post_type->map_meta_cap ) {
875 $caps[] = $post_type->cap->$cap;
876 // Prior to 3.1 we would re-call map_meta_cap here.
877 if ( 'edit_post' == $cap )
878 $cap = $post_type->cap->$cap;
882 if ( '' != $post->post_author ) {
883 $post_author_data = get_userdata( $post->post_author );
885 // No author set yet, so default to current user for cap checks.
886 $post_author_data = $author_data;
889 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
890 // If the user is the author...
891 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
892 // If the post is published...
893 if ( 'publish' == $post->post_status ) {
894 $caps[] = $post_type->cap->edit_published_posts;
895 } elseif ( 'trash' == $post->post_status ) {
896 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
897 $caps[] = $post_type->cap->edit_published_posts;
899 // If the post is draft...
900 $caps[] = $post_type->cap->edit_posts;
903 // The user is trying to edit someone else's post.
904 $caps[] = $post_type->cap->edit_others_posts;
905 // The post is published, extra cap required.
906 if ( 'publish' == $post->post_status )
907 $caps[] = $post_type->cap->edit_published_posts;
908 elseif ( 'private' == $post->post_status )
909 $caps[] = $post_type->cap->edit_private_posts;
914 $author_data = get_userdata( $user_id );
915 $post = get_post( $args[0] );
916 $post_type = get_post_type_object( $post->post_type );
918 if ( ! $post_type->map_meta_cap ) {
919 $caps[] = $post_type->cap->$cap;
920 // Prior to 3.1 we would re-call map_meta_cap here.
921 if ( 'read_post' == $cap )
922 $cap = $post_type->cap->$cap;
926 if ( 'private' != $post->post_status ) {
927 $caps[] = $post_type->cap->read;
931 if ( '' != $post->post_author ) {
932 $post_author_data = get_userdata( $post->post_author );
934 // No author set yet, so default to current user for cap checks.
935 $post_author_data = $author_data;
938 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
939 $caps[] = $post_type->cap->read;
941 $caps[] = $post_type->cap->read_private_posts;
944 $comment = get_comment( $args[0] );
945 $post = get_post( $comment->comment_post_ID );
946 $post_type_object = get_post_type_object( $post->post_type );
948 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID );
950 case 'unfiltered_upload':
951 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
954 $caps[] = 'do_not_allow';
959 if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
960 $caps[] = 'do_not_allow';
963 // Fall through if not DISALLOW_FILE_EDIT.
964 case 'update_plugins':
965 case 'delete_plugins':
966 case 'install_plugins':
967 case 'update_themes':
968 case 'delete_themes':
969 case 'install_themes':
971 // Disallow anything that creates, deletes, or edits core, plugin, or theme files.
972 // Files in uploads are excepted.
973 if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
974 $caps[] = 'do_not_allow';
977 // Fall through if not DISALLOW_FILE_MODS.
978 case 'unfiltered_html':
979 // Disallow unfiltered_html for all users, even admins and super admins.
980 if ( defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML ) {
981 $caps[] = 'do_not_allow';
984 // Fall through if not DISALLOW_UNFILTERED_HTML
987 // If multisite these caps are allowed only for super admins.
988 if ( is_multisite() && !is_super_admin( $user_id ) ) {
989 $caps[] = 'do_not_allow';
991 if ( 'delete_user' == $cap )
992 $cap = 'delete_users';
997 if ( !is_multisite() )
999 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
1002 $caps[] = 'do_not_allow';
1005 // Handle meta capabilities for custom post types.
1006 $post_type_meta_caps = _post_type_meta_capabilities();
1007 if ( isset( $post_type_meta_caps[ $cap ] ) ) {
1008 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
1009 return call_user_func_array( 'map_meta_cap', $args );
1012 // If no meta caps match, return the original cap.
1016 return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
1020 * Whether current user has capability or role.
1024 * @param string $capability Capability or role name.
1027 function current_user_can( $capability ) {
1028 $current_user = wp_get_current_user();
1030 if ( empty( $current_user ) )
1033 $args = array_slice( func_get_args(), 1 );
1034 $args = array_merge( array( $capability ), $args );
1036 return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
1040 * Whether current user has a capability or role for a given blog.
1044 * @param int $blog_id Blog ID
1045 * @param string $capability Capability or role name.
1048 function current_user_can_for_blog( $blog_id, $capability ) {
1049 $current_user = wp_get_current_user();
1051 if ( empty( $current_user ) )
1054 // Create new object to avoid stomping the global current_user.
1055 $user = new WP_User( $current_user->id) ;
1057 // Set the blog id. @todo add blog id arg to WP_User constructor?
1058 $user->for_blog( $blog_id );
1060 $args = array_slice( func_get_args(), 2 );
1061 $args = array_merge( array( $capability ), $args );
1063 return call_user_func_array( array( &$user, 'has_cap' ), $args );
1067 * Whether author of supplied post has capability or role.
1071 * @param int|object $post Post ID or post object.
1072 * @param string $capability Capability or role name.
1075 function author_can( $post, $capability ) {
1076 if ( !$post = get_post($post) )
1079 $author = new WP_User( $post->post_author );
1081 if ( empty( $author->ID ) )
1084 $args = array_slice( func_get_args(), 2 );
1085 $args = array_merge( array( $capability ), $args );
1087 return call_user_func_array( array( &$author, 'has_cap' ), $args );
1091 * Whether a particular user has capability or role.
1095 * @param int|object $user User ID or object.
1096 * @param string $capability Capability or role name.
1099 function user_can( $user, $capability ) {
1100 if ( ! is_object( $user ) )
1101 $user = new WP_User( $user );
1103 if ( ! $user || ! $user->ID )
1106 $args = array_slice( func_get_args(), 2 );
1107 $args = array_merge( array( $capability ), $args );
1109 return call_user_func_array( array( &$user, 'has_cap' ), $args );
1113 * Retrieve role object.
1115 * @see WP_Roles::get_role() Uses method to retrieve role object.
1118 * @param string $role Role name.
1121 function get_role( $role ) {
1124 if ( ! isset( $wp_roles ) )
1125 $wp_roles = new WP_Roles();
1127 return $wp_roles->get_role( $role );
1131 * Add role, if it does not exist.
1133 * @see WP_Roles::add_role() Uses method to add role.
1136 * @param string $role Role name.
1137 * @param string $display_name Display name for role.
1138 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false );
1139 * @return null|WP_Role WP_Role object if role is added, null if already exists.
1141 function add_role( $role, $display_name, $capabilities = array() ) {
1144 if ( ! isset( $wp_roles ) )
1145 $wp_roles = new WP_Roles();
1147 return $wp_roles->add_role( $role, $display_name, $capabilities );
1151 * Remove role, if it exists.
1153 * @see WP_Roles::remove_role() Uses method to remove role.
1156 * @param string $role Role name.
1159 function remove_role( $role ) {
1162 if ( ! isset( $wp_roles ) )
1163 $wp_roles = new WP_Roles();
1165 return $wp_roles->remove_role( $role );
1169 * Retrieve a list of super admins.
1173 * @uses $super_admins Super admins global variable, if set.
1175 * @return array List of super admin logins
1177 function get_super_admins() {
1178 global $super_admins;
1180 if ( isset($super_admins) )
1181 return $super_admins;
1183 return get_site_option( 'site_admins', array('admin') );
1187 * Determine if user is a site admin.
1191 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1192 * @return bool True if the user is a site admin.
1194 function is_super_admin( $user_id = false ) {
1196 $user = new WP_User( $user_id );
1198 $user = wp_get_current_user();
1200 if ( empty( $user->id ) )
1203 if ( is_multisite() ) {
1204 $super_admins = get_super_admins();
1205 if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1208 if ( $user->has_cap('delete_users') )