function generatePassword() { if ( typeof zxcvbn !== 'function' ) { setTimeout( generatePassword, 50 ); return; } else if ( ! $pass1.val() || $passwordWrapper.hasClass( 'is-open' ) ) { // zxcvbn loaded before user entered password, or generating new password. $pass1.val( $pass1.data( 'pw' ) ); $pass1.trigger( 'pwupdate' ); showOrHideWeakPasswordCheckbox(); } else { // zxcvbn loaded after the user entered password, check strength. check_pass_strength(); showOrHideWeakPasswordCheckbox(); }
/* * This works around a race condition when zxcvbn loads quickly and * causes `generatePassword()` to run prior to the toggle button being * bound. */ bindToggleButton();
// Install screen. if ( 1 !== parseInt( $toggleButton.data( 'start-masked' ), 10 ) ) { // Show the password not masked if admin_password hasn't been posted yet. $pass1.attr( 'type', 'text' ); } else { // Otherwise, mask the password. $toggleButton.trigger( 'click' ); }
// Once zxcvbn loads, passwords strength is known. $( '#pw-weak-text-label' ).text( __( 'Confirm use of weak password' ) );
// Focus the password field. if ( 'mailserver_pass' !== $pass1.prop('id' ) ) { $( $pass1 ).trigger( 'focus' ); } }
function bindPass1() { currentPass = $pass1.val();
$pass1.on( 'input' + ' pwupdate', function () { if ( $pass1.val() === currentPass ) { return; }
currentPass = $pass1.val();
// Refresh password strength area. $pass1.removeClass( 'short bad good strong' ); showOrHideWeakPasswordCheckbox(); } ); }
function resetToggle( show ) { $toggleButton .attr({ 'aria-label': show ? __( 'Show password' ) : __( 'Hide password' ) }) .find( '.text' ) .text( show ? __( 'Show' ) : __( 'Hide' ) ) .end() .find( '.dashicons' ) .removeClass( show ? 'dashicons-hidden' : 'dashicons-visibility' ) .addClass( show ? 'dashicons-visibility' : 'dashicons-hidden' ); }
function bindToggleButton() { if ( !! $toggleButton ) { // Do not rebind. return; } $toggleButton = $pass1Row.find('.wp-hide-pw'); $toggleButton.show().on( 'click', function () { if ( 'password' === $pass1.attr( 'type' ) ) { $pass1.attr( 'type', 'text' ); resetToggle( false ); } else { $pass1.attr( 'type', 'password' ); resetToggle( true ); } }); }
/** * Handle the password reset button. Sets up an ajax callback to trigger sending * a password reset email. */ function bindPasswordResetLink() { $( '#generate-reset-link' ).on( 'click', function() { var $this = $(this), data = { 'user_id': userProfileL10n.user_id, // The user to send a reset to. 'nonce': userProfileL10n.nonce // Nonce to validate the action. };
// Remove any previous error messages. $this.parent().find( '.notice-error' ).remove();
// Send the reset request. var resetAction = wp.ajax.post( 'send-password-reset', data );
/** * Helper function to insert an inline notice of success or failure. * * @param {jQuery Object} $this The button element: the message will be inserted * above this button * @param {bool} success Whether the message is a success message. * @param {string} message The message to insert. */ function addInlineNotice( $this, success, message ) { var resultDiv = $( '<div />', { role: 'alert' } );
// Set up the notice div. resultDiv.addClass( 'notice inline' );
// Add a class indicating success or failure. resultDiv.addClass( 'notice-' + ( success ? 'success' : 'error' ) );
// Add the message, wrapping in a p tag, with a fadein to highlight each message. resultDiv.text( $( $.parseHTML( message ) ).text() ).wrapInner( '<p />');
// Disable the button when the callback has succeeded. $this.prop( 'disabled', success );
// Remove any previous notices. $this.siblings( '.notice' ).remove();
// Insert the notice. $this.before( resultDiv ); }
function bindPasswordForm() { var $generateButton, $cancelButton;
$pass1 = $('#pass1, #mailserver_pass'); if ( $pass1.length ) { bindPass1(); } else { // Password field for the login form. $pass1 = $( '#user_pass' ); }
/* * Fix a LastPass mismatch issue, LastPass only changes pass2. * * This fixes the issue by copying any changes from the hidden * pass2 field to the pass1 field, then running check_pass_strength. */ $pass2 = $( '#pass2' ).on( 'input', function () { if ( $pass2.val().length > 0 ) { $pass1.val( $pass2.val() ); $pass2.val(''); currentPass = ''; $pass1.trigger( 'pwupdate' ); } } );
// Disable hidden inputs to prevent autofill and submission. if ( $pass1.is( ':hidden' ) ) { $pass1.prop( 'disabled', true ); $pass2.prop( 'disabled', true ); }
/** * Replaces "Howdy, *" in the admin toolbar whenever the display name dropdown is updated for one's own profile. */ select.on( 'change', function() { if ( user_id !== current_user_id ) { return; }
var display_name = this.value.trim() || current_name;
// Set color scheme. if ( user_id === current_user_id ) { // Load the colors stylesheet. // The default color scheme won't have one, so we'll need to create an element. if ( 0 === $stylesheet.length ) { $stylesheet = $( '<link rel="stylesheet" />' ).appendTo( 'head' ); } $stylesheet.attr( 'href', $this.children( '.css_url' ).val() );
// Warn the user if password was generated but not saved. $( window ).on( 'beforeunload', function () { if ( true === updateLock ) { return __( 'Your new password has not been saved.' ); } if ( originalFormContent !== $form.serialize() && ! isSubmitting ) { return __( 'The changes you made will be lost if you navigate away from this page.' ); } });
/* * We need to generate a password as soon as the Reset Password page is loaded, * to avoid double clicking the button to retrieve the first generated password. * See ticket #39638. */ $( function() { if ( $( '.reset-pass-submit' ).length ) { $( '.reset-pass-submit button.wp-generate-pw' ).trigger( 'click' ); } });