Make WordPress Core


Ignore:
Timestamp:
01/30/2023 10:25:53 AM (22 months 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/class-wp-locale-switcher.php

    r55010 r55161  
    1616class WP_Locale_Switcher {
    1717    /**
    18      * Locale stack.
    19      *
    20      * @since 4.7.0
    21      * @var string[]
    22      */
    23     private $locales = array();
     18     * Locale switching stack.
     19     *
     20     * @since 4.7.0
     21     * @var array
     22     */
     23    private $stack = array();
    2424
    2525    /**
     
    5454     * Initializes the locale switcher.
    5555     *
    56      * Hooks into the {@see 'locale'} filter to change the locale on the fly.
     56     * Hooks into the {@see 'locale'} and {@see 'determine_locale'} filters
     57     * to change the locale on the fly.
    5758     *
    5859     * @since 4.7.0
     
    6061    public function init() {
    6162        add_filter( 'locale', array( $this, 'filter_locale' ) );
     63        add_filter( 'determine_locale', array( $this, 'filter_locale' ) );
    6264    }
    6365
     
    6769     * @since 4.7.0
    6870     *
    69      * @param string $locale The locale to switch to.
     71     * @param string    $locale  The locale to switch to.
     72     * @param int|false $user_id Optional. User ID as context. Default false.
    7073     * @return bool True on success, false on failure.
    7174     */
    72     public function switch_to_locale( $locale ) {
     75    public function switch_to_locale( $locale, $user_id = false ) {
    7376        $current_locale = determine_locale();
    7477        if ( $current_locale === $locale ) {
     
    8083        }
    8184
    82         $this->locales[] = $locale;
     85        $this->stack[] = array( $locale, $user_id );
    8386
    8487        $this->change_locale( $locale );
     
    8891         *
    8992         * @since 4.7.0
    90          *
    91          * @param string $locale The new locale.
     93         * @since 6.2.0 The `$user_id` parameter was added.
     94         *
     95         * @param string   $locale  The new locale.
     96         * @param null|int $user_id User ID for context if available.
    9297         */
    93         do_action( 'switch_locale', $locale );
     98        do_action( 'switch_locale', $locale, $user_id );
    9499
    95100        return true;
     
    97102
    98103    /**
     104     * Switches the translations according to the given user's locale.
     105     *
     106     * @since 6.2.0
     107     *
     108     * @param int $user_id User ID.
     109     * @return bool True on success, false on failure.
     110     */
     111    public function switch_to_user_locale( $user_id ) {
     112        $locale = get_user_locale( $user_id );
     113        return $this->switch_to_locale( $locale, $user_id );
     114    }
     115
     116    /**
    99117     * Restores the translations according to the previous locale.
    100118     *
     
    104122     */
    105123    public function restore_previous_locale() {
    106         $previous_locale = array_pop( $this->locales );
     124        $previous_locale = array_pop( $this->stack );
    107125
    108126        if ( null === $previous_locale ) {
     
    111129        }
    112130
    113         $locale = end( $this->locales );
     131        $entry  = end( $this->stack );
     132        $locale = is_array( $entry ) ? $entry[0] : false;
    114133
    115134        if ( ! $locale ) {
     
    128147         * @param string $previous_locale The previous locale.
    129148         */
    130         do_action( 'restore_previous_locale', $locale, $previous_locale );
     149        do_action( 'restore_previous_locale', $locale, $previous_locale[0] );
    131150
    132151        return $locale;
     
    141160     */
    142161    public function restore_current_locale() {
    143         if ( empty( $this->locales ) ) {
    144             return false;
    145         }
    146 
    147         $this->locales = array( $this->original_locale );
     162        if ( empty( $this->stack ) ) {
     163            return false;
     164        }
     165
     166        $this->stack = array( array( $this->original_locale, false ) );
    148167
    149168        return $this->restore_previous_locale();
     
    158177     */
    159178    public function is_switched() {
    160         return ! empty( $this->locales );
     179        return ! empty( $this->stack );
     180    }
     181
     182    /**
     183     * Returns the locale currently switched to.
     184     *
     185     * @since 6.2.0
     186     *
     187     * @return string|false Locale if the locale has been switched, false otherwise.
     188     */
     189    public function get_current_locale() {
     190        $entry = end( $this->stack );
     191
     192        if ( $entry ) {
     193            return $entry[0];
     194        }
     195
     196        return false;
     197    }
     198
     199    /**
     200     * Returns the user ID related to the currently switched locale.
     201     *
     202     * @since 6.2.0
     203     *
     204     * @return int|false User ID if set and if the locale has been switched, false otherwise.
     205     */
     206    public function get_current_user_id() {
     207        $entry = end( $this->stack );
     208
     209        if ( $entry ) {
     210            return $entry[1];
     211        }
     212
     213        return false;
    161214    }
    162215
     
    170223     */
    171224    public function filter_locale( $locale ) {
    172         $switched_locale = end( $this->locales );
     225        $switched_locale = $this->get_current_locale();
    173226
    174227        if ( $switched_locale ) {
Note: See TracChangeset for help on using the changeset viewer.