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.

git-svn-id: https://develop.svn.wordpress.org/trunk@55224 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2023-02-04 20:45:36 +00:00
parent c4cba6d49c
commit 81f31d6fd8
2 changed files with 62 additions and 17 deletions

View File

@@ -92,8 +92,8 @@ class WP_Locale_Switcher {
* @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 );
@@ -186,7 +186,7 @@ class WP_Locale_Switcher {
*
* @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 ) {
@@ -203,7 +203,7 @@ class WP_Locale_Switcher {
*
* @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 ) {
@@ -222,7 +222,7 @@ class WP_Locale_Switcher {
* @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;

View File

@@ -54,6 +54,10 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
$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();
@@ -598,8 +602,8 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
*
* @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;
@@ -611,28 +615,28 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
)
);
$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' );
@@ -648,7 +652,48 @@ class Tests_L10n_wpLocaleSwitcher extends WP_UnitTestCase {
$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() {