<!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>[59128] trunk: I18N: Switch locale to admin locale when sending admin notifications.</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/59128">59128</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/59128","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>2024-09-30 15:50:19 +0000 (Mon, 30 Sep 2024)</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: Switch locale to admin locale when sending admin notifications.

If sending an email to the site administrator's email address, look up if a user with the same email exists and switch to that user's locale.
If not, falls back to the site locale as usual.

Props benniledl, swissspidy, mukesh27.
Fixes <a href="https://core.trac.wordpress.org/ticket/61518">#61518</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespluggablephp">trunk/src/wp-includes/pluggable.php</a></li>
<li><a href="#trunktestsphpunittestsuserphp">trunk/tests/phpunit/tests/user.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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       2024-09-30 15:28:26 UTC (rev 59127)
+++ trunk/src/wp-includes/pluggable.php 2024-09-30 15:50:19 UTC (rev 59128)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1719,8 +1719,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $emails = array_flip( $emails );
</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_locale() );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $comment_author_domain = '';
</span><span class="cx" style="display: block; padding: 0 10px">                if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $comment_author_domain = gethostbyaddr( $comment->comment_author_IP );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1733,74 +1731,6 @@
</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">                $comment_content = wp_specialchars_decode( $comment->comment_content );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                switch ( $comment->comment_type ) {
-                       case 'trackback':
-                               /* translators: %s: Post title. */
-                               $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
-                               /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
-                               $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
-                               /* translators: %s: Trackback/pingback/comment author URL. */
-                               $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
-                               /* translators: %s: Comment text. */
-                               $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
-                               $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n";
-                               /* translators: Trackback notification email subject. 1: Site title, 2: Post title. */
-                               $subject = sprintf( __( '[%1$s] Trackback: "%2$s"' ), $blogname, $post->post_title );
-                               break;
-
-                       case 'pingback':
-                               /* translators: %s: Post title. */
-                               $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
-                               /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
-                               $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
-                               /* translators: %s: Trackback/pingback/comment author URL. */
-                               $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
-                               /* translators: %s: Comment text. */
-                               $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
-                               $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n";
-                               /* translators: Pingback notification email subject. 1: Site title, 2: Post title. */
-                               $subject = sprintf( __( '[%1$s] Pingback: "%2$s"' ), $blogname, $post->post_title );
-                               break;
-
-                       default: // Comments.
-                               /* translators: %s: Post title. */
-                               $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
-                               /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */
-                               $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
-                               /* translators: %s: Comment author email. */
-                               $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
-                               /* translators: %s: Trackback/pingback/comment author URL. */
-                               $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
-
-                               if ( $comment->comment_parent && user_can( $post->post_author, 'edit_comment', $comment->comment_parent ) ) {
-                                       /* translators: Comment moderation. %s: Parent comment edit URL. */
-                                       $notify_message .= sprintf( __( 'In reply to: %s' ), admin_url( "comment.php?action=editcomment&c={$comment->comment_parent}#wpbody-content" ) ) . "\r\n";
-                               }
-
-                               /* translators: %s: Comment text. */
-                               $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
-                               $notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n";
-                               /* translators: Comment notification email subject. 1: Site title, 2: Post title. */
-                               $subject = sprintf( __( '[%1$s] Comment: "%2$s"' ), $blogname, $post->post_title );
-                               break;
-               }
-
-               $notify_message .= get_permalink( $comment->comment_post_ID ) . "#comments\r\n\r\n";
-               /* translators: %s: Comment URL. */
-               $notify_message .= sprintf( __( 'Permalink: %s' ), get_comment_link( $comment ) ) . "\r\n";
-
-               if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) {
-                       if ( EMPTY_TRASH_DAYS ) {
-                               /* translators: Comment moderation. %s: Comment action URL. */
-                               $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
-                       } else {
-                               /* translators: Comment moderation. %s: Comment action URL. */
-                               $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
-                       }
-                       /* translators: Comment moderation. %s: Comment action URL. */
-                       $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $wp_email = 'wordpress@' . preg_replace( '#^www\.#', '', wp_parse_url( network_home_url(), PHP_URL_HOST ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( '' === $comment->comment_author ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1823,26 +1753,6 @@
</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">-                 * Filters the comment notification email text.
-                *
-                * @since 1.5.2
-                *
-                * @param string $notify_message The comment notification email text.
-                * @param string $comment_id     Comment ID as a numeric string.
-                */
-               $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID );
-
-               /**
-                * Filters the comment notification email subject.
-                *
-                * @since 1.5.2
-                *
-                * @param string $subject    The comment notification email subject.
-                * @param string $comment_id Comment ID as a numeric string.
-                */
-               $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID );
-
-               /**
</del><span class="cx" style="display: block; padding: 0 10px">                  * Filters the comment notification email headers.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @since 1.5.2
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1853,11 +1763,107 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $message_headers = apply_filters( 'comment_notification_headers', $message_headers, $comment->comment_ID );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $emails as $email ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $user = get_user_by( 'email', $email );
+
+                       if ( $user ) {
+                               $switched_locale = switch_to_user_locale( $user->ID );
+                       } else {
+                               $switched_locale = switch_to_locale( get_locale() );
+                       }
+
+                       switch ( $comment->comment_type ) {
+                               case 'trackback':
+                                       /* translators: %s: Post title. */
+                                       $notify_message = sprintf( __( 'New trackback on your post "%s"' ), $post->post_title ) . "\r\n";
+                                       /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
+                                       $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
+                                       /* translators: %s: Trackback/pingback/comment author URL. */
+                                       $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
+                                       /* translators: %s: Comment text. */
+                                       $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
+                                       $notify_message .= __( 'You can see all trackbacks on this post here:' ) . "\r\n";
+                                       /* translators: Trackback notification email subject. 1: Site title, 2: Post title. */
+                                       $subject = sprintf( __( '[%1$s] Trackback: "%2$s"' ), $blogname, $post->post_title );
+                                       break;
+
+                               case 'pingback':
+                                       /* translators: %s: Post title. */
+                                       $notify_message = sprintf( __( 'New pingback on your post "%s"' ), $post->post_title ) . "\r\n";
+                                       /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
+                                       $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
+                                       /* translators: %s: Trackback/pingback/comment author URL. */
+                                       $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
+                                       /* translators: %s: Comment text. */
+                                       $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
+                                       $notify_message .= __( 'You can see all pingbacks on this post here:' ) . "\r\n";
+                                       /* translators: Pingback notification email subject. 1: Site title, 2: Post title. */
+                                       $subject = sprintf( __( '[%1$s] Pingback: "%2$s"' ), $blogname, $post->post_title );
+                                       break;
+
+                               default: // Comments.
+                                       /* translators: %s: Post title. */
+                                       $notify_message = sprintf( __( 'New comment on your post "%s"' ), $post->post_title ) . "\r\n";
+                                       /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */
+                                       $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
+                                       /* translators: %s: Comment author email. */
+                                       $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
+                                       /* translators: %s: Trackback/pingback/comment author URL. */
+                                       $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
+
+                                       if ( $comment->comment_parent && user_can( $post->post_author, 'edit_comment', $comment->comment_parent ) ) {
+                                               /* translators: Comment moderation. %s: Parent comment edit URL. */
+                                               $notify_message .= sprintf( __( 'In reply to: %s' ), admin_url( "comment.php?action=editcomment&c={$comment->comment_parent}#wpbody-content" ) ) . "\r\n";
+                                       }
+
+                                       /* translators: %s: Comment text. */
+                                       $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
+                                       $notify_message .= __( 'You can see all comments on this post here:' ) . "\r\n";
+                                       /* translators: Comment notification email subject. 1: Site title, 2: Post title. */
+                                       $subject = sprintf( __( '[%1$s] Comment: "%2$s"' ), $blogname, $post->post_title );
+                                       break;
+                       }
+
+                       $notify_message .= get_permalink( $comment->comment_post_ID ) . "#comments\r\n\r\n";
+                       /* translators: %s: Comment URL. */
+                       $notify_message .= sprintf( __( 'Permalink: %s' ), get_comment_link( $comment ) ) . "\r\n";
+
+                       if ( user_can( $post->post_author, 'edit_comment', $comment->comment_ID ) ) {
+                               if ( EMPTY_TRASH_DAYS ) {
+                                       /* translators: Comment moderation. %s: Comment action URL. */
+                                       $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
+                               } else {
+                                       /* translators: Comment moderation. %s: Comment action URL. */
+                                       $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
+                               }
+                               /* translators: Comment moderation. %s: Comment action URL. */
+                               $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment->comment_ID}#wpbody-content" ) ) . "\r\n";
+                       }
+
+                       /**
+                        * Filters the comment notification email text.
+                        *
+                        * @since 1.5.2
+                        *
+                        * @param string $notify_message The comment notification email text.
+                        * @param string $comment_id     Comment ID as a numeric string.
+                        */
+                       $notify_message = apply_filters( 'comment_notification_text', $notify_message, $comment->comment_ID );
+
+                       /**
+                        * Filters the comment notification email subject.
+                        *
+                        * @since 1.5.2
+                        *
+                        * @param string $subject    The comment notification email subject.
+                        * @param string $comment_id Comment ID as a numeric string.
+                        */
+                       $subject = apply_filters( 'comment_notification_subject', $subject, $comment->comment_ID );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $switched_locale ) {
-                       restore_previous_locale();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( $switched_locale ) {
+                               restore_previous_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">                return true;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1908,8 +1914,6 @@
</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_locale() );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $comment_author_domain = '';
</span><span class="cx" style="display: block; padding: 0 10px">                if ( WP_Http::is_ip_address( $comment->comment_author_IP ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $comment_author_domain = gethostbyaddr( $comment->comment_author_IP );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1924,77 +1928,6 @@
</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">                $comment_content = wp_specialchars_decode( $comment->comment_content );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                switch ( $comment->comment_type ) {
-                       case 'trackback':
-                               /* translators: %s: Post title. */
-                               $notify_message  = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
-                               $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
-                               /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
-                               $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
-                               /* translators: %s: Trackback/pingback/comment author URL. */
-                               $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
-                               $notify_message .= __( 'Trackback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n";
-                               break;
-
-                       case 'pingback':
-                               /* translators: %s: Post title. */
-                               $notify_message  = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
-                               $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
-                               /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
-                               $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
-                               /* translators: %s: Trackback/pingback/comment author URL. */
-                               $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
-                               $notify_message .= __( 'Pingback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n";
-                               break;
-
-                       default: // Comments.
-                               /* translators: %s: Post title. */
-                               $notify_message  = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
-                               $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
-                               /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */
-                               $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
-                               /* translators: %s: Comment author email. */
-                               $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
-                               /* translators: %s: Trackback/pingback/comment author URL. */
-                               $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
-
-                               if ( $comment->comment_parent ) {
-                                       /* translators: Comment moderation. %s: Parent comment edit URL. */
-                                       $notify_message .= sprintf( __( 'In reply to: %s' ), admin_url( "comment.php?action=editcomment&c={$comment->comment_parent}#wpbody-content" ) ) . "\r\n";
-                               }
-
-                               /* translators: %s: Comment text. */
-                               $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
-                               break;
-               }
-
-               /* translators: Comment moderation. %s: Comment action URL. */
-               $notify_message .= sprintf( __( 'Approve it: %s' ), admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) ) . "\r\n";
-
-               if ( EMPTY_TRASH_DAYS ) {
-                       /* translators: Comment moderation. %s: Comment action URL. */
-                       $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) ) . "\r\n";
-               } else {
-                       /* translators: Comment moderation. %s: Comment action URL. */
-                       $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) ) . "\r\n";
-               }
-
-               /* translators: Comment moderation. %s: Comment action URL. */
-               $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) ) . "\r\n";
-
-               $notify_message .= sprintf(
-                       /* translators: Comment moderation. %s: Number of comments awaiting approval. */
-                       _n(
-                               'Currently %s comment is waiting for approval. Please visit the moderation panel:',
-                               'Currently %s comments are waiting for approval. Please visit the moderation panel:',
-                               $comments_waiting
-                       ),
-                       number_format_i18n( $comments_waiting )
-               ) . "\r\n";
-               $notify_message .= admin_url( 'edit-comments.php?comment_status=moderated#wpbody-content' ) . "\r\n";
-
-               /* translators: Comment moderation notification email subject. 1: Site title, 2: Post title. */
-               $subject         = sprintf( __( '[%1$s] Please moderate: "%2$s"' ), $blogname, $post->post_title );
</del><span class="cx" style="display: block; padding: 0 10px">                 $message_headers = '';
</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">@@ -2008,26 +1941,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $emails = apply_filters( 'comment_moderation_recipients', $emails, $comment_id );
</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">-                 * Filters the comment moderation email text.
-                *
-                * @since 1.5.2
-                *
-                * @param string $notify_message Text of the comment moderation email.
-                * @param int    $comment_id     Comment ID.
-                */
-               $notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id );
-
-               /**
-                * Filters the comment moderation email subject.
-                *
-                * @since 1.5.2
-                *
-                * @param string $subject    Subject of the comment moderation email.
-                * @param int    $comment_id Comment ID.
-                */
-               $subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id );
-
-               /**
</del><span class="cx" style="display: block; padding: 0 10px">                  * Filters the comment moderation email headers.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @since 2.8.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2038,11 +1951,111 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $message_headers = apply_filters( 'comment_moderation_headers', $message_headers, $comment_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $emails as $email ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $user = get_user_by( 'email', $email );
+
+                       if ( $user ) {
+                               $switched_locale = switch_to_user_locale( $user->ID );
+                       } else {
+                               $switched_locale = switch_to_locale( get_locale() );
+                       }
+
+                       switch ( $comment->comment_type ) {
+                               case 'trackback':
+                                       /* translators: %s: Post title. */
+                                       $notify_message  = sprintf( __( 'A new trackback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
+                                       $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
+                                       /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
+                                       $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
+                                       /* translators: %s: Trackback/pingback/comment author URL. */
+                                       $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
+                                       $notify_message .= __( 'Trackback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n";
+                                       break;
+
+                               case 'pingback':
+                                       /* translators: %s: Post title. */
+                                       $notify_message  = sprintf( __( 'A new pingback on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
+                                       $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
+                                       /* translators: 1: Trackback/pingback website name, 2: Website IP address, 3: Website hostname. */
+                                       $notify_message .= sprintf( __( 'Website: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
+                                       /* translators: %s: Trackback/pingback/comment author URL. */
+                                       $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
+                                       $notify_message .= __( 'Pingback excerpt: ' ) . "\r\n" . $comment_content . "\r\n\r\n";
+                                       break;
+
+                               default: // Comments.
+                                       /* translators: %s: Post title. */
+                                       $notify_message  = sprintf( __( 'A new comment on the post "%s" is waiting for your approval' ), $post->post_title ) . "\r\n";
+                                       $notify_message .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
+                                       /* translators: 1: Comment author's name, 2: Comment author's IP address, 3: Comment author's hostname. */
+                                       $notify_message .= sprintf( __( 'Author: %1$s (IP address: %2$s, %3$s)' ), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
+                                       /* translators: %s: Comment author email. */
+                                       $notify_message .= sprintf( __( 'Email: %s' ), $comment->comment_author_email ) . "\r\n";
+                                       /* translators: %s: Trackback/pingback/comment author URL. */
+                                       $notify_message .= sprintf( __( 'URL: %s' ), $comment->comment_author_url ) . "\r\n";
+
+                                       if ( $comment->comment_parent ) {
+                                               /* translators: Comment moderation. %s: Parent comment edit URL. */
+                                               $notify_message .= sprintf( __( 'In reply to: %s' ), admin_url( "comment.php?action=editcomment&c={$comment->comment_parent}#wpbody-content" ) ) . "\r\n";
+                                       }
+
+                                       /* translators: %s: Comment text. */
+                                       $notify_message .= sprintf( __( 'Comment: %s' ), "\r\n" . $comment_content ) . "\r\n\r\n";
+                                       break;
+                       }
+
+                       /* translators: Comment moderation. %s: Comment action URL. */
+                       $notify_message .= sprintf( __( 'Approve it: %s' ), admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) ) . "\r\n";
+
+                       if ( EMPTY_TRASH_DAYS ) {
+                               /* translators: Comment moderation. %s: Comment action URL. */
+                               $notify_message .= sprintf( __( 'Trash it: %s' ), admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) ) . "\r\n";
+                       } else {
+                               /* translators: Comment moderation. %s: Comment action URL. */
+                               $notify_message .= sprintf( __( 'Delete it: %s' ), admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) ) . "\r\n";
+                       }
+
+                       /* translators: Comment moderation. %s: Comment action URL. */
+                       $notify_message .= sprintf( __( 'Spam it: %s' ), admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) ) . "\r\n";
+
+                       $notify_message .= sprintf(
+                               /* translators: Comment moderation. %s: Number of comments awaiting approval. */
+                               _n(
+                                       'Currently %s comment is waiting for approval. Please visit the moderation panel:',
+                                       'Currently %s comments are waiting for approval. Please visit the moderation panel:',
+                                       $comments_waiting
+                               ),
+                               number_format_i18n( $comments_waiting )
+                       ) . "\r\n";
+                       $notify_message .= admin_url( 'edit-comments.php?comment_status=moderated#wpbody-content' ) . "\r\n";
+
+                       /* translators: Comment moderation notification email subject. 1: Site title, 2: Post title. */
+                       $subject = sprintf( __( '[%1$s] Please moderate: "%2$s"' ), $blogname, $post->post_title );
+
+                       /**
+                        * Filters the comment moderation email text.
+                        *
+                        * @since 1.5.2
+                        *
+                        * @param string $notify_message Text of the comment moderation email.
+                        * @param int    $comment_id     Comment ID.
+                        */
+                       $notify_message = apply_filters( 'comment_moderation_text', $notify_message, $comment_id );
+
+                       /**
+                        * Filters the comment moderation email subject.
+                        *
+                        * @since 1.5.2
+                        *
+                        * @param string $subject    Subject of the comment moderation email.
+                        * @param int    $comment_id Comment ID.
+                        */
+                       $subject = apply_filters( 'comment_moderation_subject', $subject, $comment_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         wp_mail( $email, wp_specialchars_decode( $subject ), $notify_message, $message_headers );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $switched_locale ) {
-                       restore_previous_locale();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( $switched_locale ) {
+                               restore_previous_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">                return true;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2063,6 +2076,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * but check to see if it's the admin whose password we're changing, and skip this.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       $admin_user = get_user_by( 'email', get_option( 'admin_email' ) );
+
+                       if ( $admin_user ) {
+                               $switched_locale = switch_to_user_locale( $admin_user->ID );
+                       } else {
+                               $switched_locale = switch_to_locale( get_locale() );
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         /* translators: %s: User name. */
</span><span class="cx" style="display: block; padding: 0 10px">                        $message = sprintf( __( 'Password changed for user: %s' ), $user->user_login ) . "\r\n";
</span><span class="cx" style="display: block; padding: 0 10px">                        /*
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2103,6 +2125,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $wp_password_change_notification_email['message'],
</span><span class="cx" style="display: block; padding: 0 10px">                                $wp_password_change_notification_email['headers']
</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 ( $switched_locale ) {
+                               restore_previous_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"> endif;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2152,8 +2178,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $send_notification_to_admin = apply_filters( 'wp_send_new_user_notification_to_admin', true, $user );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'user' !== $notify && true === $send_notification_to_admin ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $switched_locale = switch_to_locale( get_locale() );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $admin_user = get_user_by( 'email', get_option( 'admin_email' ) );
+
+                       if ( $admin_user ) {
+                               $switched_locale = switch_to_user_locale( $admin_user->ID );
+                       } else {
+                               $switched_locale = switch_to_locale( get_locale() );
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         /* translators: %s: Site title. */
</span><span class="cx" style="display: block; padding: 0 10px">                        $message = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
</span><span class="cx" style="display: block; padding: 0 10px">                        /* translators: %s: User login. */
</span></span></pre></div>
<a id="trunktestsphpunittestsuserphp"></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/user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user.php        2024-09-30 15:28:26 UTC (rev 59127)
+++ trunk/tests/phpunit/tests/user.php  2024-09-30 15:50:19 UTC (rev 59128)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1439,6 +1439,66 @@
</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">+         * Verifies that the notification email is sent in the correct locale.
+        *
+        * @ticket 61518
+        */
+       public function test_wp_new_user_notification_switches_locale_to_matching_user() {
+               reset_phpmailer_instance();
+
+               $admin_user = get_user_by( 'email', get_option( 'admin_email' ) );
+
+               update_option( 'WPLANG', 'en_GB' );
+               update_user_meta( $admin_user->ID, 'locale', 'de_DE' );
+               update_user_meta( self::$contrib_id, 'locale', 'es_ES' );
+
+               $admin_email_locale = null;
+               $user_email_locale  = null;
+
+               add_filter(
+                       'wp_new_user_notification_email_admin',
+                       static function ( $email ) use ( &$admin_email_locale ) {
+                               $admin_email_locale = get_locale();
+                               return $email;
+                       }
+               );
+               add_filter(
+                       'wp_new_user_notification_email',
+                       static function ( $email ) use ( &$user_email_locale ) {
+                               $user_email_locale = get_locale();
+                               return $email;
+                       }
+               );
+
+               wp_new_user_notification( self::$contrib_id, null, 'both' );
+
+               $mailer = tests_retrieve_phpmailer_instance();
+
+               $was_admin_email_sent = false;
+               $was_user_email_sent  = false;
+
+               /*
+                * Check to see if a notification email was sent to the
+                * post author `blackburn@battlefield3.com` and and site admin `admin@example.org`.
+                */
+               $first_recipient = $mailer->get_recipient( 'to' );
+               if ( $first_recipient ) {
+                       $was_admin_email_sent = WP_TESTS_EMAIL === $first_recipient->address;
+                       $was_user_email_sent  = 'blackburn@battlefield3.com' === $first_recipient->address;
+               }
+
+               $second_recipient = $mailer->get_recipient( 'to', 1 );
+               if ( $second_recipient ) {
+                       $was_user_email_sent = 'blackburn@battlefield3.com' === $second_recipient->address;
+               }
+
+               $this->assertTrue( $was_admin_email_sent, 'Admin email was not sent as expected' );
+               $this->assertTrue( $was_user_email_sent, 'User email was not sent as expected' );
+               $this->assertSame( 'de_DE', $admin_email_locale, 'Admin email was not sent in the expected locale' );
+               $this->assertSame( 'es_ES', $user_email_locale, 'User email was not sent in the expected locale' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Callback that returns 0.0.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return float 0.0.
</span></span></pre>
</div>
</div>

</body>
</html>