]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-admin/js/user-profile.js
WordPress 4.7-scripts
[autoinstalls/wordpress.git] / wp-admin / js / user-profile.js
index 245d8738fd7e22bc08d4bd83896e6cb0701f7256..650fbd5d006d1f35e9f1840384d9b59bb414b6b7 100644 (file)
        function generatePassword() {
                if ( typeof zxcvbn !== 'function' ) {
                        setTimeout( generatePassword, 50 );
-               } else {
+                       return;
+               } else if ( ! $pass1.val() ) {
+                       // zxcvbn loaded before user entered password.
                        $pass1.val( $pass1.data( 'pw' ) );
-                       $pass1.trigger( 'pwupdate' ).trigger( 'wp-check-valid-field' );
-                       if ( 1 !== parseInt( $toggleButton.data( 'start-masked' ), 10 ) ) {
-                               $pass1Wrap.addClass( 'show-password' );
-                       } else {
-                               $toggleButton.trigger( 'click' );
-                       }
+                       $pass1.trigger( 'pwupdate' );
+                       showOrHideWeakPasswordCheckbox();
+               }
+               else {
+                       // zxcvbn loaded after the user entered password, check strength.
+                       check_pass_strength();
+                       showOrHideWeakPasswordCheckbox();
                }
+
+               if ( 1 !== parseInt( $toggleButton.data( 'start-masked' ), 10 ) ) {
+                       $pass1Wrap.addClass( 'show-password' );
+               } else {
+                       $toggleButton.trigger( 'click' );
+               }
+
+               // Once zxcvbn loads, passwords strength is known.
+               $( '#pw-weak-text-label' ).html( userProfileL10n.warnWeak );
        }
 
        function bindPass1() {
-               var passStrength = $('#pass-strength-result')[0];
-
                currentPass = $pass1.val();
 
                $pass1Wrap = $pass1.parent();
                                $pass1Text.val( currentPass );
                        }
                        $pass1.add( $pass1Text ).removeClass( 'short bad good strong' );
-
-                       if ( passStrength.className ) {
-                               $pass1.add( $pass1Text ).addClass( passStrength.className );
-                               if ( 'short' === passStrength.className || 'bad' === passStrength.className ) {
-                                       if ( ! $weakCheckbox.prop( 'checked' ) ) {
-                                               $submitButtons.prop( 'disabled', true );
-                                       }
-                                       $weakRow.show();
-                               } else {
-                                       $submitButtons.prop( 'disabled', false );
-                                       $weakRow.hide();
-                               }
-                       }
+                       showOrHideWeakPasswordCheckbox();
                } );
        }
 
