Skip to content

Commit b545eba

Browse files
feat: add timezone support
1 parent 292fdcc commit b545eba

5 files changed

Lines changed: 286 additions & 66 deletions

File tree

‎src/js/_enqueues/admin/application-passwords.js‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444

4545
var expires = $newAppPassExpiresField.val();
4646
if ( expires ) {
47-
var expiresDate = new Date( expires );
47+
var expiresDate = wp.date.getDate( expires );
4848

49-
if ( ! isNaN( expiresDate.getTime() ) ) {
50-
request.expires = expiresDate.toISOString();
51-
}
49+
if ( ! isNaN( expiresDate.getTime() ) ) {
50+
request.expires = expiresDate.toISOString();
51+
}
5252
}
5353

5454
/**
@@ -113,18 +113,27 @@
113113
}
114114

115115
var $form = $( '<div class="edit-expires-form"></div>' );
116-
var $input = $( '<input type="date" class="edit-expires-input" />' );
116+
var timezoneDescriptionId = 'application-password-timezone-' + uuid;
117+
var $input = $( '<input type="datetime-local" class="edit-expires-input" step="1" />' )
118+
.attr( 'aria-label', wp.i18n.__( 'Expiration date and time' ) )
119+
.attr( 'aria-describedby', timezoneDescriptionId );
117120

118121
if ( currentExpires ) {
119-
$input.val( currentExpires.split( 'T' )[0] );
122+
$input.val( wp.date.date( 'Y-m-d\\TH:i:s', currentExpires + 'Z' ) );
120123
}
124+
var initialExpires = $input.val();
121125

122126
var $buttonContainer = $( '<div class="edit-expires-button-group"></div>' );
123127
var $saveBtn = $( '<button type="button" class="button button-small button-primary">' + wp.i18n.__( 'Save' ) + '</button>' );
124128
var $cancelBtn = $( '<button type="button" class="button button-small">' + wp.i18n.__( 'Cancel' ) + '</button>' );
125129

126130
$buttonContainer.append( $saveBtn ).append( $cancelBtn );
127-
$form.append( $input ).append( $buttonContainer );
131+
$form.append( $input )
132+
.append( $( '<p class="description"></p>' ).attr( 'id', timezoneDescriptionId ).text(
133+
/* translators: %s: Site timezone. */
134+
wp.i18n.sprintf( wp.i18n.__( 'Timezone: %s' ), $appPassSection.data( 'timezone' ) )
135+
) )
136+
.append( $buttonContainer );
128137

129138
$td.append( $form );
130139
$button.hide();
@@ -148,11 +157,15 @@
148157

