Changes from branches/3.0/wp-includes/capabilities.php at r16631 to trunk/wp-includes/capabilities.php at r16910
- File:
-
- 1 edited
-
trunk/wp-includes/capabilities.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/capabilities.php
r16631 r16910 472 472 * @param int|string $id User's ID or username 473 473 * @param int $name Optional. User's username 474 * @param int $blog_id Optional Blog ID, defaults to current blog. 474 475 * @return WP_User 475 476 */ 476 function WP_User( $id, $name = '' ) {477 function WP_User( $id, $name = '', $blog_id = '' ) { 477 478 478 479 if ( empty( $id ) && empty( $name ) ) … … 497 498 498 499 $this->id = $this->ID; 499 $this-> _init_caps();500 $this->for_blog( $blog_id ); 500 501 } 501 502 … … 605 606 foreach ( (array) $this->roles as $oldrole ) 606 607 unset( $this->caps[$oldrole] ); 608 609 if ( 1 == count( $this->roles ) && $role == $this->roles[0] ) 610 return; 611 607 612 if ( !empty( $role ) ) { 608 613 $this->caps[$role] = true; … … 737 742 // Must have ALL requested caps 738 743 $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args ); 744 $capabilities['exist'] = true; // Everyone is allowed to exist 739 745 foreach ( (array) $caps as $cap ) { 740 746 //echo "Checking cap $cap<br />"; … … 816 822 break; 817 823 case 'delete_post': 824 case 'delete_page': 818 825 $author_data = get_userdata( $user_id ); 819 //echo "post ID: {$args[0]}<br />";820 826 $post = get_post( $args[0] ); 821 827 $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 ); 828 829 if ( ! $post_type->map_meta_cap ) { 830 $caps[] = $post_type->cap->$cap; 831 // Prior to 3.1 we would re-call map_meta_cap here. 832 if ( 'delete_post' == $cap ) 833 $cap = $post_type->cap->$cap; 834 break; 825 835 } 826 836 … … 828 838 $post_author_data = get_userdata( $post->post_author ); 829 839 } else { 830 // No author set yet so default to current user for cap checks840 // No author set yet, so default to current user for cap checks. 831 841 $post_author_data = $author_data; 832 842 } … … 836 846 // If the post is published... 837 847 if ( 'publish' == $post->post_status ) { 838 $caps[] = 'delete_published_posts';848 $caps[] = $post_type->cap->delete_published_posts; 839 849 } elseif ( 'trash' == $post->post_status ) { 840 850 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) 841 $caps[] = 'delete_published_posts';851 $caps[] = $post_type->cap->delete_published_posts; 842 852 } else { 843 853 // If the post is draft... 844 $caps[] = 'delete_posts';854 $caps[] = $post_type->cap->delete_posts; 845 855 } 846 856 } else { 847 857 // The user is trying to edit someone else's post. 848 $caps[] = 'delete_others_posts';858 $caps[] = $post_type->cap->delete_others_posts; 849 859 // The post is published, extra cap required. 850 860 if ( 'publish' == $post->post_status ) 851 $caps[] = 'delete_published_posts';861 $caps[] = $post_type->cap->delete_published_posts; 852 862 elseif ( 'private' == $post->post_status ) 853 $caps[] = 'delete_private_posts'; 854 } 855 break; 856 case 'delete_page': 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... 863 864 if ('' != $page->post_author) { 865 $page_author_data = get_userdata( $page->post_author ); 866 } else { 867 //No author set yet so default to current user for cap checks 868 $page_author_data = $author_data; 869 } 870 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'; 878 } else { 879 // If the page is draft... 880 $caps[] = 'delete_pages'; 881 } 882 } else { 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'; 863 $caps[] = $post_type->cap->delete_private_posts; 890 864 } 891 865 break; … … 893 867 // edit_others_posts 894 868 case 'edit_post': 869 case 'edit_page': 895 870 $author_data = get_userdata( $user_id ); 896 //echo "post ID: {$args[0]}<br />";897 871 $post = get_post( $args[0] ); 898 872 $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 ); 902 } 903 $post_author_data = get_userdata( $post->post_author ); 873 874 if ( ! $post_type->map_meta_cap ) { 875 $caps[] = $post_type->cap->$cap; 876 // Prior to 3.1 we would re-call map_meta_cap here. 877 if ( 'edit_post' == $cap ) 878 $cap = $post_type->cap->$cap; 879 break; 880 } 881 882 if ( '' != $post->post_author ) { 883 $post_author_data = get_userdata( $post->post_author ); 884 } else { 885 // No author set yet, so default to current user for cap checks. 886 $post_author_data = $author_data; 887 } 888 904 889 //echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />"; 905 890 // If the user is the author... … … 907 892 // If the post is published... 908 893 if ( 'publish' == $post->post_status ) { 909 $caps[] = 'edit_published_posts';894 $caps[] = $post_type->cap->edit_published_posts; 910 895 } elseif ( 'trash' == $post->post_status ) { 911 896 if ('publish' == get_post_meta($post->ID, '_wp_trash_meta_status', true) ) 912 $caps[] = 'edit_published_posts';897 $caps[] = $post_type->cap->edit_published_posts; 913 898 } else { 914 899 // If the post is draft... 915 $caps[] = 'edit_posts';900 $caps[] = $post_type->cap->edit_posts; 916 901 } 917 902 } else { 918 903 // The user is trying to edit someone else's post. 919 $caps[] = 'edit_others_posts';904 $caps[] = $post_type->cap->edit_others_posts; 920 905 // The post is published, extra cap required. 921 906 if ( 'publish' == $post->post_status ) 922 $caps[] = 'edit_published_posts';907 $caps[] = $post_type->cap->edit_published_posts; 923 908 elseif ( 'private' == $post->post_status ) 924 $caps[] = 'edit_private_posts'; 925 } 926 break; 927 case 'edit_page': 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'; 941 } else { 942 // If the page is draft... 943 $caps[] = 'edit_pages'; 944 } 945 } else { 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'; 909 $caps[] = $post_type->cap->edit_private_posts; 953 910 } 954 911 break; 955 912 case 'read_post': 913 case 'read_page': 914 $author_data = get_userdata( $user_id ); 956 915 $post = get_post( $args[0] ); 957 916 $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 ); 917 918 if ( ! $post_type->map_meta_cap ) { 919 $caps[] = $post_type->cap->$cap; 920 // Prior to 3.1 we would re-call map_meta_cap here. 921 if ( 'read_post' == $cap ) 922 $cap = $post_type->cap->$cap; 923 break; 961 924 } 962 925 963 926 if ( 'private' != $post->post_status ) { 964 $caps[] = 'read';927 $caps[] = $post_type->cap->read; 965 928 break; 966 929 } 967 930 968 $author_data = get_userdata( $user_id ); 969 $post_author_data = get_userdata( $post->post_author ); 931 if ( '' != $post->post_author ) { 932 $post_author_data = get_userdata( $post->post_author ); 933 } else { 934 // No author set yet, so default to current user for cap checks. 935 $post_author_data = $author_data; 936 } 937 970 938 if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) 971 $caps[] = 'read';939 $caps[] = $post_type->cap->read; 972 940 else 973 $caps[] = 'read_private_posts';941 $caps[] = $post_type->cap->read_private_posts; 974 942 break; 975 case 'read_page': 976 $page = get_page( $args[0] ); 977 978 if ( 'private' != $page->post_status ) { 979 $caps[] = 'read'; 980 break; 981 } 982 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 ) 986 $caps[] = 'read'; 987 else 988 $caps[] = 'read_private_pages'; 943 case 'edit_comment': 944 $comment = get_comment( $args[0] ); 945 $post = get_post( $comment->comment_post_ID ); 946 $post_type_object = get_post_type_object( $post->post_type ); 947 948 $caps = map_meta_cap( $post_type_object->cap->edit_post, $user_id, $post->ID ); 989 949 break; 990 950 case 'unfiltered_upload': … … 1035 995 break; 1036 996 case 'create_users': 1037 if ( is_multisite() && !get_site_option( 'add_new_users' ) ) 997 if ( !is_multisite() ) 998 $caps[] = $cap; 999 elseif ( is_super_admin() || get_site_option( 'add_new_users' ) ) 1000 $caps[] = $cap; 1001 else 1038 1002 $caps[] = 'do_not_allow'; 1039 else1040 $caps[] = $cap;1041 1003 break; 1042 1004 default: 1005 // Handle meta capabilities for custom post types. 1006 $post_type_meta_caps = _post_type_meta_capabilities(); 1007 if ( isset( $post_type_meta_caps[ $cap ] ) ) { 1008 $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args ); 1009 return call_user_func_array( 'map_meta_cap', $args ); 1010 } 1011 1043 1012 // If no meta caps match, return the original cap. 1044 1013 $caps[] = $cap; … … 1120 1089 1121 1090 /** 1091 * Whether a particular user has capability or role. 1092 * 1093 * @since 3.1.0 1094 * 1095 * @param int|object $user User ID or object. 1096 * @param string $capability Capability or role name. 1097 * @return bool 1098 */ 1099 function user_can( $user, $capability ) { 1100 if ( ! is_object( $user ) ) 1101 $user = new WP_User( $user ); 1102 1103 if ( ! $user || ! $user->ID ) 1104 return false; 1105 1106 $args = array_slice( func_get_args(), 2 ); 1107 $args = array_merge( array( $capability ), $args ); 1108 1109 return call_user_func_array( array( &$user, 'has_cap' ), $args ); 1110 } 1111 1112 /** 1122 1113 * Retrieve role object. 1123 1114 * … … 1145 1136 * @param string $role Role name. 1146 1137 * @param string $display_name Display name for role. 1147 * @param array $capabilities List of capabilities .1138 * @param array $capabilities List of capabilities, e.g. array( 'edit_posts' => true, 'delete_posts' => false ); 1148 1139 * @return null|WP_Role WP_Role object if role is added, null if already exists. 1149 1140 */ … … 1202 1193 */ 1203 1194 function is_super_admin( $user_id = false ) { 1204 if ( ! $user_id ) {1205 $ current_user = wp_get_current_user();1206 $user_id = ! empty($current_user) ? $current_user->id : 0;1207 }1208 1209 if ( ! $user_id)1195 if ( $user_id ) 1196 $user = new WP_User( $user_id ); 1197 else 1198 $user = wp_get_current_user(); 1199 1200 if ( empty( $user->id ) ) 1210 1201 return false; 1211 1212 $user = new WP_User($user_id);1213 1202 1214 1203 if ( is_multisite() ) {
Note: See TracChangeset
for help on using the changeset viewer.