1 /* global ajaxurl, pwsL10n, userProfileL10n */
3 var updateLock = false,
20 * Use feature detection to determine whether password inputs should use
21 * the `keyup` or `input` event. Input is preferred but lacks support
24 if ( 'oninput' in document.createElement( 'input' ) ) {
30 function generatePassword() {
31 if ( typeof zxcvbn !== 'function' ) {
32 setTimeout( generatePassword, 50 );
34 $pass1.val( $pass1.data( 'pw' ) );
35 $pass1.trigger( 'pwupdate' ).trigger( 'wp-check-valid-field' );
36 if ( 1 !== parseInt( $toggleButton.data( 'start-masked' ), 10 ) ) {
37 $pass1Wrap.addClass( 'show-password' );
39 $toggleButton.trigger( 'click' );
44 function bindPass1() {
45 var passStrength = $('#pass-strength-result')[0];
47 currentPass = $pass1.val();
49 $pass1Wrap = $pass1.parent();
51 $pass1Text = $( '<input type="text"/>' )
57 .addClass( $pass1[0].className )
58 .data( 'pw', $pass1.data( 'pw' ) )
60 .on( inputEvent, function () {
61 if ( $pass1Text.val() === currentPass ) {
64 $pass2.val( $pass1Text.val() );
65 $pass1.val( $pass1Text.val() ).trigger( 'pwupdate' );
66 currentPass = $pass1Text.val();
69 $pass1.after( $pass1Text );
71 if ( 1 === parseInt( $pass1.data( 'reveal' ), 10 ) ) {
75 $pass1.on( inputEvent + ' pwupdate', function () {
76 if ( $pass1.val() === currentPass ) {
80 currentPass = $pass1.val();
81 if ( $pass1Text.val() !== currentPass ) {
82 $pass1Text.val( currentPass );
84 $pass1.add( $pass1Text ).removeClass( 'short bad good strong' );
86 if ( passStrength.className ) {
87 $pass1.add( $pass1Text ).addClass( passStrength.className );
88 if ( 'short' === passStrength.className || 'bad' === passStrength.className ) {
89 if ( ! $weakCheckbox.prop( 'checked' ) ) {
90 $submitButtons.prop( 'disabled', true );
94 $submitButtons.prop( 'disabled', false );
101 function resetToggle() {
105 'aria-label': userProfileL10n.ariaHide
108 .text( userProfileL10n.hide )
110 .find( '.dashicons' )
111 .removeClass( 'dashicons-visibility' )
112 .addClass( 'dashicons-hidden' );
116 $pass1Label.attr( 'for', 'pass1-text' );
119 function bindToggleButton() {
120 $toggleButton = $pass1Row.find('.wp-hide-pw');
121 $toggleButton.show().on( 'click', function () {
122 if ( 1 === parseInt( $toggleButton.data( 'toggle' ), 10 ) ) {
123 $pass1Wrap.addClass( 'show-password' );
127 if ( ! _.isUndefined( $pass1Text[0].setSelectionRange ) ) {
128 $pass1Text[0].setSelectionRange( 0, 100 );
131 $pass1Wrap.removeClass( 'show-password' );
135 'aria-label': userProfileL10n.ariaShow
138 .text( userProfileL10n.show )
140 .find( '.dashicons' )
141 .removeClass('dashicons-hidden')
142 .addClass('dashicons-visibility');
146 $pass1Label.attr( 'for', 'pass1' );
148 if ( ! _.isUndefined( $pass1[0].setSelectionRange ) ) {
149 $pass1[0].setSelectionRange( 0, 100 );
155 function bindPasswordForm() {
156 var $passwordWrapper,
160 $pass1Row = $('.user-pass1-wrap');
161 $pass1Label = $pass1Row.find('th label').attr( 'for', 'pass1-text' );
164 $('.user-pass2-wrap').hide();
166 $submitButton = $( '#submit' ).on( 'click', function () {
170 $submitButtons = $submitButton.add( ' #createusersub' );
172 $weakRow = $( '.pw-weak' );
173 $weakCheckbox = $weakRow.find( '.pw-checkbox' );
174 $weakCheckbox.change( function() {
175 $submitButtons.prop( 'disabled', ! $weakCheckbox.prop( 'checked' ) );
178 $pass1 = $('#pass1');
179 if ( $pass1.length ) {
184 * Fix a LastPass mismatch issue, LastPass only changes pass2.
186 * This fixes the issue by copying any changes from the hidden
187 * pass2 field to the pass1 field, then running check_pass_strength.
189 $pass2 = $('#pass2').on( inputEvent, function () {
190 if ( $pass2.val().length > 0 ) {
191 $pass1.val( $pass2.val() );
194 $pass1.trigger( 'pwupdate' );
198 // Disable hidden inputs to prevent autofill and submission.
199 if ( $pass1.is( ':hidden' ) ) {
200 $pass1.prop( 'disabled', true );
201 $pass2.prop( 'disabled', true );
202 $pass1Text.prop( 'disabled', true );
205 $passwordWrapper = $pass1Row.find( '.wp-pwd' );
206 $generateButton = $pass1Row.find( 'button.wp-generate-pw' );
210 if ( $generateButton.length ) {
211 $passwordWrapper.hide();
214 $generateButton.show();
215 $generateButton.on( 'click', function () {
218 $generateButton.hide();
219 $passwordWrapper.show();
221 // Enable the inputs when showing.
222 $pass1.attr( 'disabled', false );
223 $pass2.attr( 'disabled', false );
224 $pass1Text.attr( 'disabled', false );
226 if ( $pass1Text.val().length === 0 ) {
230 _.defer( function() {
232 if ( ! _.isUndefined( $pass1Text[0].setSelectionRange ) ) {
233 $pass1Text[0].setSelectionRange( 0, 100 );
238 $cancelButton = $pass1Row.find( 'button.wp-cancel-pw' );
239 $cancelButton.on( 'click', function () {
242 // Clear any entered password.
243 $pass1Text.val( '' );
245 // Generate a new password.
246 wp.ajax.post( 'generate-password' )
247 .done( function( data ) {
248 $pass1.data( 'pw', data );
251 $generateButton.show();
252 $passwordWrapper.hide();
254 $weakRow.hide( 0, function () {
255 $weakCheckbox.removeProp( 'checked' );
258 // Disable the inputs when hiding to prevent autofill and submission.
259 $pass1.prop( 'disabled', true );
260 $pass2.prop( 'disabled', true );
261 $pass1Text.prop( 'disabled', true );
265 // Clear password field to prevent update
266 $pass1.val( '' ).trigger( 'pwupdate' );
267 $submitButtons.prop( 'disabled', false );
270 $pass1Row.closest('form').on( 'submit', function () {
273 $pass1.prop( 'disabled', false );
274 $pass2.prop( 'disabled', false );
275 $pass2.val( $pass1.val() );
276 $pass1Wrap.removeClass( 'show-password' );
280 function check_pass_strength() {
281 var pass1 = $('#pass1').val(), strength;
283 $('#pass-strength-result').removeClass('short bad good strong');
285 $('#pass-strength-result').html( ' ' );
289 strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputBlacklist(), pass1 );
291 switch ( strength ) {
293 $('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
296 $('#pass-strength-result').addClass('good').html( pwsL10n.good );
299 $('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
302 $('#pass-strength-result').addClass('short').html( pwsL10n.mismatch );
305 $('#pass-strength-result').addClass('short').html( pwsL10n['short'] );
309 $(document).ready( function() {
310 var $colorpicker, $stylesheet, user_id, current_user_id,
311 select = $( '#display_name' );
313 $('#pass1').val('').on( inputEvent + ' pwupdate', check_pass_strength );
314 $('#pass-strength-result').show();
315 $('.color-palette').click( function() {
316 $(this).siblings('input[name="admin_color"]').prop('checked', true);
319 if ( select.length ) {
320 $('#first_name, #last_name, #nickname').bind( 'blur.user_profile', function() {
323 display_nickname : $('#nickname').val() || '',
324 display_username : $('#user_login').val() || '',
325 display_firstname : $('#first_name').val() || '',
326 display_lastname : $('#last_name').val() || ''
329 if ( inputs.display_firstname && inputs.display_lastname ) {
330 inputs.display_firstlast = inputs.display_firstname + ' ' + inputs.display_lastname;
331 inputs.display_lastfirst = inputs.display_lastname + ' ' + inputs.display_firstname;
334 $.each( $('option', select), function( i, el ){
335 dub.push( el.value );
338 $.each(inputs, function( id, value ) {
343 var val = value.replace(/<\/?[a-z][^>]*>/gi, '');
345 if ( inputs[id].length && $.inArray( val, dub ) === -1 ) {
349 }).appendTo( select );
355 $colorpicker = $( '#color-picker' );
356 $stylesheet = $( '#colors-css' );
357 user_id = $( 'input#user_id' ).val();
358 current_user_id = $( 'input[name="checkuser_id"]' ).val();
360 $colorpicker.on( 'click.colorpicker', '.color-option', function() {
364 if ( $this.hasClass( 'selected' ) ) {
368 $this.siblings( '.selected' ).removeClass( 'selected' );
369 $this.addClass( 'selected' ).find( 'input[type="radio"]' ).prop( 'checked', true );
372 if ( user_id === current_user_id ) {
373 // Load the colors stylesheet.
374 // The default color scheme won't have one, so we'll need to create an element.
375 if ( 0 === $stylesheet.length ) {
376 $stylesheet = $( '<link rel="stylesheet" />' ).appendTo( 'head' );
378 $stylesheet.attr( 'href', $this.children( '.css_url' ).val() );
381 if ( typeof wp !== 'undefined' && wp.svgPainter ) {
383 colors = $.parseJSON( $this.children( '.icon_colors' ).val() );
387 wp.svgPainter.setColors( colors );
388 wp.svgPainter.paint();
392 // update user option
394 action: 'save-user-color-scheme',
395 color_scheme: $this.children( 'input[name="admin_color"]' ).val(),
396 nonce: $('#color-nonce').val()
397 }).done( function( response ) {
398 if ( response.success ) {
399 $( 'body' ).removeClass( response.data.previousScheme ).addClass( response.data.currentScheme );
408 $( '#destroy-sessions' ).on( 'click', function( e ) {
411 wp.ajax.post( 'destroy-sessions', {
412 nonce: $( '#_wpnonce' ).val(),
413 user_id: $( '#user_id' ).val()
414 }).done( function( response ) {
415 $this.prop( 'disabled', true );
416 $this.siblings( '.notice' ).remove();
417 $this.before( '<div class="notice notice-success inline"><p>' + response.message + '</p></div>' );
418 }).fail( function( response ) {
419 $this.siblings( '.notice' ).remove();
420 $this.before( '<div class="notice notice-error inline"><p>' + response.message + '</p></div>' );
426 window.generatePassword = generatePassword;
428 /* Warn the user if password was generated but not saved */
429 $( window ).on( 'beforeunload', function () {
430 if ( true === updateLock ) {
431 return userProfileL10n.warn;