Make WordPress Core

Changeset 33771


Ignore:
Timestamp:
08/27/2015 08:01:37 PM (9 years ago)
Author:
johnbillion
Message:

Improve the efficiency of is_user_member_of_blog() by removing its use of get_blogs_of_user(). Adds additional tests.

Fixes #32472
Props BinaryKitten, sammybeats, johnbillion

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user-functions.php

    r33749 r33771  
    584584 */
    585585function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
     586    global $wpdb;
     587
    586588    $user_id = (int) $user_id;
    587589    $blog_id = (int) $blog_id;
    588590
    589     if ( empty( $user_id ) )
     591    if ( empty( $user_id ) ) {
    590592        $user_id = get_current_user_id();
    591 
    592     if ( empty( $blog_id ) )
     593    }
     594
     595    // Technically not needed, but does save calls to get_blog_details and get_user_meta
     596    // in the event that the function is called when a user isn't logged in
     597    if ( empty( $user_id ) ) {
     598        return false;
     599    } else {
     600        $user = get_userdata( $user_id );
     601        if ( ! $user instanceof WP_User ) {
     602            return false;
     603        }
     604    }
     605
     606    if ( ! is_multisite() ) {
     607        return true;
     608    }
     609
     610    if ( empty( $blog_id ) ) {
    593611        $blog_id = get_current_blog_id();
    594 
    595     $blogs = get_blogs_of_user( $user_id );
    596     return array_key_exists( $blog_id, $blogs );
     612    }
     613
     614    $blog = get_blog_details( $blog_id );
     615
     616    if ( ! $blog || ! isset( $blog->domain ) || $blog->archived || $blog->spam || $blog->deleted ) {
     617        return false;
     618    }
     619
     620    $keys = get_user_meta( $user_id );
     621    if ( empty( $keys ) ) {
     622        return false;
     623    }
     624
     625    // no underscore before capabilities in $base_capabilities_key
     626    $base_capabilities_key = $wpdb->base_prefix . 'capabilities';
     627    $site_capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities';
     628
     629    if ( isset( $keys[ $base_capabilities_key ] ) && $blog_id == 1 ) {
     630        return true;
     631    }
     632
     633    if ( isset( $keys[ $site_capabilities_key ] ) ) {
     634        return true;
     635    }
     636
     637    return false;
    597638}
    598639
  • trunk/tests/phpunit/tests/user.php

    r33115 r33771  
    367367        $old_current = get_current_user_id();
    368368
     369        $this->assertSame( 0, $old_current );
     370
     371        // test for "get current user" when not logged in
     372        $this->assertFalse( is_user_member_of_blog() );
     373
    369374        $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    370375        wp_set_current_user( $user_id );
  • trunk/tests/phpunit/tests/user/multisite.php

    r33143 r33771  
    142142
    143143        $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
     144        $user2_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    144145
    145146        $old_current = get_current_user_id();
     147
     148        $this->assertSame( 0, $old_current );
     149
     150        // test for "get current user" when not logged in
     151        $this->assertFalse( is_user_member_of_blog() );
     152
    146153        wp_set_current_user( $user1_id );
    147154
     
    155162        foreach ( $blog_ids as $blog_id ) {
    156163            $this->assertInternalType( 'int', $blog_id );
     164
     165            // Current user gets added to new blogs
    157166            $this->assertTrue( is_user_member_of_blog( $user1_id, $blog_id ) );
     167            // Other users should not
     168            $this->assertFalse( is_user_member_of_blog( $user2_id, $blog_id ) );
     169
     170            switch_to_blog( $blog_id );
     171
     172            $this->assertTrue( is_user_member_of_blog( $user1_id ) );
     173            $this->assertFalse( is_user_member_of_blog( $user2_id ) );
     174
     175            // Remove user 1 from blog
    158176            $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) );
     177
     178            // Add user 2 to blog
     179            $this->assertTrue( add_user_to_blog( $blog_id, $user2_id, 'subscriber' ) );
     180
     181            $this->assertFalse( is_user_member_of_blog( $user1_id ) );
     182            $this->assertTrue( is_user_member_of_blog( $user2_id ) );
     183
     184            restore_current_blog();
     185
    159186            $this->assertFalse( is_user_member_of_blog( $user1_id, $blog_id ) );
     187            $this->assertTrue( is_user_member_of_blog( $user2_id, $blog_id ) );
    160188        }
    161189
    162190        wpmu_delete_user( $user1_id );
    163191        $user = new WP_User( $user1_id );
    164         $this->assertFalse( $user->exists(), 'WP_User->exists' );
    165         $this->assertFalse( is_user_member_of_blog( $user1_id ), 'is_user_member_of_blog' );
     192        $this->assertFalse( $user->exists() );
     193        $this->assertFalse( is_user_member_of_blog( $user1_id ) );
    166194
    167195        wp_set_current_user( $old_current );
Note: See TracChangeset for help on using the changeset viewer.