WordPress.org

Make WordPress Core

Changeset 6346


Ignore:
Timestamp:
11/27/2007 10:14:53 PM (11 years ago)
Author:
ryan
Message:

get_user_by_email() and some user caching cleanups. fixes #5392

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/user.php

    r6303 r6346  
    273273    wp_cache_delete($id, 'users');
    274274    wp_cache_delete($user->user_login, 'userlogins');
     275    wp_cache_delete($user->user_email, 'useremail');
    275276
    276277    return true;
  • trunk/wp-includes/pluggable.php

    r6330 r6346  
    6161function get_userdata( $user_id ) {
    6262    global $wpdb;
     63
    6364    $user_id = abs(intval($user_id));
    6465    if ( $user_id == 0 )
     
    6768    $user = wp_cache_get($user_id, 'users');
    6869
    69     if ( $user )
     70    if ( '0' === $user )
     71        return false;
     72    else if ( $user )
    7073        return $user;
    7174
    72     if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE ID = %d LIMIT 1", $user_id)) )
    73         return false;
    74 
    75     $wpdb->hide_errors();
    76     $metavalues = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user_id));
    77     $wpdb->show_errors();
    78 
    79     if ($metavalues) {
    80         foreach ( $metavalues as $meta ) {
    81             $value = maybe_unserialize($meta->meta_value);
    82             $user->{$meta->meta_key} = $value;
    83 
    84             // We need to set user_level from meta, not row
    85             if ( $wpdb->prefix . 'user_level' == $meta->meta_key )
    86                 $user->user_level = $meta->meta_value;
    87         } // end foreach
    88     } //end if
    89 
    90     // For backwards compat.
    91     if ( isset($user->first_name) )
    92         $user->user_firstname = $user->first_name;
    93     if ( isset($user->last_name) )
    94         $user->user_lastname = $user->last_name;
    95     if ( isset($user->description) )
    96         $user->user_description = $user->description;
    97 
    98     wp_cache_add($user_id, $user, 'users');
    99     wp_cache_add($user->user_login, $user_id, 'userlogins');
     75    if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE ID = %d LIMIT 1", $user_id)) ) {
     76        wp_cache_add($user_id, 0, 'users');
     77        return false;
     78    }
     79
     80    _fill_user($user);
     81
    10082    return $user;
    10183}
     
    11799
    118100    $user_id = wp_cache_get($user_login, 'userlogins');
    119     $userdata = wp_cache_get($user_id, 'users');
    120 
    121     if ( $userdata )
    122         return $userdata;
    123 
    124     if ( !$user_ID = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_login = %s", $user_login)) )
    125         return false;
    126 
    127     $user = get_userdata($user_ID);
     101    if ( '0' === $user_id )
     102        return false;
     103
     104    $user = false;
     105    if ( false !== $user_id )
     106        $user = wp_cache_get($user_id, 'users');
     107
     108    if ( false !== $user )
     109        return $user;
     110
     111    if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_login = %s", $user_login)) ) {
     112        wp_cache_add($user_login, 0, 'userlogins');
     113        return false;
     114    }
     115
     116    _fill_user($user);
     117
     118    return $user;
     119}
     120endif;
     121
     122if ( !function_exists('get_user_by_email') ) :
     123function get_user_by_email($email) {
     124    global $wpdb;
     125
     126    $user_id = wp_cache_get($email, 'useremail');
     127
     128    if ( '0' === $user_id )
     129        return false;
     130
     131    $user = false;
     132    if ( false !== $user_id )
     133        $user = wp_cache_get($user_id, 'users');
     134
     135    if ( false !== $user )
     136        return $user;
     137
     138    if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_email = %s", $email)) ) {
     139        wp_cache_add($email, 0, 'useremail');
     140        return false;
     141    }
     142
     143    _fill_user($user);
     144
    128145    return $user;
    129146}
  • trunk/wp-includes/registration.php

    r6302 r6346  
    77 */
    88function username_exists( $username ) {
    9     if ( $user = get_userdatabylogin( sanitize_user( $username ) ) ) {
     9    if ( $user = get_userdatabylogin( $username ) ) {
    1010        return $user->ID;
    1111    } else {
     
    2121 */
    2222function email_exists( $email ) {
    23     global $wpdb;
    24     return $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_email = %s", $email) );
     23    if ( $user = get_user_by_email($email) )
     24        return $user->ID;
     25
     26    return false;
    2527}
    2628
  • trunk/wp-includes/user.php

    r6260 r6346  
    7979    $user = get_userdata($user_id);
    8080    wp_cache_delete($user_id, 'users');
    81     wp_cache_delete($user->user_login, 'userlogins');
    8281
    8382    return true;
     
    143142    $user = get_userdata($user_id);
    144143    wp_cache_delete($user_id, 'users');
    145     wp_cache_delete($user->user_login, 'userlogins');
    146144
    147145    return true;
     
    239237}
    240238
     239function _fill_user( &$user ) {
     240    global $wpdb;
     241
     242    $wpdb->hide_errors();
     243    $metavalues = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user->ID));
     244    $wpdb->show_errors();
     245
     246    if ( $metavalues ) {
     247        foreach ( $metavalues as $meta ) {
     248            $value = maybe_unserialize($meta->meta_value);
     249            $user->{$meta->meta_key} = $value;
     250        }
     251    }
     252
     253    $level = $wpdb->prefix . 'user_level';
     254    if ( isset( $user->{$level} ) )
     255        $user->user_level = $user->{$level};
     256
     257    // For backwards compat.
     258    if ( isset($user->first_name) )
     259        $user->user_firstname = $user->first_name;
     260    if ( isset($user->last_name) )
     261        $user->user_lastname = $user->last_name;
     262    if ( isset($user->description) )
     263        $user->user_description = $user->description;
     264
     265    wp_cache_add($user->ID, $user, 'users');
     266    wp_cache_add($user->user_login, $user->ID, 'userlogins');
     267    wp_cache_add($user->user_email, $user->ID, 'useremail');
     268}
     269
    241270?>
Note: See TracChangeset for help on using the changeset viewer.