Skip to content

Commit 5d0753b

Browse files
committed
Application Passwords: Add copy button when adding new password.
Props circlecube, dhruvang21, ironprogrammer, desrosj. Fixes #62019. git-svn-id: https://develop.svn.wordpress.org/trunk@59046 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b080980 commit 5d0753b

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

src/js/_enqueues/admin/user-profile.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
* @output wp-admin/js/user-profile.js
33
*/
44

5-
/* global ajaxurl, pwsL10n, userProfileL10n */
5+
/* global ajaxurl, pwsL10n, userProfileL10n, ClipboardJS */
66
(function($) {
77
var updateLock = false,
88
isSubmitting = false,
99
__ = wp.i18n.__,
10+
clipboard = new ClipboardJS( '.application-password-display .copy-button' ),
1011
$pass1Row,
1112
$pass1,
1213
$pass2,
@@ -18,7 +19,8 @@
1819
currentPass,
1920
$form,
2021
originalFormContent,
21-
$passwordWrapper;
22+
$passwordWrapper,
23+
successTimeout;
2224

2325
function generatePassword() {
2426
if ( typeof zxcvbn !== 'function' ) {
@@ -346,6 +348,27 @@
346348
}
347349
}
348350

351+
// Debug information copy section.
352+
clipboard.on( 'success', function( e ) {
353+
var triggerElement = $( e.trigger ),
354+
successElement = $( '.success', triggerElement.closest( '.application-password-display' ) );
355+
356+
// Clear the selection and move focus back to the trigger.
357+
e.clearSelection();
358+
359+
// Show success visual feedback.
360+
clearTimeout( successTimeout );
361+
successElement.removeClass( 'hidden' );
362+
363+
// Hide success visual feedback after 3 seconds since last success.
364+
successTimeout = setTimeout( function() {
365+
successElement.addClass( 'hidden' );
366+
}, 3000 );
367+
368+
// Handle success audible feedback.
369+
wp.a11y.speak( __( 'Application password has been copied to your clipboard.' ) );
370+
} );
371+
349372
$( function() {
350373
var $colorpicker, $stylesheet, user_id, current_user_id,
351374
select = $( '#display_name' ),

src/wp-admin/css/common.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,11 @@ a#remove-post-thumbnail:hover,
916916
border: none;
917917
}
918918

919+
.application-password-display .success {
920+
color: #007017;
921+
margin-left: 0.5rem;
922+
}
923+
919924
/*------------------------------------------------------------------------------
920925
3.0 - Actions
921926
------------------------------------------------------------------------------*/

src/wp-admin/css/forms.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ table.form-table td .updated p {
10021002
}
10031003

10041004
.application-password-display input.code {
1005+
margin-bottom: 6px;
10051006
width: 19em;
10061007
}
10071008

src/wp-admin/user-edit.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,8 @@
984984
?>
985985
</label>
986986
<input id="new-application-password-value" type="text" class="code" readonly="readonly" value="{{ data.password }}" />
987+
<button type="button" class="button copy-button" data-clipboard-text="{{ data.password }}"><?php _e( 'Copy' ); ?></button>
988+
<span class="success hidden" aria-hidden="true"><?php _e( 'Copied!' ); ?></span>
987989
</p>
988990
<p><?php _e( 'Be sure to save this in a safe location. You will not be able to retrieve it.' ); ?></p>
989991
<button type="button" class="notice-dismiss">

src/wp-includes/script-loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ function wp_default_scripts( $scripts ) {
12131213
$scripts->add( 'auth-app', "/wp-admin/js/auth-app$suffix.js", array( 'jquery', 'wp-api-request', 'wp-i18n', 'wp-hooks' ), false, 1 );
12141214
$scripts->set_translations( 'auth-app' );
12151215

1216-
$scripts->add( 'user-profile', "/wp-admin/js/user-profile$suffix.js", array( 'jquery', 'password-strength-meter', 'wp-util' ), false, 1 );
1216+
$scripts->add( 'user-profile', "/wp-admin/js/user-profile$suffix.js", array( 'clipboard', 'jquery', 'password-strength-meter', 'wp-util', 'wp-a11y' ), false, 1 );
12171217
$scripts->set_translations( 'user-profile' );
12181218
$user_id = isset( $_GET['user_id'] ) ? (int) $_GET['user_id'] : 0;
12191219
did_action( 'init' ) && $scripts->localize(

tests/e2e/specs/profile/applications-passwords.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ test.describe( 'Manage applications passwords', () => {
3131
await expect(
3232
successMessage
3333
).toContainText(
34-
`Your new password for ${TEST_APPLICATION_NAME} is: \n\nBe sure to save this in a safe location. You will not be able to retrieve it.`
34+
`Your new password for ${TEST_APPLICATION_NAME} is:`
35+
);
36+
await expect(
37+
successMessage
38+
).toContainText(
39+
`Be sure to save this in a safe location. You will not be able to retrieve it.`
3540
);
3641
} );
3742

0 commit comments

Comments
 (0)