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 // Disable the inputs when hiding to prevent autofill and submission.
255 $pass1.prop( 'disabled', true );
256 $pass2.prop( 'disabled', true );
257 $pass1Text.prop( 'disabled', true );
261 // Clear password field to prevent update
262 $pass1.val( '' ).trigger( 'pwupdate' );
263 $submitButtons.prop( 'disabled', false );
266 $pass1Row.closest('form').on( 'submit', function () {
269 $pass1.prop( 'disabled', false );
270 $pass2.prop( 'disabled', false );
271 $pass2.val( $pass1.val() );
272 $pass1Wrap.removeClass( 'show-password' );
276 function check_pass_strength() {
277 var pass1 = $('#pass1').val(), strength;
279 $('#pass-strength-result').removeClass('short bad good strong');
281 $('#pass-strength-result').html( ' ' );
285 strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputBlacklist(), pass1 );
287 switch ( strength ) {
289 $('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
292 $('#pass-strength-result').addClass('good').html( pwsL10n.good );
295 $('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
298 $('#pass-strength-result').addClass('short').html( pwsL10n.mismatch );
301 $('#pass-strength-result').addClass('short').html( pwsL10n['short'] );
305 $(document).ready( function() {
306 var $colorpicker, $stylesheet, user_id, current_user_id,
307 select = $( '#display_name' );
309 $('#pass1').val('').on( inputEvent + ' pwupdate', check_pass_strength );
310 $('#pass-strength-result').show();
311 $('.color-palette').click( function() {
312 $(this).siblings('input[name="admin_color"]').prop('checked', true);
315 if ( select.length ) {
316 $('#first_name, #last_name, #nickname').bind( 'blur.user_profile', function() {
319 display_nickname : $('#nickname').val() || '',
320 display_username : $('#user_login').val() || '',
321 display_firstname : $('#first_name').val() || '',
322 display_lastname : $('#last_name').val() || ''
325 if ( inputs.display_firstname && inputs.display_lastname ) {
326 inputs.display_firstlast = inputs.display_firstname + ' ' + inputs.display_lastname;
327 inputs.display_lastfirst = inputs.display_lastname + ' ' + inputs.display_firstname;
330 $.each( $('option', select), function( i, el ){
331 dub.push( el.value );
334 $.each(inputs, function( id, value ) {
339 var val = value.replace(/<\/?[a-z][^>]*>/gi, '');
341 if ( inputs[id].length && $.inArray( val, dub ) === -1 ) {
345 }).appendTo( select );
351 $colorpicker = $( '#color-picker' );
352 $stylesheet = $( '#colors-css' );
353 user_id = $( 'input#user_id' ).val();
354 current_user_id = $( 'input[name="checkuser_id"]' ).val();
356 $colorpicker.on( 'click.colorpicker', '.color-option', function() {
360 if ( $this.hasClass( 'selected' ) ) {
364 $this.siblings( '.selected' ).removeClass( 'selected' );
365 $this.addClass( 'selected' ).find( 'input[type="radio"]' ).prop( 'checked', true );
368 if ( user_id === current_user_id ) {
369 // Load the colors stylesheet.
370 // The default color scheme won't have one, so we'll need to create an element.
371 if ( 0 === $stylesheet.length ) {
372 $stylesheet = $( '<link rel="stylesheet" />' ).appendTo( 'head' );
374 $stylesheet.attr( 'href', $this.children( '.css_url' ).val() );
377 if ( typeof wp !== 'undefined' && wp.svgPainter ) {
379 colors = $.parseJSON( $this.children( '.icon_colors' ).val() );
383 wp.svgPainter.setColors( colors );
384 wp.svgPainter.paint();
388 // update user option
390 action: 'save-user-color-scheme',
391 color_scheme: $this.children( 'input[name="admin_color"]' ).val(),
392 nonce: $('#color-nonce').val()
393 }).done( function( response ) {
394 if ( response.success ) {
395 $( 'body' ).removeClass( response.data.previousScheme ).addClass( response.data.currentScheme );
404 $( '#destroy-sessions' ).on( 'click', function( e ) {
407 wp.ajax.post( 'destroy-sessions', {
408 nonce: $( '#_wpnonce' ).val(),
409 user_id: $( '#user_id' ).val()
410 }).done( function( response ) {
411 $this.prop( 'disabled', true );
412 $this.siblings( '.notice' ).remove();
413 $this.before( '<div class="notice notice-success inline"><p>' + response.message + '</p></div>' );
414 }).fail( function( response ) {
415 $this.siblings( '.notice' ).remove();
416 $this.before( '<div class="notice notice-error inline"><p>' + response.message + '</p></div>' );
422 window.generatePassword = generatePassword;
424 /* Warn the user if password was generated but not saved */
425 $( window ).on( 'beforeunload', function () {
426 if ( true === updateLock ) {
427 return userProfileL10n.warn;