Make WordPress Core

Changeset 57287

Timestamp:
01/15/2024 07:03:27 PM (7 months ago)
Author:
swissspidy
Message:

I18N: Cache list of language file paths in WP_Textdomain_Registry.

Loading a list of language file paths using glob() can be expensive if involving thousands of files.

Expands scope of WP_Textdomain_Registry to cache list of language file paths in object cache and provides a way to invalidate that cache upon translation updates. Plugins can clear the cache using calls such as wp_cache_delete( 'cached_mo_files_' . md5( $path ), 'translations' );

Props mreishus, swissspidy
Fixes #58919

Location:
trunk
Files:
4 edited

Legend:

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

    r56178 r57287  
    5252     *
    5353     * @since 6.1.0
     54
    5455     *
    5556     * @var array
     57
     58
    5659     */
    5760    protected $cached_mo_files = array();
     
    6568     */
    6669    protected $domains_with_translations = array();
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
    6782
    6883    /**
     
    136151
    137152    /**
     153
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
     180
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
    138253     * Returns possible language directory paths for a given text domain.
    139254     *
     
    157272
    158273    /**
    159      * Gets the path to the language directory for the current locale.
     274     * Gets the path to the language directory for the current locale.
    160275     *
    161276     * Checks the plugins and themes language directories as well as any
     
    176291
    177292        foreach ( $locations as $location ) {
    178             if ( ! isset( $this->cached_mo_files[ $location ] ) ) {
    179                 $this->set_cached_mo_files( $location );
    180             }
     293            $files = $this->get_language_files_from_path( $location );
    181294
    182295            $path = "$location/$domain-$locale.mo";
    183296
    184             foreach ( $this->cached_mo_files[ $location ] as $mo_path ) {
     297            foreach ( $ as $mo_path ) {
    185298                if (
    186299                    ! in_array( $domain, $this->domains_with_translations, true ) &&
     
    216329        return false;
    217330    }
    218 
    219     /**
    220      * Reads and caches all available MO files from a given directory.
    221      *
    222      * @since 6.1.0
    223      *
    224      * @param string $path Language directory path.
    225      */
    226     private function set_cached_mo_files( $path ) {
    227         $this->cached_mo_files[ $path ] = array();
    228 
    229         $mo_files = glob( $path . '/*.mo' );
    230 
    231         if ( $mo_files ) {
    232             $this->cached_mo_files[ $path ] = $mo_files;
    233         }
    234     }
    235331}
  • trunk/src/wp-includes/l10n.php

    r57286 r57287  
    13991399 * @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter.
    14001400 *
     1401
     1402
    14011403 * @param string $dir A directory to search for language files.
    14021404 *                    Default WP_LANG_DIR.
     
    14051407 */
    14061408function get_available_languages( $dir = null ) {
     1409
     1410
    14071411    $languages = array();
    14081412
    1409     $lang_files = glob( ( is_null( $dir ) ? WP_LANG_DIR : $dir ) . '/*.mo' );
     1413    $path       = is_null( $dir ) ? WP_LANG_DIR : $dir;
     1414    $lang_files = $wp_textdomain_registry->get_language_files_from_path( $path );
     1415
    14101416    if ( $lang_files ) {
    14111417        foreach ( $lang_files as $lang_file ) {
  • trunk/src/wp-settings.php

    r57269 r57287  
    381381 */
    382382$GLOBALS['wp_textdomain_registry'] = new WP_Textdomain_Registry();
     383
    383384
    384385// Load multisite-specific files.
  • trunk/tests/phpunit/tests/l10n/wpTextdomainRegistry.php

    r55010 r57287  
    1919    }
    2020
     21
     22
     23
     24
     25
     26
     27
     28
     29
    2130    /**
    2231     * @covers ::has
     
    2534     */
    2635    public function test_set_custom_path() {
    27         $reflection          = new ReflectionClass( $this->instance );
    28         $reflection_property = $reflection->getProperty( 'cached_mo_files' );
    29         $reflection_property->setAccessible( true );
    30 
    31         $this->assertEmpty(
    32             $reflection_property->getValue( $this->instance ),
    33             'Cache not empty by default'
    34         );
    35 
    3636        $this->instance->set_custom_path( 'foo', WP_LANG_DIR . '/bar' );
    3737
     
    4949            'Custom path for textdomain not returned'
    5050        );
    51         $this->assertArrayHasKey(
    52             WP_LANG_DIR . '/bar',
    53             $reflection_property->getValue( $this->instance ),
    54             'Custom path missing from cache'
     51        $this->assertNotFalse(
     52            wp_cache_get( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . 'bar/' ), 'translations' ),
     53            'List of files in custom path not cached'
    5554        );
    5655    }
     
    6160     */
    6261    public function test_get( $domain, $locale, $expected ) {
    63         $reflection          = new ReflectionClass( $this->instance );
    64         $reflection_property = $reflection->getProperty( 'cached_mo_files' );
    65         $reflection_property->setAccessible( true );
    66 
    6762        $actual = $this->instance->get( $domain, $locale );
    6863        $this->assertSame(
     
    7065            $actual,
    7166            'Expected languages directory path not matching actual one'
    72         );
    73 
    74         $this->assertArrayHasKey(
    75             WP_LANG_DIR . '/plugins',
    76             $reflection_property->getValue( $this->instance ),
    77             'Default plugins path missing from cache'
    7867        );
    7968    }
     
    9079            $this->instance->get( 'foo-plugin', 'de_DE' )
    9180        );
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
     117
     118
     119
     120
     121
     122
     123
     124
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
    92150    }
    93151
Note: See TracChangeset for help on using the changeset viewer.