From 97518917dd00db62954b881d40f9d82ab5f2ca4f Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Tue, 23 Jan 2024 15:15:14 +0000 Subject: [PATCH] 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 --- wp-includes/l10n.php | 2 +- .../l10n/class-wp-translation-controller.php | 29 ++++++++++++------- wp-includes/version.php | 2 +- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/wp-includes/l10n.php b/wp-includes/l10n.php index 1923c8c2a2..632f432f62 100644 --- a/wp-includes/l10n.php +++ b/wp-includes/l10n.php @@ -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' ) ) ); } diff --git a/wp-includes/l10n/class-wp-translation-controller.php b/wp-includes/l10n/class-wp-translation-controller.php index fbe5fa7d0c..616dce5793 100644 --- a/wp-includes/l10n/class-wp-translation-controller.php +++ b/wp-includes/l10n/class-wp-translation-controller.php @@ -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; diff --git a/wp-includes/version.php b/wp-includes/version.php index 10606651d3..18d8cb6360 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.