Make WordPress Core


Ignore:
Timestamp:
08/24/2011 07:32:59 PM (10 years ago)
Author:
ryan
Message:

Introduce metadata_exists(), WP_User::get_data_by(), WP_User::get(), WP_User::has_prop(). Don't fill user objects with meta. Eliminate data duplication in cache and memory. Props scribu. see #15458

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/pluggable.php

    r18558 r18597  
    9999 *
    100100 * @param int $user_id User ID
    101  * @return bool|object False on failure, User DB row object
     101 * @return bool|object False on failure, WP_User object on success
    102102 */
    103103function get_userdata( $user_id ) {
     104    return get_user_by( 'id', $user_id );
     105}
     106endif;
     107
     108if ( !function_exists('get_user_by') ) :
     109/**
     110 * Retrieve user info by a given field
     111 *
     112 * @since 2.8.0
     113 *
     114 * @param string $field The field to retrieve the user with.  id | slug | email | login
     115 * @param int|string $value A value for $field.  A user ID, slug, email address, or login name.
     116 * @return bool|object False on failure, WP_User object on success
     117 */
     118function get_user_by( $field, $value ) {
     119    $userdata = WP_User::get_data_by( $field, $value );
     120
     121    if ( !$userdata )
     122        return false;
     123
     124    $user = new WP_User;
     125    $user->init( $userdata );
     126
     127    return $user;
     128}
     129endif;
     130
     131if ( !function_exists('cache_users') ) :
     132/**
     133 * Retrieve info for user lists to prevent multiple queries by get_userdata()
     134 *
     135 * @since 3.0.0
     136 *
     137 * @param array $user_ids User ID numbers list
     138 */
     139function cache_users( $user_ids ) {
    104140    global $wpdb;
    105141
    106     if ( ! is_numeric( $user_id ) )
    107         return false;
    108 
    109     $user_id = absint( $user_id );
    110     if ( ! $user_id )
    111         return false;
    112 
    113     $user = wp_cache_get( $user_id, 'users' );
    114 
    115     if ( $user )
    116         return $user;
    117 
    118     if ( ! $user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d LIMIT 1", $user_id ) ) )
    119         return false;
    120 
    121     _fill_user( $user );
    122 
    123     return $user;
    124 }
    125 endif;
    126 
    127 if ( !function_exists('cache_users') ) :
    128 /**
    129  * Retrieve info for user lists to prevent multiple queries by get_userdata()
    130  *
    131  * @since 3.0.0
    132  *
    133  * @param array $users User ID numbers list
    134  */
    135 function cache_users( $users ) {
    136     global $wpdb;
    137 
    138142    $clean = array();
    139     foreach($users as $id) {
     143    foreach ( $user_ids as $id ) {
    140144        $id = (int) $id;
    141         if (wp_cache_get($id, 'users')) {
    142             // seems to be cached already
    143         } else {
     145        if ( !wp_cache_get( $id, 'users' ) ) {
    144146            $clean[] = $id;
    145147        }
    146148    }
    147149
    148     if ( 0 == count($clean) )
     150    if ( empty( $clean ) )
    149151        return;
    150152
    151     $list = implode(',', $clean);
    152 
    153     $results = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID IN ($list)");
    154 
    155     _fill_many_users($results);
    156 }
    157 endif;
    158 
    159 if ( !function_exists('get_user_by') ) :
    160 /**
    161  * Retrieve user info by a given field
    162  *
    163  * @since 2.8.0
    164  *
    165  * @param string $field The field to retrieve the user with.  id | slug | email | login
    166  * @param int|string $value A value for $field.  A user ID, slug, email address, or login name.
    167  * @return bool|object False on failure, User DB row object
    168  */
    169 function get_user_by($field, $value) {
    170     global $wpdb;
    171 
    172     switch ($field) {
    173         case 'id':
    174             return get_userdata($value);
    175             break;
    176         case 'slug':
    177             $user_id = wp_cache_get($value, 'userslugs');
    178             $field = 'user_nicename';
    179             break;
    180         case 'email':
    181             $user_id = wp_cache_get($value, 'useremail');
    182             $field = 'user_email';
    183             break;
    184         case 'login':
    185             $value = sanitize_user( $value );
    186             $user_id = wp_cache_get($value, 'userlogins');
    187             $field = 'user_login';
    188             break;
    189         default:
    190             return false;
    191     }
    192 
    193      if ( false !== $user_id )
    194         return get_userdata($user_id);
    195 
    196     if ( !$user = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE $field = %s", $value) ) )
    197         return false;
    198 
    199     _fill_user($user);
    200 
    201     return $user;
     153    $list = implode( ',', $clean );
     154
     155    $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" );
     156
     157    $ids = array();
     158    foreach ( $users as $user ) {
     159        update_user_caches( $user );
     160        $ids[] = $user->ID;
     161    }
     162    update_meta_cache( 'user', $ids );
    202163}
    203164endif;
Note: See TracChangeset for help on using the changeset viewer.