Skip to content

Commit

Permalink
I18N: Switch locale to admin locale when sending auto update emails.
Browse files Browse the repository at this point in the history
If sending an auto update 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, explicitly switches to the site locale.

This is a follow-up to [59128] where this was previously added for other types of emails.

Props benniledl, swissspidy.
Fixes #62496.

git-svn-id: https://develop.svn.wordpress.org/trunk@59460 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
swissspidy committed Nov 26, 2024
1 parent 75c587f commit bd21a8a
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/wp-admin/includes/class-wp-automatic-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,14 @@ protected function send_email( $type, $core_update, $result = null ) {
return;
}

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

if ( $admin_user ) {
$switched_locale = switch_to_user_locale( $admin_user->ID );
} else {
$switched_locale = switch_to_locale( get_locale() );
}

switch ( $type ) {
case 'success': // We updated.
/* translators: Site updated notification email subject. 1: Site title, 2: WordPress version. */
Expand Down Expand Up @@ -1139,8 +1147,11 @@ protected function send_email( $type, $core_update, $result = null ) {
$email = apply_filters( 'auto_core_update_email', $email, $type, $core_update, $result );

wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
}

if ( $switched_locale ) {
restore_previous_locale();
}
}

/**
* Checks whether an email should be sent after attempting plugin or theme updates.
Expand Down Expand Up @@ -1255,6 +1266,14 @@ protected function send_plugin_theme_email( $type, $successful_updates, $failed_
}
}

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

if ( $admin_user ) {
$switched_locale = switch_to_user_locale( $admin_user->ID );
} else {
$switched_locale = switch_to_locale( get_locale() );
}

$body = array();
$successful_plugins = ( ! empty( $successful_updates['plugin'] ) );
$successful_themes = ( ! empty( $successful_updates['theme'] ) );
Expand Down Expand Up @@ -1526,6 +1545,10 @@ protected function send_plugin_theme_email( $type, $successful_updates, $failed_
if ( $result ) {
update_option( 'auto_plugin_theme_update_emails', $past_failure_emails );
}

if ( $switched_locale ) {
restore_previous_locale();
}
}

/**
Expand All @@ -1534,9 +1557,12 @@ protected function send_plugin_theme_email( $type, $successful_updates, $failed_
* @since 3.7.0
*/
protected function send_debug_email() {
$update_count = 0;
foreach ( $this->update_results as $type => $updates ) {
$update_count += count( $updates );
$admin_user = get_user_by( 'email', get_site_option( 'admin_email' ) );

if ( $admin_user ) {
$switched_locale = switch_to_user_locale( $admin_user->ID );
} else {
$switched_locale = switch_to_locale( get_locale() );
}

$body = array();
Expand Down Expand Up @@ -1715,6 +1741,10 @@ protected function send_debug_email() {
$email = apply_filters( 'automatic_updates_debug_email', $email, $failures, $this->update_results );

wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );

if ( $switched_locale ) {
restore_previous_locale();
}
}

/**
Expand Down

0 comments on commit bd21a8a

Please sign in to comment.