Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/25/2018 07:44:54 PM (7 years ago)
Author:
dcavins
Message:

Use WP functions to retrieve user data.

Use WP functions to retrieve user name, display name, nicename, and email.

Switch functions like bp_core_get_username() to use native WordPress functions. This also removes the need for maintaining redundant cached items, like caching individual usernames, and instead relies upon WP's userdata cache.

Props dcavins, djpaul.

Fixes #7661.

File:
1 edited

Legend:

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

    r11747 r11818  
    102102    /**
    103103     * @group bp_core_get_user_displayname
    104      * @group cache
    105      */
    106     public function test_bp_core_get_user_displayname_xprofile_populate_cache() {
    107         $xprofile_is_active = bp_is_active( 'xprofile' );
    108         buddypress()->active_components['xprofile'] = '1';
    109 
    110         $u = self::factory()->user->create( array(
    111             'display_name' => 'Foo',
    112         ) );
    113         bp_core_get_user_displayname( $u );
    114 
    115         $this->assertSame( 'Foo', wp_cache_get( 'bp_user_fullname_' . $u, 'bp' ) );
    116 
    117         if ( ! $xprofile_is_active ) {
    118             unset( buddypress()->active_components['xprofile'] );
    119         }
    120     }
    121 
    122     /**
    123      * @group bp_core_get_user_displayname
    124      * @group cache
    125      */
    126     public function test_bp_core_get_user_displayname_xprofile_bust_cache_after_xprofile_update() {
    127         $xprofile_is_active = bp_is_active( 'xprofile' );
    128         buddypress()->active_components['xprofile'] = '1';
    129 
    130         $u = self::factory()->user->create();
    131         xprofile_set_field_data( 1, $u, 'Foo Foo' );
    132 
    133         $this->assertFalse( wp_cache_get( 'bp_user_fullname_' . $u, 'bp' ) );
    134 
    135         if ( ! $xprofile_is_active ) {
    136             unset( buddypress()->active_components['xprofile'] );
    137         }
    138     }
    139 
    140     /**
    141      * @group bp_core_get_user_displayname
    142104     */
    143105    public function test_bp_core_get_user_displayname_xprofile_exists() {
     
    156118
    157119    /**
    158      * @group bp_core_get_user_displayname
    159      */
    160     public function test_bp_core_get_user_displayname_xprofile_does_not_exist() {
    161         $bp = buddypress();
    162         $xprofile_is_active = bp_is_active( 'xprofile' );
    163         $bp->active_components['xprofile'] = '1';
    164 
    165         $u = self::factory()->user->create( array(
    166             'display_name' => 'Foo Foo',
    167         ) );
    168 
    169         // Delete directly because BP won't let you delete a required
    170         // field through the API
    171         global $wpdb;
    172         $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = 1", $u ) );
    173         wp_cache_delete( 'bp_user_fullname_' . $u, 'bp' );
    174         wp_cache_delete( "{$u}:1", 'bp_xprofile_data' );
    175 
    176         $this->assertSame( '', xprofile_get_field_data( 1, $u ) );
    177         $this->assertSame( 'Foo Foo', bp_core_get_user_displayname( $u ) );
    178         $this->assertSame( 'Foo Foo', xprofile_get_field_data( 1, $u ) );
    179 
    180         if ( ! $xprofile_is_active ) {
    181             unset( $bp->active_components['xprofile'] );
    182         }
    183     }
    184 
    185     /**
    186120     * @group bp_core_get_user_displaynames
    187121     */
     
    196130        $u1 = self::factory()->user->create();
    197131        $u2 = self::factory()->user->create();
    198 
    199132        xprofile_set_field_data( 1, $u1, 'Foo' );
    200133        xprofile_set_field_data( 1, $u2, 'Bar' );
     
    216149            'display_name' => 'Bar',
    217150        ) );
    218 
    219151        xprofile_set_field_data( 1, $u1, 'Foo' );
    220 
    221         // Delete directly because BP won't let you delete a required
    222         // field through the API
    223         global $wpdb;
    224         $bp = buddypress();
    225         $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = 1", $u2 ) );
    226         wp_cache_delete( 'bp_user_fullname_' . $u2, 'bp' );
    227         wp_cache_delete( "{$u2}:1", 'bp_xprofile_data' );
    228 
    229         $expected = array(
    230             $u1 => 'Foo',
    231             $u2 => 'Bar',
    232         );
    233 
    234         $this->assertSame( $expected, bp_core_get_user_displaynames( array( $u1, $u2, ) ) );
    235     }
    236 
    237     /**
    238      * @group bp_core_get_user_displaynames
    239      */
    240     public function test_bp_core_get_user_displaynames_one_in_cache() {
    241         $u1 = self::factory()->user->create();
    242         xprofile_set_field_data( 1, $u1, 'Foo' );
    243 
    244         // Fake the cache for $u2
    245         $u2 = 123;
    246         wp_cache_set( 'bp_user_fullname_' . $u2, 'Bar', 'bp' );
    247152
    248153        $expected = array(
Note: See TracChangeset for help on using the changeset viewer.