Skip to content

Commit 2f91b1f

Browse files
committed
I18N: Improve edge case handling in WP_Translation_Controller.
Prevents PHP warnings for possibly undefined array keys. Also fixes incorrect `@covers` annotations. Follow-up to [57337]. See #59656. git-svn-id: https://develop.svn.wordpress.org/trunk@57339 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5a8e44c commit 2f91b1f

4 files changed

Lines changed: 66 additions & 19 deletions

File tree

src/wp-includes/l10n.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ function load_textdomain( $domain, $mofile, $locale = null ) {
821821
if ( 'mo' !== $preferred_format ) {
822822
array_unshift(
823823
$translation_files,
824-
substr_replace( $mofile, ".l10n.$preferred_format", - strlen( $preferred_format ) )
824+
substr_replace( $mofile, ".l10n.$preferred_format", - strlen( '.mo' ) )
825825
);
826826
}
827827

src/wp-includes/l10n/class-wp-translation-controller.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,24 @@ public function unload_file( $file, string $textdomain = 'default', string $loca
151151
}
152152

153153
if ( null !== $locale ) {
154-
foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $i => $moe ) {
155-
if ( $file === $moe || $file === $moe->get_file() ) {
156-
unset( $this->loaded_translations[ $locale ][ $textdomain ][ $i ] );
157-
unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] );
158-
return true;
154+
if ( isset( $this->loaded_translations[ $locale ][ $textdomain ] ) ) {
155+
foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $i => $moe ) {
156+
if ( $file === $moe || $file === $moe->get_file() ) {
157+
unset( $this->loaded_translations[ $locale ][ $textdomain ][ $i ] );
158+
unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] );
159+
return true;
160+
}
159161
}
160162
}
161163

162164
return true;
163165
}
164166

165167
foreach ( $this->loaded_translations as $l => $domains ) {
168+
if ( ! isset( $domains[ $textdomain ] ) ) {
169+
continue;
170+
}
171+
166172
foreach ( $domains[ $textdomain ] as $i => $moe ) {
167173
if ( $file === $moe || $file === $moe->get_file() ) {
168174
unset( $this->loaded_translations[ $l ][ $textdomain ][ $i ] );
@@ -185,18 +191,21 @@ public function unload_file( $file, string $textdomain = 'default', string $loca
185191
* @return bool True on success, false otherwise.
186192
*/
187193
public function unload_textdomain( string $textdomain = 'default', string $locale = null ): bool {
194+
$unloaded = false;
195+
188196
if ( null !== $locale ) {
189-
foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $moe ) {
190-
unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] );
197+
if ( isset( $this->loaded_translations[ $locale ][ $textdomain ] ) ) {
198+
$unloaded = true;
199+
foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $moe ) {
200+
unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] );
201+
}
191202
}
192203

193204
unset( $this->loaded_translations[ $locale ][ $textdomain ] );
194205

195-
return true;
206+
return $unloaded;
196207
}
197208

