WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/22/2010 05:27:28 PM (12 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 *
Note: See TracChangeset for help on using the changeset viewer.