Skip to content

Commit

Permalink
I18N: Improve method names in WP_Locale_Switcher().
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://develop.svn.wordpress.org/trunk@55224 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
swissspidy committed Feb 4, 2023
1 parent c4cba6d commit 81f31d6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/wp-includes/class-wp-locale-switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public function switch_to_locale( $locale, $user_id = false ) {
* @since 4.7.0
* @since 6.2.0 The `$user_id` parameter was added.
*
* @param string $locale The new locale.
* @param null|int $user_id User ID for context if available.
* @param string $locale The new locale.
* @param false|int $user_id User ID for context if available.
*/
do_action( 'switch_locale', $locale, $user_id );

Expand Down Expand Up @@ -186,7 +186,7 @@ public function is_switched() {
*
* @return string|false Locale if the locale has been switched, false otherwise.
*/
public function get_current_locale() {
public function get_switched_locale() {
$entry = end( $this->stack );

if ( $entry ) {
Expand All @@ -203,7 +203,7 @@ public function get_current_locale() {
*
* @return int|false User ID if set and if the locale has been switched, false otherwise.
*/
public function get_current_user_id() {
public function get_switched_user_id() {
$entry = end( $this->stack );

if ( $entry ) {
Expand All @@ -222,7 +222,7 @@ public function get_current_user_id() {
* @return string The locale currently being switched to.
*/
public function filter_locale( $locale ) {
$switched_locale = $this->get_current_locale();
$switched_locale = $this->get_switched_locale();

if ( $switched_locale ) {
return $switched_locale;
Expand Down
69 changes: 57 additions & 12 deletions tests/phpunit/tests/l10n/wpLocaleSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function tear_down() {

$wp_textdomain_registry = new WP_Textdomain_Registry();

// Clean up after any tests that don't restore the locale afterwards,
// before resetting $wp_locale_switcher.
restore_current_locale();

remove_filter( 'locale', array( $wp_locale_switcher, 'filter_locale' ) );
$wp_locale_switcher = new WP_Locale_Switcher();
$wp_locale_switcher->init();
Expand Down Expand Up @@ -598,8 +602,8 @@ public function test_switch_to_locale_should_work() {
*
* @covers ::switch_to_locale
* @covers ::switch_to_user_locale
* @covers WP_Locale_Switcher::get_current_locale
* @covers WP_Locale_Switcher::get_current_user_id
* @covers WP_Locale_Switcher::get_switched_locale
* @covers WP_Locale_Switcher::get_switched_user_id
*/
public function test_returns_current_locale_and_user_after_switching() {
global $wp_locale_switcher;
Expand All @@ -611,28 +615,28 @@ public function test_returns_current_locale_and_user_after_switching() {
)
);

$locale_1 = $wp_locale_switcher->get_current_locale();
$user_id_1 = $wp_locale_switcher->get_current_user_id();
$locale_1 = $wp_locale_switcher->get_switched_locale();
$user_id_1 = $wp_locale_switcher->get_switched_user_id();

switch_to_user_locale( self::$user_id );

$locale_2 = $wp_locale_switcher->get_current_locale();
$user_id_2 = $wp_locale_switcher->get_current_user_id();
$locale_2 = $wp_locale_switcher->get_switched_locale();
$user_id_2 = $wp_locale_switcher->get_switched_user_id();

switch_to_locale( 'en_GB' );

$locale_3 = $wp_locale_switcher->get_current_locale();
$user_id_3 = $wp_locale_switcher->get_current_user_id();
$locale_3 = $wp_locale_switcher->get_switched_locale();
$user_id_3 = $wp_locale_switcher->get_switched_user_id();

switch_to_user_locale( $user_2 );

$locale_4 = $wp_locale_switcher->get_current_locale();
$user_id_4 = $wp_locale_switcher->get_current_user_id();
$locale_4 = $wp_locale_switcher->get_switched_locale();
$user_id_4 = $wp_locale_switcher->get_switched_user_id();

restore_current_locale();

$locale_5 = $wp_locale_switcher->get_current_locale();
$user_id_5 = $wp_locale_switcher->get_current_user_id();
$locale_5 = $wp_locale_switcher->get_switched_locale();
$user_id_5 = $wp_locale_switcher->get_switched_user_id();

$this->assertFalse( $locale_1, 'Locale should be false before switching' );
$this->assertFalse( $user_id_1, 'User ID should be false before switching' );
Expand All @@ -648,7 +652,48 @@ public function test_returns_current_locale_and_user_after_switching() {

$this->assertFalse( $locale_5, 'Locale should be false after restoring' );
$this->assertFalse( $user_id_5, 'User ID should be false after restoring' );
}

/**
* @ticket 57123
*
* @covers ::switch_to_locale
* @covers ::switch_to_user_locale
* @covers WP_Locale_Switcher::get_switched_locale
* @covers WP_Locale_Switcher::get_switched_user_id
*/
public function test_returns_previous_locale_and_user_after_switching() {
global $wp_locale_switcher;

$locale_1 = $wp_locale_switcher->get_switched_locale();
$user_id_1 = $wp_locale_switcher->get_switched_user_id();

switch_to_user_locale( self::$user_id );

$locale_2 = $wp_locale_switcher->get_switched_locale();
$user_id_2 = $wp_locale_switcher->get_switched_user_id();

switch_to_locale( 'en_GB' );

$locale_3 = $wp_locale_switcher->get_switched_locale();
$user_id_3 = $wp_locale_switcher->get_switched_user_id();

restore_previous_locale();

$locale_4 = $wp_locale_switcher->get_switched_locale();
$user_id_4 = $wp_locale_switcher->get_switched_user_id();

$this->assertFalse( $locale_1, 'Locale should be false before switching' );
$this->assertFalse( $user_id_1, 'User ID should be false before switching' );

$this->assertSame( 'de_DE', $locale_2, 'The locale was not changed to de_DE' );
$this->assertSame( self::$user_id, $user_id_2, 'User ID should match the main admin ID' );

$this->assertSame( 'en_GB', $locale_3, 'The locale was not changed to en_GB' );
$this->assertFalse( $user_id_3, 'User ID should be false after normal locale switching' );

$this->assertSame( 'de_DE', $locale_4, 'The locale was not changed back to de_DE' );
$this->assertSame( self::$user_id, $user_id_4, 'User ID should match the main admin ID again' );
}

public function filter_locale() {
Expand Down

0 comments on commit 81f31d6

Please sign in to comment.