Make WordPress Core

Changeset 12796


Ignore:
Timestamp:
01/22/2010 05:27:28 PM (15 years ago)
Author:
ryan
Message:

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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/ms-edit.php

    r12774 r12796  
    223223        // get blog prefix
    224224        $blog_prefix = $wpdb->get_blog_prefix( $id );
     225
    225226        // user roles
    226227        if ( is_array( $_POST[ 'role' ] ) == true ) {
     
    231232                if ( ! $user )
    232233                    continue;
    233                 // Hack. Init user caps for given blog.
    234                 $user->_init_caps($blog_prefix . 'capabilities');
     234                $user->for_blog($id);
    235235                $user->set_role($role);
    236236            }
  • trunk/wp-includes/capabilities.php

    r12778 r12796  
    756756    }
    757757
     758    /**
     759     * Set the blog to operate on. Defaults to the current blog.
     760     *
     761     * @since 3.0
     762     *
     763     * @param int $blog_id Optional Blog ID, defaults to current blog.
     764     */
     765    function for_blog( $blog_id = '' ) {
     766        global $wpdb;
     767        if ( !empty($blog_id) ) {
     768            $cap_key = $wpdb->get_blog_prefix( $blog_id );
     769            $cap_key. 'capabilities';
     770        } else {
     771            $cap_key = '';
     772        }
     773        $this->_init_caps($cap_key);
     774    }
    758775}
    759776
     
    10191036
    10201037/**
     1038 * Whether current user has a capability or role for a given blog.
     1039 *
     1040 * @since 2.0.0
     1041 *
     1042 * @param int $blog_id Blog ID
     1043 * @param string $capability Capability or role name.
     1044 * @return bool
     1045 */
     1046function current_user_can_for_blog( $blog_id, $capability ) {
     1047    $current_user = wp_get_current_user();
     1048
     1049    if ( is_multisite() && is_super_admin() )
     1050        return true;
     1051
     1052    if ( empty( $current_user ) )
     1053        return false;
     1054
     1055    // Create new object to avoid stomping the global current_user.
     1056    $user = new WP_User( $current_user->id) ;
     1057
     1058    // Set the blog id.  @todo add blog id arg to WP_User constructor?
     1059    $user->for_blog( $blog_id );
     1060
     1061    $args = array_slice( func_get_args(), 2 );
     1062    $args = array_merge( array( $capability ), $args );
     1063
     1064    return call_user_func_array( array( &$user, 'has_cap' ), $args );
     1065}
     1066
     1067/**
    10211068 * Whether author of supplied post has capability or role.
    10221069 *
  • trunk/wp-includes/ms-functions.php

    r12778 r12796  
    307307
    308308    if ( is_object( $current_user ) )
    309         $current_user->_init_caps();
     309        $current_user->for_blog( $blog_id );
    310310
    311311    if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
     
    360360
    361361    if ( is_object( $current_user ) )
    362         $current_user->_init_caps();
     362        $current_user->for_blog( $blog_id );
    363363
    364364    if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
Note: See TracChangeset for help on using the changeset viewer.