Changes in trunk/wp-includes/user.php [18178:19209]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/user.php
r18178 r19209 62 62 63 63 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); 65 65 return $user; 66 66 } … … 89 89 90 90 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())); 92 92 93 93 if ( is_multisite() ) { … … 110 110 if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) ) 111 111 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() ) ); 113 113 114 114 $user = new WP_User($userdata->ID); … … 255 255 _deprecated_argument( __FUNCTION__, '3.0' ); 256 256 257 if ( empty( $user) ) {257 if ( empty( $user ) ) 258 258 $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 ); 271 269 else 272 270 $result = false; … … 648 646 * @since 3.0.0 649 647 * 650 * @param int $ id User Id651 * @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. 652 650 * @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. 653 651 */ 654 function get_blogs_of_user( $ id, $all = false ) {652 function get_blogs_of_user( $user_id, $all = false ) { 655 653 global $wpdb; 656 654 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() ) { 658 666 $blog_id = get_current_blog_id(); 659 $blogs = array( );667 $blogs = array( $blog_id => new stdClass ); 660 668 $blogs[ $blog_id ]->userblog_id = $blog_id; 661 669 $blogs[ $blog_id ]->blogname = get_option('blogname'); … … 667 675 } 668 676 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; 706 706 $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 */ 732 function 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; 741 747 } 742 748 … … 793 799 * is true. 794 800 */ 795 function get_user_meta($user_id, $key , $single = false) {801 function get_user_meta($user_id, $key = '', $single = false) { 796 802 return get_metadata('user', $user_id, $key, $single); 797 803 } … … 1043 1049 1044 1050 /** 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 the1048 * user data for the given object. Once the function has been used, the cache1049 * should be used to retrieve user data. The intention is if the current data1050 * had been cached already, there would be no need to call this function.1051 *1052 * @access private1053 * @since 2.5.01054 * @uses $wpdb WordPress database object for queries1055 *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_users1065 *1066 * @since 3.0.01067 * @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.01094 * @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.01126 * @param array $users User objects1127 */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.01149 * @uses sanitize_user_field() Used to sanitize the fields.1150 *1151 * @param object|array $user The User Object or Array1152 * @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 else1162 $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 /**1180 1051 * Sanitize user field based on context. 1181 1052 * … … 1264 1135 * @param object $user User object to be cached 1265 1136 */ 1266 function update_user_caches( &$user) {1137 function update_user_caches($user) { 1267 1138 wp_cache_add($user->ID, $user, 'users'); 1268 1139 wp_cache_add($user->user_login, $user->ID, 'userlogins'); … … 1279 1150 */ 1280 1151 function clean_user_cache($id) { 1281 $user = new WP_User($id);1152 $user = WP_User::get_data_by( 'id', $id ); 1282 1153 1283 1154 wp_cache_delete($id, 'users'); … … 1285 1156 wp_cache_delete($user->user_email, 'useremail'); 1286 1157 wp_cache_delete($user->user_nicename, 'userslugs'); 1287 wp_cache_delete('blogs_of_user-' . $id, 'users');1288 1158 } 1289 1159 … … 1297 1167 */ 1298 1168 function username_exists( $username ) { 1299 if ( $user = get_user databylogin($username ) ) {1169 if ( $user = get_user_by('login', $username ) ) { 1300 1170 return $user->ID; 1301 1171 } else { … … 1314 1184 */ 1315 1185 function email_exists( $email ) { 1316 if ( $user = get_user_by _email($email) )1186 if ( $user = get_user_by('email', $email) ) 1317 1187 return $user->ID; 1318 1188 … … 1390 1260 $ID = (int) $ID; 1391 1261 $update = true; 1392 $old_user_data = get_userdata($ID);1262 $old_user_data = WP_User::get_data_by( 'id', $ID ); 1393 1263 } else { 1394 1264 $update = false; … … 1462 1332 if ( empty($show_admin_bar_front) ) 1463 1333 $show_admin_bar_front = 'true'; 1464 1465 if ( empty($show_admin_bar_admin) )1466 $show_admin_bar_admin = is_multisite() ? 'true' : 'false';1467 1334 1468 1335 $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)); … … 1489 1356 } 1490 1357 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 ); 1509 1363 } 1510 1364 … … 1548 1402 1549 1403 // 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 } 1551 1412 1552 1413 // Escape data pulled from DB. 1553 $user = add_magic_quotes( get_object_vars($user));1414 $user = add_magic_quotes( $user ); 1554 1415 1555 1416 // If password is changing, hash it now. … … 1567 1428 // Update the cookies if the password changed. 1568 1429 $current_user = wp_get_current_user(); 1569 if ( $current_user-> id== $ID ) {1430 if ( $current_user->ID == $ID ) { 1570 1431 if ( isset($plaintext_pass) ) { 1571 1432 wp_clear_auth_cookie(); … … 1580 1441 * A simpler way of inserting an user into the database. 1581 1442 * 1582 * Creates a new user with just the username, password, and email. For amore1583 * 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. 1584 1445 * 1585 1446 * @since 2.0.0 … … 1600 1461 } 1601 1462 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 */ 1473 function _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 } 1602 1477 1603 1478 /**
Note: See TracChangeset
for help on using the changeset viewer.