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 5 commits into
base: trunk
Choose a base branch
from

Conversation

at-benni
Copy link

This patch adds a new funciton called get_admin_locale it takes the locale of the user associated with the admin_email and falls back to the site language if there is no user associated with this email address.

Also the patch switches to the get_admin_locale function in the admin email notifications instead of get_locale

Trac ticket: https://core.trac.wordpress.org/ticket/61518


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

github-actions bot commented Jun 27, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @[email protected].

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

Core Committers: Use this line as a base for the props when committing in SVN:

Props benniledl, swissspidy, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

*
* @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.

@@ -2063,6 +2063,9 @@ 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' ) ) ) {

$switched_locale = switch_to_locale( 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.

It would be better to leverage switch_to_user_locale here

Copy link
Author

@at-benni at-benni Jun 27, 2024

Choose a reason for hiding this comment

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

Hey, just to clarify, will this switch to the language of the user who changed their email? For example, if a user named "Bob" with the role of subscriber and language set to ES changes his password, will the email that is sent to the administrator be sent in ES, regardless of the site locale and 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.

No, that wouldn‘t change.

Copy link
Member

Choose a reason for hiding this comment

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

So this would become something like this:

Suggested change
$switched_locale = switch_to_locale( get_admin_locale() );
$admin_user = get_user_by( 'email', get_option( 'admin_email' ) );
$switched_locale = $admin_user && switch_to_user_locale( $admin_user );

Copy link
Author

Choose a reason for hiding this comment

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

is that change also necessary on line 2165?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

Copy link
Author

@at-benni at-benni Jun 27, 2024

Choose a reason for hiding this comment

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

With this change, when an Administrator with the locale set to something different than the site locale registers a new user, and the admin_email is not associated with a registered account, it does not fall back to the default site language. Instead, it sends the email in the locale used by the Administrator who registered the new user.

Eg.
ES = standard site locale
DE Administrator registers a new user

  1. admin_email is not associated with a user -> Expected: send email in ES, Actual: email gets sent in DE
  2. admin_email is associated with a user -> Expected and Actual: email gets sent in locale that the user associated with the admin_email has set

Copy link
Member

Choose a reason for hiding this comment

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

Then use switch_to_user_locale if a user exists and switch_to_locale if it doesn‘t

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Missing end period.

Co-authored-by: Mukesh Panchal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants