Make WordPress Core


Ignore:
Timestamp:
01/30/2023 10:25:53 AM (4 years ago)
Author:
swissspidy
Message:

I18N: Introduce switch_to_user_locale().

This new function makes it easier to switch to a specific user’s locale by reducing duplicate code and storing the user’s ID as additional context for plugins to consume. Existing usage of switch_to_locale() in core has been replaced with switch_to_user_locale() where appropriate.

Also, this change ensures WP_Locale_Switcher properly filters determine_locale so that anyyone using the determine_locale() function will get the correct locale information when switching is in effect.

Props costdev.
Fixes #57123.
See #26511.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-functions.php

    r55075 r55161  
    955955
    956956        $user            = get_user_by( 'login', $user_login );
    957         $switched_locale = switch_to_locale( get_user_locale( $user ) );
     957        $switched_locale = $user && switch_to_user_locale( $user->ID );
    958958
    959959        $message = sprintf(
     
    10691069
    10701070        $user            = get_user_by( 'login', $user_login );
    1071         $switched_locale = switch_to_locale( get_user_locale( $user ) );
     1071        $switched_locale = $user && switch_to_user_locale( $user->ID );
    10721072
    10731073        // Send email with activation link.
     
    16111611        $user = get_userdata( $user_id );
    16121612
    1613         $switched_locale = switch_to_locale( get_user_locale( $user ) );
     1613        $switched_locale = switch_to_user_locale( $user_id );
    16141614
    16151615        $welcome_email = get_site_option( 'welcome_email' );
     
    17351735        if ( $network_admin ) {
    17361736                // If the network admin email address corresponds to a user, switch to their locale.
    1737                 $switched_locale = switch_to_locale( get_user_locale( $network_admin ) );
     1737                $switched_locale = switch_to_user_locale( $network_admin->ID );
    17381738        } else {
    17391739                // Otherwise switch to the locale of the current site.
     
    18441844        $user = get_userdata( $user_id );
    18451845
    1846         $switched_locale = switch_to_locale( get_user_locale( $user ) );
     1846        $switched_locale = switch_to_user_locale( $user_id );
    18471847
    18481848        /**
     
    27272727        update_site_option( 'network_admin_hash', $new_admin_email );
    27282728
    2729         $switched_locale = switch_to_locale( get_user_locale() );
     2729        $switched_locale = switch_to_user_locale( get_current_user_id() );
    27302730
    27312731        /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
Note: See TracChangeset for help on using the changeset viewer.