1 /* global ajaxurl, pwsL10n, userProfileL10n */
3 var updateLock = false,
20 function generatePassword() {
21 if ( typeof zxcvbn !== 'function' ) {
22 setTimeout( generatePassword, 50 );
24 $pass1.val( $pass1.data( 'pw' ) );
25 $pass1.trigger( 'pwupdate' );
26 if ( 1 !== parseInt( $toggleButton.data( 'start-masked' ), 10 ) ) {
27 $pass1Wrap.addClass( 'show-password' );
29 $toggleButton.trigger( 'click' );
34 function bindPass1() {
35 var passStrength = $('#pass-strength-result')[0];
37 currentPass = $pass1.val();
39 $pass1Wrap = $pass1.parent();
41 $pass1Text = $( '<input type="text"/>' )
47 .addClass( $pass1[0].className )
48 .data( 'pw', $pass1.data( 'pw' ) )
50 .on( 'keyup', function () {
51 if ( $pass1Text.val() === currentPass ) {
54 $pass2.val( $pass1Text.val() );
55 $pass1.val( $pass1Text.val() ).trigger( 'pwupdate' );
56 currentPass = $pass1Text.val();
59 $pass1.after( $pass1Text );
61 if ( 1 === parseInt( $pass1.data( 'reveal' ), 10 ) ) {
65 $pass1.on( 'keyup pwupdate', function () {
66 if ( $pass1.val() === currentPass ) {
70 currentPass = $pass1.val();
71 if ( $pass1Text.val() !== currentPass ) {
72 $pass1Text.val( currentPass );
74 $pass1.add( $pass1Text ).removeClass( 'short bad good strong' );
76 if ( passStrength.className ) {
77 $pass1.add( $pass1Text ).addClass( passStrength.className );
78 if ( 'short' === passStrength.className || 'bad' === passStrength.className ) {
79 if ( ! $weakCheckbox.prop( 'checked' ) ) {
80 $submitButtons.prop( 'disabled', true );
84 $submitButtons.prop( 'disabled', false );
91 function bindToggleButton() {
92 $toggleButton = $pass1Row.find('.wp-hide-pw');
93 $toggleButton.show().on( 'click', function () {
94 if ( 1 === parseInt( $toggleButton.data( 'toggle' ), 10 ) ) {
95 $pass1Wrap.addClass( 'show-password' );
99 'aria-label': userProfileL10n.ariaHide
102 .text( userProfileL10n.hide )
104 .find( '.dashicons' )
105 .removeClass('dashicons-visibility')
106 .addClass('dashicons-hidden');
110 if ( ! _.isUndefined( $pass1Text[0].setSelectionRange ) ) {
111 $pass1Text[0].setSelectionRange( 0, 100 );
114 $pass1Wrap.removeClass( 'show-password' );
118 'aria-label': userProfileL10n.ariaShow
121 .text( userProfileL10n.show )
123 .find( '.dashicons' )
124 .removeClass('dashicons-hidden')
125 .addClass('dashicons-visibility');
129 if ( ! _.isUndefined( $pass1[0].setSelectionRange ) ) {
130 $pass1[0].setSelectionRange( 0, 100 );
136 function bindPasswordForm() {
137 var $passwordWrapper,
141 $pass1Row = $('.user-pass1-wrap');
143 $('.user-pass2-wrap').hide();
145 $submitButton = $( '#submit' ).on( 'click', function () {
149 $submitButtons = $submitButton.add( ' #createusersub' );
151 $weakRow = $( '.pw-weak' );
152 $weakCheckbox = $weakRow.find( '.pw-checkbox' );
153 $weakCheckbox.change( function() {
154 $submitButtons.prop( 'disabled', ! $weakCheckbox.prop( 'checked' ) );
157 $pass1 = $('#pass1');
158 if ( $pass1.length ) {
163 * Fix a LastPass mismatch issue, LastPass only changes pass2.
165 * This fixes the issue by copying any changes from the hidden
166 * pass2 field to the pass1 field, then running check_pass_strength.
168 $pass2 = $('#pass2').on( 'keyup', function () {
169 if ( $pass2.val().length > 0 ) {
170 $pass1.val( $pass2.val() );
173 $pass1.trigger( 'pwupdate' );
177 $passwordWrapper = $pass1Row.find('.wp-pwd').hide();
181 $generateButton = $pass1Row.find( 'button.wp-generate-pw' ).show();
182 $generateButton.on( 'click', function () {
185 $generateButton.hide();
186 $passwordWrapper.show();
188 if ( $pass1Text.val().length === 0 ) {
192 _.defer( function() {
194 if ( ! _.isUndefined( $pass1Text[0].setSelectionRange ) ) {
195 $pass1Text[0].setSelectionRange( 0, 100 );
200 $cancelButton = $pass1Row.find( 'button.wp-cancel-pw' );
201 $cancelButton.on( 'click', function () {
204 $generateButton.show();
205 $passwordWrapper.hide();
208 $pass1Row.closest('form').on( 'submit', function () {
211 $pass2.val( $pass1.val() );
212 $pass1Wrap.removeClass( 'show-password' );
216 function check_pass_strength() {
217 var pass1 = $('#pass1').val(), strength;
219 $('#pass-strength-result').removeClass('short bad good strong');
221 $('#pass-strength-result').html( ' ' );
225 strength = wp.passwordStrength.meter( pass1, wp.passwordStrength.userInputBlacklist(), pass1 );
227 switch ( strength ) {
229 $('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
232 $('#pass-strength-result').addClass('good').html( pwsL10n.good );
235 $('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
238 $('#pass-strength-result').addClass('short').html( pwsL10n.mismatch );
241 $('#pass-strength-result').addClass('short').html( pwsL10n['short'] );
245 $(document).ready( function() {
246 var $colorpicker, $stylesheet, user_id, current_user_id,
247 select = $( '#display_name' );
249 $('#pass1').val('').on( 'keyup pwupdate', check_pass_strength );
250 $('#pass-strength-result').show();
251 $('.color-palette').click( function() {
252 $(this).siblings('input[name="admin_color"]').prop('checked', true);
255 if ( select.length ) {
256 $('#first_name, #last_name, #nickname').bind( 'blur.user_profile', function() {
259 display_nickname : $('#nickname').val() || '',
260 display_username : $('#user_login').val() || '',
261 display_firstname : $('#first_name').val() || '',
262 display_lastname : $('#last_name').val() || ''
265 if ( inputs.display_firstname && inputs.display_lastname ) {
266 inputs.display_firstlast = inputs.display_firstname + ' ' + inputs.display_lastname;
267 inputs.display_lastfirst = inputs.display_lastname + ' ' + inputs.display_firstname;
270 $.each( $('option', select), function( i, el ){
271 dub.push( el.value );
274 $.each(inputs, function( id, value ) {
279 var val = value.replace(/<\/?[a-z][^>]*>/gi, '');
281 if ( inputs[id].length && $.inArray( val, dub ) === -1 ) {
285 }).appendTo( select );
291 $colorpicker = $( '#color-picker' );
292 $stylesheet = $( '#colors-css' );
293 user_id = $( 'input#user_id' ).val();
294 current_user_id = $( 'input[name="checkuser_id"]' ).val();
296 $colorpicker.on( 'click.colorpicker', '.color-option', function() {
300 if ( $this.hasClass( 'selected' ) ) {
304 $this.siblings( '.selected' ).removeClass( 'selected' );
305 $this.addClass( 'selected' ).find( 'input[type="radio"]' ).prop( 'checked', true );
308 if ( user_id === current_user_id ) {
309 // Load the colors stylesheet.
310 // The default color scheme won't have one, so we'll need to create an element.
311 if ( 0 === $stylesheet.length ) {
312 $stylesheet = $( '<link rel="stylesheet" />' ).appendTo( 'head' );
314 $stylesheet.attr( 'href', $this.children( '.css_url' ).val() );
317 if ( typeof wp !== 'undefined' && wp.svgPainter ) {
319 colors = $.parseJSON( $this.children( '.icon_colors' ).val() );
323 wp.svgPainter.setColors( colors );
324 wp.svgPainter.paint();
328 // update user option
330 action: 'save-user-color-scheme',
331 color_scheme: $this.children( 'input[name="admin_color"]' ).val(),
332 nonce: $('#color-nonce').val()
333 }).done( function( response ) {
334 if ( response.success ) {
335 $( 'body' ).removeClass( response.data.previousScheme ).addClass( response.data.currentScheme );
344 $( '#destroy-sessions' ).on( 'click', function( e ) {
347 wp.ajax.post( 'destroy-sessions', {
348 nonce: $( '#_wpnonce' ).val(),
349 user_id: $( '#user_id' ).val()
350 }).done( function( response ) {
351 $this.prop( 'disabled', true );
352 $this.siblings( '.notice' ).remove();
353 $this.before( '<div class="notice notice-success inline"><p>' + response.message + '</p></div>' );
354 }).fail( function( response ) {
355 $this.siblings( '.notice' ).remove();
356 $this.before( '<div class="notice notice-error inline"><p>' + response.message + '</p></div>' );
362 window.generatePassword = generatePassword;
364 /* Warn the user if password was generated but not saved */
365 $( window ).on( 'beforeunload', function () {
366 if ( true === updateLock ) {
367 return userProfileL10n.warn;