121 /** |
121 /** |
122 * Add role name with capabilities to list. |
122 * Add role name with capabilities to list. |
123 * |
123 * |
124 * Updates the list of roles, if the role doesn't already exist. |
124 * Updates the list of roles, if the role doesn't already exist. |
125 * |
125 * |
|
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. |
|
128 * |
126 * @since 2.0.0 |
129 * @since 2.0.0 |
127 * @access public |
130 * @access public |
128 * |
131 * |
129 * @param string $role Role name. |
132 * @param string $role Role name. |
130 * @param string $display_name Role display name. |
133 * @param string $display_name Role display name. |
131 * @param array $capabilities List of role capabilities. |
134 * @param array $capabilities List of role capabilities in the above format. |
132 * @return null|WP_Role WP_Role object if role is added, null if already exists. |
135 * @return null|WP_Role WP_Role object if role is added, null if already exists. |
133 */ |
136 */ |
134 function add_role( $role, $display_name, $capabilities = array() ) { |
137 function add_role( $role, $display_name, $capabilities = array() ) { |
135 if ( isset( $this->roles[$role] ) ) |
138 if ( isset( $this->roles[$role] ) ) |
136 return; |
139 return; |
172 * @since 2.0.0 |
175 * @since 2.0.0 |
173 * @access public |
176 * @access public |
174 * |
177 * |
175 * @param string $role Role name. |
178 * @param string $role Role name. |
176 * @param string $cap Capability name. |
179 * @param string $cap Capability name. |
177 * @param bool $grant Optional, default is true. Whether role is capable of preforming capability. |
180 * @param bool $grant Optional, default is true. Whether role is capable of performing capability. |
178 */ |
181 */ |
179 function add_cap( $role, $cap, $grant = true ) { |
182 function add_cap( $role, $cap, $grant = true ) { |
180 $this->roles[$role]['capabilities'][$cap] = $grant; |
183 $this->roles[$role]['capabilities'][$cap] = $grant; |
181 if ( $this->use_db ) |
184 if ( $this->use_db ) |
182 update_option( $this->role_key, $this->roles ); |
185 update_option( $this->role_key, $this->roles ); |
594 $this->roles = false; |
606 $this->roles = false; |
595 } |
607 } |
596 update_usermeta( $this->ID, $this->cap_key, $this->caps ); |
608 update_usermeta( $this->ID, $this->cap_key, $this->caps ); |
597 $this->get_role_caps(); |
609 $this->get_role_caps(); |
598 $this->update_user_level_from_caps(); |
610 $this->update_user_level_from_caps(); |
|
611 do_action( 'set_user_role', $this->ID, $role ); |
599 } |
612 } |
600 |
613 |
601 /** |
614 /** |
602 * Choose the maximum level the user has. |
615 * Choose the maximum level the user has. |
603 * |
616 * |
766 $post = get_post( $args[0] ); |
779 $post = get_post( $args[0] ); |
767 if ( 'page' == $post->post_type ) { |
780 if ( 'page' == $post->post_type ) { |
768 $args = array_merge( array( 'delete_page', $user_id ), $args ); |
781 $args = array_merge( array( 'delete_page', $user_id ), $args ); |
769 return call_user_func_array( 'map_meta_cap', $args ); |
782 return call_user_func_array( 'map_meta_cap', $args ); |
770 } |
783 } |
771 $post_author_data = get_userdata( $post->post_author ); |
784 |
772 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />"; |
785 if ('' != $post->post_author) { |
|
786 $post_author_data = get_userdata( $post->post_author ); |
|
787 } else { |
|
788 //No author set yet so default to current user for cap checks |
|
789 $post_author_data = $author_data; |
|
790 } |
|
791 |
773 // If the user is the author... |
792 // If the user is the author... |
774 if ( $user_id == $post_author_data->ID ) { |
793 if ( $user_id == $post_author_data->ID ) { |
775 // If the post is published... |
794 // If the post is published... |
776 if ( 'publish' == $post->post_status ) |
795 if ( 'publish' == $post->post_status ) { |
777 $caps[] = 'delete_published_posts'; |
796 $caps[] = 'delete_published_posts'; |
778 else |
797 } elseif ( 'trash' == $post->post_status ) { |
|
798 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) |
|
799 $caps[] = 'delete_published_posts'; |
|
800 } else { |
779 // If the post is draft... |
801 // If the post is draft... |
780 $caps[] = 'delete_posts'; |
802 $caps[] = 'delete_posts'; |
|
803 } |
781 } else { |
804 } else { |
782 // The user is trying to edit someone else's post. |
805 // The user is trying to edit someone else's post. |
783 $caps[] = 'delete_others_posts'; |
806 $caps[] = 'delete_others_posts'; |
784 // The post is published, extra cap required. |
807 // The post is published, extra cap required. |
785 if ( 'publish' == $post->post_status ) |
808 if ( 'publish' == $post->post_status ) |
793 //echo "post ID: {$args[0]}<br />"; |
816 //echo "post ID: {$args[0]}<br />"; |
794 $page = get_page( $args[0] ); |
817 $page = get_page( $args[0] ); |
795 $page_author_data = get_userdata( $page->post_author ); |
818 $page_author_data = get_userdata( $page->post_author ); |
796 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />"; |
819 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />"; |
797 // If the user is the author... |
820 // If the user is the author... |
|
821 |
|
822 if ('' != $page->post_author) { |
|
823 $page_author_data = get_userdata( $page->post_author ); |
|
824 } else { |
|
825 //No author set yet so default to current user for cap checks |
|
826 $page_author_data = $author_data; |
|
827 } |
|
828 |
798 if ( $user_id == $page_author_data->ID ) { |
829 if ( $user_id == $page_author_data->ID ) { |
799 // If the page is published... |
830 // If the page is published... |
800 if ( $page->post_status == 'publish' ) |
831 if ( $page->post_status == 'publish' ) { |
801 $caps[] = 'delete_published_pages'; |
832 $caps[] = 'delete_published_pages'; |
802 else |
833 } elseif ( 'trash' == $page->post_status ) { |
|
834 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) ) |
|
835 $caps[] = 'delete_published_pages'; |
|
836 } else { |
803 // If the page is draft... |
837 // If the page is draft... |
804 $caps[] = 'delete_pages'; |
838 $caps[] = 'delete_pages'; |
|
839 } |
805 } else { |
840 } else { |
806 // The user is trying to edit someone else's page. |
841 // The user is trying to edit someone else's page. |
807 $caps[] = 'delete_others_pages'; |
842 $caps[] = 'delete_others_pages'; |
808 // The page is published, extra cap required. |
843 // The page is published, extra cap required. |
809 if ( $page->post_status == 'publish' ) |
844 if ( $page->post_status == 'publish' ) |
825 $post_author_data = get_userdata( $post->post_author ); |
860 $post_author_data = get_userdata( $post->post_author ); |
826 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />"; |
861 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />"; |
827 // If the user is the author... |
862 // If the user is the author... |
828 if ( $user_id == $post_author_data->ID ) { |
863 if ( $user_id == $post_author_data->ID ) { |
829 // If the post is published... |
864 // If the post is published... |
830 if ( 'publish' == $post->post_status ) |
865 if ( 'publish' == $post->post_status ) { |
831 $caps[] = 'edit_published_posts'; |
866 $caps[] = 'edit_published_posts'; |
832 else |
867 } elseif ( 'trash' == $post->post_status ) { |
|
868 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) |
|
869 $caps[] = 'edit_published_posts'; |
|
870 } else { |
833 // If the post is draft... |
871 // If the post is draft... |
834 $caps[] = 'edit_posts'; |
872 $caps[] = 'edit_posts'; |
|
873 } |
835 } else { |
874 } else { |
836 // The user is trying to edit someone else's post. |
875 // The user is trying to edit someone else's post. |
837 $caps[] = 'edit_others_posts'; |
876 $caps[] = 'edit_others_posts'; |
838 // The post is published, extra cap required. |
877 // The post is published, extra cap required. |
839 if ( 'publish' == $post->post_status ) |
878 if ( 'publish' == $post->post_status ) |
849 $page_author_data = get_userdata( $page->post_author ); |
888 $page_author_data = get_userdata( $page->post_author ); |
850 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />"; |
889 //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />"; |
851 // If the user is the author... |
890 // If the user is the author... |
852 if ( $user_id == $page_author_data->ID ) { |
891 if ( $user_id == $page_author_data->ID ) { |
853 // If the page is published... |
892 // If the page is published... |
854 if ( 'publish' == $page->post_status ) |
893 if ( 'publish' == $page->post_status ) { |
855 $caps[] = 'edit_published_pages'; |
894 $caps[] = 'edit_published_pages'; |
856 else |
895 } elseif ( 'trash' == $page->post_status ) { |
|
896 if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) ) |
|
897 $caps[] = 'edit_published_pages'; |
|
898 } else { |
857 // If the page is draft... |
899 // If the page is draft... |
858 $caps[] = 'edit_pages'; |
900 $caps[] = 'edit_pages'; |
|
901 } |
859 } else { |
902 } else { |
860 // The user is trying to edit someone else's page. |
903 // The user is trying to edit someone else's page. |
861 $caps[] = 'edit_others_pages'; |
904 $caps[] = 'edit_others_pages'; |
862 // The page is published, extra cap required. |
905 // The page is published, extra cap required. |
863 if ( 'publish' == $page->post_status ) |
906 if ( 'publish' == $page->post_status ) |
933 |
976 |
934 return call_user_func_array( array( &$current_user, 'has_cap' ), $args ); |
977 return call_user_func_array( array( &$current_user, 'has_cap' ), $args ); |
935 } |
978 } |
936 |
979 |
937 /** |
980 /** |
|
981 * Whether author of supplied post has capability or role. |
|
982 * |
|
983 * @since 2.9.0 |
|
984 * |
|
985 * @param int|object $post Post ID or post object. |
|
986 * @param string $capability Capability or role name. |
|
987 * @return bool |
|
988 */ |
|
989 function author_can( $post, $capability ) { |
|
990 if ( !$post = get_post($post) ) |
|
991 return false; |
|
992 |
|
993 $author = new WP_User( $post->post_author ); |
|
994 |
|
995 if ( empty( $author ) ) |
|
996 return false; |
|
997 |
|
998 $args = array_slice( func_get_args(), 2 ); |
|
999 $args = array_merge( array( $capability ), $args ); |
|
1000 |
|
1001 return call_user_func_array( array( &$author, 'has_cap' ), $args ); |
|
1002 } |
|
1003 |
|
1004 /** |
938 * Retrieve role object. |
1005 * Retrieve role object. |
939 * |
1006 * |
940 * @see WP_Roles::get_role() Uses method to retrieve role object. |
1007 * @see WP_Roles::get_role() Uses method to retrieve role object. |
941 * @since 2.0.0 |
1008 * @since 2.0.0 |
942 * |
1009 * |