Changeset 42343 for trunk/src/wp-admin/includes/user.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-admin/includes/user.php (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/user.php
r42228 r42343 30 30 function edit_user( $user_id = 0 ) { 31 31 $wp_roles = wp_roles(); 32 $user = new stdClass;32 $user = new stdClass; 33 33 if ( $user_id ) { 34 $update = true;35 $user->ID = (int) $user_id;36 $userdata = get_userdata( $user_id );34 $update = true; 35 $user->ID = (int) $user_id; 36 $userdata = get_userdata( $user_id ); 37 37 $user->user_login = wp_slash( $userdata->user_login ); 38 38 } else { … … 40 40 } 41 41 42 if ( !$update && isset( $_POST['user_login'] ) ) 43 $user->user_login = sanitize_user($_POST['user_login'], true); 42 if ( ! $update && isset( $_POST['user_login'] ) ) { 43 $user->user_login = sanitize_user( $_POST['user_login'], true ); 44 } 44 45 45 46 $pass1 = $pass2 = ''; 46 if ( isset( $_POST['pass1'] ) ) 47 if ( isset( $_POST['pass1'] ) ) { 47 48 $pass1 = $_POST['pass1']; 48 if ( isset( $_POST['pass2'] ) ) 49 } 50 if ( isset( $_POST['pass2'] ) ) { 49 51 $pass2 = $_POST['pass2']; 52 } 50 53 51 54 if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) { 52 $new_role = sanitize_text_field( $_POST['role'] );53 $potential_role = isset( $wp_roles->role_objects[$new_role]) ? $wp_roles->role_objects[$new_role] : false;55 $new_role = sanitize_text_field( $_POST['role'] ); 56 $potential_role = isset( $wp_roles->role_objects[ $new_role ] ) ? $wp_roles->role_objects[ $new_role ] : false; 54 57 // Don't let anyone with 'edit_users' (admins) edit their own role to something without it. 55 58 // Multisite super admins can freely edit their blog roles -- they possess all caps. 56 if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ( $potential_role && $potential_role->has_cap( 'edit_users' ) ) )59 if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ( $potential_role && $potential_role->has_cap( 'edit_users' ) ) ) { 57 60 $user->role = $new_role; 61 } 58 62 59 63 // If the new role isn't editable by the logged-in user die with error 60 64 $editable_roles = get_editable_roles(); 61 if ( ! empty( $new_role ) && empty( $editable_roles[ $new_role] ) )65 if ( ! empty( $new_role ) && empty( $editable_roles[ $new_role ] ) ) { 62 66 wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 ); 63 } 64 65 if ( isset( $_POST['email'] )) 67 } 68 } 69 70 if ( isset( $_POST['email'] ) ) { 66 71 $user->user_email = sanitize_text_field( wp_unslash( $_POST['email'] ) ); 72 } 67 73 if ( isset( $_POST['url'] ) ) { 68 if ( empty ( $_POST['url'] ) || $_POST['url'] == 'http://' ) {74 if ( empty( $_POST['url'] ) || $_POST['url'] == 'http://' ) { 69 75 $user->user_url = ''; 70 76 } else { 71 77 $user->user_url = esc_url_raw( $_POST['url'] ); 72 $protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) );73 $user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url;74 } 75 } 76 if ( isset( $_POST['first_name'] ) ) 78 $protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) ); 79 $user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url ) ? $user->user_url : 'http://' . $user->user_url; 80 } 81 } 82 if ( isset( $_POST['first_name'] ) ) { 77 83 $user->first_name = sanitize_text_field( $_POST['first_name'] ); 78 if ( isset( $_POST['last_name'] ) ) 84 } 85 if ( isset( $_POST['last_name'] ) ) { 79 86 $user->last_name = sanitize_text_field( $_POST['last_name'] ); 80 if ( isset( $_POST['nickname'] ) ) 87 } 88 if ( isset( $_POST['nickname'] ) ) { 81 89 $user->nickname = sanitize_text_field( $_POST['nickname'] ); 82 if ( isset( $_POST['display_name'] ) ) 90 } 91 if ( isset( $_POST['display_name'] ) ) { 83 92 $user->display_name = sanitize_text_field( $_POST['display_name'] ); 84 85 if ( isset( $_POST['description'] ) ) 93 } 94 95 if ( isset( $_POST['description'] ) ) { 86 96 $user->description = trim( $_POST['description'] ); 97 } 87 98 88 99 foreach ( wp_get_user_contact_methods( $user ) as $method => $name ) { 89 if ( isset( $_POST[$method] )) 90 $user->$method = sanitize_text_field( $_POST[$method] ); 100 if ( isset( $_POST[ $method ] ) ) { 101 $user->$method = sanitize_text_field( $_POST[ $method ] ); 102 } 91 103 } 92 104 93 105 if ( $update ) { 94 $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' === $_POST['rich_editing'] ? 'false' : 'true';95 $user->syntax_highlighting = isset( $_POST['syntax_highlighting'] ) && 'false' === $_POST['syntax_highlighting'] ? 'false' : 'true';96 $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';106 $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' === $_POST['rich_editing'] ? 'false' : 'true'; 107 $user->syntax_highlighting = isset( $_POST['syntax_highlighting'] ) && 'false' === $_POST['syntax_highlighting'] ? 'false' : 'true'; 108 $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh'; 97 109 $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false'; 98 $user->locale = '';110 $user->locale = ''; 99 111 100 112 if ( isset( $_POST['locale'] ) ) { … … 115 127 116 128 $user->use_ssl = 0; 117 if ( ! empty($_POST['use_ssl']) )129 if ( ! empty( $_POST['use_ssl'] ) ) { 118 130 $user->use_ssl = 1; 131 } 119 132 120 133 $errors = new WP_Error(); 121 134 122 135 /* checking that username has been typed */ 123 if ( $user->user_login == '' ) 136 if ( $user->user_login == '' ) { 124 137 $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ) ); 138 } 125 139 126 140 /* checking that nickname has been typed */ … … 146 160 147 161 // Check for "\" in password. 148 if ( false !== strpos( wp_unslash( $pass1 ), "\\") ) {162 if ( false !== strpos( wp_unslash( $pass1 ), '\\' ) ) { 149 163 $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) ); 150 164 } … … 155 169 } 156 170 157 if ( ! empty( $pass1 ) )171 if ( ! empty( $pass1 ) ) { 158 172 $user->user_pass = $pass1; 159 160 if ( !$update && isset( $_POST['user_login'] ) && !validate_username( $_POST['user_login'] ) ) 161 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' )); 162 163 if ( !$update && username_exists( $user->user_login ) ) 164 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' )); 173 } 174 175 if ( ! $update && isset( $_POST['user_login'] ) && ! validate_username( $_POST['user_login'] ) ) { 176 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); 177 } 178 179 if ( ! $update && username_exists( $user->user_login ) ) { 180 $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) ); 181 } 165 182 166 183 /** This filter is documented in wp-includes/user.php */ … … 174 191 if ( empty( $user->user_email ) ) { 175 192 $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an email address.' ), array( 'form-field' => 'email' ) ); 176 } elseif ( ! is_email( $user->user_email ) ) {193 } elseif ( ! is_email( $user->user_email ) ) { 177 194 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ), array( 'form-field' => 'email' ) ); 178 } elseif ( ( $owner_id = email_exists( $user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) {179 $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) );195 } elseif ( ( $owner_id = email_exists( $user->user_email ) ) && ( ! $update || ( $owner_id != $user->ID ) ) ) { 196 $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ), array( 'form-field' => 'email' ) ); 180 197 } 181 198 … … 191 208 do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) ); 192 209 193 if ( $errors->get_error_codes() ) 210 if ( $errors->get_error_codes() ) { 194 211 return $errors; 212 } 195 213 196 214 if ( $update ) { … … 256 274 $user = get_userdata( $user_id ); 257 275 258 if ( $user ) 276 if ( $user ) { 259 277 $user->filter = 'edit'; 278 } 260 279 261 280 return $user; … … 274 293 function get_users_drafts( $user_id ) { 275 294 global $wpdb; 276 $query = $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id);295 $query = $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id ); 277 296 278 297 /** … … 310 329 } 311 330 312 $id = (int) $id;331 $id = (int) $id; 313 332 $user = new WP_User( $id ); 314 333 315 if ( ! $user->exists() )334 if ( ! $user->exists() ) { 316 335 return false; 336 } 317 337 318 338 // Normalize $reassign to null or a user ID. 'novalue' was an older default. … … 354 374 $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id ); 355 375 $post_types_to_delete = implode( "', '", $post_types_to_delete ); 356 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) );376 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) ); 357 377 if ( $post_ids ) { 358 foreach ( $post_ids as $post_id ) 378 foreach ( $post_ids as $post_id ) { 359 379 wp_delete_post( $post_id ); 380 } 360 381 } 361 382 362 383 // Clean links 363 $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) );384 $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); 364 385 365 386 if ( $link_ids ) { 366 foreach ( $link_ids as $link_id ) 367 wp_delete_link($link_id); 387 foreach ( $link_ids as $link_id ) { 388 wp_delete_link( $link_id ); 389 } 368 390 } 369 391 } else { 370 392 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) ); 371 $wpdb->update( $wpdb->posts, array( 'post_author' => $reassign), array('post_author' => $id) );393 $wpdb->update( $wpdb->posts, array( 'post_author' => $reassign ), array( 'post_author' => $id ) ); 372 394 if ( ! empty( $post_ids ) ) { 373 foreach ( $post_ids as $post_id ) 395 foreach ( $post_ids as $post_id ) { 374 396 clean_post_cache( $post_id ); 375 } 376 $link_ids = $wpdb->get_col( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id) ); 377 $wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) ); 397 } 398 } 399 $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) ); 400 $wpdb->update( $wpdb->links, array( 'link_owner' => $reassign ), array( 'link_owner' => $id ) ); 378 401 if ( ! empty( $link_ids ) ) { 379 foreach ( $link_ids as $link_id ) 402 foreach ( $link_ids as $link_id ) { 380 403 clean_bookmark_cache( $link_id ); 404 } 381 405 } 382 406 } … … 387 411 } else { 388 412 $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) ); 389 foreach ( $meta as $mid ) 413 foreach ( $meta as $mid ) { 390 414 delete_metadata_by_mid( 'user', $mid ); 415 } 391 416 392 417 $wpdb->delete( $wpdb->users, array( 'ID' => $id ) ); … … 416 441 * @param int $id User ID. 417 442 */ 418 function wp_revoke_user( $id) {443 function wp_revoke_user( $id ) { 419 444 $id = (int) $id; 420 445 421 $user = new WP_User( $id);446 $user = new WP_User( $id ); 422 447 $user->remove_all_caps(); 423 448 } … … 430 455 * @param false $errors Deprecated. 431 456 */ 432 function default_password_nag_handler( $errors = false) {457 function default_password_nag_handler( $errors = false ) { 433 458 global $user_ID; 434 459 // Short-circuit it. 435 if ( ! get_user_option( 'default_password_nag') )460 if ( ! get_user_option( 'default_password_nag' ) ) { 436 461 return; 462 } 437 463 438 464 // get_user_setting = JS saved UI setting. else no-js-fallback code. 439 if ( 'hide' == get_user_setting( 'default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) {440 delete_user_setting( 'default_password_nag');441 update_user_option( $user_ID, 'default_password_nag', false, true);465 if ( 'hide' == get_user_setting( 'default_password_nag' ) || isset( $_GET['default_password_nag'] ) && '0' == $_GET['default_password_nag'] ) { 466 delete_user_setting( 'default_password_nag' ); 467 update_user_option( $user_ID, 'default_password_nag', false, true ); 442 468 } 443 469 } … … 449 475 * @param object $old_data 450 476 */ 451 function default_password_nag_edit_user( $user_ID, $old_data) {477 function default_password_nag_edit_user( $user_ID, $old_data ) { 452 478 // Short-circuit it. 453 if ( ! get_user_option( 'default_password_nag', $user_ID) )479 if ( ! get_user_option( 'default_password_nag', $user_ID ) ) { 454 480 return; 455 456 $new_data = get_userdata($user_ID); 481 } 482 483 $new_data = get_userdata( $user_ID ); 457 484 458 485 // Remove the nag if the password has been changed. 459 486 if ( $new_data->user_pass != $old_data->user_pass ) { 460 delete_user_setting( 'default_password_nag');461 update_user_option( $user_ID, 'default_password_nag', false, true);487 delete_user_setting( 'default_password_nag' ); 488 update_user_option( $user_ID, 'default_password_nag', false, true ); 462 489 } 463 490 } … … 471 498 global $pagenow; 472 499 // Short-circuit it. 473 if ( 'profile.php' == $pagenow || ! get_user_option( 'default_password_nag') )500 if ( 'profile.php' == $pagenow || ! get_user_option( 'default_password_nag' ) ) { 474 501 return; 502 } 475 503 476 504 echo '<div class="error default-password-nag">'; 477 505 echo '<p>'; 478 echo '<strong>' . __( 'Notice:') . '</strong> ';479 _e( 'You’re using the auto-generated password for your account. Would you like to change it?');506 echo '<strong>' . __( 'Notice:' ) . '</strong> '; 507 _e( 'You’re using the auto-generated password for your account. Would you like to change it?' ); 480 508 echo '</p><p>'; 481 printf( '<a href="%s">' . __( 'Yes, take me to my profile page') . '</a> | ', get_edit_profile_url() . '#password' );482 printf( '<a href="%s" id="default-password-nag-no">' . __( 'No thanks, do not remind me again') . '</a>', '?default_password_nag=0' );509 printf( '<a href="%s">' . __( 'Yes, take me to my profile page' ) . '</a> | ', get_edit_profile_url() . '#password' ); 510 printf( '<a href="%s" id="default-password-nag-no">' . __( 'No thanks, do not remind me again' ) . '</a>', '?default_password_nag=0' ); 483 511 echo '</p></div>'; 484 512 } … … 488 516 * @access private 489 517 */ 490 function delete_users_add_js() { ?> 518 function delete_users_add_js() { 519 ?> 491 520 <script> 492 521 jQuery(document).ready( function($) { … … 512 541 * @param object $user User data object 513 542 */ 514 function use_ssl_preference( $user) {543 function use_ssl_preference( $user ) { 515 544 ?> 516 545 <tr class="user-use-ssl-wrap"> 517 <th scope="row"><?php _e( 'Use https')?></th>518 <td><label for="use_ssl"><input name="use_ssl" type="checkbox" id="use_ssl" value="1" <?php checked( '1', $user->use_ssl); ?> /> <?php _e('Always use https when visiting the admin'); ?></label></td>546 <th scope="row"><?php _e( 'Use https' ); ?></th> 547 <td><label for="use_ssl"><input name="use_ssl" type="checkbox" id="use_ssl" value="1" <?php checked( '1', $user->use_ssl ); ?> /> <?php _e( 'Always use https when visiting the admin' ); ?></label></td> 519 548 </tr> 520 549 <?php … … 522 551 523 552 /** 524 *525 553 * @param string $text 526 554 * @return string … … 528 556 function admin_created_user_email( $text ) { 529 557 $roles = get_editable_roles(); 530 $role = $roles[ $_REQUEST['role'] ];558 $role = $roles[ $_REQUEST['role'] ]; 531 559 /* translators: 1: Site name, 2: site URL, 3: role */ 532 return sprintf( __( 'Hi, 560 return sprintf( 561 __( 562 'Hi, 533 563 You\'ve been invited to join \'%1$s\' at 534 564 %2$s with the role of %3$s. … … 537 567 538 568 Please click the following link to activate your user account: 539 %%s' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), home_url(), wp_specialchars_decode( translate_user_role( $role['name'] ) ) ); 540 } 569 %%s' 570 ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), home_url(), wp_specialchars_decode( translate_user_role( $role['name'] ) ) 571 ); 572 }
Note: See TracChangeset
for help on using the changeset viewer.