Make WordPress Core

Changeset 55224


Ignore:
Timestamp:
02/04/2023 08:45:36 PM (20 months ago)
Author:
swissspidy
Message:

I18N: Improve method names in WP_Locale_Switcher().

This is a follow-up to [55161] to rename ::get_current_locale() to ::get_switched_locale() and ::get_current_user_id() to ::get_switched_user_id() for improved clarity.

Also:

  • Fix docblock for switch_locale filter. The User ID is false if missing, not null.
  • Add additional test involving restore_previous_locale() and improve test cleanup.

And most importantly: happy birthday ocean90! 🎂

Props johnjamesjacoby, ocean90.
See #57123.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-locale-switcher.php

    r55161 r55224  
    9393         * @since 6.2.0 The `$user_id` parameter was added.
    9494         *
    95          * @param string   $locale  The new locale.
    96          * @param null|int $user_id User ID for context if available.
     95         * @param string    $locale  The new locale.
     96         * @param false|int $user_id User ID for context if available.
    9797         */
    9898        do_action( 'switch_locale', $locale, $user_id );
     
    187187     * @return string|false Locale if the locale has been switched, false otherwise.
    188188     */
    189     public function get_current_locale() {
     189    public function get_switched_locale() {
    190190        $entry = end( $this->stack );
    191191
     
    204204     * @return int|false User ID if set and if the locale has been switched, false otherwise.
    205205     */
    206     public function get_current_user_id() {
     206    public function get_switched_user_id() {
    207207        $entry = end( $this->stack );
    208208
     
    223223     */
    224224    public function filter_locale( $locale ) {
    225         $switched_locale = $this->get_current_locale();
     225        $switched_locale = $this->get_switched_locale();
    226226
    227227        if ( $switched_locale ) {
  • trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php

    r55161 r55224  
    5555        $wp_textdomain_registry = new WP_Textdomain_Registry();
    5656
     57        // Clean up after any tests that don't restore the locale afterwards,
     58        // before resetting $wp_locale_switcher.
     59        restore_current_locale();
     60
    5761        remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
    5862        $wp_locale_switcher = new WP_Locale_Switcher();
     
    599603     * @covers ::switch_to_locale
    600604     * @covers ::switch_to_user_locale
    601      * @covers WP_Locale_Switcher::get_current_locale
    602      * @covers WP_Locale_Switcher::get_current_user_id
     605     * @covers WP_Locale_Switcher::get_switched_locale
     606     * @covers WP_Locale_Switcher::get_switched_user_id
    603607     */
    604608    public function test_returns_current_locale_and_user_after_switching() {
     
    612616        );
    613617
    614         $locale_1  = $wp_locale_switcher->get_current_locale();
    615         $user_id_1 = $wp_locale_switcher->get_current_user_id();
     618        $locale_1  = $wp_locale_switcher->get_switched_locale();
     619        $user_id_1 = $wp_locale_switcher->get_switched_user_id();
    616620
    617621        switch_to_user_locale( self::$user_id );
    618622
    619         $locale_2  = $wp_locale_switcher->get_current_locale();
    620         $user_id_2 = $wp_locale_switcher->get_current_user_id();
    621 
    622         switch_to_locale( 'en_GB' );
    623 
    624         $locale_3  = $wp_locale_switcher->get_current_locale();
    625         $user_id_3 = $wp_locale_switcher->get_current_user_id();
     623        $locale_2  = $wp_locale_switcher->get_switched_locale();
     624        $user_id_2 = $wp_locale_switcher->get_switched_user_id();
     625
     626        switch_to_locale( 'en_GB' );
     627
     628        $locale_3  = $wp_locale_switcher->get_switched_locale();
     629        $user_id_3 = $wp_locale_switcher->get_switched_user_id();
    626630
    627631        switch_to_user_locale( $user_2 );
    628632
    629         $locale_4  = $wp_locale_switcher->get_current_locale();
    630         $user_id_4 = $wp_locale_switcher->get_current_user_id();
    631 
    632         restore_current_locale();
    633 
    634         $locale_5  = $wp_locale_switcher->get_current_locale();
    635         $user_id_5 = $wp_locale_switcher->get_current_user_id();
     633        $locale_4  = $wp_locale_switcher->get_switched_locale();
     634        $user_id_4 = $wp_locale_switcher->get_switched_user_id();
     635
     636        restore_current_locale();
     637
     638        $locale_5  = $wp_locale_switcher->get_switched_locale();
     639        $user_id_5 = $wp_locale_switcher->get_switched_user_id();
    636640
    637641        $this->assertFalse( $locale_1, 'Locale should be false before switching' );
     
    649653        $this->assertFalse( $locale_5, 'Locale should be false after restoring' );
    650654        $this->assertFalse( $user_id_5, 'User ID should be false after restoring' );
    651 
     655    }
     656
     657    /**
     658     * @ticket 57123
     659     *
     660     * @covers ::switch_to_locale
     661     * @covers ::switch_to_user_locale
     662     * @covers WP_Locale_Switcher::get_switched_locale
     663     * @covers WP_Locale_Switcher::get_switched_user_id
     664     */
     665    public function test_returns_previous_locale_and_user_after_switching() {
     666        global $wp_locale_switcher;
     667
     668        $locale_1  = $wp_locale_switcher->get_switched_locale();
     669        $user_id_1 = $wp_locale_switcher->get_switched_user_id();
     670
     671        switch_to_user_locale( self::$user_id );
     672
     673        $locale_2  = $wp_locale_switcher->get_switched_locale();
     674        $user_id_2 = $wp_locale_switcher->get_switched_user_id();
     675
     676        switch_to_locale( 'en_GB' );
     677
     678        $locale_3  = $wp_locale_switcher->get_switched_locale();
     679        $user_id_3 = $wp_locale_switcher->get_switched_user_id();
     680
     681        restore_previous_locale();
     682
     683        $locale_4  = $wp_locale_switcher->get_switched_locale();
     684        $user_id_4 = $wp_locale_switcher->get_switched_user_id();
     685
     686        $this->assertFalse( $locale_1, 'Locale should be false before switching' );
     687        $this->assertFalse( $user_id_1, 'User ID should be false before switching' );
     688
     689        $this->assertSame( 'de_DE', $locale_2, 'The locale was not changed to de_DE' );
     690        $this->assertSame( self::$user_id, $user_id_2, 'User ID should match the main admin ID' );
     691
     692        $this->assertSame( 'en_GB', $locale_3, 'The locale was not changed to en_GB' );
     693        $this->assertFalse( $user_id_3, 'User ID should be false after normal locale switching' );
     694
     695        $this->assertSame( 'de_DE', $locale_4, 'The locale was not changed back to de_DE' );
     696        $this->assertSame( self::$user_id, $user_id_4, 'User ID should match the main admin ID again' );
    652697    }
    653698
Note: See TracChangeset for help on using the changeset viewer.