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[] = 'delete_users';
806
$caps[] = 'promote_users';
809
// Allow user to edit itself
810
if ( isset( $args[0] ) && $user_id == $args[0] )
814
// If multisite these caps are allowed only for super admins.
815
if ( is_multisite() && !is_super_admin( $user_id ) )
816
$caps[] = 'do_not_allow';
818
$caps[] = 'edit_users'; // Explicit due to primitive fall through
821
$author_data = get_userdata( $user_id );
822
//echo "post ID: {$args[0]}<br />";
823
$post = get_post( $args[0] );
824
$post_type = get_post_type_object( $post->post_type );
825
if ( $post_type && 'post' != $post_type->capability_type ) {
826
$args = array_merge( array( $post_type->cap->delete_post, $user_id ), $args );
827
return call_user_func_array( 'map_meta_cap', $args );
830
if ( '' != $post->post_author ) {
831
$post_author_data = get_userdata( $post->post_author );
833
//No author set yet so default to current user for cap checks
834
$post_author_data = $author_data;
837
// If the user is the author...
838
if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
839
// If the post is published...
840
if ( 'publish' == $post->post_status ) {
841
$caps[] = 'delete_published_posts';
842
} elseif ( 'trash' == $post->post_status ) {
843
if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
844
$caps[] = 'delete_published_posts';
846
// If the post is draft...
847
$caps[] = 'delete_posts';
850
// The user is trying to edit someone else's post.
851
$caps[] = 'delete_others_posts';
852
// The post is published, extra cap required.
853
if ( 'publish' == $post->post_status )
854
$caps[] = 'delete_published_posts';
855
elseif ( 'private' == $post->post_status )
856
$caps[] = 'delete_private_posts';
860
$author_data = get_userdata( $user_id );
861
//echo "post ID: {$args[0]}<br />";
862
$page = get_page( $args[0] );
863
$page_author_data = get_userdata( $page->post_author );
864
//echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
865
// If the user is the author...
867
if ('' != $page->post_author) {
868
$page_author_data = get_userdata( $page->post_author );
870
//No author set yet so default to current user for cap checks
871
$page_author_data = $author_data;
874
if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID ) {
875
// If the page is published...
876
if ( $page->post_status == 'publish' ) {
877
$caps[] = 'delete_published_pages';
878
} elseif ( 'trash' == $page->post_status ) {
879
if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
880
$caps[] = 'delete_published_pages';
882
// If the page is draft...
883
$caps[] = 'delete_pages';
886
// The user is trying to edit someone else's page.
887
$caps[] = 'delete_others_pages';
888
// The page is published, extra cap required.
889
if ( $page->post_status == 'publish' )
890
$caps[] = 'delete_published_pages';
891
elseif ( $page->post_status == 'private' )
892
$caps[] = 'delete_private_pages';
895
// edit_post breaks down to edit_posts, edit_published_posts, or
898
$author_data = get_userdata( $user_id );
899
//echo "post ID: {$args[0]}<br />";
900
$post = get_post( $args[0] );
901
$post_type = get_post_type_object( $post->post_type );
902
if ( $post_type && 'post' != $post_type->capability_type ) {
903
$args = array_merge( array( $post_type->cap->edit_post, $user_id ), $args );
904
return call_user_func_array( 'map_meta_cap', $args );
906
$post_author_data = get_userdata( $post->post_author );
907
//echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
908
// If the user is the author...
909
if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
910
// If the post is published...
911
if ( 'publish' == $post->post_status ) {
912
$caps[] = 'edit_published_posts';
913
} elseif ( 'trash' == $post->post_status ) {
914
if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
915
$caps[] = 'edit_published_posts';
917
// If the post is draft...
918
$caps[] = 'edit_posts';
921
// The user is trying to edit someone else's post.
922
$caps[] = 'edit_others_posts';
923
// The post is published, extra cap required.
924
if ( 'publish' == $post->post_status )
925
$caps[] = 'edit_published_posts';
926
elseif ( 'private' == $post->post_status )
927
$caps[] = 'edit_private_posts';
931
$author_data = get_userdata( $user_id );
932
//echo "post ID: {$args[0]}<br />";
933
$page = get_page( $args[0] );
934
$page_author_data = get_userdata( $page->post_author );
935
//echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
936
// If the user is the author...
937
if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID ) {
938
// If the page is published...
939
if ( 'publish' == $page->post_status ) {
940
$caps[] = 'edit_published_pages';
941
} elseif ( 'trash' == $page->post_status ) {
942
if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
943
$caps[] = 'edit_published_pages';
945
// If the page is draft...
946
$caps[] = 'edit_pages';
949
// The user is trying to edit someone else's page.
950
$caps[] = 'edit_others_pages';
951
// The page is published, extra cap required.
952
if ( 'publish' == $page->post_status )
953
$caps[] = 'edit_published_pages';
954
elseif ( 'private' == $page->post_status )
955
$caps[] = 'edit_private_pages';
959
$post = get_post( $args[0] );
960
$post_type = get_post_type_object( $post->post_type );
961
if ( $post_type && 'post' != $post_type->capability_type ) {
962
$args = array_merge( array( $post_type->cap->read_post, $user_id ), $args );
963
return call_user_func_array( 'map_meta_cap', $args );
966
if ( 'private' != $post->post_status ) {
971
$author_data = get_userdata( $user_id );
972
$post_author_data = get_userdata( $post->post_author );
973
if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
976
$caps[] = 'read_private_posts';
979
$page = get_page( $args[0] );
981
if ( 'private' != $page->post_status ) {
986
$author_data = get_userdata( $user_id );
987
$page_author_data = get_userdata( $page->post_author );
988
if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID )
991
$caps[] = 'read_private_pages';
993
case 'unfiltered_upload':
994
if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) )
997
$caps[] = 'do_not_allow';
1000
case 'edit_plugins':
1002
if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
1003
$caps[] = 'do_not_allow';
1006
// Fall through if not DISALLOW_FILE_EDIT.
1007
case 'update_plugins':
1008
case 'delete_plugins':
1009
case 'install_plugins':
1010
case 'update_themes':
1011
case 'delete_themes':
1012
case 'install_themes':
1014
// Disallow anything that creates, deletes, or edits core, plugin, or theme files.
1015
// Files in uploads are excepted.
1016
if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
1017
$caps[] = 'do_not_allow';
1020
// Fall through if not DISALLOW_FILE_MODS.
1021
case 'unfiltered_html':
1022
// Disallow unfiltered_html for all users, even admins and super admins.
1023
if ( defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML ) {
1024
$caps[] = 'do_not_allow';
1027
// Fall through if not DISALLOW_UNFILTERED_HTML
1029
case 'delete_users':
1030
// If multisite these caps are allowed only for super admins.
1031
if ( is_multisite() && !is_super_admin( $user_id ) )
1032
$caps[] = 'do_not_allow';
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 ( is_multisite() && is_super_admin() )
1085
if ( empty( $current_user ) )
1088
// Create new object to avoid stomping the global current_user.
1089
$user = new WP_User( $current_user->id) ;
1091
// Set the blog id. @todo add blog id arg to WP_User constructor?
1092
$user->for_blog( $blog_id );
1094
$args = array_slice( func_get_args(), 2 );
1095
$args = array_merge( array( $capability ), $args );
1097
return call_user_func_array( array( &$user, 'has_cap' ), $args );
1101
* Whether author of supplied post has capability or role.
1105
* @param int|object $post Post ID or post object.
1106
* @param string $capability Capability or role name.
1109
function author_can( $post, $capability ) {
1110
if ( !$post = get_post($post) )
1113
$author = new WP_User( $post->post_author );
1115
if ( empty( $author->ID ) )
1118
$args = array_slice( func_get_args(), 2 );
1119
$args = array_merge( array( $capability ), $args );
1121
return call_user_func_array( array( &$author, 'has_cap' ), $args );
1125
* Retrieve role object.
1127
* @see WP_Roles::get_role() Uses method to retrieve role object.
1130
* @param string $role Role name.
1133
function get_role( $role ) {
1136
if ( ! isset( $wp_roles ) )
1137
$wp_roles = new WP_Roles();
1139
return $wp_roles->get_role( $role );
1143
* Add role, if it does not exist.
1145
* @see WP_Roles::add_role() Uses method to add role.
1148
* @param string $role Role name.
1149
* @param string $display_name Display name for role.
1150
* @param array $capabilities List of capabilities.
1151
* @return null|WP_Role WP_Role object if role is added, null if already exists.
1153
function add_role( $role, $display_name, $capabilities = array() ) {
1156
if ( ! isset( $wp_roles ) )
1157
$wp_roles = new WP_Roles();
1159
return $wp_roles->add_role( $role, $display_name, $capabilities );
1163
* Remove role, if it exists.
1165
* @see WP_Roles::remove_role() Uses method to remove role.
1168
* @param string $role Role name.
1171
function remove_role( $role ) {
1174
if ( ! isset( $wp_roles ) )
1175
$wp_roles = new WP_Roles();
1177
return $wp_roles->remove_role( $role );
1181
* Retrieve a list of super admins.
1185
* @uses $super_admins Super admins global variable, if set.
1187
* @return array List of super admin logins
1189
function get_super_admins() {
1190
global $super_admins;
1192
if ( isset($super_admins) )
1193
return $super_admins;
1195
return get_site_option( 'site_admins', array('admin') );
1199
* Determine if user is a site admin.
1203
* @param int $user_id (Optional) The ID of a user. Defaults to the current user.
1204
* @return bool True if the user is a site admin.
1206
function is_super_admin( $user_id = false ) {
1208
$current_user = wp_get_current_user();
1209
$user_id = ! empty($current_user) ? $current_user->id : 0;
1215
$user = new WP_User($user_id);
1217
if ( is_multisite() ) {
1218
$super_admins = get_super_admins();
1219
if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
1222
if ( $user->has_cap('delete_users') )