Skip to content

Commit d09b315

Browse files
committed
Application Passwords: Send the created notification in the user's locale.
This ensures the receipient sees the notification in their chosen language when a password is added by a user that uses a different language. Developed in #16065 Props mukesh27, peterwilsoncc. Fixes #63927 git-svn-id: https://develop.svn.wordpress.org/trunk@65740 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9d7b7c2 commit d09b315

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎src/wp-includes/functions.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9641,6 +9641,8 @@ function wp_application_password_created_notification( $user_id, $new_item ) {
96419641
return;
96429642
}
96439643

9644+
$switched_locale = switch_to_user_locale( $user->ID );
9645+
96449646
/* translators: Do not translate USERNAME, APPLICATION_PASSWORD_NAME, ADMIN_EMAIL, SITENAME, SITEURL, EMAIL: those are placeholders. */
96459647
$application_password_create_text = __(
96469648
'Hi ###USERNAME###,
@@ -9724,4 +9726,8 @@ function wp_application_password_created_notification( $user_id, $new_item ) {
97249726
$email['message'],
97259727
$email['headers']
97269728
);
9729+
9730+
if ( $switched_locale ) {
9731+
restore_previous_locale();
9732+
}
97279733
}

‎tests/phpunit/tests/functions/wpApplicationPasswordCreatedNotification.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,27 @@ public function test_does_not_send_email_when_user_email_is_invalid() {
202202

203203
$this->assertEmpty( tests_retrieve_phpmailer_instance()->mock_sent );
204204
}
205+
206+
/**
207+
* Tests that the email is built in the recipient's locale, and the locale is restored after.
208+
*
209+
* @ticket 63927
210+
*/
211+
public function test_email_uses_the_user_locale() {
212+
update_user_meta( self::$user->ID, 'locale', 'de_DE' );
213+
214+
$email_locale = null;
215+
add_filter(
216+
'wp_application_password_created_email',
217+
static function ( $email ) use ( &$email_locale ) {
218+
$email_locale = get_locale();
219+
return $email;
220+
}
221+
);
222+
223+
WP_Application_Passwords::create_new_application_password( self::$user->ID, array( 'name' => 'My App' ) );
224+
225+
$this->assertSame( 'de_DE', $email_locale, 'The email should be built in the user locale.' );
226+
$this->assertFalse( is_locale_switched(), 'The previous locale should be restored after the email is sent.' );
227+
}
205228
}

0 commit comments

Comments
 (0)