Ticket #41314: #41314.patch
File #41314.patch, 7.2 KB (added by , 7 years ago) |
---|
-
src/wp-admin/admin-ajax.php
diff --git a/src/wp-admin/admin-ajax.php b/src/wp-admin/admin-ajax.php index 3213d55028..f7621a6324 100644
a b $core_actions_post = array( 64 64 'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post', 65 65 'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin', 66 66 'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme', 67 'install-theme', 'get-post-thumbnail-html', 'get-community-events', 67 'install-theme', 'get-post-thumbnail-html', 'get-community-events','validate-user-email', 68 68 ); 69 69 70 70 // Deprecated -
src/wp-admin/includes/ajax-actions.php
diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 0008ed1843..1e1cccd4e1 100644
a b function wp_ajax_search_install_plugins() { 3982 3982 3983 3983 wp_send_json_success( $status ); 3984 3984 } 3985 3986 /** 3987 * Ajax handler for validating user profile's data. 3988 * 3989 * @since 4.8.0 3990 */ 3991 function wp_ajax_validate_user_email() { 3992 if ( ! current_user_can( 'edit_users' ) ) 3993 wp_die( -1 ); 3994 3995 require_once ABSPATH . 'wp-admin/includes/user.php'; 3996 3997 _wp_ajax_validate_user_email( $_POST ); 3998 3999 wp_die(); 4000 } -
src/wp-admin/includes/user.php
diff --git a/src/wp-admin/includes/user.php b/src/wp-admin/includes/user.php index 0896a19593..fa83a8e392 100644
a b function edit_user( $user_id = 0 ) { 213 213 } 214 214 215 215 /** 216 * Validate the user's email entered in their profile 217 * 218 * @since 4.9.0 219 * 220 * @return json object 221 */ 222 function _wp_ajax_validate_user_email( $request = array() ){ 223 224 if ( isset( $request['email-address'] )) 225 $user_email = sanitize_text_field( wp_unslash( $request['email-address'] ) ); 226 227 if ( isset( $request['user-id'] ) ) 228 $user_id = $request['user-id']; 229 230 /* checking email address */ 231 if ( empty( $user_email ) ) { 232 $error = __( '<strong>ERROR</strong>: Please enter an email address.' ); 233 } elseif ( !is_email( $user_email ) ) { 234 $error = __( '<strong>ERROR</strong>: The email address isn’t correct.' ); 235 } elseif ( ( $owner_id = email_exists($user_email) ) && $owner_id != $user_id ) { 236 $error = __('<strong>ERROR</strong>: This email is already registered, please choose another one.'); 237 } 238 239 echo wp_json_encode( 240 array( 241 'validated' => isset( $error ) ? false : true, 242 'error' => isset( $error ) ? $error : 'noerror' 243 ) 244 ); 245 246 } 247 248 /** 216 249 * Fetch a filtered list of user roles that the current user is 217 250 * allowed to edit. 218 251 * -
new file src/wp-admin/js/user-profile-validator.js
diff --git a/src/wp-admin/js/user-profile-validator.js b/src/wp-admin/js/user-profile-validator.js new file mode 100644 index 0000000000..ac444bd98e
- + 1 (function($){ 2 3 // Bind an action to form submission 4 $( 'form' ).on( 'submit', validationHandler ); 5 6 function validationHandler ( e ){ 7 8 // Prevent the from from being submitted 9 e.preventDefault(); 10 11 // Set the default values for error indicators 12 var nicknameError = passwordError = false; 13 14 // Check if nickname is empty 15 if ( ! $('#nickname').val() ){ 16 17 // Print an error message if there isn't already one 18 if ( ! $('#nickname-error').length ) 19 $( '#your-profile' ).before( '<div id="nickname-error" class="error"><p>' + userProfileValidatorL10n.empty_nickname + '</p></div>' ); 20 21 // Scroll up to the message 22 $( 'html,body' ).animate({ 23 scrollTop:0 24 },0 25 ); 26 27 // The nickname has error 28 nicknameError = true; 29 } else { 30 31 // If the nickname is fine, remove any existing error 32 if ( $('#nickname-error').length ) $('#nickname-error').remove(); 33 } 34 35 // If the password contains '\', disable the form submission 36 if ( $( '#pass1' ).val().includes("\\") ) { 37 38 // Print an error if there is not already one 39 if ( ! $('#password-error').length ) 40 $( '#your-profile' ).before( '<div id="password-error" class="error"><p>' + userProfileValidatorL10n.invalid_password + '</p></div>' ); 41 42 // Scroll to the message 43 $( 'html,body' ).animate({ 44 scrollTop:0 45 },0 46 ); 47 48 // There is an error in the password 49 passwordError = true; 50 51 } else { 52 53 // The password is fine, remove any existing error message 54 if( $('#password-error').length ) $('#password-error').remove(); 55 56 } 57 58 // Check if the email is valid by doing an AJAX request. Set the parameters for the request 59 params = { 60 'action': 'validate-user-email', // AJAX handler for verifying email 61 'email-address': $( '#email' ).val(), // Entered email address 62 'user-id': $('#user_id').val(), // Current user's ID 63 }; 64 65 // Send the request to the server 66 $.post( ajaxurl, params, function(resp) { 67 processAjaxEmailValidation(resp, nicknameError, passwordError); 68 },"json"); 69 }; 70 71 // Process the AJAX response and form submission 72 function processAjaxEmailValidation(resp, nicknameError, passwordError){ 73 74 // If the email is valid 75 if ( true == resp.validated ) { 76 77 // Remove current error message if any 78 if ( $('#email-error').length ) $('#email-error').remove(); 79 80 // If password and nickname are valid too, submit the form 81 if ( ! nicknameError && ! passwordError ) { 82 83 // Unbind the handler 84 $( 'form' ).off( 'submit', validationHandler ); 85 86 //Submit the form 87 $('form').submit(); 88 } 89 90 } else if ( false == resp.validated ) { // If the email is invalid, then print a proper error 91 92 // Remove existing error messages 93 if ( $('#email-error').length ) $('#email-error').remove(); 94 95 // Print the proper error 96 $( '#your-profile' ).before( '<div id="email-error" class="error"><p>' + resp.error + '</p></div>' ); 97 98 // Scroll to the message 99 $( 'html,body' ).animate({ 100 scrollTop:0 101 },0 102 ); 103 } 104 } 105 })(jQuery); 106 No newline at end of file -
src/wp-admin/user-edit.php
diff --git a/src/wp-admin/user-edit.php b/src/wp-admin/user-edit.php index 80cef618e5..344713d1dc 100644
a b elseif ( ! get_userdata( $user_id ) ) 24 24 wp_die( __('Invalid user ID.') ); 25 25 26 26 wp_enqueue_script('user-profile'); 27 wp_enqueue_script('user-profile-validator'); 27 28 28 29 if ( IS_PROFILE_PAGE ) { 29 30 $title = __( 'Profile' ); -
src/wp-includes/script-loader.php
diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 0058c5e956..737ab7c3f0 100644
a b function wp_default_scripts( &$scripts ) { 419 419 'ariaHide' => esc_attr__( 'Hide password' ), 420 420 ) ); 421 421 422 $scripts->add( 'user-profile-validator', "/wp-admin/js/user-profile-validator$suffix.js", array( 'jquery' ), false, 1 ); 423 did_action( 'init' ) && $scripts->localize( 'user-profile-validator', 'userProfileValidatorL10n', array( 424 'empty_nickname' => __( '<strong>ERROR</strong>: Please enter a nickname.' ), 425 'invalid_password' => __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), 426 ) ); 422 427 $scripts->add( 'language-chooser', "/wp-admin/js/language-chooser$suffix.js", array( 'jquery' ), false, 1 ); 423 428 424 429 $scripts->add( 'user-suggest', "/wp-admin/js/user-suggest$suffix.js", array( 'jquery-ui-autocomplete' ), false, 1 );