Make WordPress Core

Changeset 57339


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.

Location:
trunk
Files:
4 edited

Legend:

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

    r57338 r57339  
    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        }
  • 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 ) {
  • trunk/tests/phpunit/tests/l10n/wpTranslationController.php

    r57337 r57339  
    111111         * @return void
    112112         */
     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
     132        /**
     133         * @covers ::load_textdomain
     134         *
     135         * @return void
     136         */
    113137        public function test_load_textdomain_reads_php_files_if_filtered_format_is_unsupported() {
    114138                add_filter(
     
    298322
    299323        /**
     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
     338        /**
    300339         * @covers ::load_textdomain
    301340         * @covers ::unload_textdomain
  • trunk/tests/phpunit/tests/l10n/wpTranslationsConvert.php

    r57337 r57339  
    2929
    3030        /**
    31          * @covers ::unload
     31         * @covers ::unload_textdomain
    3232         *
    3333         * @return void
     
    4141        /**
    4242         * @covers ::load
    43          * @covers ::unload
     43         * @covers ::unload_textdomain
    4444         * @covers ::is_textdomain_loaded
    4545         * @covers ::translate
     
    6363
    6464        /**
    65          * @covers ::unload
     65         * @covers ::unload_file
    6666         * @covers WP_Translation_File::get_file
    6767         *
     
    7878
    7979        /**
    80          * @covers ::unload
     80         * @covers ::unload_textdomain
    8181         * @covers ::is_textdomain_loaded
    8282         *
     
    236236        /**
    237237         * @covers ::load
    238          * @covers ::unload
     238         * @covers ::unload_file
    239239         * @covers ::is_textdomain_loaded
    240240         * @covers ::translate
     
    290290         * @covers ::get_locale
    291291         * @covers ::load
    292          * @covers ::unload
     292         * @covers ::unload_file
    293293         * @covers ::is_textdomain_loaded
    294294         * @covers ::translate
     
    334334
    335335        /**
    336          * @covers ::unload
     336         * @covers ::unload_textdomain
    337337         *
    338338         * @return void
     
    412412        /**
    413413         * @covers ::load
    414          * @covers ::unload
    415414         * @covers ::is_textdomain_loaded
    416415         * @covers ::translate
Note: See TracChangeset for help on using the changeset viewer.