[ Index ]

PHP Cross Reference of WordPress

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          $generateButton.show();
 150          $generateButton.on( 'click', function () {
 151              updateLock = true;
 152  
 153              // Make sure the password fields are shown.
 154              $generateButton.attr( 'aria-expanded', 'true' );
 155              $passwordWrapper
 156                  .show()
 157                  .addClass( 'is-open' );
 158  
 159              // Enable the inputs when showing.
 160              $pass1.attr( 'disabled', false );
 161              $pass2.attr( 'disabled', false );
 162  
 163              // Set the password to the generated value.
 164              generatePassword();
 165  
 166              // Show generated password in plaintext by default.
 167              resetToggle ( false );
 168  
 169              // Generate the next password and cache.
 170              wp.ajax.post( 'generate-password' )
 171                  .done( function( data ) {
 172                      $pass1.data( 'pw', data );
 173                  } );
 174          } );
 175  
 176          $cancelButton = $pass1Row.find( 'button.wp-cancel-pw' );
 177          $cancelButton.on( 'click', function () {
 178              updateLock = false;
 179  
 180              // Disable the inputs when hiding to prevent autofill and submission.
 181              $pass1.prop( 'disabled', true );
 182              $pass2.prop( 'disabled', true );
 183  
 184              // Clear password field and update the UI.
 185              $pass1.val( '' ).trigger( 'pwupdate' );
 186              resetToggle( false );
 187  
 188              // Hide password controls.
 189              $passwordWrapper
 190                  .hide()
 191                  .removeClass( 'is-open' );
 192  
 193              // Stop an empty password from being submitted as a change.
 194              $submitButtons.prop( 'disabled', false );
 195          } );
 196  
 197          $pass1Row.closest( 'form' ).on( 'submit', function () {
 198              updateLock = false;
 199  
 200              $pass1.prop( 'disabled', false );
 201              $pass2.prop( 'disabled', false );
 202              $pass2.val( $pass1.val() );
 203          });
 204      }
 205  
 206  	function check_pass_strength() {
 207          var pass1 = $('#pass1').val(), strength;
 208  
 209          $('#pass-strength-result').removeClass('short bad good strong empty');
 210          if ( ! pass1 || '' ===  pass1.trim() ) {
 211              $( '#pass-strength-result' ).addClass( 'empty' ).html( ' ' );
 212              return;
 213          }
 214  
 215          strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputDisallowedList(), pass1 );
 216  
 217          switch ( strength ) {
 218              case -1:
 219                  $( '#pass-strength-result' ).addClass( 'bad' ).html( pwsL10n.unknown );
 220                  break;
 221              case 2:
 222                  $('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
 223                  break;
 224              case 3:
 225                  $('#pass-strength-result').addClass('good').html( pwsL10n.good );
 226                  break;
 227              case 4:
 228                  $('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
 229                  break;
 230              case 5:
 231                  $('#pass-strength-result').addClass('short').html( pwsL10n.mismatch );
 232                  break;
 233              default:
 234                  $('#pass-strength-result').addClass('short').html( pwsL10n['short'] );
 235          }
 236      }
 237  
 238  	function showOrHideWeakPasswordCheckbox() {
 239          var passStrength = $('#pass-strength-result')[0];
 240  
 241          if ( passStrength.className ) {
 242              $pass1.addClass( passStrength.className );
 243              if ( $( passStrength ).is( '.short, .bad' ) ) {
 244                  if ( ! $weakCheckbox.prop( 'checked' ) ) {
 245                      $submitButtons.prop( 'disabled', true );
 246                  }
 247                  $weakRow.show();
 248              } else {
 249                  if ( $( passStrength ).is( '.empty' ) ) {
 250                      $submitButtons.prop( 'disabled', true );
 251                      $weakCheckbox.prop( 'checked', false );
 252                  } else {
 253                      $submitButtons.prop( 'disabled', false );
 254                  }
 255                  $weakRow.hide();
 256              }
 257          }
 258      }
 259  
 260      $(document).ready( function() {
 261          var $colorpicker, $stylesheet, user_id, current_user_id,
 262              select       = $( '#display_name' ),
 263              current_name = select.val(),
 264              greeting     = $( '#wp-admin-bar-my-account' ).find( '.display-name' );
 265  
 266          $( '#pass1' ).val( '' ).on( 'input' + ' pwupdate', check_pass_strength );
 267          $('#pass-strength-result').show();
 268          $('.color-palette').click( function() {
 269              $(this).siblings('input[name="admin_color"]').prop('checked', true);
 270          });
 271  
 272          if ( select.length ) {
 273              $('#first_name, #last_name, #nickname').bind( 'blur.user_profile', function() {
 274                  var dub = [],
 275                      inputs = {
 276                          display_nickname  : $('#nickname').val() || '',
 277                          display_username  : $('#user_login').val() || '',
 278                          display_firstname : $('#first_name').val() || '',
 279                          display_lastname  : $('#last_name').val() || ''
 280                      };
 281  
 282                  if ( inputs.display_firstname && inputs.display_lastname ) {
 283                      inputs.display_firstlast = inputs.display_firstname + ' ' + inputs.display_lastname;
 284                      inputs.display_lastfirst = inputs.display_lastname + ' ' + inputs.display_firstname;
 285                  }
 286  
 287                  $.each( $('option', select), function( i, el ){
 288                      dub.push( el.value );
 289                  });
 290  
 291                  $.each(inputs, function( id, value ) {
 292                      if ( ! value ) {
 293                          return;
 294                      }
 295  
 296                      var val = value.replace(/<\/?[a-z][^>]*>/gi, '');
 297  
 298                      if ( inputs[id].length && $.inArray( val, dub ) === -1 ) {
 299                          dub.push(val);
 300                          $('<option />', {
 301                              'text': val
 302                          }).appendTo( select );
 303                      }
 304                  });
 305              });
 306  
 307              /**
 308               * Replaces "Howdy, *" in the admin toolbar whenever the display name dropdown is updated for one's own profile.
 309               */
 310              select.on( 'change', function() {
 311                  if ( user_id !== current_user_id ) {
 312                      return;
 313                  }
 314  
 315                  var display_name = $.trim( this.value ) || current_name;
 316  
 317                  greeting.text( display_name );
 318              } );
 319          }
 320  
 321          $colorpicker = $( '#color-picker' );
 322          $stylesheet = $( '#colors-css' );
 323          user_id = $( 'input#user_id' ).val();
 324          current_user_id = $( 'input[name="checkuser_id"]' ).val();
 325  
 326          $colorpicker.on( 'click.colorpicker', '.color-option', function() {
 327              var colors,
 328                  $this = $(this);
 329  
 330              if ( $this.hasClass( 'selected' ) ) {
 331                  return;
 332              }
 333  
 334              $this.siblings( '.selected' ).removeClass( 'selected' );
 335              $this.addClass( 'selected' ).find( 'input[type="radio"]' ).prop( 'checked', true );
 336  
 337              // Set color scheme.
 338              if ( user_id === current_user_id ) {
 339                  // Load the colors stylesheet.
 340                  // The default color scheme won't have one, so we'll need to create an element.
 341                  if ( 0 === $stylesheet.length ) {
 342                      $stylesheet = $( '<link rel="stylesheet" />' ).appendTo( 'head' );
 343                  }
 344                  $stylesheet.attr( 'href', $this.children( '.css_url' ).val() );
 345  
 346                  // Repaint icons.
 347                  if ( typeof wp !== 'undefined' && wp.svgPainter ) {
 348                      try {
 349                          colors = $.parseJSON( $this.children( '.icon_colors' ).val() );
 350                      } catch ( error ) {}
 351  
 352                      if ( colors ) {
 353                          wp.svgPainter.setColors( colors );
 354                          wp.svgPainter.paint();
 355                      }
 356                  }
 357  
 358                  // Update user option.
 359                  $.post( ajaxurl, {
 360                      action:       'save-user-color-scheme',
 361                      color_scheme: $this.children( 'input[name="admin_color"]' ).val(),
 362                      nonce:        $('#color-nonce').val()
 363                  }).done( function( response ) {
 364                      if ( response.success ) {
 365                          $( 'body' ).removeClass( response.data.previousScheme ).addClass( response.data.currentScheme );
 366                      }
 367                  });
 368              }
 369          });
 370  
 371          bindPasswordForm();
 372      });
 373  
 374      $( '#destroy-sessions' ).on( 'click', function( e ) {
 375          var $this = $(this);
 376  
 377          wp.ajax.post( 'destroy-sessions', {
 378              nonce: $( '#_wpnonce' ).val(),
 379              user_id: $( '#user_id' ).val()
 380          }).done( function( response ) {
 381              $this.prop( 'disabled', true );
 382              $this.siblings( '.notice' ).remove();
 383              $this.before( '<div class="notice notice-success inline"><p>' + response.message + '</p></div>' );
 384          }).fail( function( response ) {
 385              $this.siblings( '.notice' ).remove();
 386              $this.before( '<div class="notice notice-error inline"><p>' + response.message + '</p></div>' );
 387          });
 388  
 389          e.preventDefault();
 390      });
 391  
 392      window.generatePassword = generatePassword;
 393  
 394      // Warn the user if password was generated but not saved.
 395      $( window ).on( 'beforeunload', function () {
 396          if ( true === updateLock ) {
 397              return __( 'Your new password has not been saved.' );
 398          }
 399      } );
 400  
 401  })(jQuery);


Generated: Sat Dec 5 01:00:03 2020 Cross-referenced by PHPXref 0.7.1