I18N: Introduce switch_to_user_locale().

This new function makes it easier to switch to a specific user’s locale by reducing duplicate code and storing the user’s ID as additional context for plugins to consume. Existing usage of `switch_to_locale()` in core has been replaced with `switch_to_user_locale()` where appropriate.

Also, this change ensures `WP_Locale_Switcher` properly filters `determine_locale` so that anyyone using the `determine_locale()` function will get the correct locale information when switching is in effect.

Props costdev.
Fixes #57123.
See #26511.

git-svn-id: https://develop.svn.wordpress.org/trunk@55161 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2023-01-30 10:25:53 +00:00
parent 3b4132d6c0
commit bce2a0a534
13 changed files with 215 additions and 72 deletions
+6 -10
View File
@@ -2578,7 +2578,7 @@ function wp_update_user( $userdata ) {
$switched_locale = false;
if ( ! empty( $send_password_change_email ) || ! empty( $send_email_change_email ) ) {
$switched_locale = switch_to_locale( get_user_locale( $user_id ) );
$switched_locale = switch_to_user_locale( $user_id );
}
if ( ! empty( $send_password_change_email ) ) {
@@ -3139,7 +3139,7 @@ function retrieve_password( $user_login = null ) {
// Localize password reset message content for user.
$locale = get_user_locale( $user_data );
$switched_locale = switch_to_locale( $locale );
$switched_locale = switch_to_user_locale( $user_data->ID );
if ( is_multisite() ) {
$site_name = get_network()->site_name;
@@ -4244,13 +4244,11 @@ function _wp_privacy_send_erasure_fulfillment_notification( $request_id ) {
// Localize message content for user; fallback to site default for visitors.
if ( ! empty( $request->user_id ) ) {
$locale = get_user_locale( $request->user_id );
$switched_locale = switch_to_user_locale( $request->user_id );
} else {
$locale = get_locale();
$switched_locale = switch_to_locale( get_locale() );
}
$switched_locale = switch_to_locale( $locale );
/**
* Filters the recipient of the data erasure fulfillment notification.
*
@@ -4655,13 +4653,11 @@ function wp_send_user_request( $request_id ) {
// Localize message content for user; fallback to site default for visitors.
if ( ! empty( $request->user_id ) ) {
$locale = get_user_locale( $request->user_id );
$switched_locale = switch_to_user_locale( $request->user_id );
} else {
$locale = get_locale();
$switched_locale = switch_to_locale( get_locale() );
}
$switched_locale = switch_to_locale( $locale );
$email_data = array(
'request' => $request,
'email' => $request->email,