Make WordPress Core


Ignore:
Timestamp:
10/24/2022 10:01:01 AM (13 months ago)
Author:
swissspidy
Message:

I18N: Change how WP_Textdomain_Registry stores the default languages path.

WP_Textdomain_Registry was introduced in [53874] to store text domains and their language directory paths, addressing issues with just-in-time loading of textdomains when using locale switching and when usingload_*_textdomain() functions.

Said change has inadvertently caused a performance regression exactly when usingload_*_textdomain(), which still often is the case, where the cached information was not further used or even overridden.

This change addresses that issue by storing the default languages paths in a separate way, while at the same time making WP_Textdomain_Registry easier to maintain and adding new tests to catch future regressions.

Props flixos90, spacedmonkey, ocean90, SergeyBiryukov, costdev.
Fixes #39210.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php

    r53874 r54669  
    99    protected $theme_root;
    1010    protected static $user_id;
    11     private $locale_count;
    1211
    1312    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     
    2524        $this->theme_root     = DIR_TESTDATA . '/themedir1';
    2625        $this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
    27         $this->locale_count   = 0;
    2826
    2927        // /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
     
    3836        global $wp_textdomain_registry;
    3937
    40         $wp_textdomain_registry->reset();
     38        $wp_textdomain_registry = new WP_Textdomain_Registry();
    4139    }
    4240
     
    4947        global $wp_textdomain_registry;
    5048
    51         $wp_textdomain_registry->reset();
     49        $wp_textdomain_registry = new WP_Textdomain_Registry();
    5250
    5351        parent::tear_down();
     
    263261        $textdomain = 'foo-bar-baz';
    264262
    265         add_filter( 'locale', array( $this, '_filter_locale_count' ) );
     263        $filter = new MockAction();
     264        add_filter( 'locale', array( $filter, 'filter' ) );
    266265
    267266        __( 'Foo', $textdomain );
     
    271270        __( 'Foo Bar Baz', $textdomain );
    272271
    273         remove_filter( 'locale', array( $this, '_filter_locale_count' ) );
    274 
    275272        $this->assertFalse( is_textdomain_loaded( $textdomain ) );
    276         $this->assertSame( 1, $this->locale_count );
    277     }
    278 
    279     public function _filter_locale_count( $locale ) {
    280         ++$this->locale_count;
    281 
    282         return $locale;
     273        $this->assertSame( 1, $filter->get_call_count() );
     274    }
     275
     276    /**
     277     * @ticket 37997
     278     * @ticket 39210
     279     *
     280     * @covers ::_load_textdomain_just_in_time
     281     */
     282    public function test_get_locale_is_called_only_once_per_textdomain_with_custom_lang_dir() {
     283        load_plugin_textdomain( 'custom-internationalized-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
     284
     285        $textdomain = 'custom-internationalized-plugin';
     286
     287        $filter = new MockAction();
     288        add_filter( 'locale', array( $filter, 'filter' ) );
     289
     290        __( 'Foo', $textdomain );
     291        __( 'Bar', $textdomain );
     292        __( 'Baz', $textdomain );
     293        __( 'Foo Bar', $textdomain );
     294        __( 'Foo Bar Baz', $textdomain );
     295
     296        $this->assertFalse( is_textdomain_loaded( $textdomain ) );
     297        $this->assertSame( 1, $filter->get_call_count() );
    283298    }
    284299}
Note: See TracChangeset for help on using the changeset viewer.