Make WordPress Core


Ignore:
Timestamp:
10/24/2022 06:35:30 PM (16 months ago)
Author:
ocean90
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.
Merges [54669] to the 6.1 branch.
See #39210.

Location:
branches/6.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1

  • branches/6.1/src/wp-includes/class-wp-textdomain-registry.php

    r54133 r54682  
    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    }
Note: See TracChangeset for help on using the changeset viewer.