Make WordPress Core


Ignore:
Timestamp:
09/30/2024 03:50:19 PM (2 months ago)
Author:
swissspidy
Message:

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 #61518.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user.php

    r57987 r59128  
    14401440
    14411441    /**
     1442     * Verifies that the notification email is sent in the correct locale.
     1443     *
     1444     * @ticket 61518
     1445     */
     1446    public function test_wp_new_user_notification_switches_locale_to_matching_user() {
     1447        reset_phpmailer_instance();
     1448
     1449        $admin_user = get_user_by( 'email', get_option( 'admin_email' ) );
     1450
     1451        update_option( 'WPLANG', 'en_GB' );
     1452        update_user_meta( $admin_user->ID, 'locale', 'de_DE' );
     1453        update_user_meta( self::$contrib_id, 'locale', 'es_ES' );
     1454
     1455        $admin_email_locale = null;
     1456        $user_email_locale  = null;
     1457
     1458        add_filter(
     1459            'wp_new_user_notification_email_admin',
     1460            static function ( $email ) use ( &$admin_email_locale ) {
     1461                $admin_email_locale = get_locale();
     1462                return $email;
     1463            }
     1464        );
     1465        add_filter(
     1466            'wp_new_user_notification_email',
     1467            static function ( $email ) use ( &$user_email_locale ) {
     1468                $user_email_locale = get_locale();
     1469                return $email;
     1470            }
     1471        );
     1472
     1473        wp_new_user_notification( self::$contrib_id, null, 'both' );
     1474
     1475        $mailer = tests_retrieve_phpmailer_instance();
     1476
     1477        $was_admin_email_sent = false;
     1478        $was_user_email_sent  = false;
     1479
     1480        /*
     1481         * Check to see if a notification email was sent to the
     1482         * post author `blackburn@battlefield3.com` and and site admin `admin@example.org`.
     1483         */
     1484        $first_recipient = $mailer->get_recipient( 'to' );
     1485        if ( $first_recipient ) {
     1486            $was_admin_email_sent = WP_TESTS_EMAIL === $first_recipient->address;
     1487            $was_user_email_sent  = 'blackburn@battlefield3.com' === $first_recipient->address;
     1488        }
     1489
     1490        $second_recipient = $mailer->get_recipient( 'to', 1 );
     1491        if ( $second_recipient ) {
     1492            $was_user_email_sent = 'blackburn@battlefield3.com' === $second_recipient->address;
     1493        }
     1494
     1495        $this->assertTrue( $was_admin_email_sent, 'Admin email was not sent as expected' );
     1496        $this->assertTrue( $was_user_email_sent, 'User email was not sent as expected' );
     1497        $this->assertSame( 'de_DE', $admin_email_locale, 'Admin email was not sent in the expected locale' );
     1498        $this->assertSame( 'es_ES', $user_email_locale, 'User email was not sent in the expected locale' );
     1499    }
     1500
     1501    /**
    14421502     * Callback that returns 0.0.
    14431503     *
Note: See TracChangeset for help on using the changeset viewer.