|
2 | 2 | * @output wp-admin/js/user-profile.js |
3 | 3 | */ |
4 | 4 |
|
5 | | -/* global ajaxurl, pwsL10n */ |
| 5 | +/* global ajaxurl, pwsL10n, userProfileL10n */ |
6 | 6 | (function($) { |
7 | 7 | var updateLock = false, |
8 | 8 | __ = wp.i18n.__, |
|
91 | 91 | }); |
92 | 92 | } |
93 | 93 |
|
| 94 | + /** |
| 95 | + * Handle the password reset button. Sets up an ajax callback to trigger sending |
| 96 | + * a password reset email. |
| 97 | + */ |
| 98 | + function bindPasswordRestLink() { |
| 99 | + $( '#generate-reset-link' ).on( 'click', function() { |
| 100 | + var $this = $(this), |
| 101 | + data = { |
| 102 | + 'user_id': userProfileL10n.user_id, // The user to send a reset to. |
| 103 | + 'nonce': userProfileL10n.nonce // Nonce to validate the action. |
| 104 | + }; |
| 105 | + |
| 106 | + // Remove any previous error messages. |
| 107 | + $this.parent().find( '.notice-error' ).remove(); |
| 108 | + |
| 109 | + // Send the reset request. |
| 110 | + var resetAction = wp.ajax.post( 'send-password-reset', data ); |
| 111 | + |
| 112 | + // Handle reset success. |
| 113 | + resetAction.done( function( response ) { |
| 114 | + addInlineNotice( $this, true, response ); |
| 115 | + } ); |
| 116 | + |
| 117 | + // Handle reset failure. |
| 118 | + resetAction.fail( function( response ) { |
| 119 | + addInlineNotice( $this, false, response ); |
| 120 | + } ); |
| 121 | + |
| 122 | + }); |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Helper function to insert an inline notice of success or failure. |
| 128 | + * |
| 129 | + * @param {jQuery Object} $this The button element: the message will be inserted |
| 130 | + * above this button |
| 131 | + * @param {bool} success Whether the message is a success message. |
| 132 | + * @param {string} message The message to insert. |
| 133 | + */ |
| 134 | + function addInlineNotice( $this, success, message ) { |
| 135 | + var resultDiv = $( '<div />' ); |
| 136 | + |
| 137 | + // Set up the notice div. |
| 138 | + resultDiv.addClass( 'notice inline' ); |
| 139 | + |
| 140 | + // Add a class indicating success or failure. |
| 141 | + resultDiv.addClass( 'notice-' + ( success ? 'success' : 'error' ) ); |
| 142 | + |
| 143 | + // Add the message, wrapping in a p tag, with a fadein to highlight each message. |
| 144 | + resultDiv.text( $( $.parseHTML( message ) ).text() ).wrapInner( '<p />'); |
| 145 | + |
| 146 | + // Disable the button when the callback has succeeded. |
| 147 | + $this.prop( 'disabled', success ); |
| 148 | + |
| 149 | + // Remove any previous notices. |
| 150 | + $this.siblings( '.notice' ).remove(); |
| 151 | + |
| 152 | + // Insert the notice. |
| 153 | + $this.before( resultDiv ); |
| 154 | + } |
| 155 | + |
94 | 156 | function bindPasswordForm() { |
95 | 157 | var $generateButton, |
96 | 158 | $cancelButton; |
|
369 | 431 | }); |
370 | 432 |
|
371 | 433 | bindPasswordForm(); |
| 434 | + bindPasswordRestLink(); |
372 | 435 | }); |
373 | 436 |
|
374 | 437 | $( '#destroy-sessions' ).on( 'click', function( e ) { |
|
0 commit comments