<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[55161] trunk: I18N: Introduce `switch_to_user_locale()`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/55161">55161</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/55161","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>swissspidy</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-01-30 10:25:53 +0000 (Mon, 30 Jan 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/57123">#57123</a>.
See <a href="https://core.trac.wordpress.org/ticket/26511">#26511</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesmiscphp">trunk/src/wp-admin/includes/misc.php</a></li>
<li><a href="#trunksrcwpadminincludesprivacytoolsphp">trunk/src/wp-admin/includes/privacy-tools.php</a></li>
<li><a href="#trunksrcwpadminusernewphp">trunk/src/wp-admin/user-new.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizewidgetsphp">trunk/src/wp-includes/class-wp-customize-widgets.php</a></li>
<li><a href="#trunksrcwpincludesclasswplocaleswitcherphp">trunk/src/wp-includes/class-wp-locale-switcher.php</a></li>
<li><a href="#trunksrcwpincludesclasswprecoverymodeemailservicephp">trunk/src/wp-includes/class-wp-recovery-mode-email-service.php</a></li>
<li><a href="#trunksrcwpincludescustomizeclasswpcustomizeselectiverefreshphp">trunk/src/wp-includes/customize/class-wp-customize-selective-refresh.php</a></li>
<li><a href="#trunksrcwpincludesl10nphp">trunk/src/wp-includes/l10n.php</a></li>
<li><a href="#trunksrcwpincludesmsfunctionsphp">trunk/src/wp-includes/ms-functions.php</a></li>
<li><a href="#trunksrcwpincludespluggablephp">trunk/src/wp-includes/pluggable.php</a></li>
<li><a href="#trunksrcwpincludesuserphp">trunk/src/wp-includes/user.php</a></li>
<li><a href="#trunktestsphpunittestsl10nwpLocaleSwitcherphp">trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesmiscphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/misc.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/misc.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-admin/includes/misc.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1457,7 +1457,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> update_option( 'adminhash', $new_admin_email );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( get_current_user_id() );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
</span><span class="cx" style="display: block; padding: 0 10px"> $email_text = __(
</span></span></pre></div>
<a id="trunksrcwpadminincludesprivacytoolsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/privacy-tools.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/privacy-tools.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-admin/includes/privacy-tools.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -595,13 +595,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Localize message content for user; fallback to site default for visitors.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request->user_id ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale = get_user_locale( $request->user_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $request->user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale = get_locale();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_locale( get_locale() );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( $locale );
-
</del><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-includes/functions.php */
</span><span class="cx" style="display: block; padding: 0 10px"> $expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
</span><span class="cx" style="display: block; padding: 0 10px"> $expiration_date = date_i18n( get_option( 'date_format' ), time() + $expiration );
</span></span></pre></div>
<a id="trunksrcwpadminusernewphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/user-new.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/user-new.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-admin/user-new.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -110,7 +110,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> do_action( 'invite_user', $user_id, $role, $newuser_key );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale( $user_details ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( '' !== get_option( 'blogname' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $site_title = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-manager.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-includes/class-wp-customize-manager.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2134,7 +2134,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $allowed_hosts[] = $host;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( get_current_user_id() );
</ins><span class="cx" style="display: block; padding: 0 10px"> $l10n = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'shiftClickToEdit' => __( 'Shift-click to edit this element.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'linkUnpreviewable' => __( 'This link is not live-previewable.' ),
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizewidgetsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-widgets.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-includes/class-wp-customize-widgets.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1264,7 +1264,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function export_preview_data() {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_registered_sidebars, $wp_registered_widgets;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( get_current_user_id() );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $l10n = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'widgetTooltip' => __( 'Shift-click to edit this widget.' ),
</span></span></pre></div>
<a id="trunksrcwpincludesclasswplocaleswitcherphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-locale-switcher.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-locale-switcher.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-includes/class-wp-locale-switcher.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -15,12 +15,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> #[AllowDynamicProperties]
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_Locale_Switcher {
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Locale stack.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Locale switching stack.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @var string[]
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @var array
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private $locales = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private $stack = array();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Original locale.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -53,12 +53,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Initializes the locale switcher.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Hooks into the {@see 'locale'} filter to change the locale on the fly.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Hooks into the {@see 'locale'} and {@see 'determine_locale'} filters
+ * to change the locale on the fly.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function init() {
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'locale', array( $this, 'filter_locale' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_filter( 'determine_locale', array( $this, 'filter_locale' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -66,10 +68,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $locale The locale to switch to.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $locale The locale to switch to.
+ * @param int|false $user_id Optional. User ID as context. Default false.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return bool True on success, false on failure.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function switch_to_locale( $locale ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function switch_to_locale( $locale, $user_id = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $current_locale = determine_locale();
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $current_locale === $locale ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -79,7 +82,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->locales[] = $locale;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->stack[] = array( $locale, $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->change_locale( $locale );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -87,15 +90,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires when the locale is switched.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.2.0 The `$user_id` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $locale The new locale.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $locale The new locale.
+ * @param null|int $user_id User ID for context if available.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- do_action( 'switch_locale', $locale );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ do_action( 'switch_locale', $locale, $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Switches the translations according to the given user's locale.
+ *
+ * @since 6.2.0
+ *
+ * @param int $user_id User ID.
+ * @return bool True on success, false on failure.
+ */
+ public function switch_to_user_locale( $user_id ) {
+ $locale = get_user_locale( $user_id );
+ return $this->switch_to_locale( $locale, $user_id );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Restores the translations according to the previous locale.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -103,7 +121,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string|false Locale on success, false on failure.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function restore_previous_locale() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $previous_locale = array_pop( $this->locales );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $previous_locale = array_pop( $this->stack );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( null === $previous_locale ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // The stack is empty, bail.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -110,7 +128,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale = end( $this->locales );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $entry = end( $this->stack );
+ $locale = is_array( $entry ) ? $entry[0] : false;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $locale ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // There's nothing left in the stack: go back to the original locale.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -127,7 +146,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $locale The new locale.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $previous_locale The previous locale.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- do_action( 'restore_previous_locale', $locale, $previous_locale );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ do_action( 'restore_previous_locale', $locale, $previous_locale[0] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $locale;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -140,11 +159,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string|false Locale on success, false on failure.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function restore_current_locale() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $this->locales ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $this->stack ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->locales = array( $this->original_locale );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->stack = array( array( $this->original_locale, false ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $this->restore_previous_locale();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -157,10 +176,44 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return bool True if the locale has been switched, false otherwise.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function is_switched() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return ! empty( $this->locales );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return ! empty( $this->stack );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Returns the locale currently switched to.
+ *
+ * @since 6.2.0
+ *
+ * @return string|false Locale if the locale has been switched, false otherwise.
+ */
+ public function get_current_locale() {
+ $entry = end( $this->stack );
+
+ if ( $entry ) {
+ return $entry[0];
+ }
+
+ return false;
+ }
+
+ /**
+ * Returns the user ID related to the currently switched locale.
+ *
+ * @since 6.2.0
+ *
+ * @return int|false User ID if set and if the locale has been switched, false otherwise.
+ */
+ public function get_current_user_id() {
+ $entry = end( $this->stack );
+
+ if ( $entry ) {
+ return $entry[1];
+ }
+
+ return false;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Filters the locale of the WordPress installation.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -169,7 +222,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string The locale currently being switched to.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function filter_locale( $locale ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = end( $this->locales );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = $this->get_current_locale();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $switched_locale ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $switched_locale;
</span></span></pre></div>
<a id="trunksrcwpincludesclasswprecoverymodeemailservicephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-recovery-mode-email-service.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-recovery-mode-email-service.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-includes/class-wp-recovery-mode-email-service.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -116,13 +116,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $url = $this->link_service->generate_url();
</span><span class="cx" style="display: block; padding: 0 10px"> $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_locale( get_locale() );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The switch_to_locale() function is loaded before it can actually be used.
- if ( function_exists( 'switch_to_locale' ) && isset( $GLOBALS['wp_locale_switcher'] ) ) {
- $switched_locale = switch_to_locale( get_locale() );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( $extension ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $cause = $this->get_cause( $extension );
</span><span class="cx" style="display: block; padding: 0 10px"> $details = wp_strip_all_tags( wp_get_extension_error_description( $error ) );
</span></span></pre></div>
<a id="trunksrcwpincludescustomizeclasswpcustomizeselectiverefreshphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/customize/class-wp-customize-selective-refresh.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/customize/class-wp-customize-selective-refresh.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-includes/customize/class-wp-customize-selective-refresh.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -172,7 +172,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( get_current_user_id() );
</ins><span class="cx" style="display: block; padding: 0 10px"> $l10n = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'shiftClickToEdit' => __( 'Shift-click to edit this element.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'clickEditMenu' => __( 'Click to edit this menu.' ),
</span></span></pre></div>
<a id="trunksrcwpincludesl10nphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/l10n.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/l10n.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-includes/l10n.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1665,10 +1665,35 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /* @var WP_Locale_Switcher $wp_locale_switcher */
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_locale_switcher;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $wp_locale_switcher ) {
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $wp_locale_switcher->switch_to_locale( $locale );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Switches the translations according to the given user's locale.
+ *
+ * @since 6.2.0
+ *
+ * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
+ *
+ * @param int $user_id User ID.
+ * @return bool True on success, false on failure.
+ */
+function switch_to_user_locale( $user_id ) {
+ /* @var WP_Locale_Switcher $wp_locale_switcher */
+ global $wp_locale_switcher;
+
+ if ( ! $wp_locale_switcher ) {
+ return false;
+ }
+
+ return $wp_locale_switcher->switch_to_user_locale( $user_id );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Restores the translations according to the previous locale.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1681,6 +1706,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /* @var WP_Locale_Switcher $wp_locale_switcher */
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_locale_switcher;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $wp_locale_switcher ) {
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $wp_locale_switcher->restore_previous_locale();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1697,6 +1726,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /* @var WP_Locale_Switcher $wp_locale_switcher */
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_locale_switcher;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $wp_locale_switcher ) {
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $wp_locale_switcher->restore_current_locale();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/ms-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/ms-functions.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-includes/ms-functions.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -954,7 +954,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user = get_user_by( 'login', $user_login );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale( $user ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = $user && switch_to_user_locale( $user->ID );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $message = sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1068,7 +1068,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user = get_user_by( 'login', $user_login );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale( $user ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = $user && switch_to_user_locale( $user->ID );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Send email with activation link.
</span><span class="cx" style="display: block; padding: 0 10px"> $admin_email = get_site_option( 'admin_email' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1610,7 +1610,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user = get_userdata( $user_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale( $user ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $welcome_email = get_site_option( 'welcome_email' );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( false == $welcome_email ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1734,7 +1734,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $network_admin ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // If the network admin email address corresponds to a user, switch to their locale.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale( $network_admin ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $network_admin->ID );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> // Otherwise switch to the locale of the current site.
</span><span class="cx" style="display: block; padding: 0 10px"> $switched_locale = switch_to_locale( get_locale() );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1843,7 +1843,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user = get_userdata( $user_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale( $user ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the content of the welcome email after user activation.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2726,7 +2726,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> update_site_option( 'network_admin_hash', $new_admin_email );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( get_current_user_id() );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
</span><span class="cx" style="display: block; padding: 0 10px"> $email_text = __(
</span></span></pre></div>
<a id="trunksrcwpincludespluggablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/pluggable.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/pluggable.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-includes/pluggable.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2188,7 +2188,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale( $user ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: User login. */
</span><span class="cx" style="display: block; padding: 0 10px"> $message = sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n";
</span></span></pre></div>
<a id="trunksrcwpincludesuserphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/user.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/src/wp-includes/user.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2578,7 +2578,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $switched_locale = false;
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $send_password_change_email ) || ! empty( $send_email_change_email ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( get_user_locale( $user_id ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $send_password_change_email ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3139,7 +3139,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Localize password reset message content for user.
</span><span class="cx" style="display: block; padding: 0 10px"> $locale = get_user_locale( $user_data );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( $locale );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $user_data->ID );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_multisite() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $site_name = get_network()->site_name;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4244,13 +4244,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Localize message content for user; fallback to site default for visitors.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request->user_id ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale = get_user_locale( $request->user_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $request->user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale = get_locale();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_locale( get_locale() );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( $locale );
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the recipient of the data erasure fulfillment notification.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4655,13 +4653,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Localize message content for user; fallback to site default for visitors.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request->user_id ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale = get_user_locale( $request->user_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_user_locale( $request->user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $locale = get_locale();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $switched_locale = switch_to_locale( get_locale() );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $switched_locale = switch_to_locale( $locale );
-
</del><span class="cx" style="display: block; padding: 0 10px"> $email_data = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'request' => $request,
</span><span class="cx" style="display: block; padding: 0 10px"> 'email' => $request->email,
</span></span></pre></div>
<a id="trunktestsphpunittestsl10nwpLocaleSwitcherphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php 2023-01-29 16:24:02 UTC (rev 55160)
+++ trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php 2023-01-30 10:25:53 UTC (rev 55161)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -16,6 +16,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected $previous_locale = '';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @var int
+ */
+ protected static $user_id;
+
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$user_id = $factory->user->create(
+ array(
+ 'role' => 'administrator',
+ 'locale' => 'de_DE',
+ )
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function set_up() {
</span><span class="cx" style="display: block; padding: 0 10px"> parent::set_up();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -90,7 +104,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 57123
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers ::switch_to_locale
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ */
+ public function test_switch_to_locale_changes_determined_locale() {
+ switch_to_locale( 'en_GB' );
+
+ $locale = determine_locale();
+
+ // Cleanup.
+ restore_previous_locale();
+
+ $this->assertSame( 'en_GB', $locale );
+ }
+
+ /**
+ * @covers ::switch_to_locale
</ins><span class="cx" style="display: block; padding: 0 10px"> * @covers ::translate
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::__
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -332,14 +362,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $site_locale = get_locale();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $user_id = self::factory()->user->create(
- array(
- 'role' => 'administrator',
- 'locale' => 'de_DE',
- )
- );
-
- wp_set_current_user( $user_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_set_current_user( self::$user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> set_current_screen( 'dashboard' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Reset $wp_locale_switcher so it thinks es_ES is the original locale.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -382,14 +405,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $site_locale = get_locale();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $user_id = self::factory()->user->create(
- array(
- 'role' => 'administrator',
- 'locale' => 'de_DE',
- )
- );
-
- wp_set_current_user( $user_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_set_current_user( self::$user_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> set_current_screen( 'dashboard' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Reset $wp_locale_switcher so it thinks es_ES is the original locale.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -430,14 +446,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_multiple_switches_to_site_locale_and_user_locale() {
</span><span class="cx" style="display: block; padding: 0 10px"> $site_locale = get_locale();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $user_id = self::factory()->user->create(
- array(
- 'role' => 'administrator',
- 'locale' => 'en_GB',
- )
- );
-
- wp_set_current_user( $user_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_set_current_user( self::$user_id );
+ update_user_meta( self::$user_id, 'locale', 'en_GB' );
</ins><span class="cx" style="display: block; padding: 0 10px"> set_current_screen( 'dashboard' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user_locale = get_user_locale();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -583,6 +593,64 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'Este es un plugin dummy', $actual_es_es );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @ticket 57123
+ *
+ * @covers ::switch_to_locale
+ * @covers ::switch_to_user_locale
+ * @covers WP_Locale_Switcher::get_current_locale
+ * @covers WP_Locale_Switcher::get_current_user_id
+ */
+ public function test_returns_current_locale_and_user_after_switching() {
+ global $wp_locale_switcher;
+
+ $user_2 = self::factory()->user->create(
+ array(
+ 'role' => 'administrator',
+ 'locale' => 'es_ES',
+ )
+ );
+
+ $locale_1 = $wp_locale_switcher->get_current_locale();
+ $user_id_1 = $wp_locale_switcher->get_current_user_id();
+
+ switch_to_user_locale( self::$user_id );
+
+ $locale_2 = $wp_locale_switcher->get_current_locale();
+ $user_id_2 = $wp_locale_switcher->get_current_user_id();
+
+ switch_to_locale( 'en_GB' );
+
+ $locale_3 = $wp_locale_switcher->get_current_locale();
+ $user_id_3 = $wp_locale_switcher->get_current_user_id();
+
+ switch_to_user_locale( $user_2 );
+
+ $locale_4 = $wp_locale_switcher->get_current_locale();
+ $user_id_4 = $wp_locale_switcher->get_current_user_id();
+
+ restore_current_locale();
+
+ $locale_5 = $wp_locale_switcher->get_current_locale();
+ $user_id_5 = $wp_locale_switcher->get_current_user_id();
+
+ $this->assertFalse( $locale_1, 'Locale should be false before switching' );
+ $this->assertFalse( $user_id_1, 'User ID should be false before switching' );
+
+ $this->assertSame( 'de_DE', $locale_2, 'The locale was not changed to de_DE' );
+ $this->assertSame( self::$user_id, $user_id_2, 'User ID should match the main admin ID' );
+
+ $this->assertSame( 'en_GB', $locale_3, 'The locale was not changed to en_GB' );
+ $this->assertFalse( $user_id_3, 'User ID should be false after normal locale switching' );
+
+ $this->assertSame( 'es_ES', $locale_4, 'The locale was not changed to es_ES' );
+ $this->assertSame( $user_2, $user_id_4, 'User ID should match the second admin ID' );
+
+ $this->assertFalse( $locale_5, 'Locale should be false after restoring' );
+ $this->assertFalse( $user_id_5, 'User ID should be false after restoring' );
+
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function filter_locale() {
</span><span class="cx" style="display: block; padding: 0 10px"> return 'es_ES';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>