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.
Built from https://develop.svn.wordpress.org/trunk@57339


git-svn-id: http://core.svn.wordpress.org/trunk@56845 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-01-23 15:15:14 +00:00
parent a40f2f7555
commit 97518917dd
3 changed files with 21 additions and 12 deletions

View File

@ -821,7 +821,7 @@ function load_textdomain( $domain, $mofile, $locale = null ) {
if ( 'mo' !== $preferred_format ) {
array_unshift(
$translation_files,
substr_replace( $mofile, ".l10n.$preferred_format", - strlen( $preferred_format ) )
substr_replace( $mofile, ".l10n.$preferred_format", - strlen( '.mo' ) )
);
}

View File

@ -151,11 +151,13 @@ final class WP_Translation_Controller {
}
if ( null !== $locale ) {
foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $i => $moe ) {
if ( $file === $moe || $file === $moe->get_file() ) {
unset( $this->loaded_translations[ $locale ][ $textdomain ][ $i ] );
unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] );
return true;
if ( isset( $this->loaded_translations[ $locale ][ $textdomain ] ) ) {
foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $i => $moe ) {
if ( $file === $moe || $file === $moe->get_file() ) {
unset( $this->loaded_translations[ $locale ][ $textdomain ][ $i ] );
unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] );
return true;
}
}
}
@ -163,6 +165,10 @@ final class WP_Translation_Controller {
}
foreach ( $this->loaded_translations as $l => $domains ) {
if ( ! isset( $domains[ $textdomain ] ) ) {
continue;
}
foreach ( $domains[ $textdomain ] as $i => $moe ) {
if ( $file === $moe || $file === $moe->get_file() ) {
unset( $this->loaded_translations[ $l ][ $textdomain ][ $i ] );
@ -185,18 +191,21 @@ final class WP_Translation_Controller {
* @return bool True on success, false otherwise.
*/
public function unload_textdomain( string $textdomain = 'default', string $locale = null ): bool {
$unloaded = false;
if ( null !== $locale ) {
foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $moe ) {
unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] );
if ( isset( $this->loaded_translations[ $locale ][ $textdomain ] ) ) {
$unloaded = true;
foreach ( $this->loaded_translations[ $locale ][ $textdomain ] as $moe ) {
unset( $this->loaded_files[ $moe->get_file() ][ $locale ][ $textdomain ] );
}
}
unset( $this->loaded_translations[ $locale ][ $textdomain ] );
return true;
return $unloaded;
}
$unloaded = false;
foreach ( $this->loaded_translations as $l => $domains ) {
if ( ! isset( $domains[ $textdomain ] ) ) {
continue;

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57338';
$wp_version = '6.5-alpha-57339';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.