[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/js/ -> user-profile.js (source)

   1  /**
   2   * @output wp-admin/js/user-profile.js
   3   */
   4  
   5  /* global ajaxurl, pwsL10n */
   6  (function($) {
   7      var updateLock = false,
   8          __ = wp.i18n.__,
   9          $pass1Row,
  10          $pass1,
  11          $pass2,
  12          $weakRow,
  13          $weakCheckbox,
  14          $toggleButton,
  15          $submitButtons,
  16          $submitButton,
  17          currentPass,
  18          $passwordWrapper;
  19  
  20  	function generatePassword() {
  21          if ( typeof zxcvbn !== 'function' ) {
  22              setTimeout( generatePassword, 50 );
  23              return;
  24          } else if ( ! $pass1.val() || $passwordWrapper.hasClass( 'is-open' ) ) {
  25              // zxcvbn loaded before user entered password, or generating new password.
  26              $pass1.val( $pass1.data( 'pw' ) );
  27              $pass1.trigger( 'pwupdate' );
  28              showOrHideWeakPasswordCheckbox();
  29          } else {
  30              // zxcvbn loaded after the user entered password, check strength.
  31              check_pass_strength();
  32              showOrHideWeakPasswordCheckbox();
  33          }
  34  
  35          // Install screen.
  36          if ( 1 !== parseInt( $toggleButton.data( 'start-masked' ), 10 ) ) {
  37              // Show the password not masked if admin_password hasn't been posted yet.
  38              $pass1.attr( 'type', 'text' );
  39          } else {
  40              // Otherwise, mask the password.
  41              $toggleButton.trigger( 'click' );
  42          }
  43  
  44          // Once zxcvbn loads, passwords strength is known.
  45          $( '#pw-weak-text-label' ).text( __( 'Confirm use of weak password' ) );
  46      }
  47  
  48  	function bindPass1() {
  49          currentPass = $pass1.val();
  50  
  51          if ( 1 === parseInt( $pass1.data( 'reveal' ), 10 ) ) {
  52              generatePassword();
  53          }
  54  
  55          $pass1.on( 'input' + ' pwupdate', function () {
  56              if ( $pass1.val() === currentPass ) {
  57                  return;
  58              }
  59  
  60              currentPass = $pass1.val();
  61  
  62              // Refresh password strength area.
  63              $pass1.removeClass( 'short bad good strong' );
  64              showOrHideWeakPasswordCheckbox();
  65          } );
  66      }
  67  
  68  	function resetToggle( show ) {
  69          $toggleButton
  70              .attr({
  71                  'aria-label': show ? __( 'Show password' ) : __( 'Hide password' )
  72              })
  73              .find( '.text' )
  74                  .text( show ? __( 'Show' ) : __( 'Hide' ) )
  75              .end()
  76              .find( '.dashicons' )
  77                  .removeClass( show ? 'dashicons-hidden' : 'dashicons-visibility' )
  78                  .addClass( show ? 'dashicons-visibility' : 'dashicons-hidden' );
  79      }
  80  
  81  	function bindToggleButton() {
  82          $toggleButton = $pass1Row.find('.wp-hide-pw');
  83          $toggleButton.show().on( 'click', function () {
  84              if ( 'password' === $pass1.attr( 'type' ) ) {
  85                  $pass1.attr( 'type', 'text' );
  86                  resetToggle( false );
  87              } else {
  88                  $pass1.attr( 'type', 'password' );
  89                  resetToggle( true );
  90              }
  91          });
  92      }
  93  
  94  	function bindPasswordForm() {
  95          var $generateButton,
  96              $cancelButton;
  97  
  98          $pass1Row = $( '.user-pass1-wrap, .user-pass-wrap' );
  99  
 100          // Hide the confirm password field when JavaScript support is enabled.
 101          $('.user-pass2-wrap').hide();
 102  
 103          $submitButton = $( '#submit, #wp-submit' ).on( 'click', function () {
 104              updateLock = false;
 105          });
 106  
 107          $submitButtons = $submitButton.add( ' #createusersub' );
 108  
 109          $weakRow = $( '.pw-weak' );
 110          $weakCheckbox = $weakRow.find( '.pw-checkbox' );
 111          $weakCheckbox.change( function() {
 112              $submitButtons.prop( 'disabled', ! $weakCheckbox.prop( 'checked' ) );
 113          } );
 114  
 115          $pass1 = $('#pass1');
 116          if ( $pass1.length ) {
 117              bindPass1();
 118          } else {
 119              // Password field for the login form.
 120              $pass1 = $( '#user_pass' );
 121          }
 122  
 123          /*
 124           * Fix a LastPass mismatch issue, LastPass only changes pass2.
 125           *
 126           * This fixes the issue by copying any changes from the hidden
 127           * pass2 field to the pass1 field, then running check_pass_strength.
 128           */
 129          $pass2 = $( '#pass2' ).on( 'input', function () {
 130              if ( $pass2.val().length > 0 ) {
 131                  $pass1.val( $pass2.val() );
 132                  $pass2.val('');
 133                  currentPass = '';
 134                  $pass1.trigger( 'pwupdate' );
 135              }
 136          } );
 137  
 138          // Disable hidden inputs to prevent autofill and submission.
 139          if ( $pass1.is( ':hidden' ) ) {
 140              $pass1.prop( 'disabled', true );
 141              $pass2.prop( 'disabled', true );
 142          }
 143  
 144          $passwordWrapper = $pass1Row.find( '.wp-pwd' );
 145          $generateButton  = $pass1Row.find( 'button.wp-generate-pw' );
 146  
 147          bindToggleButton();
 148  
 149          // Generate the first password and cache it, but don't set it yet.
 150          wp.ajax.post( 'generate-password' )
 151              .done( function( data ) {
 152                  // Cache password.
 153                  $pass1.data( 'pw', data );
 154              } );
 155  
 156          $generateButton.show();
 157          $generateButton.on( 'click', function () {
 158              updateLock = true;
 159  
 160              // Make sure the password fields are shown.
 161              $generateButton.attr( 'aria-expanded', 'true' );
 162              $passwordWrapper
 163                  .show()
 164                  .addClass( 'is-open' );
 165  
 166              // Enable the inputs when showing.
 167              $pass1.attr( 'disabled', false );
 168              $pass2.attr( 'disabled', false );
 169  
 170              // Set the password to the generated value.
 171              generatePassword();
 172  
 173              // Show generated password in plaintext by default.
 174              resetToggle ( false );
 175  
 176              // Generate the next password and cache.
 177              wp.ajax.post( 'generate-password' )
 178                  .done( function( data ) {
 179                      $pass1.data( 'pw', data );
 180                  } );
 181          } );
 182  
 183          $cancelButton = $pass1Row.find( 'button.wp-cancel-pw' );
 184          $cancelButton.on( 'click', function () {
 185              updateLock = false;
 186  
 187              // Disable the inputs when hiding to prevent autofill and submission.
 188              $pass1.prop( 'disabled', true );
 189              $pass2.prop( 'disabled', true );
 190  
 191              // Clear password field and update the UI.
 192              $pass1.val( '' ).trigger( 'pwupdate' );
 193              resetToggle( false );
 194  
 195              // Hide password controls.
 196              $passwordWrapper
 197                  .hide()
 198                  .removeClass( 'is-open' );
 199  
 200              // Stop an empty password from being submitted as a change.
 201              $submitButtons.prop( 'disabled', false );
 202          } );
 203  
 204          $pass1Row.closest( 'form' ).on( 'submit', function () {
 205              updateLock = false;
 206  
 207              $pass1.prop( 'disabled', false );
 208              $pass2.prop( 'disabled', false );
 209              $pass2.val( $pass1.val() );
 210          });
 211      }
 212  
 213  	function check_pass_strength() {
 214          var pass1 = $('#pass1').val(), strength;
 215  
 216          $('#pass-strength-result').removeClass('short bad good strong empty');
 217          if ( ! pass1 || '' ===  pass1.trim() ) {
 218              $( '#pass-strength-result' ).addClass( 'empty' ).html( ' ' );
 219              return;
 220          }
 221  
 222          strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputDisallowedList(), pass1 );
 223  
 224          switch ( strength ) {
 225              case -1:
 226                  $( '#pass-strength-result' ).addClass( 'bad' ).html( pwsL10n.unknown );
 227                  break;
 228              case 2:
 229                  $('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
 230                  break;
 231              case 3:
 232                  $('#pass-strength-result').addClass('good').html( pwsL10n.good );
 233                  break;
 234              case 4:
 235                  $('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
 236                  break;
 237              case 5:
 238                  $('#pass-strength-result').addClass('short').html( pwsL10n.mismatch );
 239                  break;
 240              default:
 241                  $('#pass-strength-result').addClass('short').html( pwsL10n['short'] );
 242          }
 243      }
 244  
 245  	function showOrHideWeakPasswordCheckbox() {
 246          var passStrength = $('#pass-strength-result')[0];
 247  
 248          if ( passStrength.className ) {
 249              $pass1.addClass( passStrength.className );
 250              if ( $( passStrength ).is( '.short, .bad' ) ) {
 251                  if ( ! $weakCheckbox.prop( 'checked' ) ) {
 252                      $submitButtons.prop( 'disabled', true );
 253                  }
 254                  $weakRow.show();
 255              } else {
 256                  if ( $( passStrength ).is( '.empty' ) ) {
 257                      $submitButtons.prop( 'disabled', true );
 258                      $weakCheckbox.prop( 'checked', false );
 259                  } else {
 260                      $submitButtons.prop( 'disabled', false );
 261                  }
 262                  $weakRow.hide();
 263              }
 264          }
 265      }
 266  
 267      $(document).ready( function() {
 268          var $colorpicker, $stylesheet, user_id, current_user_id,
 269              select       = $( '#display_name' ),
 270              current_name = select.val(),
 271              greeting     = $( '#wp-admin-bar-my-account' ).find( '.display-name' );
 272  
 273          $( '#pass1' ).val( '' ).on( 'input' + ' pwupdate', check_pass_strength );
 274          $('#pass-strength-result').show();
 275          $('.color-palette').click( function() {
 276              $(this).siblings('input[name="admin_color"]').prop('checked', true);
 277          });
 278  
 279          if ( select.length ) {
 280              $('#first_name, #last_name, #nickname').bind( 'blur.user_profile', function() {
 281                  var dub = [],
 282                      inputs = {
 283                          display_nickname  : $('#nickname').val() || '',
 284                          display_username  : $('#user_login').val() || '',
 285                          display_firstname : $('#first_name').val() || '',
 286                          display_lastname  : $('#last_name').val() || ''
 287                      };
 288  
 289                  if ( inputs.display_firstname && inputs.display_lastname ) {
 290                      inputs.display_firstlast = inputs.display_firstname + ' ' + inputs.display_lastname;
 291                      inputs.display_lastfirst = inputs.display_lastname + ' ' + inputs.display_firstname;
 292                  }
 293  
 294                  $.each( $('option', select), function( i, el ){
 295                      dub.push( el.value );
 296                  });
 297  
 298                  $.each(inputs, function( id, value ) {
 299                      if ( ! value ) {
 300                          return;
 301                      }
 302  
 303                      var val = value.replace(/<\/?[a-z][^>]*>/gi, '');
 304  
 305                      if ( inputs[id].length && $.inArray( val, dub ) === -1 ) {
 306                          dub.push(val);
 307                          $('<option />', {
 308                              'text': val
 309                          }).appendTo( select );
 310                      }
 311                  });
 312              });
 313  
 314              /**
 315               * Replaces "Howdy, *" in the admin toolbar whenever the display name dropdown is updated for one's own profile.
 316               */
 317              select.on( 'change', function() {
 318                  if ( user_id !== current_user_id ) {
 319                      return;
 320                  }
 321  
 322                  var display_name = $.trim( this.value ) || current_name;
 323  
 324                  greeting.text( display_name );
 325              } );
 326          }
 327  
 328          $colorpicker = $( '#color-picker' );
 329          $stylesheet = $( '#colors-css' );
 330          user_id = $( 'input#user_id' ).val();
 331          current_user_id = $( 'input[name="checkuser_id"]' ).val();
 332  
 333          $colorpicker.on( 'click.colorpicker', '.color-option', function() {
 334              var colors,
 335                  $this = $(this);
 336  
 337              if ( $this.hasClass( 'selected' ) ) {
 338                  return;
 339              }
 340  
 341              $this.siblings( '.selected' ).removeClass( 'selected' );
 342              $this.addClass( 'selected' ).find( 'input[type="radio"]' ).prop( 'checked', true );
 343  
 344              // Set color scheme.
 345              if ( user_id === current_user_id ) {
 346                  // Load the colors stylesheet.
 347                  // The default color scheme won't have one, so we'll need to create an element.
 348                  if ( 0 === $stylesheet.length ) {
 349                      $stylesheet = $( '<link rel="stylesheet" />' ).appendTo( 'head' );
 350                  }
 351                  $stylesheet.attr( 'href', $this.children( '.css_url' ).val() );
 352  
 353                  // Repaint icons.
 354                  if ( typeof wp !== 'undefined' && wp.svgPainter ) {
 355                      try {
 356                          colors = $.parseJSON( $this.children( '.icon_colors' ).val() );
 357                      } catch ( error ) {}
 358  
 359                      if ( colors ) {
 360                          wp.svgPainter.setColors( colors );
 361                          wp.svgPainter.paint();
 362                      }
 363                  }
 364  
 365                  // Update user option.
 366                  $.post( ajaxurl, {
 367                      action:       'save-user-color-scheme',
 368                      color_scheme: $this.children( 'input[name="admin_color"]' ).val(),
 369                      nonce:        $('#color-nonce').val()
 370                  }).done( function( response ) {
 371                      if ( response.success ) {
 372                          $( 'body' ).removeClass( response.data.previousScheme ).addClass( response.data.currentScheme );
 373                      }
 374                  });
 375              }
 376          });
 377  
 378          bindPasswordForm();
 379      });
 380  
 381      $( '#destroy-sessions' ).on( 'click', function( e ) {
 382          var $this = $(this);
 383  
 384          wp.ajax.post( 'destroy-sessions', {
 385              nonce: $( '#_wpnonce' ).val(),
 386              user_id: $( '#user_id' ).val()
 387          }).done( function( response ) {
 388              $this.prop( 'disabled', true );
 389              $this.siblings( '.notice' ).remove();
 390              $this.before( '<div class="notice notice-success inline"><p>' + response.message + '</p></div>' );
 391          }).fail( function( response ) {
 392              $this.siblings( '.notice' ).remove();
 393              $this.before( '<div class="notice notice-error inline"><p>' + response.message + '</p></div>' );
 394          });
 395  
 396          e.preventDefault();
 397      });
 398  
 399      window.generatePassword = generatePassword;
 400  
 401      // Warn the user if password was generated but not saved.
 402      $( window ).on( 'beforeunload', function () {
 403          if ( true === updateLock ) {
 404              return __( 'Your new password has not been saved.' );
 405          }
 406      } );
 407  
 408  })(jQuery);


Generated : Mon Oct 26 08:20:01 2020 Cross-referenced by PHPXref