From a61525049f574a826cf20a8b1b624e1b42970b10 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 25 Jan 2024 07:51:12 +0000 Subject: [PATCH] I18N: Rename `WP_Translation_Controller::instance()` method to `get_instance()`. This improves consistency as `get_instance()` is more commonly used in core. See #59656. git-svn-id: https://develop.svn.wordpress.org/trunk@57350 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-locale-switcher.php | 2 +- src/wp-includes/l10n.php | 4 +- .../l10n/class-wp-translation-controller.php | 22 +++++++---- src/wp-settings.php | 2 +- .../tests/l10n/wpTranslationController.php | 38 +++++++++---------- .../tests/l10n/wpTranslationsConvert.php | 4 +- 6 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/wp-includes/class-wp-locale-switcher.php b/src/wp-includes/class-wp-locale-switcher.php index b3e163014aa90..9f1c4831ed446 100644 --- a/src/wp-includes/class-wp-locale-switcher.php +++ b/src/wp-includes/class-wp-locale-switcher.php @@ -283,7 +283,7 @@ private function change_locale( $locale ) { $wp_locale = new WP_Locale(); - WP_Translation_Controller::instance()->set_locale( $locale ); + WP_Translation_Controller::get_instance()->set_locale( $locale ); /** * Fires when the locale is switched to or restored. diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 632f432f62545..228c40757c3ff 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -797,7 +797,7 @@ function load_textdomain( $domain, $mofile, $locale = null ) { $locale = determine_locale(); } - $i18n_controller = WP_Translation_Controller::instance(); + $i18n_controller = WP_Translation_Controller::get_instance(); // Ensures the correct locale is set as the current one, in case it was filtered. $i18n_controller->set_locale( $locale ); @@ -911,7 +911,7 @@ function unload_textdomain( $domain, $reloadable = false ) { // Since multiple locales are supported, reloadable text domains don't actually need to be unloaded. if ( ! $reloadable ) { - WP_Translation_Controller::instance()->unload_textdomain( $domain ); + WP_Translation_Controller::get_instance()->unload_textdomain( $domain ); } if ( isset( $l10n[ $domain ] ) ) { diff --git a/src/wp-includes/l10n/class-wp-translation-controller.php b/src/wp-includes/l10n/class-wp-translation-controller.php index 616dce5793c5c..b44384c013dcd 100644 --- a/src/wp-includes/l10n/class-wp-translation-controller.php +++ b/src/wp-includes/l10n/class-wp-translation-controller.php @@ -42,20 +42,28 @@ final class WP_Translation_Controller { protected $loaded_files = array(); /** - * Returns the WP_Translation_Controller singleton. + * Container for the main instance of the class. + * + * @since 6.5.0 + * @var WP_Translation_Controller|null + */ + private static $instance = null; + + /** + * Utility method to retrieve the main instance of the class. + * + * The instance will be created if it does not exist yet. * * @since 6.5.0 * * @return WP_Translation_Controller */ - public static function instance(): WP_Translation_Controller { - static $instance; - - if ( ! $instance ) { - $instance = new self(); + public static function get_instance(): WP_Translation_Controller { + if ( null === self::$instance ) { + self::$instance = new self(); } - return $instance; + return self::$instance; } /** diff --git a/src/wp-settings.php b/src/wp-settings.php index 28bcdded99704..e30eeb493b86b 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -622,7 +622,7 @@ $GLOBALS['wp_locale_switcher'] = new WP_Locale_Switcher(); $GLOBALS['wp_locale_switcher']->init(); -WP_Translation_Controller::instance()->set_locale( $locale ); +WP_Translation_Controller::get_instance()->set_locale( $locale ); // Load the functions for the active theme, for both parent and child theme if applicable. foreach ( wp_get_active_and_valid_themes() as $theme ) { diff --git a/tests/phpunit/tests/l10n/wpTranslationController.php b/tests/phpunit/tests/l10n/wpTranslationController.php index dd5028214e396..6c8fb6dc9d300 100644 --- a/tests/phpunit/tests/l10n/wpTranslationController.php +++ b/tests/phpunit/tests/l10n/wpTranslationController.php @@ -30,9 +30,9 @@ public function test_load_textdomain() { $compat_instance = $l10n['wp-tests-domain'] ?? null; - $is_loaded = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' ); - $headers = WP_Translation_Controller::instance()->get_headers( 'wp-tests-domain' ); - $entries = WP_Translation_Controller::instance()->get_entries( 'wp-tests-domain' ); + $is_loaded = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' ); + $headers = WP_Translation_Controller::get_instance()->get_headers( 'wp-tests-domain' ); + $entries = WP_Translation_Controller::get_instance()->get_entries( 'wp-tests-domain' ); $unload_successful = unload_textdomain( 'wp-tests-domain' ); @@ -76,7 +76,7 @@ public function test_load_textdomain_existing_override() { $is_loaded_wp = is_textdomain_loaded( 'wp-tests-domain' ); - $is_loaded = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' ); + $is_loaded = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' ); remove_filter( 'override_load_textdomain', '__return_true' ); @@ -102,7 +102,7 @@ public function test_load_textdomain_php_files() { public function test_load_textdomain_prefers_php_files_by_default() { $load_successful = load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' ); - $instance = WP_Translation_Controller::instance(); + $instance = WP_Translation_Controller::get_instance(); $is_loaded = $instance->is_textdomain_loaded( 'wp-tests-domain', 'en_US' ); @@ -259,9 +259,9 @@ public function test_unload_textdomain() { $compat_instance = $l10n['wp-tests-domain'] ?? null; - $is_loaded = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' ); - $headers = WP_Translation_Controller::instance()->get_headers( 'wp-tests-domain' ); - $entries = WP_Translation_Controller::instance()->get_entries( 'wp-tests-domain' ); + $is_loaded = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' ); + $headers = WP_Translation_Controller::get_instance()->get_headers( 'wp-tests-domain' ); + $entries = WP_Translation_Controller::get_instance()->get_entries( 'wp-tests-domain' ); $this->assertNull( $compat_instance, 'Compat instance was not removed' ); $this->assertTrue( $unload_successful, 'Text domain not successfully unloaded' ); @@ -281,13 +281,13 @@ public function test_unload_textdomain_existing_override() { $unload_successful = unload_textdomain( 'wp-tests-domain' ); - $is_loaded = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' ); + $is_loaded = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' ); remove_filter( 'override_unload_textdomain', '__return_true' ); $unload_successful_after = unload_textdomain( 'wp-tests-domain' ); - $is_loaded_after = WP_Translation_Controller::instance()->is_textdomain_loaded( 'wp-tests-domain' ); + $is_loaded_after = WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'wp-tests-domain' ); $this->assertTrue( $unload_successful ); $this->assertTrue( $is_loaded ); @@ -317,14 +317,14 @@ public function test_switch_to_locale_translations_stay_loaded_default_textdomai $actual = __( 'Invalid parameter.' ); - $this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded() ); - $this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'default', 'es_ES' ) ); + $this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded() ); + $this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'default', 'es_ES' ) ); restore_previous_locale(); $actual_2 = __( 'Invalid parameter.' ); - $this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'default', 'es_ES' ) ); + $this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'default', 'es_ES' ) ); $this->assertSame( 'ParĂ¡metro no vĂ¡lido. ', $actual ); $this->assertSame( 'Invalid parameter.', $actual_2 ); @@ -336,7 +336,7 @@ public function test_switch_to_locale_translations_stay_loaded_default_textdomai * @covers ::change_locale */ public function test_switch_to_locale_translations_stay_loaded_custom_textdomain() { - $this->assertSame( 'en_US', WP_Translation_Controller::instance()->get_locale() ); + $this->assertSame( 'en_US', WP_Translation_Controller::get_instance()->get_locale() ); require_once DIR_TESTDATA . '/plugins/internationalized-plugin.php'; @@ -346,16 +346,16 @@ public function test_switch_to_locale_translations_stay_loaded_custom_textdomain $actual = i18n_plugin_test(); - $this->assertSame( 'es_ES', WP_Translation_Controller::instance()->get_locale() ); - $this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'internationalized-plugin', 'es_ES' ) ); - $this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'default', 'es_ES' ) ); - $this->assertFalse( WP_Translation_Controller::instance()->is_textdomain_loaded( 'foo-bar', 'es_ES' ) ); + $this->assertSame( 'es_ES', WP_Translation_Controller::get_instance()->get_locale() ); + $this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'internationalized-plugin', 'es_ES' ) ); + $this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'default', 'es_ES' ) ); + $this->assertFalse( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'foo-bar', 'es_ES' ) ); restore_previous_locale(); $after = i18n_plugin_test(); - $this->assertTrue( WP_Translation_Controller::instance()->is_textdomain_loaded( 'internationalized-plugin', 'es_ES' ) ); + $this->assertTrue( WP_Translation_Controller::get_instance()->is_textdomain_loaded( 'internationalized-plugin', 'es_ES' ) ); $this->assertSame( 'This is a dummy plugin', $before ); $this->assertSame( 'Este es un plugin dummy', $actual ); diff --git a/tests/phpunit/tests/l10n/wpTranslationsConvert.php b/tests/phpunit/tests/l10n/wpTranslationsConvert.php index fc532d6886faf..8f3aa41c05bb9 100644 --- a/tests/phpunit/tests/l10n/wpTranslationsConvert.php +++ b/tests/phpunit/tests/l10n/wpTranslationsConvert.php @@ -10,8 +10,8 @@ class WP_Translation_Controller_Convert_Tests extends WP_UnitTestCase { * @covers ::instance */ public function test_get_instance() { - $instance = WP_Translation_Controller::instance(); - $instance2 = WP_Translation_Controller::instance(); + $instance = WP_Translation_Controller::get_instance(); + $instance2 = WP_Translation_Controller::get_instance(); $this->assertSame( $instance, $instance2 ); }