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 * Setup 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.
101 global $wp_user_roles;
102 $this->role_key = $wpdb->prefix . 'user_roles';
103 if ( ! empty( $wp_user_roles ) ) {
104 $this->roles = $wp_user_roles;
105 $this->use_db = false;
107 $this->roles = get_option( $this->role_key );
110 if ( empty( $this->roles ) )
113 $this->role_objects = array();
114 $this->role_names = array();
115 foreach ( (array) $this->roles as $role => $data ) {
116 $this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
117 $this->role_names[$role] = $this->roles[$role]['name'];
122 * Add role name with capabilities to list.
124 * Updates the list of roles, if the role doesn't already exist.
129 * @param string $role Role name.
130 * @param string $display_name Role display name.
131 * @param array $capabilities List of role capabilities.
132 * @return null|WP_Role WP_Role object if role is added, null if already exists.
134 function add_role( $role, $display_name, $capabilities = array() ) {
135 if ( isset( $this->roles[$role] ) )
138 $this->roles[$role] = array(
139 'name' => $display_name,
140 'capabilities' => $capabilities
143 update_option( $this->role_key, $this->roles );
144 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
145 $this->role_names[$role] = $display_name;
146 return $this->role_objects[$role];
150 * Remove role by name.
155 * @param string $role Role name.
157 function remove_role( $role ) {
158 if ( ! isset( $this->role_objects[$role] ) )
161 unset( $this->role_objects[$role] );
162 unset( $this->role_names[$role] );
163 unset( $this->roles[$role] );
166 update_option( $this->role_key, $this->roles );
170 * Add capability to role.
175 * @param string $role Role name.
176 * @param string $cap Capability name.
177 * @param bool $grant Optional, default is true. Whether role is capable of preforming capability.
179 function add_cap( $role, $cap, $grant = true ) {
180 $this->roles[$role]['capabilities'][$cap] = $grant;
182 update_option( $this->role_key, $this->roles );
186 * Remove capability from role.
191 * @param string $role Role name.
192 * @param string $cap Capability name.
194 function remove_cap( $role, $cap ) {
195 unset( $this->roles[$role]['capabilities'][$cap] );
197 update_option( $this->role_key, $this->roles );
201 * Retrieve role object by name.
206 * @param string $role Role name.
207 * @return object|null Null, if role does not exist. WP_Role object, if found.
209 function &get_role( $role ) {
210 if ( isset( $this->role_objects[$role] ) )
211 return $this->role_objects[$role];
217 * Retrieve list of role names.
222 * @return array List of role names.
224 function get_names() {
225 return $this->role_names;
229 * Whether role name is currently in the list of available roles.
234 * @param string $role Role name to look up.
237 function is_role( $role )
239 return isset( $this->role_names[$role] );
244 * WordPress Role class.
261 * List of capabilities the role contains.
270 * PHP4 Constructor - Setup object properties.
272 * The list of capabilities, must have the key as the name of the capability
273 * and the value a boolean of whether it is granted to the role or not.
278 * @param string $role Role name.
279 * @param array $capabilities List of capabilities.
282 function WP_Role( $role, $capabilities ) {
284 $this->capabilities = $capabilities;
288 * Assign role a capability.
290 * @see WP_Roles::add_cap() Method uses implementation for role.
294 * @param string $cap Capability name.
295 * @param bool $grant Whether role has capability privilege.
297 function add_cap( $cap, $grant = true ) {
300 if ( ! isset( $wp_roles ) )
301 $wp_roles = new WP_Roles();
303 $this->capabilities[$cap] = $grant;
304 $wp_roles->add_cap( $this->name, $cap, $grant );
308 * Remove capability from role.
310 * This is a container for {@link WP_Roles::remove_cap()} to remove the
311 * capability from the role. That is to say, that {@link
312 * WP_Roles::remove_cap()} implements the functionality, but it also makes
313 * sense to use this class, because you don't need to enter the role name.
318 * @param string $cap Capability name.
320 function remove_cap( $cap ) {
323 if ( ! isset( $wp_roles ) )
324 $wp_roles = new WP_Roles();
326 unset( $this->capabilities[$cap] );
327 $wp_roles->remove_cap( $this->name, $cap );
331 * Whether role has capability.
333 * The capabilities is passed through the 'role_has_cap' filter. The first
334 * parameter for the hook is the list of capabilities the class has
335 * assigned. The second parameter is the capability name to look for. The
336 * third and final parameter for the hook is the role name.
341 * @param string $cap Capability name.
342 * @return bool True, if user has capability. False, if doesn't have capability.
344 function has_cap( $cap ) {
345 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
346 if ( !empty( $capabilities[$cap] ) )
347 return $capabilities[$cap];
355 * WordPress User class.
363 * User data container.
365 * This will be set as properties of the object.
383 * The deprecated user's ID.
387 * @deprecated Use WP_User::$ID
394 * The individual capabilities the user has been given.
403 * User metadata option name.
412 * The roles the user is part of.
418 var $roles = array();
421 * All capabilities the user has, including individual and role based.
427 var $allcaps = array();
430 * First name of the user.
432 * Created to prevent notices.
438 var $first_name = '';
441 * Last name of the user.
443 * Created to prevent notices.
452 * PHP4 Constructor - Sets up the object properties.
454 * Retrieves the userdata and then assigns all of the data keys to direct
455 * properties of the object. Calls {@link WP_User::_init_caps()} after
456 * setting up the object's user data properties.
461 * @param int|string $id User's ID or username
462 * @param int $name Optional. User's username
465 function WP_User( $id, $name = '' ) {
467 if ( empty( $id ) && empty( $name ) )
470 if ( ! is_numeric( $id ) ) {
475 if ( ! empty( $id ) )
476 $this->data = get_userdata( $id );
478 $this->data = get_userdatabylogin( $name );
480 if ( empty( $this->data->ID ) )
483 foreach ( get_object_vars( $this->data ) as $key => $value ) {
484 $this->{$key} = $value;
487 $this->id = $this->ID;
492 * Setup capability object properties.
494 * Will set the value for the 'cap_key' property to current database table
495 * prefix, followed by 'capabilities'. Will then check to see if the
496 * property matching the 'cap_key' exists and is an array. If so, it will be
502 function _init_caps() {
504 $this->cap_key = $wpdb->prefix . 'capabilities';
505 $this->caps = &$this->{$this->cap_key};
506 if ( ! is_array( $this->caps ) )
507 $this->caps = array();
508 $this->get_role_caps();
512 * Retrieve all of the role capabilities and merge with individual capabilities.
514 * All of the capabilities of the roles the user belongs to are merged with
515 * the users individual roles. This also means that the user can be denied
516 * specific roles that their role might have, but the specific user isn't
517 * granted permission to.
523 function get_role_caps() {
526 if ( ! isset( $wp_roles ) )
527 $wp_roles = new WP_Roles();
529 //Filter out caps that are not role names and assign to $this->roles
530 if ( is_array( $this->caps ) )
531 $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
533 //Build $allcaps from role caps, overlay user's $caps
534 $this->allcaps = array();
535 foreach ( (array) $this->roles as $role ) {
536 $role =& $wp_roles->get_role( $role );
537 $this->allcaps = array_merge( $this->allcaps, $role->capabilities );
539 $this->allcaps = array_merge( $this->allcaps, $this->caps );
545 * Updates the user's meta data option with capabilities and roles.
550 * @param string $role Role name.
552 function add_role( $role ) {
553 $this->caps[$role] = true;
554 update_usermeta( $this->ID, $this->cap_key, $this->caps );
555 $this->get_role_caps();
556 $this->update_user_level_from_caps();
560 * Remove role from user.
565 * @param string $role Role name.
567 function remove_role( $role ) {
568 if ( empty( $this->roles[$role] ) || ( count( $this->roles ) <= 1 ) )
570 unset( $this->caps[$role] );
571 update_usermeta( $this->ID, $this->cap_key, $this->caps );
572 $this->get_role_caps();
576 * Set the role of the user.
578 * This will remove the previous roles of the user and assign the user the
579 * new one. You can set the role to an empty string and it will remove all
580 * of the roles from the user.
585 * @param string $role Role name.
587 function set_role( $role ) {
588 foreach ( (array) $this->roles as $oldrole )
589 unset( $this->caps[$oldrole] );
590 if ( !empty( $role ) ) {
591 $this->caps[$role] = true;
592 $this->roles = array( $role => true );
594 $this->roles = false;
596 update_usermeta( $this->ID, $this->cap_key, $this->caps );
597 $this->get_role_caps();
598 $this->update_user_level_from_caps();
602 * Choose the maximum level the user has.
604 * Will compare the level from the $item parameter against the $max
605 * parameter. If the item is incorrect, then just the $max parameter value
608 * Used to get the max level based on the capabilities the user has. This
609 * is also based on roles, so if the user is assigned the Administrator role
610 * then the capability 'level_10' will exist and the user will get that
616 * @param int $max Max level of user.
617 * @param string $item Level capability name.
618 * @return int Max Level.
620 function level_reduction( $max, $item ) {
621 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
622 $level = intval( $matches[1] );
623 return max( $max, $level );
630 * Update the maximum user level for the user.
632 * Updates the 'user_level' user metadata (includes prefix that is the
633 * database table prefix) with the maximum user level. Gets the value from
634 * the all of the capabilities that the user has.
639 function update_user_level_from_caps() {
641 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
642 update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level );
646 * Add capability and grant or deny access to capability.
651 * @param string $cap Capability name.
652 * @param bool $grant Whether to grant capability to user.
654 function add_cap( $cap, $grant = true ) {
655 $this->caps[$cap] = $grant;
656 update_usermeta( $this->ID, $this->cap_key, $this->caps );
660 * Remove capability from user.
665 * @param string $cap Capability name.
667 function remove_cap( $cap ) {
668 if ( empty( $this->caps[$cap] ) ) return;
669 unset( $this->caps[$cap] );
670 update_usermeta( $this->ID, $this->cap_key, $this->caps );
674 * Remove all of the capabilities of the user.
679 function remove_all_caps() {
681 $this->caps = array();
682 update_usermeta( $this->ID, $this->cap_key, '' );
683 update_usermeta( $this->ID, $wpdb->prefix.'user_level', '' );
684 $this->get_role_caps();
688 * Whether user has capability or role name.
690 * This is useful for looking up whether the user has a specific role
691 * assigned to the user. The second optional parameter can also be used to
692 * check for capabilities against a specfic post.
697 * @param string|int $cap Capability or role name to search.
698 * @param int $post_id Optional. Post ID to check capability against specific post.
699 * @return bool True, if user has capability; false, if user does not have capability.
701 function has_cap( $cap ) {
702 if ( is_numeric( $cap ) )
703 $cap = $this->translate_level_to_cap( $cap );
705 $args = array_slice( func_get_args(), 1 );
706 $args = array_merge( array( $cap, $this->ID ), $args );
707 $caps = call_user_func_array( 'map_meta_cap', $args );
708 // Must have ALL requested caps
709 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
710 foreach ( (array) $caps as $cap ) {
711 //echo "Checking cap $cap<br />";
712 if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
720 * Convert numeric level to level capability name.
722 * Prepends 'level_' to level number.
727 * @param int $level Level number, 1 to 10.
730 function translate_level_to_cap( $level ) {
731 return 'level_' . $level;
737 * Map meta capabilities to primitive capabilities.
739 * This does not actually compare whether the user ID has the actual capability,
740 * just what the capability or capabilities are. Meta capability list value can
741 * be 'delete_user', 'edit_user', 'delete_post', 'delete_page', 'edit_post',
742 * 'edit_page', 'read_post', or 'read_page'.
746 * @param string $cap Capability name.
747 * @param int $user_id User ID.
748 * @return array Actual capabilities for meta capability.
750 function map_meta_cap( $cap, $user_id ) {
751 $args = array_slice( func_get_args(), 2 );
756 $caps[] = 'delete_users';
759 if ( !isset( $args[0] ) || $user_id != $args[0] ) {
760 $caps[] = 'edit_users';
764 $author_data = get_userdata( $user_id );
765 //echo "post ID: {$args[0]}<br />";
766 $post = get_post( $args[0] );
767 if ( 'page' == $post->post_type ) {
768 $args = array_merge( array( 'delete_page', $user_id ), $args );
769 return call_user_func_array( 'map_meta_cap', $args );
771 $post_author_data = get_userdata( $post->post_author );
772 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
773 // If the user is the author...
774 if ( $user_id == $post_author_data->ID ) {
775 // If the post is published...
776 if ( 'publish' == $post->post_status )
777 $caps[] = 'delete_published_posts';
779 // If the post is draft...
780 $caps[] = 'delete_posts';
782 // The user is trying to edit someone else's post.
783 $caps[] = 'delete_others_posts';
784 // The post is published, extra cap required.
785 if ( 'publish' == $post->post_status )
786 $caps[] = 'delete_published_posts';
787 elseif ( 'private' == $post->post_status )
788 $caps[] = 'delete_private_posts';
792 $author_data = get_userdata( $user_id );
793 //echo "post ID: {$args[0]}<br />";
794 $page = get_page( $args[0] );
795 $page_author_data = get_userdata( $page->post_author );
796 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
797 // If the user is the author...
798 if ( $user_id == $page_author_data->ID ) {
799 // If the page is published...
800 if ( $page->post_status == 'publish' )
801 $caps[] = 'delete_published_pages';
803 // If the page is draft...
804 $caps[] = 'delete_pages';
806 // The user is trying to edit someone else's page.
807 $caps[] = 'delete_others_pages';
808 // The page is published, extra cap required.
809 if ( $page->post_status == 'publish' )
810 $caps[] = 'delete_published_pages';
811 elseif ( $page->post_status == 'private' )
812 $caps[] = 'delete_private_pages';
815 // edit_post breaks down to edit_posts, edit_published_posts, or
818 $author_data = get_userdata( $user_id );
819 //echo "post ID: {$args[0]}<br />";
820 $post = get_post( $args[0] );
821 if ( 'page' == $post->post_type ) {
822 $args = array_merge( array( 'edit_page', $user_id ), $args );
823 return call_user_func_array( 'map_meta_cap', $args );
825 $post_author_data = get_userdata( $post->post_author );
826 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
827 // If the user is the author...
828 if ( $user_id == $post_author_data->ID ) {
829 // If the post is published...
830 if ( 'publish' == $post->post_status )
831 $caps[] = 'edit_published_posts';
833 // If the post is draft...
834 $caps[] = 'edit_posts';
836 // The user is trying to edit someone else's post.
837 $caps[] = 'edit_others_posts';
838 // The post is published, extra cap required.
839 if ( 'publish' == $post->post_status )
840 $caps[] = 'edit_published_posts';
841 elseif ( 'private' == $post->post_status )
842 $caps[] = 'edit_private_posts';
846 $author_data = get_userdata( $user_id );
847 //echo "post ID: {$args[0]}<br />";
848 $page = get_page( $args[0] );
849 $page_author_data = get_userdata( $page->post_author );
850 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
851 // If the user is the author...
852 if ( $user_id == $page_author_data->ID ) {
853 // If the page is published...
854 if ( 'publish' == $page->post_status )
855 $caps[] = 'edit_published_pages';
857 // If the page is draft...
858 $caps[] = 'edit_pages';
860 // The user is trying to edit someone else's page.
861 $caps[] = 'edit_others_pages';
862 // The page is published, extra cap required.
863 if ( 'publish' == $page->post_status )
864 $caps[] = 'edit_published_pages';
865 elseif ( 'private' == $page->post_status )
866 $caps[] = 'edit_private_pages';
870 $post = get_post( $args[0] );
871 if ( 'page' == $post->post_type ) {
872 $args = array_merge( array( 'read_page', $user_id ), $args );
873 return call_user_func_array( 'map_meta_cap', $args );
876 if ( 'private' != $post->post_status ) {
881 $author_data = get_userdata( $user_id );
882 $post_author_data = get_userdata( $post->post_author );
883 if ( $user_id == $post_author_data->ID )
886 $caps[] = 'read_private_posts';
889 $page = get_page( $args[0] );
891 if ( 'private' != $page->post_status ) {
896 $author_data = get_userdata( $user_id );
897 $page_author_data = get_userdata( $page->post_author );
898 if ( $user_id == $page_author_data->ID )
901 $caps[] = 'read_private_pages';
904 // If no meta caps match, return the original cap.
912 * Whether current user has capability or role.
916 * @param string $capability Capability or role name.
919 function current_user_can( $capability ) {
920 $current_user = wp_get_current_user();
922 if ( empty( $current_user ) )
925 $args = array_slice( func_get_args(), 1 );
926 $args = array_merge( array( $capability ), $args );
928 return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
932 * Retrieve role object.
934 * @see WP_Roles::get_role() Uses method to retrieve role object.
937 * @param string $role Role name.
940 function get_role( $role ) {
943 if ( ! isset( $wp_roles ) )
944 $wp_roles = new WP_Roles();
946 return $wp_roles->get_role( $role );
950 * Add role, if it does not exist.
952 * @see WP_Roles::add_role() Uses method to add role.
955 * @param string $role Role name.
956 * @param string $display_name Display name for role.
957 * @param array $capabilities List of capabilities.
958 * @return null|WP_Role WP_Role object if role is added, null if already exists.
960 function add_role( $role, $display_name, $capabilities = array() ) {
963 if ( ! isset( $wp_roles ) )
964 $wp_roles = new WP_Roles();
966 return $wp_roles->add_role( $role, $display_name, $capabilities );
970 * Remove role, if it exists.
972 * @see WP_Roles::remove_role() Uses method to remove role.
975 * @param string $role Role name.
978 function remove_role( $role ) {
981 if ( ! isset( $wp_roles ) )
982 $wp_roles = new WP_Roles();
984 return $wp_roles->remove_role( $role );