PageRenderTime 31ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/js/user-profile.js

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