user-profile.js 12 KB

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