Introduce WP_User::for_blog() and current_user_can_for_blog() to avoid calls to WP_User::_init_caps(). fixes #11781

git-svn-id: http://svn.automattic.com/wordpress/trunk@12796 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2010-01-22 17:27:28 +00:00
parent 6847f8ea95
commit 92bf8d124c
3 changed files with 51 additions and 4 deletions

View File

@ -222,6 +222,7 @@ switch ( $_GET['action'] ) {
// get blog prefix // get blog prefix
$blog_prefix = $wpdb->get_blog_prefix( $id ); $blog_prefix = $wpdb->get_blog_prefix( $id );
// user roles // user roles
if ( is_array( $_POST[ 'role' ] ) == true ) { if ( is_array( $_POST[ 'role' ] ) == true ) {
$newroles = $_POST[ 'role' ]; $newroles = $_POST[ 'role' ];
@ -230,8 +231,7 @@ switch ( $_GET['action'] ) {
$user = new WP_User($userid); $user = new WP_User($userid);
if ( ! $user ) if ( ! $user )
continue; continue;
// Hack. Init user caps for given blog. $user->for_blog($id);
$user->_init_caps($blog_prefix . 'capabilities');
$user->set_role($role); $user->set_role($role);
} }
} }

View File

@ -755,6 +755,23 @@ class WP_User {
return 'level_' . $level; return 'level_' . $level;
} }
/**
* Set the blog to operate on. Defaults to the current blog.
*
* @since 3.0
*
* @param int $blog_id Optional Blog ID, defaults to current blog.
*/
function for_blog( $blog_id = '' ) {
global $wpdb;
if ( !empty($blog_id) ) {
$cap_key = $wpdb->get_blog_prefix( $blog_id );
$cap_key. 'capabilities';
} else {
$cap_key = '';
}
$this->_init_caps($cap_key);
}
} }
/** /**
@ -1017,6 +1034,36 @@ function current_user_can( $capability ) {
return call_user_func_array( array( &$current_user, 'has_cap' ), $args ); return call_user_func_array( array( &$current_user, 'has_cap' ), $args );
} }
/**
* Whether current user has a capability or role for a given blog.
*
* @since 2.0.0
*
* @param int $blog_id Blog ID
* @param string $capability Capability or role name.
* @return bool
*/
function current_user_can_for_blog( $blog_id, $capability ) {
$current_user = wp_get_current_user();
if ( is_multisite() && is_super_admin() )
return true;
if ( empty( $current_user ) )
return false;
// Create new object to avoid stomping the global current_user.
$user = new WP_User( $current_user->id) ;
// Set the blog id. @todo add blog id arg to WP_User constructor?
$user->for_blog( $blog_id );
$args = array_slice( func_get_args(), 2 );
$args = array_merge( array( $capability ), $args );
return call_user_func_array( array( &$user, 'has_cap' ), $args );
}
/** /**
* Whether author of supplied post has capability or role. * Whether author of supplied post has capability or role.
* *

View File

@ -306,7 +306,7 @@ function switch_to_blog( $new_blog ) {
} }
if ( is_object( $current_user ) ) if ( is_object( $current_user ) )
$current_user->_init_caps(); $current_user->for_blog( $blog_id );
if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
$global_groups = $wp_object_cache->global_groups; $global_groups = $wp_object_cache->global_groups;
@ -359,7 +359,7 @@ function restore_current_blog() {
} }
if ( is_object( $current_user ) ) if ( is_object( $current_user ) )
$current_user->_init_caps(); $current_user->for_blog( $blog_id );
if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
$global_groups = $wp_object_cache->global_groups; $global_groups = $wp_object_cache->global_groups;