web/wp-includes/capabilities.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
   106 		if ( empty( $this->roles ) )
   106 		if ( empty( $this->roles ) )
   107 			return;
   107 			return;
   108 
   108 
   109 		$this->role_objects = array();
   109 		$this->role_objects = array();
   110 		$this->role_names =  array();
   110 		$this->role_names =  array();
   111 		foreach ( (array) $this->roles as $role => $data ) {
   111 		foreach ( array_keys( $this->roles ) as $role ) {
       
   112 			$this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
       
   113 			$this->role_names[$role] = $this->roles[$role]['name'];
       
   114 		}
       
   115 	}
       
   116 
       
   117 	/**
       
   118 	 * Reinitialize the object
       
   119 	 *
       
   120 	 * Recreates the role objects. This is typically called only by switch_to_blog()
       
   121 	 * after switching wpdb to a new blog ID.
       
   122 	 *
       
   123 	 * @since 3.5.0
       
   124 	 * @access public
       
   125 	 */
       
   126 	function reinit() {
       
   127 		// There is no need to reinit if using the wp_user_roles global.
       
   128 		if ( ! $this->use_db )
       
   129 			return;
       
   130 
       
   131 		global $wpdb, $wp_user_roles;
       
   132 
       
   133 		// Duplicated from _init() to avoid an extra function call.
       
   134 		$this->role_key = $wpdb->prefix . 'user_roles';
       
   135 		$this->roles = get_option( $this->role_key );
       
   136 		if ( empty( $this->roles ) )
       
   137 			return;
       
   138 
       
   139 		$this->role_objects = array();
       
   140 		$this->role_names =  array();
       
   141 		foreach ( array_keys( $this->roles ) as $role ) {
   112 			$this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
   142 			$this->role_objects[$role] = new WP_Role( $role, $this->roles[$role]['capabilities'] );
   113 			$this->role_names[$role] = $this->roles[$role]['name'];
   143 			$this->role_names[$role] = $this->roles[$role]['name'];
   114 		}
   144 		}
   115 	}
   145 	}
   116 
   146 
   174 	 * @param string $role Role name.
   204 	 * @param string $role Role name.
   175 	 * @param string $cap Capability name.
   205 	 * @param string $cap Capability name.
   176 	 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
   206 	 * @param bool $grant Optional, default is true. Whether role is capable of performing capability.
   177 	 */
   207 	 */
   178 	function add_cap( $role, $cap, $grant = true ) {
   208 	function add_cap( $role, $cap, $grant = true ) {
       
   209 		if ( ! isset( $this->roles[$role] ) )
       
   210 			return;
       
   211 
   179 		$this->roles[$role]['capabilities'][$cap] = $grant;
   212 		$this->roles[$role]['capabilities'][$cap] = $grant;
   180 		if ( $this->use_db )
   213 		if ( $this->use_db )
   181 			update_option( $this->role_key, $this->roles );
   214 			update_option( $this->role_key, $this->roles );
   182 	}
   215 	}
   183 
   216 
   189 	 *
   222 	 *
   190 	 * @param string $role Role name.
   223 	 * @param string $role Role name.
   191 	 * @param string $cap Capability name.
   224 	 * @param string $cap Capability name.
   192 	 */
   225 	 */
   193 	function remove_cap( $role, $cap ) {
   226 	function remove_cap( $role, $cap ) {
       
   227 		if ( ! isset( $this->roles[$role] ) )
       
   228 			return;
       
   229 
   194 		unset( $this->roles[$role]['capabilities'][$cap] );
   230 		unset( $this->roles[$role]['capabilities'][$cap] );
   195 		if ( $this->use_db )
   231 		if ( $this->use_db )
   196 			update_option( $this->role_key, $this->roles );
   232 			update_option( $this->role_key, $this->roles );
   197 	}
   233 	}
   198 
   234 
   231 	 * @access public
   267 	 * @access public
   232 	 *
   268 	 *
   233 	 * @param string $role Role name to look up.
   269 	 * @param string $role Role name to look up.
   234 	 * @return bool
   270 	 * @return bool
   235 	 */
   271 	 */
   236 	function is_role( $role )
   272 	function is_role( $role ) {
   237 	{
       
   238 		return isset( $this->role_names[$role] );
   273 		return isset( $this->role_names[$role] );
   239 	}
   274 	}
   240 }
   275 }
   241 
   276 
   242 /**
   277 /**
   428 	 * Retrieves the userdata and passes it to {@link WP_User::init()}.
   463 	 * Retrieves the userdata and passes it to {@link WP_User::init()}.
   429 	 *
   464 	 *
   430 	 * @since 2.0.0
   465 	 * @since 2.0.0
   431 	 * @access public
   466 	 * @access public
   432 	 *
   467 	 *
   433 	 * @param int|string $id User's ID
   468 	 * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
   434 	 * @param string $name Optional. User's username
   469 	 * @param string $name Optional. User's username
   435 	 * @param int $blog_id Optional Blog ID, defaults to current blog.
   470 	 * @param int $blog_id Optional Blog ID, defaults to current blog.
   436 	 * @return WP_User
   471 	 * @return WP_User
   437 	 */
   472 	 */
   438 	function __construct( $id = 0, $name = '', $blog_id = '' ) {
   473 	function __construct( $id = 0, $name = '', $blog_id = '' ) {
   444 				'user_description' => 'description',
   479 				'user_description' => 'description',
   445 				'user_level' => $prefix . 'user_level',
   480 				'user_level' => $prefix . 'user_level',
   446 				$prefix . 'usersettings' => $prefix . 'user-settings',
   481 				$prefix . 'usersettings' => $prefix . 'user-settings',
   447 				$prefix . 'usersettingstime' => $prefix . 'user-settings-time',
   482 				$prefix . 'usersettingstime' => $prefix . 'user-settings-time',
   448 			);
   483 			);
       
   484 		}
       
   485 
       
   486 		if ( is_a( $id, 'WP_User' ) ) {
       
   487 			$this->init( $id->data, $blog_id );
       
   488 			return;
       
   489 		} elseif ( is_object( $id ) ) {
       
   490 			$this->init( $id, $blog_id );
       
   491 			return;
   449 		}
   492 		}
   450 
   493 
   451 		if ( ! empty( $id ) && ! is_numeric( $id ) ) {
   494 		if ( ! empty( $id ) && ! is_numeric( $id ) ) {
   452 			$name = $id;
   495 			$name = $id;
   453 			$id = 0;
   496 			$id = 0;
   634 	 */
   677 	 */
   635 	function has_prop( $key ) {
   678 	function has_prop( $key ) {
   636 		return $this->__isset( $key );
   679 		return $this->__isset( $key );
   637 	}
   680 	}
   638 
   681 
       
   682 	/*
       
   683 	 * Return an array representation.
       
   684 	 *
       
   685 	 * @since 3.5.0
       
   686 	 *
       
   687 	 * @return array Array representation.
       
   688 	 */
       
   689 	function to_array() {
       
   690 		return get_object_vars( $this->data );
       
   691 	}
       
   692 
   639 	/**
   693 	/**
   640 	 * Set up capability object properties.
   694 	 * Set up capability object properties.
   641 	 *
   695 	 *
   642 	 * Will set the value for the 'cap_key' property to current database table
   696 	 * Will set the value for the 'cap_key' property to current database table
   643 	 * prefix, followed by 'capabilities'. Will then check to see if the
   697 	 * prefix, followed by 'capabilities'. Will then check to see if the
   683 		if ( ! isset( $wp_roles ) )
   737 		if ( ! isset( $wp_roles ) )
   684 			$wp_roles = new WP_Roles();
   738 			$wp_roles = new WP_Roles();
   685 
   739 
   686 		//Filter out caps that are not role names and assign to $this->roles
   740 		//Filter out caps that are not role names and assign to $this->roles
   687 		if ( is_array( $this->caps ) )
   741 		if ( is_array( $this->caps ) )
   688 			$this->roles = array_filter( array_keys( $this->caps ), array( &$wp_roles, 'is_role' ) );
   742 			$this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
   689 
   743 
   690 		//Build $allcaps from role caps, overlay user's $caps
   744 		//Build $allcaps from role caps, overlay user's $caps
   691 		$this->allcaps = array();
   745 		$this->allcaps = array();
   692 		foreach ( (array) $this->roles as $role ) {
   746 		foreach ( (array) $this->roles as $role ) {
   693 			$the_role = $wp_roles->get_role( $role );
   747 			$the_role = $wp_roles->get_role( $role );
   741 	 * @access public
   795 	 * @access public
   742 	 *
   796 	 *
   743 	 * @param string $role Role name.
   797 	 * @param string $role Role name.
   744 	 */
   798 	 */
   745 	function set_role( $role ) {
   799 	function set_role( $role ) {
       
   800 		if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
       
   801 			return;
       
   802 
   746 		foreach ( (array) $this->roles as $oldrole )
   803 		foreach ( (array) $this->roles as $oldrole )
   747 			unset( $this->caps[$oldrole] );
   804 			unset( $this->caps[$oldrole] );
   748 
       
   749 		if ( 1 == count( $this->roles ) && $role == $this->roles[0] )
       
   750 			return;
       
   751 
   805 
   752 		if ( !empty( $role ) ) {
   806 		if ( !empty( $role ) ) {
   753 			$this->caps[$role] = true;
   807 			$this->caps[$role] = true;
   754 			$this->roles = array( $role => true );
   808 			$this->roles = array( $role => true );
   755 		} else {
   809 		} else {
   826 	 * @access public
   880 	 * @access public
   827 	 *
   881 	 *
   828 	 * @param string $cap Capability name.
   882 	 * @param string $cap Capability name.
   829 	 */
   883 	 */
   830 	function remove_cap( $cap ) {
   884 	function remove_cap( $cap ) {
   831 		if ( empty( $this->caps[$cap] ) )
   885 		if ( ! isset( $this->caps[$cap] ) )
   832 			return;
   886 			return;
   833 		unset( $this->caps[$cap] );
   887 		unset( $this->caps[$cap] );
   834 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   888 		update_user_meta( $this->ID, $this->cap_key, $this->caps );
   835 	}
   889 	}
   836 
   890 
   945 		break;
   999 		break;
   946 	case 'promote_user':
  1000 	case 'promote_user':
   947 		$caps[] = 'promote_users';
  1001 		$caps[] = 'promote_users';
   948 		break;
  1002 		break;
   949 	case 'edit_user':
  1003 	case 'edit_user':
       
  1004 	case 'edit_users':
   950 		// Allow user to edit itself
  1005 		// Allow user to edit itself
   951 		if ( isset( $args[0] ) && $user_id == $args[0] )
  1006 		if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
   952 			break;
  1007 			break;
   953 		// Fall through
  1008 
   954 	case 'edit_users':
       
   955 		// If multisite these caps are allowed only for super admins.
  1009 		// If multisite these caps are allowed only for super admins.
   956 		if ( is_multisite() && !is_super_admin( $user_id ) )
  1010 		if ( is_multisite() && !is_super_admin( $user_id ) )
   957 			$caps[] = 'do_not_allow';
  1011 			$caps[] = 'do_not_allow';
   958 		else
  1012 		else
   959 			$caps[] = 'edit_users'; // Explicit due to primitive fall through
  1013 			$caps[] = 'edit_users'; // edit_user maps to edit_users.
   960 		break;
  1014 		break;
   961 	case 'delete_post':
  1015 	case 'delete_post':
   962 	case 'delete_page':
  1016 	case 'delete_page':
   963 		$author_data = get_userdata( $user_id );
       
   964 		$post = get_post( $args[0] );
  1017 		$post = get_post( $args[0] );
   965 
  1018 
   966 		if ( 'revision' == $post->post_type ) {
  1019 		if ( 'revision' == $post->post_type ) {
   967 			$post = get_post( $post->post_parent );
  1020 			$post = get_post( $post->post_parent );
   968 		}
  1021 		}
   975 			if ( 'delete_post' == $cap )
  1028 			if ( 'delete_post' == $cap )
   976 				$cap = $post_type->cap->$cap;
  1029 				$cap = $post_type->cap->$cap;
   977 			break;
  1030 			break;
   978 		}
  1031 		}
   979 
  1032 
   980 		if ( '' != $post->post_author ) {
  1033 		$post_author_id = $post->post_author;
   981 			$post_author_data = get_userdata( $post->post_author );
  1034 
   982 		} else {
  1035 		// If no author set yet, default to current user for cap checks.
   983 			// No author set yet, so default to current user for cap checks.
  1036 		if ( ! $post_author_id )
   984 			$post_author_data = $author_data;
  1037 			$post_author_id = $user_id;
   985 		}
  1038 
       
  1039 		$post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
   986 
  1040 
   987 		// If the user is the author...
  1041 		// If the user is the author...
   988 		if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
  1042 		if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
   989 			// If the post is published...
  1043 			// If the post is published...
   990 			if ( 'publish' == $post->post_status ) {
  1044 			if ( 'publish' == $post->post_status ) {
  1008 		break;
  1062 		break;
  1009 		// edit_post breaks down to edit_posts, edit_published_posts, or
  1063 		// edit_post breaks down to edit_posts, edit_published_posts, or
  1010 		// edit_others_posts
  1064 		// edit_others_posts
  1011 	case 'edit_post':
  1065 	case 'edit_post':
  1012 	case 'edit_page':
  1066 	case 'edit_page':
  1013 		$author_data = get_userdata( $user_id );
       
  1014 		$post = get_post( $args[0] );
  1067 		$post = get_post( $args[0] );
  1015 
  1068 
  1016 		if ( 'revision' == $post->post_type ) {
  1069 		if ( 'revision' == $post->post_type ) {
  1017 			$post = get_post( $post->post_parent );
  1070 			$post = get_post( $post->post_parent );
  1018 		}
  1071 		}
  1025 			if ( 'edit_post' == $cap )
  1078 			if ( 'edit_post' == $cap )
  1026 				$cap = $post_type->cap->$cap;
  1079 				$cap = $post_type->cap->$cap;
  1027 			break;
  1080 			break;
  1028 		}
  1081 		}
  1029 
  1082 
  1030 		if ( '' != $post->post_author ) {
  1083 		$post_author_id = $post->post_author;
  1031 			$post_author_data = get_userdata( $post->post_author );
  1084 
  1032 		} else {
  1085 		// If no author set yet, default to current user for cap checks.
  1033 			// No author set yet, so default to current user for cap checks.
  1086 		if ( ! $post_author_id )
  1034 			$post_author_data = $author_data;
  1087 			$post_author_id = $user_id;
  1035 		}
  1088 
  1036 
  1089 		$post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
  1037 		//echo "current user id : $user_id, post author id: " . $post_author_data->ID . "<br />";
  1090 
  1038 		// If the user is the author...
  1091 		// If the user is the author...
  1039 		if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
  1092 		if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID ) {
  1040 			// If the post is published...
  1093 			// If the post is published...
  1041 			if ( 'publish' == $post->post_status ) {
  1094 			if ( 'publish' == $post->post_status ) {
  1042 				$caps[] = $post_type->cap->edit_published_posts;
  1095 				$caps[] = $post_type->cap->edit_published_posts;
  1057 				$caps[] = $post_type->cap->edit_private_posts;
  1110 				$caps[] = $post_type->cap->edit_private_posts;
  1058 		}
  1111 		}
  1059 		break;
  1112 		break;
  1060 	case 'read_post':
  1113 	case 'read_post':
  1061 	case 'read_page':
  1114 	case 'read_page':
  1062 		$author_data = get_userdata( $user_id );
       
  1063 		$post = get_post( $args[0] );
  1115 		$post = get_post( $args[0] );
  1064 
  1116 
  1065 		if ( 'revision' == $post->post_type ) {
  1117 		if ( 'revision' == $post->post_type ) {
  1066 			$post = get_post( $post->post_parent );
  1118 			$post = get_post( $post->post_parent );
  1067 		}
  1119 		}
  1080 		if ( $status_obj->public ) {
  1132 		if ( $status_obj->public ) {
  1081 			$caps[] = $post_type->cap->read;
  1133 			$caps[] = $post_type->cap->read;
  1082 			break;
  1134 			break;
  1083 		}
  1135 		}
  1084 
  1136 
  1085 		if ( '' != $post->post_author ) {
  1137 		$post_author_id = $post->post_author;
  1086 			$post_author_data = get_userdata( $post->post_author );
  1138 
  1087 		} else {
  1139 		// If no author set yet, default to current user for cap checks.
  1088 			// No author set yet, so default to current user for cap checks.
  1140 		if ( ! $post_author_id )
  1089 			$post_author_data = $author_data;
  1141 			$post_author_id = $user_id;
  1090 		}
  1142 
       
  1143 		$post_author_data = $post_author_id == get_current_user_id() ? wp_get_current_user() : get_userdata( $post_author_id );
  1091 
  1144 
  1092 		if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
  1145 		if ( is_object( $post_author_data ) && $user_id == $post_author_data->ID )
  1093 			$caps[] = $post_type->cap->read;
  1146 			$caps[] = $post_type->cap->read;
  1094 		elseif ( $status_obj->private )
  1147 		elseif ( $status_obj->private )
  1095 			$caps[] = $post_type->cap->read_private_posts;
  1148 			$caps[] = $post_type->cap->read_private_posts;
  1096 		else
  1149 		else
  1097 			$caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
  1150 			$caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
       
  1151 		break;
       
  1152 	case 'publish_post':
       
  1153 		$post = get_post( $args[0] );
       
  1154 		$post_type = get_post_type_object( $post->post_type );
       
  1155 
       
  1156 		$caps[] = $post_type->cap->publish_posts;
  1098 		break;
  1157 		break;
  1099 	case 'edit_post_meta':
  1158 	case 'edit_post_meta':
  1100 	case 'delete_post_meta':
  1159 	case 'delete_post_meta':
  1101 	case 'add_post_meta':
  1160 	case 'add_post_meta':
  1102 		$post = get_post( $args[0] );
  1161 		$post = get_post( $args[0] );
  1136 			$caps[] = $cap;
  1195 			$caps[] = $cap;
  1137 		break;
  1196 		break;
  1138 	case 'edit_files':
  1197 	case 'edit_files':
  1139 	case 'edit_plugins':
  1198 	case 'edit_plugins':
  1140 	case 'edit_themes':
  1199 	case 'edit_themes':
  1141 		if ( defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT ) {
  1200 		// Disallow the file editors.
       
  1201 		if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT )
  1142 			$caps[] = 'do_not_allow';
  1202 			$caps[] = 'do_not_allow';
  1143 			break;
  1203 		elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
  1144 		}
  1204 			$caps[] = 'do_not_allow';
  1145 		// Fall through if not DISALLOW_FILE_EDIT.
  1205 		elseif ( is_multisite() && ! is_super_admin( $user_id ) )
       
  1206 			$caps[] = 'do_not_allow';
       
  1207 		else
       
  1208 			$caps[] = $cap;
       
  1209 		break;
  1146 	case 'update_plugins':
  1210 	case 'update_plugins':
  1147 	case 'delete_plugins':
  1211 	case 'delete_plugins':
  1148 	case 'install_plugins':
  1212 	case 'install_plugins':
  1149 	case 'update_themes':
  1213 	case 'update_themes':
  1150 	case 'delete_themes':
  1214 	case 'delete_themes':
  1151 	case 'install_themes':
  1215 	case 'install_themes':
  1152 	case 'update_core':
  1216 	case 'update_core':
  1153 		// Disallow anything that creates, deletes, or edits core, plugin, or theme files.
  1217 		// Disallow anything that creates, deletes, or updates core, plugin, or theme files.
  1154 		// Files in uploads are excepted.
  1218 		// Files in uploads are excepted.
  1155 		if ( defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS ) {
  1219 		if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
  1156 			$caps[] = 'do_not_allow';
  1220 			$caps[] = 'do_not_allow';
  1157 			break;
  1221 		elseif ( is_multisite() && ! is_super_admin( $user_id ) )
  1158 		}
  1222 			$caps[] = 'do_not_allow';
  1159 		// Fall through if not DISALLOW_FILE_MODS.
  1223 		else
       
  1224 			$caps[] = $cap;
       
  1225 		break;
       
  1226 	case 'activate_plugins':
       
  1227 		$caps[] = $cap;
       
  1228 		if ( is_multisite() ) {
       
  1229 			// update_, install_, and delete_ are handled above with is_super_admin().
       
  1230 			$menu_perms = get_site_option( 'menu_items', array() );
       
  1231 			if ( empty( $menu_perms['plugins'] ) )
       
  1232 				$caps[] = 'manage_network_plugins';
       
  1233 		}
       
  1234 		break;
  1160 	case 'delete_user':
  1235 	case 'delete_user':
  1161 	case 'delete_users':
  1236 	case 'delete_users':
  1162 		// If multisite these caps are allowed only for super admins.
  1237 		// If multisite only super admins can delete users.
  1163 		if ( is_multisite() && !is_super_admin( $user_id ) ) {
  1238 		if ( is_multisite() && ! is_super_admin( $user_id ) )
  1164 			$caps[] = 'do_not_allow';
  1239 			$caps[] = 'do_not_allow';
  1165 		} else {
  1240 		else
  1166 			if ( 'delete_user' == $cap )
  1241 			$caps[] = 'delete_users'; // delete_user maps to delete_users.
  1167 				$cap = 'delete_users';
       
  1168 			$caps[] = $cap;
       
  1169 		}
       
  1170 		break;
  1242 		break;
  1171 	case 'create_users':
  1243 	case 'create_users':
  1172 		if ( !is_multisite() )
  1244 		if ( !is_multisite() )
  1173 			$caps[] = $cap;
  1245 			$caps[] = $cap;
  1174 		elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
  1246 		elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
  1175 			$caps[] = $cap;
  1247 			$caps[] = $cap;
  1176 		else
  1248 		else
  1177 			$caps[] = 'do_not_allow';
  1249 			$caps[] = 'do_not_allow';
  1178 		break;
  1250 		break;
       
  1251 	case 'manage_links' :
       
  1252 		if ( get_option( 'link_manager_enabled' ) )
       
  1253 			$caps[] = $cap;
       
  1254 		else
       
  1255 			$caps[] = 'do_not_allow';
       
  1256 		break;
  1179 	default:
  1257 	default:
  1180 		// Handle meta capabilities for custom post types.
  1258 		// Handle meta capabilities for custom post types.
  1181 		$post_type_meta_caps = _post_type_meta_capabilities();
  1259 		$post_type_meta_caps = _post_type_meta_capabilities();
  1182 		if ( isset( $post_type_meta_caps[ $cap ] ) ) {
  1260 		if ( isset( $post_type_meta_caps[ $cap ] ) ) {
  1183 			$args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
  1261 			$args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args );
  1219  * @param int $blog_id Blog ID
  1297  * @param int $blog_id Blog ID
  1220  * @param string $capability Capability or role name.
  1298  * @param string $capability Capability or role name.
  1221  * @return bool
  1299  * @return bool
  1222  */
  1300  */
  1223 function current_user_can_for_blog( $blog_id, $capability ) {
  1301 function current_user_can_for_blog( $blog_id, $capability ) {
       
  1302 	if ( is_multisite() )
       
  1303 		switch_to_blog( $blog_id );
       
  1304 
  1224 	$current_user = wp_get_current_user();
  1305 	$current_user = wp_get_current_user();
  1225 
  1306 
  1226 	if ( empty( $current_user ) )
  1307 	if ( empty( $current_user ) )
  1227 		return false;
  1308 		return false;
  1228 
  1309 
  1229 	// Create new object to avoid stomping the global current_user.
       
  1230 	$user = new WP_User( $current_user->ID) ;
       
  1231 
       
  1232 	// Set the blog id. @todo add blog id arg to WP_User constructor?
       
  1233 	$user->for_blog( $blog_id );
       
  1234 
       
  1235 	$args = array_slice( func_get_args(), 2 );
  1310 	$args = array_slice( func_get_args(), 2 );
  1236 	$args = array_merge( array( $capability ), $args );
  1311 	$args = array_merge( array( $capability ), $args );
  1237 
  1312 
  1238 	return call_user_func_array( array( &$user, 'has_cap' ), $args );
  1313 	$can = call_user_func_array( array( $current_user, 'has_cap' ), $args );
       
  1314 
       
  1315 	if ( is_multisite() )
       
  1316 		restore_current_blog();
       
  1317 
       
  1318 	return $can;
  1239 }
  1319 }
  1240 
  1320 
  1241 /**
  1321 /**
  1242  * Whether author of supplied post has capability or role.
  1322  * Whether author of supplied post has capability or role.
  1243  *
  1323  *
  1249  */
  1329  */
  1250 function author_can( $post, $capability ) {
  1330 function author_can( $post, $capability ) {
  1251 	if ( !$post = get_post($post) )
  1331 	if ( !$post = get_post($post) )
  1252 		return false;
  1332 		return false;
  1253 
  1333 
  1254 	$author = new WP_User( $post->post_author );
  1334 	$author = get_userdata( $post->post_author );
  1255 
  1335 
  1256 	if ( empty( $author->ID ) )
  1336 	if ( ! $author )
  1257 		return false;
  1337 		return false;
  1258 
  1338 
  1259 	$args = array_slice( func_get_args(), 2 );
  1339 	$args = array_slice( func_get_args(), 2 );
  1260 	$args = array_merge( array( $capability ), $args );
  1340 	$args = array_merge( array( $capability ), $args );
  1261 
  1341 
  1262 	return call_user_func_array( array( &$author, 'has_cap' ), $args );
  1342 	return call_user_func_array( array( $author, 'has_cap' ), $args );
  1263 }
  1343 }
  1264 
  1344 
  1265 /**
  1345 /**
  1266  * Whether a particular user has capability or role.
  1346  * Whether a particular user has capability or role.
  1267  *
  1347  *
  1271  * @param string $capability Capability or role name.
  1351  * @param string $capability Capability or role name.
  1272  * @return bool
  1352  * @return bool
  1273  */
  1353  */
  1274 function user_can( $user, $capability ) {
  1354 function user_can( $user, $capability ) {
  1275 	if ( ! is_object( $user ) )
  1355 	if ( ! is_object( $user ) )
  1276 		$user = new WP_User( $user );
  1356 		$user = get_userdata( $user );
  1277 
  1357 
  1278 	if ( ! $user || ! $user->exists() )
  1358 	if ( ! $user || ! $user->exists() )
  1279 		return false;
  1359 		return false;
  1280 
  1360 
  1281 	$args = array_slice( func_get_args(), 2 );
  1361 	$args = array_slice( func_get_args(), 2 );
  1282 	$args = array_merge( array( $capability ), $args );
  1362 	$args = array_merge( array( $capability ), $args );
  1283 
  1363 
  1284 	return call_user_func_array( array( &$user, 'has_cap' ), $args );
  1364 	return call_user_func_array( array( $user, 'has_cap' ), $args );
  1285 }
  1365 }
  1286 
  1366 
  1287 /**
  1367 /**
  1288  * Retrieve role object.
  1368  * Retrieve role object.
  1289  *
  1369  *
  1365  *
  1445  *
  1366  * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  1446  * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  1367  * @return bool True if the user is a site admin.
  1447  * @return bool True if the user is a site admin.
  1368  */
  1448  */
  1369 function is_super_admin( $user_id = false ) {
  1449 function is_super_admin( $user_id = false ) {
  1370 	if ( $user_id )
  1450 	if ( ! $user_id || $user_id == get_current_user_id() )
  1371 		$user = new WP_User( $user_id );
  1451 		$user = wp_get_current_user();
  1372 	else
  1452 	else
  1373 		$user = wp_get_current_user();
  1453 		$user = get_userdata( $user_id );
  1374 
  1454 
  1375 	if ( ! $user->exists() )
  1455 	if ( ! $user || ! $user->exists() )
  1376 		return false;
  1456 		return false;
  1377 
  1457 
  1378 	if ( is_multisite() ) {
  1458 	if ( is_multisite() ) {
  1379 		$super_admins = get_super_admins();
  1459 		$super_admins = get_super_admins();
  1380 		if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )
  1460 		if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins ) )