Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch locale to admin locale #6919

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/wp-includes/l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ function get_user_locale( $user = 0 ) {
return $locale ? $locale : get_locale();
}

/**
* Retrieves the locale of a user that is associated with the admin_email.
*
* If there is no user associated with the admin_email, defaults to the site_language
at-benni marked this conversation as resolved.
Show resolved Hide resolved
*
* @since 6.7.0
*
* @return string The locale of the admin.
*/
function get_admin_locale() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this name clashes with https://core.trac.wordpress.org/ticket/49971

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not just the name, but the whole idea is also very similar :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes very similar, but instead of adding a new "Admin language" setting, it's trying to figure out the language by looking for a user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this function is no longer needed :-)

$admin_email = get_option( 'admin_email' );
$admin_user = get_user_by( 'email', $admin_email );
if ( $admin_user ) {
$locale = get_user_locale( $admin_user );
}

return $locale ?? get_locale();
}

/**
* Determines the current locale desired for the request.
*
Expand Down
14 changes: 13 additions & 1 deletion src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,11 @@ function wp_password_change_notification( $user ) {
* but check to see if it's the admin whose password we're changing, and skip this.
*/
if ( 0 !== strcasecmp( $user->user_email, get_option( 'admin_email' ) ) ) {

$admin_user = get_user_by( 'email', get_option( 'admin_email' ) );

$switched_locale = $admin_user && switch_to_user_locale( $admin_user );

/* translators: %s: User name. */
$message = sprintf( __( 'Password changed for user: %s' ), $user->user_login ) . "\r\n";
/*
Expand Down Expand Up @@ -2103,6 +2108,10 @@ function wp_password_change_notification( $user ) {
$wp_password_change_notification_email['message'],
$wp_password_change_notification_email['headers']
);

if ( $switched_locale ) {
restore_previous_locale();
}
}
}
endif;
Expand Down Expand Up @@ -2152,7 +2161,10 @@ function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' )
$send_notification_to_admin = apply_filters( 'wp_send_new_user_notification_to_admin', true, $user );

if ( 'user' !== $notify && true === $send_notification_to_admin ) {
$switched_locale = switch_to_locale( get_locale() );

$admin_user = get_user_by( 'email', get_option( 'admin_email' ) );

$switched_locale = $admin_user && switch_to_user_locale( $admin_user );

/* translators: %s: Site title. */
$message = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n";
Expand Down
Loading