Changeset 42343 for trunk/src/wp-includes/user.php
- Timestamp:
- 11/30/2017 11:09:33 PM (9 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/user.php (modified) (76 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/user.php
r41714 r42343 32 32 */ 33 33 function wp_signon( $credentials = array(), $secure_cookie = '' ) { 34 if ( empty( $credentials) ) {34 if ( empty( $credentials ) ) { 35 35 $credentials = array(); // Back-compat for plugins passing an empty string. 36 36 37 if ( ! empty( $_POST['log']) )37 if ( ! empty( $_POST['log'] ) ) { 38 38 $credentials['user_login'] = $_POST['log']; 39 if ( ! empty($_POST['pwd']) ) 39 } 40 if ( ! empty( $_POST['pwd'] ) ) { 40 41 $credentials['user_password'] = $_POST['pwd']; 41 if ( ! empty($_POST['rememberme']) ) 42 } 43 if ( ! empty( $_POST['rememberme'] ) ) { 42 44 $credentials['remember'] = $_POST['rememberme']; 43 } 44 45 if ( !empty($credentials['remember']) ) 45 } 46 } 47 48 if ( ! empty( $credentials['remember'] ) ) { 46 49 $credentials['remember'] = true; 47 else50 } else { 48 51 $credentials['remember'] = false; 52 } 49 53 50 54 /** … … 63 67 do_action_ref_array( 'wp_authenticate', array( &$credentials['user_login'], &$credentials['user_password'] ) ); 64 68 65 if ( '' === $secure_cookie ) 69 if ( '' === $secure_cookie ) { 66 70 $secure_cookie = is_ssl(); 71 } 67 72 68 73 /** … … 73 78 * @param bool $secure_cookie Whether to use a secure sign-on cookie. 74 79 * @param array $credentials { 75 * Array of entered sign-on data.76 *77 * @type string $user_login Username.78 * @type string $user_password Password entered.80 * Array of entered sign-on data. 81 * 82 * @type string $user_login Username. 83 * @type string $user_password Password entered. 79 84 * @type bool $remember Whether to 'remember' the user. Increases the time 80 85 * that the cookie will be kept. Default false. 81 * }86 * } 82 87 */ 83 88 $secure_cookie = apply_filters( 'secure_signon_cookie', $secure_cookie, $credentials ); … … 86 91 $auth_secure_cookie = $secure_cookie; 87 92 88 add_filter( 'authenticate', 'wp_authenticate_cookie', 30, 3);89 90 $user = wp_authenticate( $credentials['user_login'], $credentials['user_password']);91 92 if ( is_wp_error( $user) ) {93 if ( $user->get_error_codes() == array( 'empty_username', 'empty_password') ) {94 $user = new WP_Error( '', '');93 add_filter( 'authenticate', 'wp_authenticate_cookie', 30, 3 ); 94 95 $user = wp_authenticate( $credentials['user_login'], $credentials['user_password'] ); 96 97 if ( is_wp_error( $user ) ) { 98 if ( $user->get_error_codes() == array( 'empty_username', 'empty_password' ) ) { 99 $user = new WP_Error( '', '' ); 95 100 } 96 101 … … 98 103 } 99 104 100 wp_set_auth_cookie( $user->ID, $credentials['remember'], $secure_cookie);105 wp_set_auth_cookie( $user->ID, $credentials['remember'], $secure_cookie ); 101 106 /** 102 107 * Fires after the user has successfully logged in. … … 121 126 * @return WP_User|WP_Error WP_User on success, WP_Error on failure. 122 127 */ 123 function wp_authenticate_username_password( $user, $username, $password) {128 function wp_authenticate_username_password( $user, $username, $password ) { 124 129 if ( $user instanceof WP_User ) { 125 130 return $user; 126 131 } 127 132 128 if ( empty( $username) || empty($password) ) {129 if ( is_wp_error( $user ) ) 133 if ( empty( $username ) || empty( $password ) ) { 134 if ( is_wp_error( $user ) ) { 130 135 return $user; 136 } 131 137 132 138 $error = new WP_Error(); 133 139 134 if ( empty($username) ) 135 $error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.')); 136 137 if ( empty($password) ) 138 $error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.')); 140 if ( empty( $username ) ) { 141 $error->add( 'empty_username', __( '<strong>ERROR</strong>: The username field is empty.' ) ); 142 } 143 144 if ( empty( $password ) ) { 145 $error->add( 'empty_password', __( '<strong>ERROR</strong>: The password field is empty.' ) ); 146 } 139 147 140 148 return $error; 141 149 } 142 150 143 $user = get_user_by('login', $username); 144 145 if ( !$user ) { 146 return new WP_Error( 'invalid_username', 151 $user = get_user_by( 'login', $username ); 152 153 if ( ! $user ) { 154 return new WP_Error( 155 'invalid_username', 147 156 __( '<strong>ERROR</strong>: Invalid username.' ) . 148 157 ' <a href="' . wp_lostpassword_url() . '">' . … … 162 171 */ 163 172 $user = apply_filters( 'wp_authenticate_user', $user, $password ); 164 if ( is_wp_error( $user) )173 if ( is_wp_error( $user ) ) { 165 174 return $user; 175 } 166 176 167 177 if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) { 168 return new WP_Error( 'incorrect_password', 178 return new WP_Error( 179 'incorrect_password', 169 180 sprintf( 170 181 /* translators: %s: user name */ … … 222 233 223 234 if ( ! $user ) { 224 return new WP_Error( 'invalid_email', 235 return new WP_Error( 236 'invalid_email', 225 237 __( '<strong>ERROR</strong>: Invalid email address.' ) . 226 238 ' <a href="' . wp_lostpassword_url() . '">' . … … 238 250 239 251 if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) { 240 return new WP_Error( 'incorrect_password', 252 return new WP_Error( 253 'incorrect_password', 241 254 sprintf( 242 255 /* translators: %s: email address */ … … 265 278 * @return WP_User|WP_Error WP_User on success, WP_Error on failure. 266 279 */ 267 function wp_authenticate_cookie( $user, $username, $password) {280 function wp_authenticate_cookie( $user, $username, $password ) { 268 281 if ( $user instanceof WP_User ) { 269 282 return $user; 270 283 } 271 284 272 if ( empty( $username) && empty($password) ) {285 if ( empty( $username ) && empty( $password ) ) { 273 286 $user_id = wp_validate_auth_cookie(); 274 if ( $user_id ) 275 return new WP_User($user_id); 287 if ( $user_id ) { 288 return new WP_User( $user_id ); 289 } 276 290 277 291 global $auth_secure_cookie; 278 292 279 if ( $auth_secure_cookie ) 293 if ( $auth_secure_cookie ) { 280 294 $auth_cookie = SECURE_AUTH_COOKIE; 281 else295 } else { 282 296 $auth_cookie = AUTH_COOKIE; 283 284 if ( !empty($_COOKIE[$auth_cookie]) ) 285 return new WP_Error('expired_session', __('Please log in again.')); 297 } 298 299 if ( ! empty( $_COOKIE[ $auth_cookie ] ) ) { 300 return new WP_Error( 'expired_session', __( 'Please log in again.' ) ); 301 } 286 302 287 303 // If the cookie is not set, be silent. … … 312 328 $spammed = apply_filters( 'check_is_user_spammed', is_user_spammy( $user ), $user ); 313 329 314 if ( $spammed ) 330 if ( $spammed ) { 315 331 return new WP_Error( 'spammer_account', __( '<strong>ERROR</strong>: Your account has been marked as a spammer.' ) ); 332 } 316 333 } 317 334 return $user; … … 338 355 } 339 356 340 if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[ LOGGED_IN_COOKIE] ) ) {357 if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) { 341 358 return false; 342 359 } 343 360 344 return wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE], 'logged_in' );361 return wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' ); 345 362 } 346 363 … … 398 415 399 416 $count = array(); 400 if ( empty( $users ) || ! is_array( $users ) ) 417 if ( empty( $users ) || ! is_array( $users ) ) { 401 418 return $count; 419 } 402 420 403 421 $userlist = implode( ',', array_map( 'absint', $users ) ); 404 $where = get_posts_by_author_sql( $post_type, true, null, $public_only );422 $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); 405 423 406 424 $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); … … 410 428 411 429 foreach ( $users as $id ) { 412 if ( ! isset( $count[ $id ] ) ) 430 if ( ! isset( $count[ $id ] ) ) { 413 431 $count[ $id ] = 0; 432 } 414 433 } 415 434 … … 429 448 */ 430 449 function get_current_user_id() { 431 if ( ! function_exists( 'wp_get_current_user' ) ) 450 if ( ! function_exists( 'wp_get_current_user' ) ) { 432 451 return 0; 452 } 433 453 $user = wp_get_current_user(); 434 454 return ( isset( $user->ID ) ? (int) $user->ID : 0 ); … … 457 477 global $wpdb; 458 478 459 if ( ! empty( $deprecated ) )479 if ( ! empty( $deprecated ) ) { 460 480 _deprecated_argument( __FUNCTION__, '3.0.0' ); 461 462 if ( empty( $user ) ) 481 } 482 483 if ( empty( $user ) ) { 463 484 $user = get_current_user_id(); 464 465 if ( ! $user = get_userdata( $user ) ) 485 } 486 487 if ( ! $user = get_userdata( $user ) ) { 466 488 return false; 489 } 467 490 468 491 $prefix = $wpdb->get_blog_prefix(); 469 if ( $user->has_prop( $prefix . $option ) ) // Blog specific492 if ( $user->has_prop( $prefix . $option ) ) { // Blog specific 470 493 $result = $user->get( $prefix . $option ); 471 elseif ( $user->has_prop( $option ) )// User specific and cross-blog494 } elseif ( $user->has_prop( $option ) ) { // User specific and cross-blog 472 495 $result = $user->get( $option ); 473 else496 } else { 474 497 $result = false; 498 } 475 499 476 500 /** … … 512 536 global $wpdb; 513 537 514 if ( ! $global )538 if ( ! $global ) { 515 539 $option_name = $wpdb->get_blog_prefix() . $option_name; 540 } 516 541 517 542 return update_user_meta( $user_id, $option_name, $newvalue ); … … 538 563 global $wpdb; 539 564 540 if ( ! $global )565 if ( ! $global ) { 541 566 $option_name = $wpdb->get_blog_prefix() . $option_name; 567 } 542 568 return delete_user_meta( $user_id, $option_name ); 543 569 } … … 556 582 function get_users( $args = array() ) { 557 583 558 $args = wp_parse_args( $args );584 $args = wp_parse_args( $args ); 559 585 $args['count_total'] = false; 560 586 561 $user_search = new WP_User_Query( $args);587 $user_search = new WP_User_Query( $args ); 562 588 563 589 return (array) $user_search->get_results(); … … 584 610 585 611 // Logged out users can't have sites 586 if ( empty( $user_id ) ) 612 if ( empty( $user_id ) ) { 587 613 return array(); 614 } 588 615 589 616 /** … … 607 634 608 635 $keys = get_user_meta( $user_id ); 609 if ( empty( $keys ) ) 636 if ( empty( $keys ) ) { 610 637 return array(); 638 } 611 639 612 640 if ( ! is_multisite() ) { 613 $site_id = get_current_blog_id();614 $sites = array( $site_id => new stdClass );641 $site_id = get_current_blog_id(); 642 $sites = array( $site_id => new stdClass ); 615 643 $sites[ $site_id ]->userblog_id = $site_id; 616 $sites[ $site_id ]->blogname = get_option('blogname');617 $sites[ $site_id ]->domain = '';618 $sites[ $site_id ]->path = '';619 $sites[ $site_id ]->site_id = 1;620 $sites[ $site_id ]->siteurl = get_option('siteurl');621 $sites[ $site_id ]->archived = 0;622 $sites[ $site_id ]->spam = 0;623 $sites[ $site_id ]->deleted = 0;644 $sites[ $site_id ]->blogname = get_option( 'blogname' ); 645 $sites[ $site_id ]->domain = ''; 646 $sites[ $site_id ]->path = ''; 647 $sites[ $site_id ]->site_id = 1; 648 $sites[ $site_id ]->siteurl = get_option( 'siteurl' ); 649 $sites[ $site_id ]->archived = 0; 650 $sites[ $site_id ]->spam = 0; 651 $sites[ $site_id ]->deleted = 0; 624 652 return $sites; 625 653 } … … 635 663 636 664 foreach ( $keys as $key ) { 637 if ( 'capabilities' !== substr( $key, -12 ) ) 665 if ( 'capabilities' !== substr( $key, -12 ) ) { 638 666 continue; 639 if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) 667 } 668 if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) { 640 669 continue; 670 } 641 671 $site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key ); 642 if ( ! is_numeric( $site_id ) ) 672 if ( ! is_numeric( $site_id ) ) { 643 673 continue; 674 } 644 675 645 676 $site_ids[] = (int) $site_id; … … 767 798 * @return int|false Meta ID on success, false on failure. 768 799 */ 769 function add_user_meta( $user_id, $meta_key, $meta_value, $unique = false) {770 return add_metadata( 'user', $user_id, $meta_key, $meta_value, $unique);800 function add_user_meta( $user_id, $meta_key, $meta_value, $unique = false ) { 801 return add_metadata( 'user', $user_id, $meta_key, $meta_value, $unique ); 771 802 } 772 803 … … 786 817 * @return bool True on success, false on failure. 787 818 */ 788 function delete_user_meta( $user_id, $meta_key, $meta_value = '') {789 return delete_metadata( 'user', $user_id, $meta_key, $meta_value);819 function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) { 820 return delete_metadata( 'user', $user_id, $meta_key, $meta_value ); 790 821 } 791 822 … … 801 832 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. 802 833 */ 803 function get_user_meta( $user_id, $key = '', $single = false) {804 return get_metadata( 'user', $user_id, $key, $single);834 function get_user_meta( $user_id, $key = '', $single = false ) { 835 return get_metadata( 'user', $user_id, $key, $single ); 805 836 } 806 837 … … 822 853 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. 823 854 */ 824 function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '') {825 return update_metadata( 'user', $user_id, $meta_key, $meta_value, $prev_value);855 function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) { 856 return update_metadata( 'user', $user_id, $meta_key, $meta_value, $prev_value ); 826 857 } 827 858 … … 853 884 } 854 885 $blog_prefix = $wpdb->get_blog_prefix( $site_id ); 855 $result = array();886 $result = array(); 856 887 857 888 if ( 'time' == $strategy ) { … … 867 898 $select_count = array(); 868 899 foreach ( $avail_roles as $this_role => $name ) { 869 $select_count[] = $wpdb->prepare( "COUNT(NULLIF(`meta_value` LIKE %s, false))", '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%');900 $select_count[] = $wpdb->prepare( 'COUNT(NULLIF(`meta_value` LIKE %s, false))', '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%' ); 870 901 } 871 902 $select_count[] = "COUNT(NULLIF(`meta_value` = 'a:0:{}', false))"; 872 $select_count = implode(', ', $select_count);903 $select_count = implode( ', ', $select_count ); 873 904 874 905 // Add the meta_value index to the selection list, then run the query. 875 $row = $wpdb->get_row( " 906 $row = $wpdb->get_row( 907 " 876 908 SELECT {$select_count}, COUNT(*) 877 909 FROM {$wpdb->usermeta} 878 910 INNER JOIN {$wpdb->users} ON user_id = ID 879 911 WHERE meta_key = '{$blog_prefix}capabilities' 880 ", ARRAY_N ); 912 ", ARRAY_N 913 ); 881 914 882 915 // Run the previous loop again to associate results with role names. 883 $col = 0;916 $col = 0; 884 917 $role_counts = array(); 885 918 foreach ( $avail_roles as $this_role => $name ) { 886 $count = (int) $row[ $col++];887 if ( $count > 0) {888 $role_counts[ $this_role] = $count;919 $count = (int) $row[ $col++ ]; 920 if ( $count > 0 ) { 921 $role_counts[ $this_role ] = $count; 889 922 } 890 923 } 891 924 892 $role_counts['none'] = (int) $row[ $col++];925 $role_counts['none'] = (int) $row[ $col++ ]; 893 926 894 927 // Get the meta_value index from the end of the result set. 895 $total_users = (int) $row[ $col];928 $total_users = (int) $row[ $col ]; 896 929 897 930 $result['total_users'] = $total_users; … … 902 935 ); 903 936 904 $users_of_blog = $wpdb->get_col( " 937 $users_of_blog = $wpdb->get_col( 938 " 905 939 SELECT meta_value 906 940 FROM {$wpdb->usermeta} 907 941 INNER JOIN {$wpdb->users} ON user_id = ID 908 942 WHERE meta_key = '{$blog_prefix}capabilities' 909 " ); 943 " 944 ); 910 945 911 946 foreach ( $users_of_blog as $caps_meta ) { 912 $b_roles = maybe_unserialize( $caps_meta);913 if ( ! is_array( $b_roles ) ) 947 $b_roles = maybe_unserialize( $caps_meta ); 948 if ( ! is_array( $b_roles ) ) { 914 949 continue; 950 } 915 951 if ( empty( $b_roles ) ) { 916 952 $avail_roles['none']++; 917 953 } 918 954 foreach ( $b_roles as $b_role => $val ) { 919 if ( isset( $avail_roles[$b_role]) ) {920 $avail_roles[ $b_role]++;955 if ( isset( $avail_roles[ $b_role ] ) ) { 956 $avail_roles[ $b_role ]++; 921 957 } else { 922 $avail_roles[ $b_role] = 1;958 $avail_roles[ $b_role ] = 1; 923 959 } 924 960 } … … 953 989 * @param int $for_user_id Optional. User ID to set up global data. 954 990 */ 955 function setup_userdata( $for_user_id = '') {991 function setup_userdata( $for_user_id = '' ) { 956 992 global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_identity; 957 993 958 if ( '' == $for_user_id ) 994 if ( '' == $for_user_id ) { 959 995 $for_user_id = get_current_user_id(); 996 } 960 997 $user = get_userdata( $for_user_id ); 961 998 962 999 if ( ! $user ) { 963 $user_ID = 0;1000 $user_ID = 0; 964 1001 $user_level = 0; 965 $userdata = null;1002 $userdata = null; 966 1003 $user_login = $user_email = $user_url = $user_identity = ''; 967 1004 return; 968 1005 } 969 1006 970 $user_ID = (int) $user->ID;971 $user_level = (int) $user->user_level;972 $userdata = $user;973 $user_login = $user->user_login;974 $user_email = $user->user_email;975 $user_url = $user->user_url;1007 $user_ID = (int) $user->ID; 1008 $user_level = (int) $user->user_level; 1009 $userdata = $user; 1010 $user_login = $user->user_login; 1011 $user_email = $user->user_email; 1012 $user_url = $user->user_url; 976 1013 $user_identity = $user->display_name; 977 1014 } … … 1042 1079 function wp_dropdown_users( $args = '' ) { 1043 1080 $defaults = array( 1044 'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '', 1045 'orderby' => 'display_name', 'order' => 'ASC', 1046 'include' => '', 'exclude' => '', 'multi' => 0, 1047 'show' => 'display_name', 'echo' => 1, 1048 'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '', 1049 'blog_id' => get_current_blog_id(), 'who' => '', 'include_selected' => false, 1050 'option_none_value' => -1, 1051 'role' => '', 1052 'role__in' => array(), 1053 'role__not_in' => array(), 1081 'show_option_all' => '', 1082 'show_option_none' => '', 1083 'hide_if_only_one_author' => '', 1084 'orderby' => 'display_name', 1085 'order' => 'ASC', 1086 'include' => '', 1087 'exclude' => '', 1088 'multi' => 0, 1089 'show' => 'display_name', 1090 'echo' => 1, 1091 'selected' => 0, 1092 'name' => 'user', 1093 'class' => '', 1094 'id' => '', 1095 'blog_id' => get_current_blog_id(), 1096 'who' => '', 1097 'include_selected' => false, 1098 'option_none_value' => -1, 1099 'role' => '', 1100 'role__in' => array(), 1101 'role__not_in' => array(), 1054 1102 ); 1055 1103 … … 1071 1119 $query_args['fields'] = $fields; 1072 1120 1073 $show_option_all = $r['show_option_all'];1074 $show_option_none = $r['show_option_none'];1121 $show_option_all = $r['show_option_all']; 1122 $show_option_none = $r['show_option_none']; 1075 1123 $option_none_value = $r['option_none_value']; 1076 1124 … … 1103 1151 if ( $show_option_none ) { 1104 1152 $_selected = selected( $option_none_value, $r['selected'], false ); 1105 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";1153 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n"; 1106 1154 } 1107 1155 1108 1156 if ( $r['include_selected'] && ( $r['selected'] > 0 ) ) { 1109 1157 $found_selected = false; 1110 $r['selected'] = (int) $r['selected'];1158 $r['selected'] = (int) $r['selected']; 1111 1159 foreach ( (array) $users as $user ) { 1112 1160 $user->ID = (int) $user->ID; … … 1132 1180 1133 1181 $_selected = selected( $user->ID, $r['selected'], false ); 1134 $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";1135 } 1136 1137 $output .= "</select>";1182 $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n"; 1183 } 1184 1185 $output .= '</select>'; 1138 1186 } 1139 1187 … … 1169 1217 * @return mixed Sanitized value. 1170 1218 */ 1171 function sanitize_user_field( $field, $value, $user_id, $context) {1172 $int_fields = array( 'ID');1173 if ( in_array( $field, $int_fields) )1219 function sanitize_user_field( $field, $value, $user_id, $context ) { 1220 $int_fields = array( 'ID' ); 1221 if ( in_array( $field, $int_fields ) ) { 1174 1222 $value = (int) $value; 1175 1176 if ( 'raw' == $context ) 1223 } 1224 1225 if ( 'raw' == $context ) { 1177 1226 return $value; 1178 1179 if ( !is_string($value) && !is_numeric($value) ) 1227 } 1228 1229 if ( ! is_string( $value ) && ! is_numeric( $value ) ) { 1180 1230 return $value; 1231 } 1181 1232 1182 1233 $prefixed = false !== strpos( $field, 'user_' ); … … 1203 1254 } 1204 1255 1205 if ( 'description' == $field ) 1256 if ( 'description' == $field ) { 1206 1257 $value = esc_html( $value ); // textarea_escaped? 1207 else 1208 $value = esc_attr($value); 1258 } else { 1259 $value = esc_attr( $value ); 1260 } 1209 1261 } elseif ( 'db' == $context ) { 1210 1262 if ( $prefixed ) { … … 1218 1270 * The dynamic portion of the hook name, `$field`, refers to the prefixed user 1219 1271 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc. 1220 *1272 * 1221 1273 * @since 2.9.0 1222 1274 * … … 1249 1301 } 1250 1302 1251 if ( 'user_url' == $field ) 1252 $value = esc_url($value); 1303 if ( 'user_url' == $field ) { 1304 $value = esc_url( $value ); 1305 } 1253 1306 1254 1307 if ( 'attribute' == $context ) { … … 1277 1330 } 1278 1331 1279 wp_cache_add( $user->ID, $user, 'users');1280 wp_cache_add( $user->user_login, $user->ID, 'userlogins');1281 wp_cache_add( $user->user_email, $user->ID, 'useremail');1282 wp_cache_add( $user->user_nicename, $user->ID, 'userslugs');1332 wp_cache_add( $user->ID, $user, 'users' ); 1333 wp_cache_add( $user->user_login, $user->ID, 'userlogins' ); 1334 wp_cache_add( $user->user_email, $user->ID, 'useremail' ); 1335 wp_cache_add( $user->user_nicename, $user->ID, 'userslugs' ); 1283 1336 } 1284 1337 … … 1292 1345 */ 1293 1346 function clean_user_cache( $user ) { 1294 if ( is_numeric( $user ) ) 1347 if ( is_numeric( $user ) ) { 1295 1348 $user = new WP_User( $user ); 1296 1297 if ( ! $user->exists() ) 1349 } 1350 1351 if ( ! $user->exists() ) { 1298 1352 return; 1353 } 1299 1354 1300 1355 wp_cache_delete( $user->ID, 'users' ); … … 1349 1404 */ 1350 1405 function email_exists( $email ) { 1351 if ( $user = get_user_by( 'email', $email ) ) {1406 if ( $user = get_user_by( 'email', $email ) ) { 1352 1407 return $user->ID; 1353 1408 } … … 1366 1421 function validate_username( $username ) { 1367 1422 $sanitized = sanitize_user( $username, true ); 1368 $valid = ( $sanitized == $username && ! empty( $sanitized ) );1423 $valid = ( $sanitized == $username && ! empty( $sanitized ) ); 1369 1424 1370 1425 /** … … 1444 1499 // Are we updating or creating? 1445 1500 if ( ! empty( $userdata['ID'] ) ) { 1446 $ID = (int) $userdata['ID'];1447 $update = true;1501 $ID = (int) $userdata['ID']; 1502 $update = true; 1448 1503 $old_user_data = get_userdata( $ID ); 1449 1504 … … 1478 1533 // user_login must be between 0 and 60 characters. 1479 1534 if ( empty( $user_login ) ) { 1480 return new WP_Error( 'empty_user_login', __('Cannot create a user with an empty login name.') );1535 return new WP_Error( 'empty_user_login', __( 'Cannot create a user with an empty login name.' ) ); 1481 1536 } elseif ( mb_strlen( $user_login ) > 60 ) { 1482 1537 return new WP_Error( 'user_login_too_long', __( 'Username may not be longer than 60 characters.' ) ); … … 1636 1691 $meta['comment_shortcuts'] = empty( $userdata['comment_shortcuts'] ) || 'false' === $userdata['comment_shortcuts'] ? 'false' : 'true'; 1637 1692 1638 $admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];1693 $admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color']; 1639 1694 $meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $admin_color ); 1640 1695 … … 1647 1702 $meta['locale'] = isset( $userdata['locale'] ) ? $userdata['locale'] : ''; 1648 1703 1649 $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));1704 $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 ) ); 1650 1705 1651 1706 if ( $user_nicename_check ) { 1652 1707 $suffix = 2; 1653 while ( $user_nicename_check) {1708 while ( $user_nicename_check ) { 1654 1709 // user_nicename allows 50 chars. Subtract one for a hyphen, plus the length of the suffix. 1655 $base_length = 49 - mb_strlen( $suffix );1656 $alt_user_nicename = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix";1657 $user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));1710 $base_length = 49 - mb_strlen( $suffix ); 1711 $alt_user_nicename = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix"; 1712 $user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $alt_user_nicename, $user_login ) ); 1658 1713 $suffix++; 1659 1714 } … … 1662 1717 1663 1718 $compacted = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' ); 1664 $data = wp_unslash( $compacted );1719 $data = wp_unslash( $compacted ); 1665 1720 1666 1721 if ( ! $update ) { … … 1706 1761 1707 1762 /** 1708 * Filters a user's meta values and keys immediately after the user is created or updated1709 * and before any user meta is inserted or updated.1710 *1711 * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.1712 *1713 * @since 4.4.01714 *1715 * @param array $meta {1716 * Default meta values and keys for the user.1717 *1718 * @type string $nickname The user's nickname. Default is the user's username.1763 * Filters a user's meta values and keys immediately after the user is created or updated 1764 * and before any user meta is inserted or updated. 1765 * 1766 * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`. 1767 * 1768 * @since 4.4.0 1769 * 1770 * @param array $meta { 1771 * Default meta values and keys for the user. 1772 * 1773 * @type string $nickname The user's nickname. Default is the user's username. 1719 1774 * @type string $first_name The user's first name. 1720 1775 * @type string $last_name The user's last name. … … 1728 1783 * @type bool $show_admin_bar_front Whether to show the admin bar on the front end for the user. 1729 1784 * Default true. 1730 * }1785 * } 1731 1786 * @param WP_User $user User object. 1732 1787 * @param bool $update Whether the user is being updated rather than created. 1733 */1788 */ 1734 1789 $meta = apply_filters( 'insert_user_meta', $meta, $user, $update ); 1735 1790 … … 1748 1803 $user->set_role( $userdata['role'] ); 1749 1804 } elseif ( ! $update ) { 1750 $user->set_role( get_option('default_role'));1805 $user->set_role( get_option( 'default_role' ) ); 1751 1806 } 1752 1807 wp_cache_delete( $user_id, 'users' ); … … 1793 1848 * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated. 1794 1849 */ 1795 function wp_update_user( $userdata) {1850 function wp_update_user( $userdata ) { 1796 1851 if ( $userdata instanceof stdClass ) { 1797 1852 $userdata = get_object_vars( $userdata ); … … 1823 1878 if ( ! empty( $userdata['user_pass'] ) && $userdata['user_pass'] !== $user_obj->user_pass ) { 1824 1879 // If password is changing, hash it now 1825 $plaintext_pass = $userdata['user_pass'];1880 $plaintext_pass = $userdata['user_pass']; 1826 1881 $userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] ); 1827 1882 … … 1836 1891 * @param array $user The original user array. 1837 1892 * @param array $userdata The updated user array. 1838 *1839 1893 */ 1840 1894 $send_password_change_email = apply_filters( 'send_password_change_email', true, $user, $userdata ); … … 1852 1906 * @param array $user The original user array. 1853 1907 * @param array $userdata The updated user array. 1854 *1855 1908 */ 1856 1909 $send_email_change_email = apply_filters( 'send_email_change_email', true, $user, $userdata ); … … 1862 1915 // Merge old and new fields with new fields overwriting old ones. 1863 1916 $userdata = array_merge( $user, $userdata ); 1864 $user_id = wp_insert_user( $userdata );1917 $user_id = wp_insert_user( $userdata ); 1865 1918 1866 1919 if ( ! is_wp_error( $user_id ) ) { … … 1875 1928 if ( ! empty( $send_password_change_email ) ) { 1876 1929 /* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */ 1877 $pass_change_text = __( 'Hi ###USERNAME###, 1930 $pass_change_text = __( 1931 'Hi ###USERNAME###, 1878 1932 1879 1933 This notice confirms that your password was changed on ###SITENAME###. … … 1886 1940 Regards, 1887 1941 All at ###SITENAME### 1888 ###SITEURL###' ); 1942 ###SITEURL###' 1943 ); 1889 1944 1890 1945 $pass_change_email = array( … … 1916 1971 * @param array $user The original user array. 1917 1972 * @param array $userdata The updated user array. 1918 *1919 1973 */ 1920 1974 $pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata ); … … 1931 1985 if ( ! empty( $send_email_change_email ) ) { 1932 1986 /* translators: Do not translate USERNAME, ADMIN_EMAIL, NEW_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */ 1933 $email_change_text = __( 'Hi ###USERNAME###, 1987 $email_change_text = __( 1988 'Hi ###USERNAME###, 1934 1989 1935 1990 This notice confirms that your email address on ###SITENAME### was changed to ###NEW_EMAIL###. … … 1942 1997 Regards, 1943 1998 All at ###SITENAME### 1944 ###SITEURL###' ); 1999 ###SITEURL###' 2000 ); 1945 2001 1946 2002 $email_change_email = array( … … 1994 2050 $current_user = wp_get_current_user(); 1995 2051 if ( $current_user->ID == $ID ) { 1996 if ( isset( $plaintext_pass) ) {2052 if ( isset( $plaintext_pass ) ) { 1997 2053 wp_clear_auth_cookie(); 1998 2054 1999 2055 // Here we calculate the expiration length of the current auth cookie and compare it to the default expiration. 2000 2056 // If it's greater than this, then we know the user checked 'Remember Me' when they logged in. 2001 $logged_in_cookie = wp_parse_auth_cookie( '', 'logged_in' );2057 $logged_in_cookie = wp_parse_auth_cookie( '', 'logged_in' ); 2002 2058 /** This filter is documented in wp-includes/pluggable.php */ 2003 2059 $default_cookie_life = apply_filters( 'auth_cookie_expiration', ( 2 * DAY_IN_SECONDS ), $ID, false ); … … 2026 2082 * be created. 2027 2083 */ 2028 function wp_create_user( $username, $password, $email = '') {2084 function wp_create_user( $username, $password, $email = '' ) { 2029 2085 $user_login = wp_slash( $username ); 2030 $user_email = wp_slash( $email );2031 $user_pass = $password;2032 2033 $userdata = compact( 'user_login', 'user_email', 'user_pass');2034 return wp_insert_user( $userdata);2086 $user_email = wp_slash( $email ); 2087 $user_pass = $password; 2088 2089 $userdata = compact( 'user_login', 'user_email', 'user_pass' ); 2090 return wp_insert_user( $userdata ); 2035 2091 } 2036 2092 … … 2068 2124 'aim' => __( 'AIM' ), 2069 2125 'yim' => __( 'Yahoo IM' ), 2070 'jabber' => __( 'Jabber / Google Talk' ) 2126 'jabber' => __( 'Jabber / Google Talk' ), 2071 2127 ); 2072 2128 } … … 2078 2134 * 2079 2135 * @param array $methods Array of contact methods and their labels. 2080 * @param WP_User $user WP_User object.2136 * @param WP_User $user WP_User object. 2081 2137 */ 2082 2138 return apply_filters( 'user_contactmethods', $methods, $user ); … … 2193 2249 $wp_hasher = new PasswordHash( 8, true ); 2194 2250 } 2195 $hashed = time() . ':' . $wp_hasher->HashPassword( $key );2251 $hashed = time() . ':' . $wp_hasher->HashPassword( $key ); 2196 2252 $key_saved = $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) ); 2197 2253 if ( false === $key_saved ) { … … 2219 2275 * @return WP_User|WP_Error WP_User object on success, WP_Error object for invalid or expired keys. 2220 2276 */ 2221 function check_password_reset_key( $key, $login) {2277 function check_password_reset_key( $key, $login ) { 2222 2278 global $wpdb, $wp_hasher; 2223 2279 2224 $key = preg_replace('/[^a-z0-9]/i', '', $key); 2225 2226 if ( empty( $key ) || !is_string( $key ) ) 2227 return new WP_Error('invalid_key', __('Invalid key')); 2228 2229 if ( empty($login) || !is_string($login) ) 2230 return new WP_Error('invalid_key', __('Invalid key')); 2280 $key = preg_replace( '/[^a-z0-9]/i', '', $key ); 2281 2282 if ( empty( $key ) || ! is_string( $key ) ) { 2283 return new WP_Error( 'invalid_key', __( 'Invalid key' ) ); 2284 } 2285 2286 if ( empty( $login ) || ! is_string( $login ) ) { 2287 return new WP_Error( 'invalid_key', __( 'Invalid key' ) ); 2288 } 2231 2289 2232 2290 $row = $wpdb->get_row( $wpdb->prepare( "SELECT ID, user_activation_key FROM $wpdb->users WHERE user_login = %s", $login ) ); 2233 if ( ! $row ) 2234 return new WP_Error('invalid_key', __('Invalid key')); 2291 if ( ! $row ) { 2292 return new WP_Error( 'invalid_key', __( 'Invalid key' ) ); 2293 } 2235 2294 2236 2295 if ( empty( $wp_hasher ) ) { … … 2250 2309 if ( false !== strpos( $row->user_activation_key, ':' ) ) { 2251 2310 list( $pass_request_time, $pass_key ) = explode( ':', $row->user_activation_key, 2 ); 2252 $expiration_time = $pass_request_time + $expiration_duration;2311 $expiration_time = $pass_request_time + $expiration_duration; 2253 2312 } else { 2254 $pass_key = $row->user_activation_key;2313 $pass_key = $row->user_activation_key; 2255 2314 $expiration_time = false; 2256 2315 } … … 2270 2329 2271 2330 if ( hash_equals( $row->user_activation_key, $key ) || ( $hash_is_correct && ! $expiration_time ) ) { 2272 $return = new WP_Error( 'expired_key', __( 'Invalid key' ) );2331 $return = new WP_Error( 'expired_key', __( 'Invalid key' ) ); 2273 2332 $user_id = $row->ID; 2274 2333 … … 2403 2462 $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email ); 2404 2463 2405 if ( $errors->get_error_code() ) 2464 if ( $errors->get_error_code() ) { 2406 2465 return $errors; 2466 } 2407 2467 2408 2468 $user_pass = wp_generate_password( 12, false ); 2409 $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );2469 $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email ); 2410 2470 if ( ! $user_id || is_wp_error( $user_id ) ) { 2411 2471 $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn’t register you… please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) ); … … 2530 2590 } 2531 2591 2532 $regex = implode( '|', array_keys( $role_names ) ); 2533 $regex = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex ); 2534 $users = $wpdb->get_col( $wpdb->prepare( " 2592 $regex = implode( '|', array_keys( $role_names ) ); 2593 $regex = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex ); 2594 $users = $wpdb->get_col( 2595 $wpdb->prepare( 2596 " 2535 2597 SELECT user_id 2536 2598 FROM $wpdb->usermeta 2537 2599 WHERE meta_key = '{$prefix}capabilities' 2538 2600 AND meta_value NOT REGEXP %s 2539 ", $regex ) ); 2601 ", $regex 2602 ) 2603 ); 2540 2604 2541 2605 return $users; … … 2571 2635 // Upgrade stdClass to WP_User 2572 2636 if ( is_object( $current_user ) && isset( $current_user->ID ) ) { 2573 $cur_id = $current_user->ID;2637 $cur_id = $current_user->ID; 2574 2638 $current_user = null; 2575 2639 wp_set_current_user( $cur_id ); … … 2583 2647 } 2584 2648 2585 if ( defined( 'XMLRPC_REQUEST') && XMLRPC_REQUEST ) {2649 if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { 2586 2650 wp_set_current_user( 0 ); 2587 2651 return $current_user; … … 2635 2699 if ( $current_user->user_email != $_POST['email'] ) { 2636 2700 if ( ! is_email( $_POST['email'] ) ) { 2637 $errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address isn’t correct." ), array( 2638 'form-field' => 'email', 2639 ) ); 2701 $errors->add( 2702 'user_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ), array( 2703 'form-field' => 'email', 2704 ) 2705 ); 2640 2706 2641 2707 return; … … 2643 2709 2644 2710 if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_email FROM {$wpdb->users} WHERE user_email=%s", $_POST['email'] ) ) ) { 2645 $errors->add( 'user_email', __( "<strong>ERROR</strong>: The email address is already used." ), array( 2646 'form-field' => 'email', 2647 ) ); 2711 $errors->add( 2712 'user_email', __( '<strong>ERROR</strong>: The email address is already used.' ), array( 2713 'form-field' => 'email', 2714 ) 2715 ); 2648 2716 delete_user_meta( $current_user->ID, '_new_email' ); 2649 2717 … … 2665 2733 2666 2734 /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */ 2667 $email_text = __( 'Howdy ###USERNAME###, 2735 $email_text = __( 2736 'Howdy ###USERNAME###, 2668 2737 2669 2738 You recently requested to have the email address on your account changed. … … 2679 2748 Regards, 2680 2749 All at ###SITENAME### 2681 ###SITEURL###' ); 2750 ###SITEURL###' 2751 ); 2682 2752 2683 2753 /**
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)