Skip to:
Content

BuddyPress.org

Changeset 9447


Ignore:
Timestamp:
02/07/2015 08:32:55 PM (5 years ago)
Author:
boonebgorges
Message:

When prefetching member types in the loop, cache arrays instead of strings.

Member type caches should always be arrays of all member types for a user.

Props imath.
Fixes #6193.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-cache.php

    r9210 r9447  
    2121    ) );
    2222
     23    // Rekey by user ID.
     24    $keyed_member_types = array();
     25    foreach ( $member_types as $member_type ) {
     26        if ( ! isset( $keyed_member_types[ $member_type->object_id ] ) ) {
     27            $keyed_member_types[ $member_type->object_id ] = array();
     28        }
     29
     30        $keyed_member_types[ $member_type->object_id ][] = $member_type->name;
     31    }
     32
    2333    $cached_member_ids = array();
    24     foreach ( $member_types as $member_type ) {
    25         wp_cache_set( $member_type->object_id, $member_type->name, 'bp_member_type' );
    26         $cached_member_ids[] = $member_type->object_id;
     34    foreach ( $keyed_member_types as $user_id => $user_member_types ) {
     35        wp_cache_set( $user_id, $user_member_types, 'bp_member_type' );
     36        $cached_member_ids[] = $user_id;
    2737    }
    2838
  • trunk/tests/phpunit/testcases/core/class-bp-user-query.php

    r9210 r9447  
    515515        ) );
    516516
    517         $this->assertSame( 'foo', wp_cache_get( $users[0], 'bp_member_type' ) );
    518         $this->assertSame( 'bar', wp_cache_get( $users[1], 'bp_member_type' ) );
    519         $this->assertSame( 'foo', wp_cache_get( $users[2], 'bp_member_type' ) );
     517        $this->assertSame( array( 'foo' ), wp_cache_get( $users[0], 'bp_member_type' ) );
     518        $this->assertSame( array( 'bar' ), wp_cache_get( $users[1], 'bp_member_type' ) );
     519        $this->assertSame( array( 'foo' ), wp_cache_get( $users[2], 'bp_member_type' ) );
    520520        $this->assertSame( '', wp_cache_get( $users[3], 'bp_member_type' ) );
    521521    }
  • trunk/tests/phpunit/testcases/members/types.php

    r9371 r9447  
    163163
    164164    /**
     165     * @group BP6193
     166     */
     167    public function test_bp_members_prefetch_member_type_array_cache_set() {
     168        $u1 = $this->factory->user->create();
     169        $u2 = $this->factory->user->create();
     170        bp_register_member_type( 'foo' );
     171        bp_register_member_type( 'bar' );
     172        bp_set_member_type( $u1, 'foo' );
     173        bp_set_member_type( $u1, 'bar', true );
     174
     175        // Get users so that the 'bp_user_query_populate_extras' is fired
     176        // and members type prefetched
     177        $users = bp_core_get_users( array( 'include' => array( $u1, $u2 ) ) );
     178
     179        // Get single member type
     180        $this->assertSame( 'foo', bp_get_member_type( $u1, true ) );
     181        $this->assertEmpty( bp_get_member_type( $u2, true ) );
     182
     183        // Get all member types for the user
     184        $this->assertEqualSets( array( 'foo', 'bar' ), bp_get_member_type( $u1, false ) );
     185        $this->assertEmpty( bp_get_member_type( $u2, false ) );
     186    }
     187
     188    /**
    165189     * @group cache
    166190     */
Note: See TracChangeset for help on using the changeset viewer.