Make WordPress Core

Changeset 54669


Ignore:
Timestamp:
10/24/2022 10:01:01 AM (6 weeks 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.

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-textdomain-registry.php

    r54133 r54669  
    3434
    3535    /**
     36     * List of domains and their custom language directory paths.
     37     *
     38     * @see load_plugin_textdomain()
     39     * @see load_theme_textdomain()
     40     *
     41     * @since 6.1.0
     42     *
     43     * @var array
     44     */
     45    protected $custom_paths = array();
     46
     47    /**
    3648     * Holds a cached list of available .mo files to improve performance.
    3749     *
     
    4355
    4456    /**
    45      * Returns the MO file path for a specific domain and locale.
     57     * Returns the languages directory path for a specific domain and locale.
    4658     *
    4759     * @since 6.1.0
     
    6375     * Determines whether any MO file paths are available for the domain.
    6476     *
     77     * This is the case if a path has been set for the current locale,
     78     * or if there is no information stored yet, in which case
     79     * {@see _load_textdomain_just_in_time()} will fetch the information first.
     80     *
    6581     * @since 6.1.0
    6682     *
     
    6985     */
    7086    public function has( $domain ) {
    71         return ! empty( $this->all[ $domain ] );
     87        return ! empty( $this->current[ $domain ] ) || empty( $this->all[ $domain ] );
    7288    }
    7389
    7490    /**
    75      * Returns the current (most recent) MO file path for a specific domain.
    76      *
    77      * @since 6.1.0
    78      *
    79      * @param string $domain Text domain.
    80      * @return string|false Current MO file path or false if there is none available.
    81      */
    82     public function get_current( $domain ) {
    83         if ( isset( $this->current[ $domain ] ) ) {
    84             return $this->current[ $domain ];
    85         }
    86 
    87         return false;
    88     }
    89 
    90     /**
    91      * Sets the MO file path for a specific domain and locale.
     91     * Sets the language directory path for a specific domain and locale.
    9292     *
    9393     * Also sets the 'current' property for direct access
     
    106106
    107107    /**
    108      * Resets the registry state.
     108     * Sets the custom path to the plugin's/theme's languages directory.
    109109     *
    110      * @since 6.1.0
     110     * Used by {@see load_plugin_textdomain()} and {@see load_theme_textdomain()}.
     111     *
     112     * @param string $domain Text domain.
     113     * @param string $path   Language directory path.
    111114     */
    112     public function reset() {
    113         $this->cached_mo_files = null;
    114         $this->all             = array();
    115         $this->current         = array();
     115    public function set_custom_path( $domain, $path ) {
     116        $this->custom_paths[ $domain ] = untrailingslashit( $path );
    116117    }
    117118
    118119    /**
    119      * Gets the path to a translation file in the languages directory for the current locale.
     120     * Gets the path to the language directory for the current locale.
     121     *
     122     * Checks the plugins and themes language directories as well as any
     123     * custom directory set via {@see load_plugin_textdomain()} or {@see load_theme_textdomain()}.
    120124     *
    121125     * @since 6.1.0
    122126     *
     127     * @see _get_path_to_translation_from_lang_dir()
     128     *
    123129     * @param string $domain Text domain.
    124130     * @param string $locale Locale.
    125      * @return string|false MO file path or false if there is none available.
     131     * @return string|false Language directory path or false if there is none available.
    126132     */
    127133    private function get_path_from_lang_dir( $domain, $locale ) {
    128         if ( null === $this->cached_mo_files ) {
    129             $this->set_cached_mo_files();
     134        $locations = array(
     135            WP_LANG_DIR . '/plugins',
     136            WP_LANG_DIR . '/themes',
     137        );
     138
     139        if ( isset( $this->custom_paths[ $domain ] ) ) {
     140            $locations[] = $this->custom_paths[ $domain ];
    130141        }
    131142
    132         $mofile = "{$domain}-{$locale}.mo";
     143        $mofile = "$domain-$locale.mo";
    133144
    134         $path = WP_LANG_DIR . '/plugins/' . $mofile;
     145        foreach ( $locations as $location ) {
     146            if ( ! isset( $this->cached_mo_files[ $location ] ) ) {
     147                $this->set_cached_mo_files( $location );
     148            }
    135149
    136         if ( in_array( $path, $this->cached_mo_files, true ) ) {
    137             $path = WP_LANG_DIR . '/plugins/';
    138             $this->set( $domain, $locale, $path );
     150            $path = $location . '/' . $mofile;
    139151
    140             return $path;
     152            if ( in_array( $path, $this->cached_mo_files[ $location ], true ) ) {
     153                $this->set( $domain, $locale, $location );
     154
     155                return trailingslashit( $location );
     156            }
    141157        }
    142158
    143         $path = WP_LANG_DIR . '/themes/' . $mofile;
    144         if ( in_array( $path, $this->cached_mo_files, true ) ) {
    145             $path = WP_LANG_DIR . '/themes/';
     159        // If no path is found for the given locale and a custom path has been set
     160        // using load_plugin_textdomain/load_theme_textdomain, use that one.
     161        if ( 'en_US' !== $locale && isset( $this->custom_paths[ $domain ] ) ) {
     162            $path = trailingslashit( $this->custom_paths[ $domain ] );
    146163            $this->set( $domain, $locale, $path );
    147 
    148164            return $path;
    149         }
    150 
    151         // If no path is found for the given locale, check if an entry for the default
    152         // en_US locale exists. This is the case when e.g. using load_plugin_textdomain
    153         // with a custom path.
    154         if ( 'en_US' !== $locale && isset( $this->all[ $domain ]['en_US'] ) ) {
    155             $this->set( $domain, $locale, $this->all[ $domain ]['en_US'] );
    156             return $this->all[ $domain ]['en_US'];
    157165        }
    158166
     
    163171
    164172    /**
    165      * Reads and caches all available MO files from the plugins and themes language directories.
     173     * Reads and caches all available MO files from a given directory.
    166174     *
    167175     * @since 6.1.0
     176     *
     177     * @param string $path Language directory path.
    168178     */
    169     protected function set_cached_mo_files() {
    170         $this->cached_mo_files = array();
     179    private function set_cached_mo_files( $path ) {
     180        $this->cached_mo_files[ $path ] = array();
    171181
    172         $locations = array(
    173             WP_LANG_DIR . '/plugins',
    174             WP_LANG_DIR . '/themes',
    175         );
     182        $mo_files = glob( $path . '/*.mo' );
    176183
    177         foreach ( $locations as $location ) {
    178             $mo_files = glob( $location . '/*.mo' );
    179 
    180             if ( $mo_files ) {
    181                 $this->cached_mo_files = array_merge( $this->cached_mo_files, $mo_files );
    182             }
     184        if ( $mo_files ) {
     185            $this->cached_mo_files[ $path ] = $mo_files;
    183186        }
    184187    }
  • trunk/src/wp-includes/l10n.php

    r54351 r54669  
    935935    }
    936936
    937     $wp_textdomain_registry->set( $domain, $locale, $path );
     937    $wp_textdomain_registry->set_custom_path( $domain, $path );
    938938
    939939    return load_textdomain( $domain, $path . '/' . $mofile, $locale );
     
    969969    $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' );
    970970
    971     $wp_textdomain_registry->set( $domain, $locale, $path );
     971    $wp_textdomain_registry->set_custom_path( $domain, $path );
    972972
    973973    return load_textdomain( $domain, $path . '/' . $mofile, $locale );
     
    10171017    }
    10181018
    1019     $wp_textdomain_registry->set( $domain, $locale, $path );
     1019    $wp_textdomain_registry->set_custom_path( $domain, $path );
    10201020
    10211021    return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale );
     
    12661266    }
    12671267
    1268     if ( $wp_textdomain_registry->has( $domain ) && ! $wp_textdomain_registry->get_current( $domain ) ) {
     1268    if ( ! $wp_textdomain_registry->has( $domain ) ) {
    12691269        return false;
    12701270    }
  • trunk/tests/phpunit/tests/l10n/loadTextdomain.php

    r53874 r54669  
    2929        global $wp_textdomain_registry;
    3030
    31         $wp_textdomain_registry->reset();
     31        $wp_textdomain_registry = new WP_Textdomain_Registry();
    3232    }
    3333
     
    3737        global $wp_textdomain_registry;
    3838
    39         $wp_textdomain_registry->reset();
     39        $wp_textdomain_registry = new WP_Textdomain_Registry();
    4040
    4141        parent::tear_down();
  • 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}
  • trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php

    r54088 r54669  
    2828        global $wp_textdomain_registry;
    2929
    30         $wp_textdomain_registry->reset();
     30        $wp_textdomain_registry = new WP_Textdomain_Registry();
    3131    }
    3232
     
    3737        global $wp_textdomain_registry;
    3838
    39         $wp_textdomain_registry->reset();
     39        $wp_textdomain_registry = new WP_Textdomain_Registry();
    4040
    4141        parent::tear_down();
     
    479479        require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php';
    480480
     481        $actual = custom_i18n_plugin_test();
     482
     483        switch_to_locale( 'es_ES' );
     484
    481485        $registry_value = $wp_textdomain_registry->get( 'custom-internationalized-plugin', determine_locale() );
    482486
    483         $actual = custom_i18n_plugin_test();
    484 
    485         switch_to_locale( 'es_ES' );
    486487        switch_to_locale( 'de_DE' );
    487488
     
    518519        require_once get_stylesheet_directory() . '/functions.php';
    519520
     521        $actual = custom_i18n_theme_test();
     522
     523        switch_to_locale( 'es_ES' );
     524
    520525        $registry_value = $wp_textdomain_registry->get( 'custom-internationalized-theme', determine_locale() );
    521526
    522         $actual = custom_i18n_theme_test();
    523 
    524         switch_to_locale( 'es_ES' );
    525527        switch_to_locale( 'de_DE' );
    526528
Note: See TracChangeset for help on using the changeset viewer.