149158
$saveBtn.on( 'click', function() {
150159
var newExpires = $input.val();
151-
var expiresDate = newExpires ? new Date( newExpires ) : null;
160+
var expiresDate = newExpires ? wp.date.getDate( newExpires ) : null;
152161
var requestData = {
153162
expires: ( expiresDate && ! isNaN( expiresDate.getTime() ) ) ? expiresDate.toISOString() : null
154163
};
155164

165+
if ( currentExpires && newExpires === initialExpires ) {
166+
requestData.expires = currentExpires + 'Z';
167+
}
168+
156169
clearNotices();
157170
$saveBtn.prop( 'disabled', true );
158171
$cancelBtn.prop( 'disabled', true );

‎src/wp-admin/css/forms.css‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ table.form-table td .updated p {
10721072
.edit-expires-input {
10731073
display: block;
10741074
width: 100%;
1075-
max-width: 150px;
1075+
max-width: 20em;
10761076
margin-bottom: 5px;
10771077
}
10781078

‎src/wp-admin/includes/class-wp-application-passwords-list-table.php‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,20 @@ public function column_expires( $item ) {
113113
if ( empty( $item['expires'] ) ) {
114114
echo '&mdash;';
115115
} else {
116-
$date = date_i18n( __( 'F j, Y' ), $item['expires'] );
116+
$date = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $item['expires'] );
117117
if ( time() > $item['expires'] ) {
118118
printf(
119119
'%s',
120-
/* translators: %s: Expiration date for the Application Password. */
121-
sprintf( __( 'Expired on %s' ), $date )
120+
/* translators: %s: Expiration date and time for the Application Password. */
121+
sprintf( esc_html__( 'Expired on %s' ), esc_html( $date ) )
122122
);
123123
} else {
124-
echo $date;
124+
echo esc_html( $date );
125125
}
126126
}
127127
printf(
128128
'<br><button type="button" class="button-link edit-expires" aria-label="%s">%s</button>',
129-
esc_attr__( 'Edit Application Password Expiration Date' ),
129+
esc_attr__( 'Edit Expiration Date and Time' ),
130130
esc_html__( 'Edit Expiry' )
131131
);
132132
}
@@ -208,7 +208,8 @@ protected function display_tablenav( $which ) {
208208
* @param array $item The current item.
209209
*/
210210
public function single_row( $item ) {
211-
echo '<tr data-uuid="' . esc_attr( $item['uuid'] ) . '">';
211+
$expires = ! empty( $item['expires'] ) ? gmdate( 'Y-m-d\TH:i:s', $item['expires'] ) : '';
212+
echo '<tr data-uuid="' . esc_attr( $item['uuid'] ) . '" data-expires="' . esc_attr( $expires ) . '">';
212213
$this->single_row_columns( $item );
213214
echo '</tr>';
214215
}
@@ -232,7 +233,7 @@ protected function get_default_primary_column_name() {
232233
public function print_js_template_row() {
233234
list( $columns, $hidden, , $primary ) = $this->get_column_info();
234235

235-
echo '<tr data-uuid="{{ data.uuid }}">';
236+
echo '<tr data-uuid="{{ data.uuid }}" data-expires="{{ data.expires || \'\' }}">';
236237

237238
foreach ( $columns as $column_name => $display_name ) {
238239
$is_primary = $primary === $column_name;
@@ -270,11 +271,12 @@ public function print_js_template_row() {
270271
case 'expires':
271272
?>
272273
<# if ( data.expires ) { #>
273-
<# var isExpired = new Date().getTime() > new Date( data.expires ).getTime(); #>
274-
<# var formattedDate = wp.date.dateI18n( <?php echo wp_json_encode( __( 'F j, Y' ) ); ?>, data.expires ); #>
274+
<# var expiresDate = new Date( data.expires + 'Z' ); #>
275+
<# var isExpired = new Date().getTime() > expiresDate.getTime(); #>
276+
<# var formattedDate = wp.date.dateI18n( <?php echo wp_json_encode( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ); ?>, expiresDate ); #>
275277
<# if ( isExpired ) { #>
276278
<?php
277-
/* translators: %s: Expiration date for the Application Password. */
279+
/* translators: %s: Expiration date and time for the Application Password. */
278280
printf( esc_html__( 'Expired on %s' ), '{{ formattedDate }}' );
279281
?>
280282
<# } else { #>
@@ -283,7 +285,7 @@ public function print_js_template_row() {
283285
<# } else { #>
284286
—
285287
<# } #>
286-
<br><button type="button" class="button-link edit-expires" aria-label="<?php esc_attr_e( 'Edit Expiration Date' ); ?>"><?php esc_html_e( 'Edit Expiry' ); ?></button>
288+
<br><button type="button" class="button-link edit-expires" aria-label="<?php esc_attr_e( 'Edit Expiration Date and Time' ); ?>"><?php esc_html_e( 'Edit Expiry' ); ?></button>
287289
<?php
288290
break;
289291
case 'revoke':

‎src/wp-admin/user-edit.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@
811811
</table>
812812

813813
<?php if ( wp_is_application_passwords_available_for_user( $user_id ) || ! wp_is_application_passwords_supported() ) : ?>
814-
<div class="application-passwords hide-if-no-js" id="application-passwords-section">
814+
<div class="application-passwords hide-if-no-js" id="application-passwords-section" data-timezone="<?php echo esc_attr( wp_timezone_string() ); ?>">
815815
<h2><?php _e( 'Application Passwords' ); ?></h2>
816816
<p><?php _e( 'Application passwords allow authentication via non-interactive systems, such as XML-RPC or the REST API, without providing your actual password. Application passwords can be easily revoked. They cannot be used for traditional logins to your website.' ); ?></p>
817817
<?php if ( wp_is_application_passwords_available_for_user( $user_id ) ) : ?>
@@ -862,8 +862,8 @@
862862

863863
<div class="form-field">
864864
<label for="new_application_password_expires"><?php _e( 'Expires on' ); ?></label>
865-
<input type="date" id="new_application_password_expires" name="new_application_password_expires" class="input ltr" />
866-
<p class="description"><?php _e( 'Optional. Set an expiration date for this password.' ); ?></p>
865+
<input type="datetime-local" id="new_application_password_expires" name="new_application_password_expires" class="input ltr" step="1" aria-describedby="new_application_password_expires_desc application-passwords-timezone" />
866+
<p class="description" id="new_application_password_expires_desc"><?php _e( 'Optional. Set an expiration date and time for this password.' ); ?></p>
867867
</div>
868868

869869
<?php
@@ -877,6 +877,12 @@
877877
do_action( 'wp_create_application_password_form', $profile_user );
878878
?>
879879

880+
<p class="description" id="application-passwords-timezone">
881+
<?php
882+
/* translators: %s: Site timezone. */
883+
printf( esc_html__( 'Timezone: %s' ), esc_html( wp_timezone_string() ) );
884+
?>
885+
</p>
880886
<button type="button" name="do_new_application_password" id="do_new_application_password" class="button button-secondary"><?php _e( 'Add Application Password' ); ?></button>
881887
</div>
882888
<?php

0 commit comments

Comments
 (0)