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
476 function WP_User( $id, $name = '' ) {
478 if ( empty( $id ) && empty( $name ) )
481 if ( ! is_numeric( $id ) ) {
486 if ( ! empty( $id ) )
487 $this->data = get_userdata( $id );
489 $this->data = get_userdatabylogin( $name );
491 if ( empty( $this->data->ID ) )
494 foreach ( get_object_vars( $this->data ) as $key => $value ) {
495 $this->{$key} = $value;
498 $this->id = $this->ID;
503 * Set up capability object properties.
505 * Will set the value for the 'cap_key' property to current database table
506 * prefix, followed by 'capabilities'. Will then check to see if the
507 * property matching the 'cap_key' exists and is an array. If so, it will be
512 * @param string $cap_key Optional capability key
515 function _init_caps( $cap_key = '' ) {
517 if ( empty($cap_key) )
518 $this->cap_key = $wpdb->prefix . 'capabilities';
520 $this->cap_key = $cap_key;
521 $this->caps = &$this->{$this->cap_key};
522 if ( ! is_array( $this->caps ) )
523 $this->caps = array();
524 $this->get_role_caps();
528 * Retrieve all of the role capabilities and merge with individual capabilities.
530 * All of the capabilities of the roles the user belongs to are merged with
531 * the users individual roles. This also means that the user can be denied
532 * specific roles that their role might have, but the specific user isn't
533 * granted permission to.
539 function get_role_caps() {
542 if ( ! isset( $wp_roles ) )
543 $wp_roles = new WP_Roles();
545 //Filter out caps that are not role names and assign to $this->roles
546 if ( is_array( $this->caps ) )
547 $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
549 //Build $allcaps from role caps, overlay user's $caps
550 $this->allcaps = array();
551 foreach ( (array) $this->roles as $role ) {
552 $the_role =& $wp_roles->get_role( $role );
553 $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
555 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
561 * Updates the user's meta data option with capabilities and roles.
566 * @param string $role Role name.
568 function add_role( $role ) {
569 $this->caps[$role] = true;
570 update_user_meta( $this->ID, $this->cap_key, $this->caps );
571 $this->get_role_caps();
572 $this->update_user_level_from_caps();
576 * Remove role from user.
581 * @param string $role Role name.
583 function remove_role( $role ) {
584 if ( !in_array($role, $this->roles) )
586 unset( $this->caps[$role] );
587 update_user_meta( $this->ID, $this->cap_key, $this->caps );
588 $this->get_role_caps();
589 $this->update_user_level_from_caps();
593 * Set the role of the user.
595 * This will remove the previous roles of the user and assign the user the
596 * new one. You can set the role to an empty string and it will remove all
597 * of the roles from the user.
602 * @param string $role Role name.
604 function set_role( $role ) {
605 foreach ( (array) $this->roles as $oldrole )
606 unset( $this->caps[$oldrole] );
607 if ( !empty( $role ) ) {
608 $this->caps[$role] = true;
609 $this->roles = array( $role => true );
611 $this->roles = false;
613 update_user_meta( $this->ID, $this->cap_key, $this->caps );
614 $this->get_role_caps();
615 $this->update_user_level_from_caps();
616 do_action( 'set_user_role', $this->ID, $role );
620 * Choose the maximum level the user has.
622 * Will compare the level from the $item parameter against the $max
623 * parameter. If the item is incorrect, then just the $max parameter value
626 * Used to get the max level based on the capabilities the user has. This
627 * is also based on roles, so if the user is assigned the Administrator role
628 * then the capability 'level_10' will exist and the user will get that
634 * @param int $max Max level of user.
635 * @param string $item Level capability name.
636 * @return int Max Level.
638 function level_reduction( $max, $item ) {
639 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
640 $level = intval( $matches[1] );
641 return max( $max, $level );
648 * Update the maximum user level for the user.
650 * Updates the 'user_level' user metadata (includes prefix that is the
651 * database table prefix) with the maximum user level. Gets the value from
652 * the all of the capabilities that the user has.
657 function update_user_level_from_caps() {
659 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
660 update_user_meta( $this->ID, $wpdb->prefix . 'user_level', $this->user_level );
664 * Add capability and grant or deny access to capability.
669 * @param string $cap Capability name.
670 * @param bool $grant Whether to grant capability to user.
672 function add_cap( $cap, $grant = true ) {
673 $this->caps[$cap] = $grant;
674 update_user_meta( $this->ID, $this->cap_key, $this->caps );
678 * Remove capability from user.
683 * @param string $cap Capability name.
685 function remove_cap( $cap ) {
686 if ( empty( $this->caps[$cap] ) )
688 unset( $this->caps[$cap] );
689 update_user_meta( $this->ID, $this->cap_key, $this->caps );
693 * Remove all of the capabilities of the user.
698 function remove_all_caps() {
700 $this->caps = array();
701 delete_user_meta( $this->ID, $this->cap_key );
702 delete_user_meta( $this->ID, $wpdb->prefix . 'user_level' );
703 $this->get_role_caps();
707 * Whether user has capability or role name.
709 * This is useful for looking up whether the user has a specific role
710 * assigned to the user. The second optional parameter can also be used to
711 * check for capabilities against a specfic post.
716 * @param string|int $cap Capability or role name to search.
717 * @param int $post_id Optional. Post ID to check capability against specific post.
718 * @return bool True, if user has capability; false, if user does not have capability.
720 function has_cap( $cap ) {
721 if ( is_numeric( $cap ) ) {
722 _deprecated_argument( __FUNCTION__, '2.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
723 $cap = $this->translate_level_to_cap( $cap );
726 $args = array_slice( func_get_args(), 1 );
727 $args = array_merge( array( $cap, $this->ID ), $args );
728 $caps = call_user_func_array( 'map_meta_cap', $args );
730 // Multisite super admin has all caps by definition, Unless specifically denied.
731 if ( is_multisite() && is_super_admin( $this->ID ) ) {
732 if ( in_array('do_not_allow', $caps) )
737 // Must have ALL requested caps
738 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
739 foreach ( (array) $caps as $cap ) {
740 //echo "Checking cap $cap<br />";
741 if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
749 * Convert numeric level to level capability name.
751 * Prepends 'level_' to level number.
756 * @param int $level Level number, 1 to 10.
759 function translate_level_to_cap( $level ) {
760 return 'level_' . $level;
764 * Set the blog to operate on. Defaults to the current blog.
768 * @param int $blog_id Optional Blog ID, defaults to current blog.
770 function for_blog( $blog_id = '' ) {
772 if ( ! empty( $blog_id ) )
773 $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
776 $this->_init_caps( $cap_key );
781 * Map meta capabilities to primitive capabilities.
783 * This does not actually compare whether the user ID has the actual capability,
784 * just what the capability or capabilities are. Meta capability list value can
785 * be 'delete_user', 'edit_user', 'remove_user', 'promote_user', 'delete_post',
786 * 'delete_page', 'edit_post', 'edit_page', 'read_post', or 'read_page'.
790 * @param string $cap Capability name.
791 * @param int $user_id User ID.
792 * @return array Actual capabilities for meta capability.
794 function map_meta_cap( $cap, $user_id ) {
795 $args = array_slice( func_get_args(), 2 );
800 $caps[] = 'remove_users';
803 $caps[] = 'promote_users';
806 // Allow user to edit itself
807 if ( isset( $args[0] ) && $user_id == $args[0] )
811 // If multisite these caps are allowed only for super admins.
812 if ( is_multisite() && !is_super_admin( $user_id ) )
813 $caps[] = 'do_not_allow';
815 $caps[] = 'edit_users'; // Explicit due to primitive fall through
818 $author_data = get_userdata( $user_id );
819 //echo "post ID: {$args[0]}<br />";
820 $post = get_post( $args[0] );
821 $post_type = get_post_type_object( $post->post_type );
822 if ( $post_type && 'post' != $post_type->capability_type ) {
823 $args = array_merge( array( $post_type->cap->delete_post, $user_id ), $args );
824 return call_user_func_array( 'map_meta_cap', $args );
827 if ( '' != $post->post_author ) {
828 $post_author_data = get_userdata( $post->post_author );
830 //No author set yet so default to current user for cap checks
831 $post_author_data = $author_data;
834 // If the user is the author...
835 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
836 // If the post is published...
837 if ( 'publish' == $post->post_status ) {
838 $caps[] = 'delete_published_posts';
839 } elseif ( 'trash' == $post->post_status ) {
840 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
841 $caps[] = 'delete_published_posts';
843 // If the post is draft...
844 $caps[] = 'delete_posts';
847 // The user is trying to edit someone else's post.
848 $caps[] = 'delete_others_posts';
849 // The post is published, extra cap required.
850 if ( 'publish' == $post->post_status )
851 $caps[] = 'delete_published_posts';
852 elseif ( 'private' == $post->post_status )
853 $caps[] = 'delete_private_posts';
857 $author_data = get_userdata( $user_id );
858 //echo "post ID: {$args[0]}<br />";
859 $page = get_page( $args[0] );
860 $page_author_data = get_userdata( $page->post_author );
861 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
862 // If the user is the author...
864 if ('' != $page->post_author) {
865 $page_author_data = get_userdata( $page->post_author );
867 //No author set yet so default to current user for cap checks
868 $page_author_data = $author_data;
871 if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID ) {
872 // If the page is published...
873 if ( $page->post_status == 'publish' ) {
874 $caps[] = 'delete_published_pages';
875 } elseif ( 'trash' == $page->post_status ) {
876 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
877 $caps[] = 'delete_published_pages';
879 // If the page is draft...
880 $caps[] = 'delete_pages';
883 // The user is trying to edit someone else's page.
884 $caps[] = 'delete_others_pages';
885 // The page is published, extra cap required.
886 if ( $page->post_status == 'publish' )
887 $caps[] = 'delete_published_pages';
888 elseif ( $page->post_status == 'private' )
889 $caps[] = 'delete_private_pages';
892 // edit_post breaks down to edit_posts, edit_published_posts, or
895 $author_data = get_userdata( $user_id );
896 //echo "post ID: {$args[0]}<br />";
897 $post = get_post( $args[0] );
898 $post_type = get_post_type_object( $post->post_type );
899 if ( $post_type && 'post' != $post_type->capability_type ) {
900 $args = array_merge( array( $post_type->cap->edit_post, $user_id ), $args );
901 return call_user_func_array( 'map_meta_cap', $args );
903 $post_author_data = get_userdata( $post->post_author );
904 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
905 // If the user is the author...
906 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
907 // If the post is published...
908 if ( 'publish' == $post->post_status ) {
909 $caps[] = 'edit_published_posts';
910 } elseif ( 'trash' == $post->post_status ) {
911 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
912 $caps[] = 'edit_published_posts';
914 // If the post is draft...
915 $caps[] = 'edit_posts';
918 // The user is trying to edit someone else's post.
919 $caps[] = 'edit_others_posts';
920 // The post is published, extra cap required.
921 if ( 'publish' == $post->post_status )
922 $caps[] = 'edit_published_posts';
923 elseif ( 'private' == $post->post_status )
924 $caps[] = 'edit_private_posts';
928 $author_data = get_userdata( $user_id );
929 //echo "post ID: {$args[0]}<br />";
930 $page = get_page( $args[0] );
931 $page_author_data = get_userdata( $page->post_author );
932 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
933 // If the user is the author...
934 if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID ) {
935 // If the page is published...
936 if ( 'publish' == $page->post_status ) {
937 $caps[] = 'edit_published_pages';
938 } elseif ( 'trash' == $page->post_status ) {
939 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
940 $caps[] = 'edit_published_pages';
942 // If the page is draft...
943 $caps[] = 'edit_pages';
946 // The user is trying to edit someone else's page.
947 $caps[] = 'edit_others_pages';
948 // The page is published, extra cap required.
949 if ( 'publish' == $page->post_status )
950 $caps[] = 'edit_published_pages';
951 elseif ( 'private' == $page->post_status )
952 $caps[] = 'edit_private_pages';
956 $post = get_post( $args[0] );
957 $post_type = get_post_type_object( $post->post_type );
958 if ( $post_type && 'post' != $post_type->capability_type ) {
959 $args = array_merge( array( $post_type->cap->read_post, $user_id ), $args );
960 return call_user_func_array( 'map_meta_cap', $args );
963 if ( 'private' != $post->post_status ) {
968 $author_data = get_userdata( $user_id );
969 $post_author_data = get_userdata( $post->post_author );
970 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
973 $caps[] = 'read_private_posts';
976 $page = get_page( $args[0] );
978 if ( 'private' != $page->post_status ) {
983 $author_data = get_userdata( $user_id );
984 $page_author_data = get_userdata( $page->post_author );
985 if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID )
988 $caps[] = 'read_private_pages';
990 case 'unfiltered_upload':
991 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
994 $caps[] = 'do_not_allow';
999 if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
1000 $caps[] = 'do_not_allow';
1003 // Fall through if not DISALLOW_FILE_EDIT.
1004 case 'update_plugins':
1005 case 'delete_plugins':
1006 case 'install_plugins':
1007 case 'update_themes':
1008 case 'delete_themes':
1009 case 'install_themes':
1011 // Disallow anything that creates, deletes, or edits core, plugin, or theme files.
1012 // Files in uploads are excepted.
1013 if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
1014 $caps[] = 'do_not_allow';
1017 // Fall through if not DISALLOW_FILE_MODS.
1018 case 'unfiltered_html':
1019 // Disallow unfiltered_html for all users, even admins and super admins.
1020 if ( defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML ) {
1021 $caps[] = 'do_not_allow';
1024 // Fall through if not DISALLOW_UNFILTERED_HTML
1026 case 'delete_users':
1027 // If multisite these caps are allowed only for super admins.
1028 if ( is_multisite() && !is_super_admin( $user_id ) ) {
1029 $caps[] = 'do_not_allow';
1031 if ( 'delete_user' == $cap )
1032 $cap = 'delete_users';
1036 case 'create_users':
1037 if ( is_multisite() && !get_site_option( 'add_new_users' ) )
1038 $caps[] = 'do_not_allow';
1043 // If no meta caps match, return the original cap.
1047 return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
1051 * Whether current user has capability or role.
1055 * @param string $capability Capability or role name.
1058 function current_user_can( $capability ) {
1059 $current_user = wp_get_current_user();
1061 if ( empty( $current_user ) )
1064 $args = array_slice( func_get_args(), 1 );
1065 $args = array_merge( array( $capability ), $args );
1067 return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
1071 * Whether current user has a capability or role for a given blog.
1075 * @param int $blog_id Blog ID
1076 * @param string $capability Capability or role name.
1079 function current_user_can_for_blog( $blog_id, $capability ) {
1080 $current_user = wp_get_current_user();
1082 if ( empty( $current_user ) )
1085 // Create new object to avoid stomping the global current_user.
1086 $user = new WP_User( $current_user->id) ;
1088 // Set the blog id. @todo add blog id arg to WP_User constructor?
1089 $user->for_blog( $blog_id );
1091 $args = array_slice( func_get_args(), 2 );
1092 $args = array_merge( array( $capability ), $args );
1094 return call_user_func_array( array( &$user, 'has_cap' ), $args );
1098 * Whether author of supplied post has capability or role.
1102 * @param int|object $post Post ID or post object.
1103 * @param string $capability Capability or role name.
1106 function author_can( $post, $capability ) {
1107 if ( !$post = get_post($post) )
1110 $author = new WP_User( $post->post_author );
1112 if ( empty( $author->ID ) )
1115 $args = array_slice( func_get_args(), 2 );
1116 $args = array_merge( array( $capability ), $args );
1118 return call_user_func_array( array( &$author, 'has_cap' ), $args );
1122 * Retrieve role object.
1124 * @see WP_Roles::get_role() Uses method to retrieve role object.
1127 * @param string $role Role name.
1130 function get_role( $role ) {
1133 if ( ! isset( $wp_roles ) )
1134 $wp_roles = new WP_Roles();
1136 return $wp_roles->get_role( $role );
1140 * Add role, if it does not exist.
1142 * @see WP_Roles::add_role() Uses method to add role.
1145 * @param string $role Role name.
1146 * @param string $display_name Display name for role.
1147 * @param array $capabilities List of capabilities.
1148 * @return null|WP_Role WP_Role object if role is added, null if already exists.
1150 function add_role( $role, $display_name, $capabilities = array() ) {
1153 if ( ! isset( $wp_roles ) )
1154 $wp_roles = new WP_Roles();
1156 return $wp_roles->add_role( $role, $display_name, $capabilities );
1160 * Remove role, if it exists.
1162 * @see WP_Roles::remove_role() Uses method to remove role.
1165 * @param string $role Role name.
1168 function remove_role( $role ) {
1171 if ( ! isset( $wp_roles ) )
1172 $wp_roles = new WP_Roles();
1174 return $wp_roles->remove_role( $role );
1178 * Retrieve a list of super admins.
1182 * @uses $super_admins Super admins global variable, if set.
1184 * @return array List of super admin logins
1186 function get_super_admins() {
1187 global $super_admins;
1189 if ( isset($super_admins) )
1190 return $super_admins;
1192 return get_site_option( 'site_admins', array('admin') );
1196 * Determine if user is a site admin.
1200 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1201 * @return bool True if the user is a site admin.
1203 function is_super_admin( $user_id = false ) {
1205 $current_user = wp_get_current_user();
1206 $user_id = ! empty($current_user) ? $current_user->id : 0;
1212 $user = new WP_User($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') )