From 92bf8d124c6d80bb3ad6b2dc11bca06c1092c47e Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 22 Jan 2010 17:27:28 +0000 Subject: [PATCH] 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 --- wp-admin/ms-edit.php | 4 +-- wp-includes/capabilities.php | 47 ++++++++++++++++++++++++++++++++++++ wp-includes/ms-functions.php | 4 +-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/wp-admin/ms-edit.php b/wp-admin/ms-edit.php index aa10de8b4..22c4f77e5 100644 --- a/wp-admin/ms-edit.php +++ b/wp-admin/ms-edit.php @@ -222,6 +222,7 @@ switch ( $_GET['action'] ) { // get blog prefix $blog_prefix = $wpdb->get_blog_prefix( $id ); + // user roles if ( is_array( $_POST[ 'role' ] ) == true ) { $newroles = $_POST[ 'role' ]; @@ -230,8 +231,7 @@ switch ( $_GET['action'] ) { $user = new WP_User($userid); if ( ! $user ) continue; - // Hack. Init user caps for given blog. - $user->_init_caps($blog_prefix . 'capabilities'); + $user->for_blog($id); $user->set_role($role); } } diff --git a/wp-includes/capabilities.php b/wp-includes/capabilities.php index 1509ffff0..dcd308e16 100644 --- a/wp-includes/capabilities.php +++ b/wp-includes/capabilities.php @@ -755,6 +755,23 @@ class WP_User { 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 ); } +/** + * 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. * diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php index 1b82a8d1d..9dc214a8a 100644 --- a/wp-includes/ms-functions.php +++ b/wp-includes/ms-functions.php @@ -306,7 +306,7 @@ function switch_to_blog( $new_blog ) { } 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 ) ) $global_groups = $wp_object_cache->global_groups; @@ -359,7 +359,7 @@ function restore_current_blog() { } 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 ) ) $global_groups = $wp_object_cache->global_groups;