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


Generated: Mon Aug 3 01:00:04 2020 Cross-referenced by PHPXref 0.7.1