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.
126 * The capabilities are defined in the following format `array( 'read' => true );`
127 * To explicitly deny a role a capability you set the value for that capability to false.
132 * @param string $role Role name.
133 * @param string $display_name Role display name.
134 * @param array $capabilities List of role capabilities in the above format.
135 * @return null|WP_Role WP_Role object if role is added, null if already exists.
137 function add_role( $role, $display_name, $capabilities = array() ) {
138 if ( isset( $this->roles[$role] ) )
141 $this->roles[$role] = array(
142 'name' => $display_name,
143 'capabilities' => $capabilities
146 update_option( $this->role_key, $this->roles );
147 $this->role_objects[$role] = new WP_Role( $role, $capabilities );
148 $this->role_names[$role] = $display_name;
149 return $this->role_objects[$role];
153 * Remove role by name.
158 * @param string $role Role name.
160 function remove_role( $role ) {
161 if ( ! isset( $this->role_objects[$role] ) )
164 unset( $this->role_objects[$role] );
165 unset( $this->role_names[$role] );
166 unset( $this->roles[$role] );
169 update_option( $this->role_key, $this->roles );
173 * Add capability to role.
178 * @param string $role Role name.
179 * @param string $cap Capability name.
180 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
182 function add_cap( $role, $cap, $grant = true ) {
183 $this->roles[$role]['capabilities'][$cap] = $grant;
185 update_option( $this->role_key, $this->roles );
189 * Remove capability from role.
194 * @param string $role Role name.
195 * @param string $cap Capability name.
197 function remove_cap( $role, $cap ) {
198 unset( $this->roles[$role]['capabilities'][$cap] );
200 update_option( $this->role_key, $this->roles );
204 * Retrieve role object by name.
209 * @param string $role Role name.
210 * @return object|null Null, if role does not exist. WP_Role object, if found.
212 function &get_role( $role ) {
213 if ( isset( $this->role_objects[$role] ) )
214 return $this->role_objects[$role];
220 * Retrieve list of role names.
225 * @return array List of role names.
227 function get_names() {
228 return $this->role_names;
232 * Whether role name is currently in the list of available roles.
237 * @param string $role Role name to look up.
240 function is_role( $role )
242 return isset( $this->role_names[$role] );
247 * WordPress Role class.
264 * List of capabilities the role contains.
273 * PHP4 Constructor - Setup object properties.
275 * The list of capabilities, must have the key as the name of the capability
276 * and the value a boolean of whether it is granted to the role or not.
281 * @param string $role Role name.
282 * @param array $capabilities List of capabilities.
285 function WP_Role( $role, $capabilities ) {
287 $this->capabilities = $capabilities;
291 * Assign role a capability.
293 * @see WP_Roles::add_cap() Method uses implementation for role.
297 * @param string $cap Capability name.
298 * @param bool $grant Whether role has capability privilege.
300 function add_cap( $cap, $grant = true ) {
303 if ( ! isset( $wp_roles ) )
304 $wp_roles = new WP_Roles();
306 $this->capabilities[$cap] = $grant;
307 $wp_roles->add_cap( $this->name, $cap, $grant );
311 * Remove capability from role.
313 * This is a container for {@link WP_Roles::remove_cap()} to remove the
314 * capability from the role. That is to say, that {@link
315 * WP_Roles::remove_cap()} implements the functionality, but it also makes
316 * sense to use this class, because you don't need to enter the role name.
321 * @param string $cap Capability name.
323 function remove_cap( $cap ) {
326 if ( ! isset( $wp_roles ) )
327 $wp_roles = new WP_Roles();
329 unset( $this->capabilities[$cap] );
330 $wp_roles->remove_cap( $this->name, $cap );
334 * Whether role has capability.
336 * The capabilities is passed through the 'role_has_cap' filter. The first
337 * parameter for the hook is the list of capabilities the class has
338 * assigned. The second parameter is the capability name to look for. The
339 * third and final parameter for the hook is the role name.
344 * @param string $cap Capability name.
345 * @return bool True, if user has capability. False, if doesn't have capability.
347 function has_cap( $cap ) {
348 $capabilities = apply_filters( 'role_has_cap', $this->capabilities, $cap, $this->name );
349 if ( !empty( $capabilities[$cap] ) )
350 return $capabilities[$cap];
358 * WordPress User class.
366 * User data container.
368 * This will be set as properties of the object.
386 * The deprecated user's ID.
390 * @deprecated Use WP_User::$ID
397 * The individual capabilities the user has been given.
406 * User metadata option name.
415 * The roles the user is part of.
421 var $roles = array();
424 * All capabilities the user has, including individual and role based.
430 var $allcaps = array();
433 * First name of the user.
435 * Created to prevent notices.
441 var $first_name = '';
444 * Last name of the user.
446 * Created to prevent notices.
455 * The filter context applied to user data fields.
464 * PHP4 Constructor - Sets up the object properties.
466 * Retrieves the userdata and then assigns all of the data keys to direct
467 * properties of the object. Calls {@link WP_User::_init_caps()} after
468 * setting up the object's user data properties.
473 * @param int|string $id User's ID or username
474 * @param int $name Optional. User's username
477 function WP_User( $id, $name = '' ) {
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;
504 * Setup 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
514 function _init_caps() {
516 $this->cap_key = $wpdb->prefix . 'capabilities';
517 $this->caps = &$this->{$this->cap_key};
518 if ( ! is_array( $this->caps ) )
519 $this->caps = array();
520 $this->get_role_caps();
524 * Retrieve all of the role capabilities and merge with individual capabilities.
526 * All of the capabilities of the roles the user belongs to are merged with
527 * the users individual roles. This also means that the user can be denied
528 * specific roles that their role might have, but the specific user isn't
529 * granted permission to.
535 function get_role_caps() {
538 if ( ! isset( $wp_roles ) )
539 $wp_roles = new WP_Roles();
541 //Filter out caps that are not role names and assign to $this->roles
542 if ( is_array( $this->caps ) )
543 $this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
545 //Build $allcaps from role caps, overlay user's $caps
546 $this->allcaps = array();
547 foreach ( (array) $this->roles as $role ) {
548 $role =& $wp_roles->get_role( $role );
549 $this->allcaps = array_merge( (array) $this->allcaps, (array) $role->capabilities );
551 $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
557 * Updates the user's meta data option with capabilities and roles.
562 * @param string $role Role name.
564 function add_role( $role ) {
565 $this->caps[$role] = true;
566 update_usermeta( $this->ID, $this->cap_key, $this->caps );
567 $this->get_role_caps();
568 $this->update_user_level_from_caps();
572 * Remove role from user.
577 * @param string $role Role name.
579 function remove_role( $role ) {
580 if ( empty( $this->roles[$role] ) || ( count( $this->roles ) <= 1 ) )
582 unset( $this->caps[$role] );
583 update_usermeta( $this->ID, $this->cap_key, $this->caps );
584 $this->get_role_caps();
588 * Set the role of the user.
590 * This will remove the previous roles of the user and assign the user the
591 * new one. You can set the role to an empty string and it will remove all
592 * of the roles from the user.
597 * @param string $role Role name.
599 function set_role( $role ) {
600 foreach ( (array) $this->roles as $oldrole )
601 unset( $this->caps[$oldrole] );
602 if ( !empty( $role ) ) {
603 $this->caps[$role] = true;
604 $this->roles = array( $role => true );
606 $this->roles = false;
608 update_usermeta( $this->ID, $this->cap_key, $this->caps );
609 $this->get_role_caps();
610 $this->update_user_level_from_caps();
611 do_action( 'set_user_role', $this->ID, $role );
615 * Choose the maximum level the user has.
617 * Will compare the level from the $item parameter against the $max
618 * parameter. If the item is incorrect, then just the $max parameter value
621 * Used to get the max level based on the capabilities the user has. This
622 * is also based on roles, so if the user is assigned the Administrator role
623 * then the capability 'level_10' will exist and the user will get that
629 * @param int $max Max level of user.
630 * @param string $item Level capability name.
631 * @return int Max Level.
633 function level_reduction( $max, $item ) {
634 if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
635 $level = intval( $matches[1] );
636 return max( $max, $level );
643 * Update the maximum user level for the user.
645 * Updates the 'user_level' user metadata (includes prefix that is the
646 * database table prefix) with the maximum user level. Gets the value from
647 * the all of the capabilities that the user has.
652 function update_user_level_from_caps() {
654 $this->user_level = array_reduce( array_keys( $this->allcaps ), array( &$this, 'level_reduction' ), 0 );
655 update_usermeta( $this->ID, $wpdb->prefix.'user_level', $this->user_level );
659 * Add capability and grant or deny access to capability.
664 * @param string $cap Capability name.
665 * @param bool $grant Whether to grant capability to user.
667 function add_cap( $cap, $grant = true ) {
668 $this->caps[$cap] = $grant;
669 update_usermeta( $this->ID, $this->cap_key, $this->caps );
673 * Remove capability from user.
678 * @param string $cap Capability name.
680 function remove_cap( $cap ) {
681 if ( empty( $this->caps[$cap] ) ) return;
682 unset( $this->caps[$cap] );
683 update_usermeta( $this->ID, $this->cap_key, $this->caps );
687 * Remove all of the capabilities of the user.
692 function remove_all_caps() {
694 $this->caps = array();
695 update_usermeta( $this->ID, $this->cap_key, '' );
696 update_usermeta( $this->ID, $wpdb->prefix.'user_level', '' );
697 $this->get_role_caps();
701 * Whether user has capability or role name.
703 * This is useful for looking up whether the user has a specific role
704 * assigned to the user. The second optional parameter can also be used to
705 * check for capabilities against a specfic post.
710 * @param string|int $cap Capability or role name to search.
711 * @param int $post_id Optional. Post ID to check capability against specific post.
712 * @return bool True, if user has capability; false, if user does not have capability.
714 function has_cap( $cap ) {
715 if ( is_numeric( $cap ) )
716 $cap = $this->translate_level_to_cap( $cap );
718 $args = array_slice( func_get_args(), 1 );
719 $args = array_merge( array( $cap, $this->ID ), $args );
720 $caps = call_user_func_array( 'map_meta_cap', $args );
721 // Must have ALL requested caps
722 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args );
723 foreach ( (array) $caps as $cap ) {
724 //echo "Checking cap $cap<br />";
725 if ( empty( $capabilities[$cap] ) || !$capabilities[$cap] )
733 * Convert numeric level to level capability name.
735 * Prepends 'level_' to level number.
740 * @param int $level Level number, 1 to 10.
743 function translate_level_to_cap( $level ) {
744 return 'level_' . $level;
750 * Map meta capabilities to primitive capabilities.
752 * This does not actually compare whether the user ID has the actual capability,
753 * just what the capability or capabilities are. Meta capability list value can
754 * be 'delete_user', 'edit_user', 'delete_post', 'delete_page', 'edit_post',
755 * 'edit_page', 'read_post', or 'read_page'.
759 * @param string $cap Capability name.
760 * @param int $user_id User ID.
761 * @return array Actual capabilities for meta capability.
763 function map_meta_cap( $cap, $user_id ) {
764 $args = array_slice( func_get_args(), 2 );
769 $caps[] = 'delete_users';
772 if ( !isset( $args[0] ) || $user_id != $args[0] ) {
773 $caps[] = 'edit_users';
777 $author_data = get_userdata( $user_id );
778 //echo "post ID: {$args[0]}<br />";
779 $post = get_post( $args[0] );
780 if ( 'page' == $post->post_type ) {
781 $args = array_merge( array( 'delete_page', $user_id ), $args );
782 return call_user_func_array( 'map_meta_cap', $args );
785 if ('' != $post->post_author) {
786 $post_author_data = get_userdata( $post->post_author );
788 //No author set yet so default to current user for cap checks
789 $post_author_data = $author_data;
792 // If the user is the author...
793 if ( $user_id == $post_author_data->ID ) {
794 // If the post is published...
795 if ( 'publish' == $post->post_status ) {
796 $caps[] = 'delete_published_posts';
797 } elseif ( 'trash' == $post->post_status ) {
798 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
799 $caps[] = 'delete_published_posts';
801 // If the post is draft...
802 $caps[] = 'delete_posts';
805 // The user is trying to edit someone else's post.
806 $caps[] = 'delete_others_posts';
807 // The post is published, extra cap required.
808 if ( 'publish' == $post->post_status )
809 $caps[] = 'delete_published_posts';
810 elseif ( 'private' == $post->post_status )
811 $caps[] = 'delete_private_posts';
815 $author_data = get_userdata( $user_id );
816 //echo "post ID: {$args[0]}<br />";
817 $page = get_page( $args[0] );
818 $page_author_data = get_userdata( $page->post_author );
819 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
820 // If the user is the author...
822 if ('' != $page->post_author) {
823 $page_author_data = get_userdata( $page->post_author );
825 //No author set yet so default to current user for cap checks
826 $page_author_data = $author_data;
829 if ( $user_id == $page_author_data->ID ) {
830 // If the page is published...
831 if ( $page->post_status == 'publish' ) {
832 $caps[] = 'delete_published_pages';
833 } elseif ( 'trash' == $page->post_status ) {
834 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
835 $caps[] = 'delete_published_pages';
837 // If the page is draft...
838 $caps[] = 'delete_pages';
841 // The user is trying to edit someone else's page.
842 $caps[] = 'delete_others_pages';
843 // The page is published, extra cap required.
844 if ( $page->post_status == 'publish' )
845 $caps[] = 'delete_published_pages';
846 elseif ( $page->post_status == 'private' )
847 $caps[] = 'delete_private_pages';
850 // edit_post breaks down to edit_posts, edit_published_posts, or
853 $author_data = get_userdata( $user_id );
854 //echo "post ID: {$args[0]}<br />";
855 $post = get_post( $args[0] );
856 if ( 'page' == $post->post_type ) {
857 $args = array_merge( array( 'edit_page', $user_id ), $args );
858 return call_user_func_array( 'map_meta_cap', $args );
860 $post_author_data = get_userdata( $post->post_author );
861 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
862 // If the user is the author...
863 if ( $user_id == $post_author_data->ID ) {
864 // If the post is published...
865 if ( 'publish' == $post->post_status ) {
866 $caps[] = 'edit_published_posts';
867 } elseif ( 'trash' == $post->post_status ) {
868 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) )
869 $caps[] = 'edit_published_posts';
871 // If the post is draft...
872 $caps[] = 'edit_posts';
875 // The user is trying to edit someone else's post.
876 $caps[] = 'edit_others_posts';
877 // The post is published, extra cap required.
878 if ( 'publish' == $post->post_status )
879 $caps[] = 'edit_published_posts';
880 elseif ( 'private' == $post->post_status )
881 $caps[] = 'edit_private_posts';
885 $author_data = get_userdata( $user_id );
886 //echo "post ID: {$args[0]}<br />";
887 $page = get_page( $args[0] );
888 $page_author_data = get_userdata( $page->post_author );
889 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
890 // If the user is the author...
891 if ( $user_id == $page_author_data->ID ) {
892 // If the page is published...
893 if ( 'publish' == $page->post_status ) {
894 $caps[] = 'edit_published_pages';
895 } elseif ( 'trash' == $page->post_status ) {
896 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
897 $caps[] = 'edit_published_pages';
899 // If the page is draft...
900 $caps[] = 'edit_pages';
903 // The user is trying to edit someone else's page.
904 $caps[] = 'edit_others_pages';
905 // The page is published, extra cap required.
906 if ( 'publish' == $page->post_status )
907 $caps[] = 'edit_published_pages';
908 elseif ( 'private' == $page->post_status )
909 $caps[] = 'edit_private_pages';
913 $post = get_post( $args[0] );
914 if ( 'page' == $post->post_type ) {
915 $args = array_merge( array( 'read_page', $user_id ), $args );
916 return call_user_func_array( 'map_meta_cap', $args );
919 if ( 'private' != $post->post_status ) {
924 $author_data = get_userdata( $user_id );
925 $post_author_data = get_userdata( $post->post_author );
926 if ( $user_id == $post_author_data->ID )
929 $caps[] = 'read_private_posts';
932 $page = get_page( $args[0] );
934 if ( 'private' != $page->post_status ) {
939 $author_data = get_userdata( $user_id );
940 $page_author_data = get_userdata( $page->post_author );
941 if ( $user_id == $page_author_data->ID )
944 $caps[] = 'read_private_pages';
946 case 'unfiltered_upload':
947 if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS == true )
950 $caps[] = 'do_not_allow';
953 // If no meta caps match, return the original cap.
957 return apply_filters('map_meta_cap', $caps, $cap, $user_id, $args);
961 * Whether current user has capability or role.
965 * @param string $capability Capability or role name.
968 function current_user_can( $capability ) {
969 $current_user = wp_get_current_user();
971 if ( empty( $current_user ) )
974 $args = array_slice( func_get_args(), 1 );
975 $args = array_merge( array( $capability ), $args );
977 return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
981 * Whether author of supplied post has capability or role.
985 * @param int|object $post Post ID or post object.
986 * @param string $capability Capability or role name.
989 function author_can( $post, $capability ) {
990 if ( !$post = get_post($post) )
993 $author = new WP_User( $post->post_author );
995 if ( empty( $author ) )
998 $args = array_slice( func_get_args(), 2 );
999 $args = array_merge( array( $capability ), $args );
1001 return call_user_func_array( array( &$author, 'has_cap' ), $args );
1005 * Retrieve role object.
1007 * @see WP_Roles::get_role() Uses method to retrieve role object.
1010 * @param string $role Role name.
1013 function get_role( $role ) {
1016 if ( ! isset( $wp_roles ) )
1017 $wp_roles = new WP_Roles();
1019 return $wp_roles->get_role( $role );
1023 * Add role, if it does not exist.
1025 * @see WP_Roles::add_role() Uses method to add role.
1028 * @param string $role Role name.
1029 * @param string $display_name Display name for role.
1030 * @param array $capabilities List of capabilities.
1031 * @return null|WP_Role WP_Role object if role is added, null if already exists.
1033 function add_role( $role, $display_name, $capabilities = array() ) {
1036 if ( ! isset( $wp_roles ) )
1037 $wp_roles = new WP_Roles();
1039 return $wp_roles->add_role( $role, $display_name, $capabilities );
1043 * Remove role, if it exists.
1045 * @see WP_Roles::remove_role() Uses method to remove role.
1048 * @param string $role Role name.
1051 function remove_role( $role ) {
1054 if ( ! isset( $wp_roles ) )
1055 $wp_roles = new WP_Roles();
1057 return $wp_roles->remove_role( $role );