198-
$unloaded = false;
199-
200209
foreach ( $this->loaded_translations as $l => $domains ) {
201210
if ( ! isset( $domains[ $textdomain ] ) ) {
202211
continue;

tests/phpunit/tests/l10n/wpTranslationController.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ public function test_load_textdomain_php_files() {
105105
$this->assertTrue( $unload_php_successful );
106106
}
107107

108+
/**
109+
* @covers ::load_textdomain
110+
*
111+
* @return void
112+
*/
113+
public function test_load_textdomain_prefers_php_files_by_default() {
114+
$load_successful = load_textdomain( 'wp-tests-domain', DIR_TESTDATA . '/pomo/simple.mo' );
115+
116+
$instance = WP_Translation_Controller::instance();
117+
118+
$is_loaded = $instance->is_textdomain_loaded( 'wp-tests-domain', 'en_US' );
119+
120+
$unload_mo = $instance->unload_file( DIR_TESTDATA . '/pomo/simple.mo', 'wp-tests-domain' );
121+
$unload_php = $instance->unload_file( DIR_TESTDATA . '/pomo/simple.l10n.php', 'wp-tests-domain' );
122+
123+
$unload_successful = unload_textdomain( 'wp-tests-domain' );
124+
125+
$this->assertTrue( $load_successful, 'Translation not successfully loaded' );
126+
$this->assertTrue( $is_loaded );
127+
$this->assertFalse( $unload_mo );
128+
$this->assertTrue( $unload_php );
129+
$this->assertTrue( $unload_successful );
130+
}
131+
108132
/**
109133
* @covers ::load_textdomain
110134
*
@@ -296,6 +320,21 @@ public function test_unload_textdomain_existing_override() {
296320
$this->assertFalse( $is_loaded_after );
297321
}
298322

323+
/**
324+
* @covers ::unload_file
325+
* @covers ::unload_textdomain
326+
*
327+
* @return void
328+
*/
329+
public function test_unload_non_existent_files_and_textdomains() {
330+
$controller = new WP_Translation_Controller();
331+
$this->assertFalse( $controller->unload_textdomain( 'foobarbaz' ) );
332+
$this->assertFalse( $controller->unload_textdomain( 'foobarbaz', 'es_ES' ) );
333+
$this->assertFalse( $controller->unload_textdomain( 'default', 'es_ES' ) );
334+
$this->assertFalse( $controller->unload_file( DIR_TESTDATA . '/l10n/fa_IR.mo' ) );
335+
$this->assertFalse( $controller->unload_file( DIR_TESTDATA . '/l10n/fa_IR.mo', 'es_ES' ) );
336+
}
337+
299338
/**
300339
* @covers ::load_textdomain
301340
* @covers ::unload_textdomain

tests/phpunit/tests/l10n/wpTranslationsConvert.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function test_no_files_loaded_returns_false() {
2828
}
2929

3030
/**
31-
* @covers ::unload
31+
* @covers ::unload_textdomain
3232
*
3333
* @return void
3434
*/
@@ -40,7 +40,7 @@ public function test_unload_not_loaded() {
4040

4141
/**
4242
* @covers ::load
43-
* @covers ::unload
43+
* @covers ::unload_textdomain
4444
* @covers ::is_textdomain_loaded
4545
* @covers ::translate
4646
* @covers ::locate_translation
@@ -62,7 +62,7 @@ public function test_unload_entire_textdomain() {
6262
}
6363

6464
/**
65-
* @covers ::unload
65+
* @covers ::unload_file
6666
* @covers WP_Translation_File::get_file
6767
*
6868
* @return void
@@ -77,7 +77,7 @@ public function test_unload_file_is_not_actually_loaded() {
7777
}
7878

7979
/**
80-
* @covers ::unload
80+
* @covers ::unload_textdomain
8181
* @covers ::is_textdomain_loaded
8282
*
8383
* @return void
@@ -235,7 +235,7 @@ public function data_simple_example_files(): array {
235235

236236
/**
237237
* @covers ::load
238-
* @covers ::unload
238+
* @covers ::unload_file
239239
* @covers ::is_textdomain_loaded
240240
* @covers ::translate
241241
* @covers ::translate_plural
@@ -289,7 +289,7 @@ public function test_load_multiple_files() {
289289
* @covers ::set_locale
290290
* @covers ::get_locale
291291
* @covers ::load
292-
* @covers ::unload
292+
* @covers ::unload_file
293293
* @covers ::is_textdomain_loaded
294294
* @covers ::translate
295295
* @covers ::translate_plural
@@ -333,7 +333,7 @@ public function test_load_multiple_locales() {
333333
}
334334

335335
/**
336-
* @covers ::unload
336+
* @covers ::unload_textdomain
337337
*
338338
* @return void
339339
*/
@@ -411,7 +411,6 @@ public function test_load_file_is_already_loaded_for_different_textdomain() {
411411

412412
/**
413413
* @covers ::load
414-
* @covers ::unload
415414
* @covers ::is_textdomain_loaded
416415
* @covers ::translate
417416
* @covers ::translate_plural

0 commit comments

Comments
 (0)