Make WordPress Core


Ignore:
Timestamp:
01/23/2024 03:13:32 PM (3 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n/class-wp-translation-controller.php

    r57337 r57339  
    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                        }
     
    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() ) {
     
    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;
    196                 }
    197 
    198                 $unloaded = false;
     206                        return $unloaded;
     207                }
    199208
    200209                foreach ( $this->loaded_translations as $l => $domains ) {
Note: See TracChangeset for help on using the changeset viewer.