Skip to:
Content

BuddyPress.org

Ticket #5696: 5696.01.patch

File 5696.01.patch, 2.6 KB (added by r-a-y, 10 years ago)
  • src/bp-members/bp-members-functions.php

     
    195195}
    196196
    197197/**
    198  * Return the user_id for a user based on their user_login.
     198 * Return the user ID based on a user's user_login.
    199199 *
    200  * @todo Refactor to check relevant WP caches, or to use get_user_by()
     200 * @since BuddyPress (1.0.0)
    201201 *
    202  * @param string $username Username to check.
    203  * @return int|bool The ID of the matched user, or false.
     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 ) )
     206        if ( empty( $username ) ) {
    209207                return false;
     208        }
     209
     210        $user = get_user_by( 'login', $username );
    210211
    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        return apply_filters( 'bp_core_get_userid', ! empty( $user->ID ) ? $user->ID : NULL, $username );
    212213}
    213214
    214215/**
    215  * Returns the user_id for a user based on their user_nicename.
     216 * Return the user ID based on a user's user_nicename.
    216217 *
    217  * @todo Refactor to check relevant WP caches, or to use get_user_by()
     218 * @since BuddyPress (1.2.3)
    218219 *
    219  * @param string $username Username to check.
    220  * @return int|bool The ID of the matched user, or false.
     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 ) )
     224        if ( empty( $user_nicename ) ) {
    226225                return false;
     226        }
     227
     228        $user = get_user_by( 'slug', $user_nicename );
    227229
    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 );
     230        return apply_filters( 'bp_core_get_userid_from_nicename', ! empty( $user->ID ) ? $user->ID : NULL, $user_nicename );
    229231}
    230232
    231233/**
  • tests/phpunit/testcases/members/functions.php

     
    399399
    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}