+       function resetToggle() {
+               $toggleButton
+                       .data( 'toggle', 0 )
+                       .attr({
+                               'aria-label': userProfileL10n.ariaHide
+                       })
+                       .find( '.text' )
+                               .text( userProfileL10n.hide )
+                       .end()
+                       .find( '.dashicons' )
+                               .removeClass( 'dashicons-visibility' )
+                               .addClass( 'dashicons-hidden' );
+
+               $pass1Text.focus();
+
+               $pass1Label.attr( 'for', 'pass1-text' );
+       }
+
        function bindToggleButton() {
                $toggleButton = $pass1Row.find('.wp-hide-pw');
                $toggleButton.show().on( 'click', function () {
                        if ( 1 === parseInt( $toggleButton.data( 'toggle' ), 10 ) ) {
                                $pass1Wrap.addClass( 'show-password' );
-                               $toggleButton
-                                       .data( 'toggle', 0 )
-                                       .attr({
-                                               'aria-label': userProfileL10n.ariaHide
-                                       })
-                                       .find( '.text' )
-                                               .text( userProfileL10n.hide )
-                                       .end()
-                                       .find( '.dashicons' )
-                                               .removeClass('dashicons-visibility')
-                                               .addClass('dashicons-hidden');
 
-                               $pass1Text.focus();
-
-                               $pass1Label.attr( 'for', 'pass1-text' );
+                               resetToggle();
 
                                if ( ! _.isUndefined( $pass1Text[0].setSelectionRange ) ) {
                                        $pass1Text[0].setSelectionRange( 0, 100 );
                        }
                } );
 
-               $passwordWrapper = $pass1Row.find('.wp-pwd').hide();
+               // Disable hidden inputs to prevent autofill and submission.
+               if ( $pass1.is( ':hidden' ) ) {
+                       $pass1.prop( 'disabled', true );
+                       $pass2.prop( 'disabled', true );
+                       $pass1Text.prop( 'disabled', true );
+               }
+
+               $passwordWrapper = $pass1Row.find( '.wp-pwd' );
+               $generateButton  = $pass1Row.find( 'button.wp-generate-pw' );
 
                bindToggleButton();
 
-               $generateButton = $pass1Row.find( 'button.wp-generate-pw' ).show();
+               if ( $generateButton.length ) {
+                       $passwordWrapper.hide();
+               }
+
+               $generateButton.show();
                $generateButton.on( 'click', function () {
                        updateLock = true;
 
                        $generateButton.hide();
                        $passwordWrapper.show();
 
+                       // Enable the inputs when showing.
+                       $pass1.attr( 'disabled', false );
+                       $pass2.attr( 'disabled', false );
+                       $pass1Text.attr( 'disabled', false );
+
                        if ( $pass1Text.val().length === 0 ) {
                                generatePassword();
                        }
                $cancelButton.on( 'click', function () {
                        updateLock = false;
 
+                       // Clear any entered password.
+                       $pass1Text.val( '' );
+
+                       // Generate a new password.
+                       wp.ajax.post( 'generate-password' )
+                               .done( function( data ) {
+                                       $pass1.data( 'pw', data );
+                               } );
+
                        $generateButton.show();
                        $passwordWrapper.hide();
 
-                       // Clear password field to prevent update
-                       $pass1.val( '' ).trigger( 'pwupdate' );
-                       $submitButtons.prop( 'disabled', false );
+                       $weakRow.hide( 0, function () {
+                               $weakCheckbox.removeProp( 'checked' );
+                       } );
+
+                       // Disable the inputs when hiding to prevent autofill and submission.
+                       $pass1.prop( 'disabled', true );
+                       $pass2.prop( 'disabled', true );
+                       $pass1Text.prop( 'disabled', true );
+
+                       resetToggle();
+
+                       if ( $pass1Row.closest( 'form' ).is( '#your-profile' ) ) {
+                               // Clear password field to prevent update
+                               $pass1.val( '' ).trigger( 'pwupdate' );
+                               $submitButtons.prop( 'disabled', false );
+                       }
                } );
 
-               $pass1Row.closest('form').on( 'submit', function () {
+               $pass1Row.closest( 'form' ).on( 'submit', function () {
                        updateLock = false;
 
+                       $pass1.prop( 'disabled', false );
+                       $pass2.prop( 'disabled', false );
                        $pass2.val( $pass1.val() );
                        $pass1Wrap.removeClass( 'show-password' );
                });
                strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputBlacklist(), pass1 );
 
                switch ( strength ) {
+                       case -1:
+                               $( '#pass-strength-result' ).addClass( 'bad' ).html( pwsL10n.unknown );
+                               break;
                        case 2:
                                $('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
                                break;
                }
        }
 
+       function showOrHideWeakPasswordCheckbox() {
+               var passStrength = $('#pass-strength-result')[0];
+
+               if ( passStrength.className ) {
+                       $pass1.add( $pass1Text ).addClass( passStrength.className );
+                       if ( 'short' === passStrength.className || 'bad' === passStrength.className ) {
+                               if ( ! $weakCheckbox.prop( 'checked' ) ) {
+                                       $submitButtons.prop( 'disabled', true );
+                               }
+                               $weakRow.show();
+                       } else {
+                               $submitButtons.prop( 'disabled', false );
+                               $weakRow.hide();
+                       }
+               }
+       }
+
        $(document).ready( function() {
                var $colorpicker, $stylesheet, user_id, current_user_id,
                        select = $( '#display_name' );