Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r18178 r19209  
    6262
    6363    wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);
    64     do_action('wp_login', $credentials['user_login']);
     64    do_action('wp_login', $user->user_login, $user);
    6565    return $user;
    6666}
     
    8989
    9090    if ( !$userdata )
    91         return new WP_Error('invalid_username', sprintf(__('<strong>ERROR</strong>: Invalid username. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login')));
     91        return new WP_Error('invalid_username', sprintf(__('<strong>ERROR</strong>: Invalid username. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), wp_lostpassword_url()));
    9292
    9393    if ( is_multisite() ) {
     
    110110    if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) )
    111111        return new WP_Error( 'incorrect_password', sprintf( __( '<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s" title="Password Lost and Found">Lost your password</a>?' ),
    112         $username, site_url( 'wp-login.php?action=lostpassword', 'login' ) ) );
     112        $username, wp_lostpassword_url() ) );
    113113
    114114    $user =  new WP_User($userdata->ID);
     
    255255        _deprecated_argument( __FUNCTION__, '3.0' );
    256256
    257     if ( empty($user) ) {
     257    if ( empty( $user ) )
    258258        $user = wp_get_current_user();
    259         $user = $user->ID;
    260     }
    261 
    262     $user = get_userdata($user);
    263 
    264     // Keys used as object vars cannot have dashes.
    265     $key = str_replace('-', '', $option);
    266 
    267     if ( isset( $user->{$wpdb->prefix . $key} ) ) // Blog specific
    268         $result = $user->{$wpdb->prefix . $key};
    269     elseif ( isset( $user->{$key} ) ) // User specific and cross-blog
    270         $result = $user->{$key};
     259    else
     260        $user = new WP_User( $user );
     261
     262    if ( ! isset( $user->ID ) )
     263        return false;
     264
     265    if ( $user->has_prop( $wpdb->prefix . $option ) ) // Blog specific
     266        $result = $user->get( $wpdb->prefix . $option );
     267    elseif ( $user->has_prop( $option ) ) // User specific and cross-blog
     268        $result = $user->get( $option );
    271269    else
    272270        $result = false;
     
    648646 * @since 3.0.0
    649647 *
    650  * @param int $id User Id
    651  * @param bool $all Whether to retrieve all blogs or only blogs that are not marked as deleted, archived, or spam.
     648 * @param int $user_id User ID
     649 * @param bool $all Whether to retrieve all blogs, or only blogs that are not marked as deleted, archived, or spam.
    652650 * @return array A list of the user's blogs. False if the user was not found or an empty array if the user has no blogs.
    653651 */
    654 function get_blogs_of_user( $id, $all = false ) {
     652function get_blogs_of_user( $user_id, $all = false ) {
    655653    global $wpdb;
    656654
    657     if ( !is_multisite() ) {
     655    $user_id = (int) $user_id;
     656
     657    // Logged out users can't have blogs
     658    if ( empty( $user_id ) )
     659        return false;
     660
     661    $keys = get_user_meta( $user_id );
     662    if ( empty( $keys ) )
     663        return false;
     664
     665    if ( ! is_multisite() ) {
    658666        $blog_id = get_current_blog_id();
    659         $blogs = array();
     667        $blogs = array( $blog_id => new stdClass );
    660668        $blogs[ $blog_id ]->userblog_id = $blog_id;
    661669        $blogs[ $blog_id ]->blogname = get_option('blogname');
     
    667675    }
    668676
    669     $blogs = wp_cache_get( 'blogs_of_user-' . $id, 'users' );
    670 
    671     // Try priming the new cache from the old cache
    672     if ( false === $blogs ) {
    673         $cache_suffix = $all ? '_all' : '_short';
    674         $blogs = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' );
    675         if ( is_array( $blogs ) ) {
    676             $blogs = array_keys( $blogs );
    677             if ( $all )
    678                 wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' );
    679         }
    680     }
    681 
    682     if ( false === $blogs ) {
    683         $user = get_userdata( (int) $id );
    684         if ( !$user )
    685             return false;
    686 
    687         $blogs = $match = array();
    688         $prefix_length = strlen($wpdb->base_prefix);
    689         foreach ( (array) $user as $key => $value ) {
    690             if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
    691                 continue;
    692             if ( substr($key, -12, 12) != 'capabilities' )
    693                 continue;
    694             if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) {
    695                 if ( count( $match ) > 2 )
    696                     $blogs[] = (int) $match[ 2 ];
    697                 else
    698                     $blogs[] = 1;
    699             }
    700         }
    701         wp_cache_set( 'blogs_of_user-' . $id, $blogs, 'users' );
    702     }
    703 
    704     $blog_deets = array();
    705     foreach ( (array) $blogs as $blog_id ) {
     677    $blogs = array();
     678
     679    if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) {
     680        $blog = get_blog_details( 1 );
     681        if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
     682            $blogs[ 1 ] = (object) array(
     683                'userblog_id' => 1,
     684                'blogname'    => $blog->blogname,
     685                'domain'      => $blog->domain,
     686                'path'        => $blog->path,
     687                'site_id'     => $blog->site_id,
     688                'siteurl'     => $blog->siteurl,
     689            );
     690        }
     691        unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
     692    }
     693
     694    $keys = array_keys( $keys );
     695
     696    foreach ( $keys as $key ) {
     697        if ( 'capabilities' !== substr( $key, -12 ) )
     698            continue;
     699        if ( 0 !== strpos( $key, $wpdb->base_prefix ) )
     700            continue;
     701        $blog_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
     702        if ( ! is_numeric( $blog_id ) )
     703            continue;
     704
     705        $blog_id = (int) $blog_id;
    706706        $blog = get_blog_details( $blog_id );
    707         if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
    708             $blog_deets[ $blog_id ]->userblog_id    = $blog_id;
    709             $blog_deets[ $blog_id ]->blogname       = $blog->blogname;
    710             $blog_deets[ $blog_id ]->domain     = $blog->domain;
    711             $blog_deets[ $blog_id ]->path           = $blog->path;
    712             $blog_deets[ $blog_id ]->site_id        = $blog->site_id;
    713             $blog_deets[ $blog_id ]->siteurl        = $blog->siteurl;
    714         }
    715     }
    716 
    717     return apply_filters( 'get_blogs_of_user', $blog_deets, $id, $all );
    718 }
    719 
    720 /**
    721  * Checks if the current user belong to a given blog.
    722  *
    723  * @since 3.0.0
    724  *
    725  * @param int $blog_id Blog ID
    726  * @return bool True if the current users belong to $blog_id, false if not.
    727  */
    728 function is_blog_user( $blog_id = 0 ) {
    729     global $wpdb;
    730 
    731     $current_user = wp_get_current_user();
    732     if ( !$blog_id )
    733         $blog_id = $wpdb->blogid;
    734 
    735     $cap_key = $wpdb->base_prefix . $blog_id . '_capabilities';
    736 
    737     if ( is_array($current_user->$cap_key) && in_array(1, $current_user->$cap_key) )
    738         return true;
    739 
    740     return false;
     707        if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) {
     708            $blogs[ $blog_id ] = (object) array(
     709                'userblog_id' => $blog_id,
     710                'blogname'    => $blog->blogname,
     711                'domain'      => $blog->domain,
     712                'path'        => $blog->path,
     713                'site_id'     => $blog->site_id,
     714                'siteurl'     => $blog->siteurl,
     715            );
     716        }
     717    }
     718
     719    return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all );
     720}
     721
     722/**
     723 * Find out whether a user is a member of a given blog.
     724 *
     725 * @since MU 1.1
     726 * @uses get_blogs_of_user()
     727 *
     728 * @param int $user_id The unique ID of the user
     729 * @param int $blog Optional. If no blog_id is provided, current site is used
     730 * @return bool
     731 */
     732function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) {
     733    $user_id = (int) $user_id;
     734    $blog_id = (int) $blog_id;
     735
     736    if ( empty( $user_id ) )
     737        $user_id = get_current_user_id();
     738
     739    if ( empty( $blog_id ) )
     740        $blog_id = get_current_blog_id();
     741
     742    $blogs = get_blogs_of_user( $user_id );
     743    if ( is_array( $blogs ) )
     744        return array_key_exists( $blog_id, $blogs );
     745    else
     746        return false;
    741747}
    742748
     
    793799 *  is true.
    794800 */
    795 function get_user_meta($user_id, $key, $single = false) {
     801function get_user_meta($user_id, $key = '', $single = false) {
    796802    return get_metadata('user', $user_id, $key, $single);
    797803}
     
    10431049
    10441050/**
    1045  * Add user meta data as properties to given user object.
    1046  *
    1047  * The finished user data is cached, but the cache is not used to fill in the
    1048  * user data for the given object. Once the function has been used, the cache
    1049  * should be used to retrieve user data. The intention is if the current data
    1050  * had been cached already, there would be no need to call this function.
    1051  *
    1052  * @access private
    1053  * @since 2.5.0
    1054  * @uses $wpdb WordPress database object for queries
    1055  *
    1056  * @param object $user The user data object.
    1057  */
    1058 function _fill_user( &$user ) {
    1059     $metavalues = get_user_metavalues(array($user->ID));
    1060     _fill_single_user($user, $metavalues[$user->ID]);
    1061 }
    1062 
    1063 /**
    1064  * Perform the query to get the $metavalues array(s) needed by _fill_user and _fill_many_users
    1065  *
    1066  * @since 3.0.0
    1067  * @param array $ids User ID numbers list.
    1068  * @return array of arrays. The array is indexed by user_id, containing $metavalues object arrays.
    1069  */
    1070 function get_user_metavalues($ids) {
    1071     $objects = array();
    1072 
    1073     $ids = array_map('intval', $ids);
    1074     foreach ( $ids as $id )
    1075         $objects[$id] = array();
    1076 
    1077     $metas = update_meta_cache('user', $ids);
    1078 
    1079     foreach ( $metas as $id => $meta ) {
    1080         foreach ( $meta as $key => $metavalues ) {
    1081             foreach ( $metavalues as $value ) {
    1082                 $objects[$id][] = (object)array( 'user_id' => $id, 'meta_key' => $key, 'meta_value' => $value);
    1083             }
    1084         }
    1085     }
    1086 
    1087     return $objects;
    1088 }
    1089 
    1090 /**
    1091  * Unserialize user metadata, fill $user object, then cache everything.
    1092  *
    1093  * @since 3.0.0
    1094  * @param object $user The User object.
    1095  * @param array $metavalues An array of objects provided by get_user_metavalues()
    1096  */
    1097 function _fill_single_user( &$user, &$metavalues ) {
    1098     global $wpdb;
    1099 
    1100     foreach ( $metavalues as $meta ) {
    1101         $value = maybe_unserialize($meta->meta_value);
    1102         // Keys used as object vars cannot have dashes.
    1103         $key = str_replace('-', '', $meta->meta_key);
    1104         $user->{$key} = $value;
    1105     }
    1106 
    1107     $level = $wpdb->prefix . 'user_level';
    1108     if ( isset( $user->{$level} ) )
    1109         $user->user_level = $user->{$level};
    1110 
    1111     // For backwards compat.
    1112     if ( isset($user->first_name) )
    1113         $user->user_firstname = $user->first_name;
    1114     if ( isset($user->last_name) )
    1115         $user->user_lastname = $user->last_name;
    1116     if ( isset($user->description) )
    1117         $user->user_description = $user->description;
    1118 
    1119     update_user_caches($user);
    1120 }
    1121 
    1122 /**
    1123  * Take an array of user objects, fill them with metas, and cache them.
    1124  *
    1125  * @since 3.0.0
    1126  * @param array $users User objects
    1127  */
    1128 function _fill_many_users( &$users ) {
    1129     $ids = array();
    1130     foreach( $users as $user_object ) {
    1131         $ids[] = $user_object->ID;
    1132     }
    1133 
    1134     $metas = get_user_metavalues($ids);
    1135 
    1136     foreach ( $users as $user_object ) {
    1137         if ( isset($metas[$user_object->ID]) ) {
    1138             _fill_single_user($user_object, $metas[$user_object->ID]);
    1139         }
    1140     }
    1141 }
    1142 
    1143 /**
    1144  * Sanitize every user field.
    1145  *
    1146  * If the context is 'raw', then the user object or array will get minimal santization of the int fields.
    1147  *
    1148  * @since 2.3.0
    1149  * @uses sanitize_user_field() Used to sanitize the fields.
    1150  *
    1151  * @param object|array $user The User Object or Array
    1152  * @param string $context Optional, default is 'display'. How to sanitize user fields.
    1153  * @return object|array The now sanitized User Object or Array (will be the same type as $user)
    1154  */
    1155 function sanitize_user_object($user, $context = 'display') {
    1156     if ( is_object($user) ) {
    1157         if ( !isset($user->ID) )
    1158             $user->ID = 0;
    1159         if ( isset($user->data) )
    1160             $vars = get_object_vars( $user->data );
    1161         else
    1162             $vars = get_object_vars($user);
    1163         foreach ( array_keys($vars) as $field ) {
    1164             if ( is_string($user->$field) || is_numeric($user->$field) )
    1165                 $user->$field = sanitize_user_field($field, $user->$field, $user->ID, $context);
    1166         }
    1167         $user->filter = $context;
    1168     } else {
    1169         if ( !isset($user['ID']) )
    1170             $user['ID'] = 0;
    1171         foreach ( array_keys($user) as $field )
    1172             $user[$field] = sanitize_user_field($field, $user[$field], $user['ID'], $context);
    1173         $user['filter'] = $context;
    1174     }
    1175 
    1176     return $user;
    1177 }
    1178 
    1179 /**
    11801051 * Sanitize user field based on context.
    11811052 *
     
    12641135 * @param object $user User object to be cached
    12651136 */
    1266 function update_user_caches(&$user) {
     1137function update_user_caches($user) {
    12671138    wp_cache_add($user->ID, $user, 'users');
    12681139    wp_cache_add($user->user_login, $user->ID, 'userlogins');
     
    12791150 */
    12801151function clean_user_cache($id) {
    1281     $user = new WP_User($id);
     1152    $user = WP_User::get_data_by( 'id', $id );
    12821153
    12831154    wp_cache_delete($id, 'users');
     
    12851156    wp_cache_delete($user->user_email, 'useremail');
    12861157    wp_cache_delete($user->user_nicename, 'userslugs');
    1287     wp_cache_delete('blogs_of_user-' . $id, 'users');
    12881158}
    12891159
     
    12971167 */
    12981168function username_exists( $username ) {
    1299     if ( $user = get_userdatabylogin( $username ) ) {
     1169    if ( $user = get_user_by('login', $username ) ) {
    13001170        return $user->ID;
    13011171    } else {
     
    13141184 */
    13151185function email_exists( $email ) {
    1316     if ( $user = get_user_by_email($email) )
     1186    if ( $user = get_user_by('email', $email) )
    13171187        return $user->ID;
    13181188
     
    13901260        $ID = (int) $ID;
    13911261        $update = true;
    1392         $old_user_data = get_userdata($ID);
     1262        $old_user_data = WP_User::get_data_by( 'id', $ID );
    13931263    } else {
    13941264        $update = false;
     
    14621332    if ( empty($show_admin_bar_front) )
    14631333        $show_admin_bar_front = 'true';
    1464 
    1465     if ( empty($show_admin_bar_admin) )
    1466         $show_admin_bar_admin = is_multisite() ? 'true' : 'false';
    14671334
    14681335    $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
     
    14891356    }
    14901357
    1491     update_user_meta( $user_id, 'first_name', $first_name );
    1492     update_user_meta( $user_id, 'last_name', $last_name );
    1493     update_user_meta( $user_id, 'nickname', $nickname );
    1494     update_user_meta( $user_id, 'description', $description );
    1495     update_user_meta( $user_id, 'rich_editing', $rich_editing );
    1496     update_user_meta( $user_id, 'comment_shortcuts', $comment_shortcuts );
    1497     update_user_meta( $user_id, 'admin_color', $admin_color );
    1498     update_user_meta( $user_id, 'use_ssl', $use_ssl );
    1499     update_user_meta( $user_id, 'show_admin_bar_front', $show_admin_bar_front );
    1500     update_user_meta( $user_id, 'show_admin_bar_admin', $show_admin_bar_admin );
    1501 
    1502     $user = new WP_User($user_id);
    1503 
    1504     foreach ( _wp_get_user_contactmethods( $user ) as $method => $name ) {
    1505         if ( empty($$method) )
    1506             $$method = '';
    1507 
    1508         update_user_meta( $user_id, $method, $$method );
     1358    $user = new WP_User( $user_id );
     1359
     1360    foreach ( _get_additional_user_keys( $user ) as $key ) {
     1361        if ( isset( $$key ) )
     1362            update_user_meta( $user_id, $key, $$key );
    15091363    }
    15101364
     
    15481402
    15491403    // First, get all of the original fields
    1550     $user = get_userdata($ID);
     1404    $user_obj = get_userdata( $ID );
     1405
     1406    $user = get_object_vars( $user_obj->data );
     1407
     1408    // Add additional custom fields
     1409    foreach ( _get_additional_user_keys( $user_obj ) as $key ) {
     1410        $user[ $key ] = get_user_meta( $ID, $key, true );
     1411    }
    15511412
    15521413    // Escape data pulled from DB.
    1553     $user = add_magic_quotes(get_object_vars($user));
     1414    $user = add_magic_quotes( $user );
    15541415
    15551416    // If password is changing, hash it now.
     
    15671428    // Update the cookies if the password changed.
    15681429    $current_user = wp_get_current_user();
    1569     if ( $current_user->id == $ID ) {
     1430    if ( $current_user->ID == $ID ) {
    15701431        if ( isset($plaintext_pass) ) {
    15711432            wp_clear_auth_cookie();
     
    15801441 * A simpler way of inserting an user into the database.
    15811442 *
    1582  * Creates a new user with just the username, password, and email. For a more
    1583  * detail creation of a user, use wp_insert_user() to specify more infomation.
     1443 * Creates a new user with just the username, password, and email. For more
     1444 * complex user creation use wp_insert_user() to specify more information.
    15841445 *
    15851446 * @since 2.0.0
     
    16001461}
    16011462
     1463
     1464/**
     1465 * Return a list of meta keys that wp_insert_user() is supposed to set.
     1466 *
     1467 * @access private
     1468 * @since 3.3.0
     1469 *
     1470 * @param object $user WP_User instance
     1471 * @return array
     1472 */
     1473function _get_additional_user_keys( $user ) {
     1474    $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front' );
     1475    return array_merge( $keys, array_keys( _wp_get_user_contactmethods( $user ) ) );
     1476}
    16021477
    16031478/**
Note: See TracChangeset for help on using the changeset viewer.