Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

456 рядки
12 KiB

  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. $('#pass1').val('').on( inputEvent + ' pwupdate', check_pass_strength );
  278. $('#pass-strength-result').show();
  279. $('.color-palette').click( function() {
  280. $(this).siblings('input[name="admin_color"]').prop('checked', true);
  281. });
  282. if ( select.length ) {
  283. $('#first_name, #last_name, #nickname').bind( 'blur.user_profile', function() {
  284. var dub = [],
  285. inputs = {
  286. display_nickname : $('#nickname').val() || '',
  287. display_username : $('#user_login').val() || '',
  288. display_firstname : $('#first_name').val() || '',
  289. display_lastname : $('#last_name').val() || ''
  290. };
  291. if ( inputs.display_firstname && inputs.display_lastname ) {
  292. inputs.display_firstlast = inputs.display_firstname + ' ' + inputs.display_lastname;
  293. inputs.display_lastfirst = inputs.display_lastname + ' ' + inputs.display_firstname;
  294. }
  295. $.each( $('option', select), function( i, el ){
  296. dub.push( el.value );
  297. });
  298. $.each(inputs, function( id, value ) {
  299. if ( ! value ) {
  300. return;
  301. }
  302. var val = value.replace(/<\/?[a-z][^>]*>/gi, '');
  303. if ( inputs[id].length && $.inArray( val, dub ) === -1 ) {
  304. dub.push(val);
  305. $('<option />', {
  306. 'text': val
  307. }).appendTo( select );
  308. }
  309. });
  310. });
  311. }
  312. $colorpicker = $( '#color-picker' );
  313. $stylesheet = $( '#colors-css' );
  314. user_id = $( 'input#user_id' ).val();
  315. current_user_id = $( 'input[name="checkuser_id"]' ).val();
  316. $colorpicker.on( 'click.colorpicker', '.color-option', function() {
  317. var colors,
  318. $this = $(this);
  319. if ( $this.hasClass( 'selected' ) ) {
  320. return;
  321. }
  322. $this.siblings( '.selected' ).removeClass( 'selected' );
  323. $this.addClass( 'selected' ).find( 'input[type="radio"]' ).prop( 'checked', true );
  324. // Set color scheme
  325. if ( user_id === current_user_id ) {
  326. // Load the colors stylesheet.
  327. // The default color scheme won't have one, so we'll need to create an element.
  328. if ( 0 === $stylesheet.length ) {
  329. $stylesheet = $( '<link rel="stylesheet" />' ).appendTo( 'head' );
  330. }
  331. $stylesheet.attr( 'href', $this.children( '.css_url' ).val() );
  332. // repaint icons
  333. if ( typeof wp !== 'undefined' && wp.svgPainter ) {
  334. try {
  335. colors = $.parseJSON( $this.children( '.icon_colors' ).val() );
  336. } catch ( error ) {}
  337. if ( colors ) {
  338. wp.svgPainter.setColors( colors );
  339. wp.svgPainter.paint();
  340. }
  341. }
  342. // update user option
  343. $.post( ajaxurl, {
  344. action: 'save-user-color-scheme',
  345. color_scheme: $this.children( 'input[name="admin_color"]' ).val(),
  346. nonce: $('#color-nonce').val()
  347. }).done( function( response ) {
  348. if ( response.success ) {
  349. $( 'body' ).removeClass( response.data.previousScheme ).addClass( response.data.currentScheme );
  350. }
  351. });
  352. }
  353. });
  354. bindPasswordForm();
  355. });
  356. $( '#destroy-sessions' ).on( 'click', function( e ) {
  357. var $this = $(this);
  358. wp.ajax.post( 'destroy-sessions', {
  359. nonce: $( '#_wpnonce' ).val(),
  360. user_id: $( '#user_id' ).val()
  361. }).done( function( response ) {
  362. $this.prop( 'disabled', true );
  363. $this.siblings( '.notice' ).remove();
  364. $this.before( '<div class="notice notice-success inline"><p>' + response.message + '</p></div>' );
  365. }).fail( function( response ) {
  366. $this.siblings( '.notice' ).remove();
  367. $this.before( '<div class="notice notice-error inline"><p>' + response.message + '</p></div>' );
  368. });
  369. e.preventDefault();
  370. });
  371. window.generatePassword = generatePassword;
  372. /* Warn the user if password was generated but not saved */
  373. $( window ).on( 'beforeunload', function () {
  374. if ( true === updateLock ) {
  375. return userProfileL10n.warn;
  376. }
  377. } );
  378. })(jQuery);