Skip to:
Content

BuddyPress.org

Changeset 8532


Ignore:
Timestamp:
06/15/2014 08:07:22 PM (10 years ago)
Author:
r-a-y
Message:

Use get_user_by() for bp_core_get_userid_from_nicename() and bp_core_get_userid()

Previously, these functions used direct DB queries to grab the user ID when
a user_nicename or user_login was passed.

This commit switches these direct DB queries to use get_user_by(), which
caches the query when a persistent object cache is enabled.

Fixes #5696.

Location:
trunk
Files:
2 edited

Legend:

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

    r8333 r8532  
    196196
    197197/**
    198  * Return the user_id for a user based on their user_login.
    199  *
    200  * @todo Refactor to check relevant WP caches, or to use get_user_by()
    201  *
    202  * @param string $username Username to check.
    203  * @return int|bool The ID of the matched user, or false.
     198 * Return the user ID based on a user's user_login.
     199 *
     200 * @since BuddyPress (1.0.0)
     201 *
     202 * @param string $username user_login to check.
     203 * @return int|null The ID of the matched user on success, null on failure.
    204204 */
    205205function bp_core_get_userid( $username ) {
    206     global $wpdb;
    207 
    208     if ( empty( $username ) )
    209         return false;
    210 
    211     return apply_filters( 'bp_core_get_userid', $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->users} WHERE user_login = %s", $username ) ), $username );
    212 }
    213 
    214 /**
    215  * Returns the user_id for a user based on their user_nicename.
    216  *
    217  * @todo Refactor to check relevant WP caches, or to use get_user_by()
    218  *
    219  * @param string $username Username to check.
    220  * @return int|bool The ID of the matched user, or false.
     206    if ( empty( $username ) ) {
     207        return false;
     208    }
     209
     210    $user = get_user_by( 'login', $username );
     211
     212    return apply_filters( 'bp_core_get_userid', ! empty( $user->ID ) ? $user->ID : NULL, $username );
     213}
     214
     215/**
     216 * Return the user ID based on a user's user_nicename.
     217 *
     218 * @since BuddyPress (1.2.3)
     219 *
     220 * @param string $user_nicename user_nicename to check.
     221 * @return int|null The ID of the matched user on success, null on failure.
    221222 */
    222223function bp_core_get_userid_from_nicename( $user_nicename ) {
    223     global $wpdb;
    224 
    225     if ( empty( $user_nicename ) )
    226         return false;
    227 
    228     return apply_filters( 'bp_core_get_userid_from_nicename', $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->users} WHERE user_nicename = %s", $user_nicename ) ), $user_nicename );
     224    if ( empty( $user_nicename ) ) {
     225        return false;
     226    }
     227
     228    $user = get_user_by( 'slug', $user_nicename );
     229
     230    return apply_filters( 'bp_core_get_userid_from_nicename', ! empty( $user->ID ) ? $user->ID : NULL, $user_nicename );
    229231}
    230232
  • trunk/tests/phpunit/testcases/members/functions.php

    r8333 r8532  
    400400        $this->assertSame( $expected, $found );
    401401    }
     402
     403    /**
     404     * @group bp_core_get_userid_from_nicename
     405     */
     406    public function test_bp_core_get_userid_from_nicename_failure() {
     407        $this->assertSame( NULL, bp_core_get_userid_from_nicename( 'non_existent_user' ) );
     408    }
    402409}
Note: See TracChangeset for help on using the changeset viewer.