Make WordPress Core

Ticket #32472: is_user_member_of_blog_(v5).diff

File is_user_member_of_blog_(v5).diff, 2.7 KB (added by BinaryKitten, 9 years ago)

Updated Patch - Based from Develop.git.wordpress.org repo, updated unit tests and code to match.

  • src/wp-includes/user.php

    diff --git src/wp-includes/user.php src/wp-includes/user.php
    index 1cd2db2..6d89bcb 100644
    function get_blogs_of_user( $user_id, $all = false ) { 
    12701270 * @return bool
    12711271 */
    12721272function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
     1273        global $wpdb;
     1274
    12731275        $user_id = (int) $user_id;
    12741276        $blog_id = (int) $blog_id;
    12751277
    1276         if ( empty( $user_id ) )
     1278        if ( empty( $user_id ) ) {
    12771279                $user_id = get_current_user_id();
     1280        }
     1281
     1282        //Technically not needed, but does save calls to get_blog_details and get_user_meta
     1283        //in the event that the function is called when a user isn't logged in
     1284        if ( empty( $user_id ) ) {
     1285                return false;
     1286        } else {
     1287                $user = get_userdata( $user_id );
     1288                if ( ! $user instanceof WP_User ) {
     1289                        return false;
     1290                }
     1291        }
    12781292
    1279         if ( empty( $blog_id ) )
     1293        if ( ! is_multisite() ) {
     1294                return true;
     1295        }
     1296
     1297        if ( empty( $blog_id ) ) {
    12801298                $blog_id = get_current_blog_id();
     1299        }
     1300
     1301        $blog = get_blog_details( $blog_id );
    12811302
    1282         $blogs = get_blogs_of_user( $user_id );
    1283         return array_key_exists( $blog_id, $blogs );
     1303        if ( ! $blog || ! isset( $blog->domain ) || $blog->archived || $blog->spam || $blog->deleted ) {
     1304                return false;
     1305        }
     1306
     1307        $keys = get_user_meta( $user_id );
     1308        if ( empty( $keys ) ) {
     1309                return false;
     1310        }
     1311
     1312        //no underscore before capabilities in $base_capabilities_key
     1313        $base_capabilities_key = $wpdb->base_prefix . 'capabilities';
     1314        $site_capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities';
     1315
     1316        if ( isset( $keys[ $base_capabilities_key ] ) && $blog_id == 1 ) {
     1317                return true;
     1318        }
     1319
     1320        if ( isset( $keys[ $site_capabilities_key ] ) ) {
     1321                return true;
     1322        }
     1323
     1324        return false;
    12841325}
    12851326
    12861327/**
  • tests/phpunit/tests/user.php

    diff --git tests/phpunit/tests/user.php tests/phpunit/tests/user.php
    index f63d838..32fe283 100644
    class Tests_User extends WP_UnitTestCase { 
    366366        function test_is_user_member_of_blog() {
    367367                $old_current = get_current_user_id();
    368368
     369                // test for "get current user" when not logged in
     370                $this->assertFalse( is_user_member_of_blog() );
     371
    369372                $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    370373                wp_set_current_user( $user_id );
    371374
  • tests/phpunit/tests/user/multisite.php

    diff --git tests/phpunit/tests/user/multisite.php tests/phpunit/tests/user/multisite.php
    index 41b0e88..59cf898 100644
    class Tests_Multisite_User extends WP_UnitTestCase { 
    140140        function test_is_user_member_of_blog() {
    141141                global $wpdb;
    142142
     143                // test for "get current user" when not logged in
     144                $this->assertFalse( is_user_member_of_blog() );
     145
    143146                $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    144147
    145148                $old_current = get_current_user_id();