Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/03/2014 09:35:48 PM (11 years ago)
Author:
boonebgorges
Message:

Introduce bp_core_user_displaynames(), for quick, bulk displayname lookup

The technique BP uses for determining a user's displayname takes into
consideration whether the xprofile component is active, whether the user has a
WP display_name, and a number of other factors. Because the values are used so
often throughout BP, they are then cached in WP's object cache. However, there
are a number of places throughout BuddyPress where displaynames are fetched
in a way that is inconsistent with the "canonical" workflow (ie, by querying
the xprofile tables correctly). This causes inconsistent results, and also can
result in performance degradation when the persistent cache is skipped.
Moreover, the singular nature of bp_core_get_user_displayname() meant that
doing it the "right" way meant introducing large numbers of queries to a given
page load.

This changeset introduces bp_core_get_user_displaynames(), which contains all
the critical logic of bp_core_get_user_displayname() - including fallbacks for
various setups and support for the object cache - but allows for fetching large
numbers of displaynames without multiple queries. bp_core_get_user_displayname()
is now a wrapper for the more general function.

The function has been swapped in throughout BuddyPress as appropriate.

See #5445

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/testcases/members/functions.php

    r8022 r8027  
    210210        }
    211211    }
     212
     213    /**
     214     * @group bp_core_get_user_displaynames
     215     */
     216    public function test_bp_core_get_user_displayname_arrays_all_bad_entries() {
     217        $this->assertSame( array(), bp_core_get_user_displaynames( array( 0, 'foo', ) ) );
     218    }
     219
     220    /**
     221     * @group bp_core_get_user_displaynames
     222     */
     223    public function test_bp_core_get_user_displaynames_all_uncached() {
     224        $u1 = $this->create_user();
     225        $u2 = $this->create_user();
     226
     227        xprofile_set_field_data( 1, $u1, 'Foo' );
     228        xprofile_set_field_data( 1, $u2, 'Bar' );
     229
     230        $expected = array(
     231            $u1 => 'Foo',
     232            $u2 => 'Bar',
     233        );
     234
     235        $this->assertSame( $expected, bp_core_get_user_displaynames( array( $u1, $u2, ) ) );
     236    }
     237
     238    /**
     239     * @group bp_core_get_user_displaynames
     240     */
     241    public function test_bp_core_get_user_displaynames_one_not_in_xprofile() {
     242        $u1 = $this->create_user();
     243        $u2 = $this->create_user( array(
     244            'display_name' => 'Bar',
     245        ) );
     246
     247        xprofile_set_field_data( 1, $u1, 'Foo' );
     248
     249        // Delete directly because BP won't let you delete a required
     250        // field through the API
     251        global $wpdb, $bp;
     252        $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = 1", $u2 ) );
     253        wp_cache_delete( 'bp_user_fullname_' . $u2, 'bp' );
     254        wp_cache_delete( 1, 'bp_xprofile_data_' . $u2, 'bp' );
     255
     256        $expected = array(
     257            $u1 => 'Foo',
     258            $u2 => 'Bar',
     259        );
     260
     261        $this->assertSame( $expected, bp_core_get_user_displaynames( array( $u1, $u2, ) ) );
     262    }
     263
     264    /**
     265     * @group bp_core_get_user_displaynames
     266     */
     267    public function test_bp_core_get_user_displaynames_one_in_cache() {
     268        $u1 = $this->create_user();
     269        xprofile_set_field_data( 1, $u1, 'Foo' );
     270
     271        // Fake the cache for $u2
     272        $u2 = 123;
     273        wp_cache_set( 'bp_user_fullname_' . $u2, 'Bar', 'bp' );
     274
     275        $expected = array(
     276            $u1 => 'Foo',
     277            $u2 => 'Bar',
     278        );
     279
     280        $this->assertSame( $expected, bp_core_get_user_displaynames( array( $u1, $u2, ) ) );
     281    }
    212282}
Note: See TracChangeset for help on using the changeset viewer.