user-profile.js 11 KB

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