mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-10 21:00:59 +01:00
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 Built from https://develop.svn.wordpress.org/trunk@57287 git-svn-id: http://core.svn.wordpress.org/trunk@56793 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
b713c5eb9c
commit
4d37fc7865
@ -51,8 +51,11 @@ class WP_Textdomain_Registry {
|
|||||||
* Holds a cached list of available .mo files to improve performance.
|
* Holds a cached list of available .mo files to improve performance.
|
||||||
*
|
*
|
||||||
* @since 6.1.0
|
* @since 6.1.0
|
||||||
|
* @since 6.5.0 This property is no longer used.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
|
*
|
||||||
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
protected $cached_mo_files = array();
|
protected $cached_mo_files = array();
|
||||||
|
|
||||||
@ -65,6 +68,18 @@ class WP_Textdomain_Registry {
|
|||||||
*/
|
*/
|
||||||
protected $domains_with_translations = array();
|
protected $domains_with_translations = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the registry.
|
||||||
|
*
|
||||||
|
* Hooks into the {@see 'upgrader_process_complete'} filter
|
||||||
|
* to invalidate MO files caches.
|
||||||
|
*
|
||||||
|
* @since 6.5.0
|
||||||
|
*/
|
||||||
|
public function init() {
|
||||||
|
add_action( 'upgrader_process_complete', array( $this, 'invalidate_mo_files_cache' ), 10, 2 );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the languages directory path for a specific domain and locale.
|
* Returns the languages directory path for a specific domain and locale.
|
||||||
*
|
*
|
||||||
@ -134,6 +149,106 @@ class WP_Textdomain_Registry {
|
|||||||
$this->custom_paths[ $domain ] = rtrim( $path, '/' );
|
$this->custom_paths[ $domain ] = rtrim( $path, '/' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves .mo files from the specified path.
|
||||||
|
*
|
||||||
|
* Allows early retrieval through the {@see 'pre_get_mo_files_from_path'} filter to optimize
|
||||||
|
* performance, especially in directories with many files.
|
||||||
|
*
|
||||||
|
* @since 6.5.0
|
||||||
|
*
|
||||||
|
* @param string $path The directory path to search for .mo files.
|
||||||
|
* @return array Array of .mo file paths.
|
||||||
|
*/
|
||||||
|
public function get_language_files_from_path( $path ) {
|
||||||
|
$path = trailingslashit( $path );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the .mo files retrieved from a specified path before the actual lookup.
|
||||||
|
*
|
||||||
|
* Returning a non-null value from the filter will effectively short-circuit
|
||||||
|
* the MO files lookup, returning that value instead.
|
||||||
|
*
|
||||||
|
* This can be useful in situations where the directory contains a large number of files
|
||||||
|
* and the default glob() function becomes expensive in terms of performance.
|
||||||
|
*
|
||||||
|
* @since 6.5.0
|
||||||
|
*
|
||||||
|
* @param null|array $mo_files List of .mo files. Default null.
|
||||||
|
* @param string $path The path from which .mo files are being fetched.
|
||||||
|
**/
|
||||||
|
$mo_files = apply_filters( 'pre_get_language_files_from_path', null, $path );
|
||||||
|
|
||||||
|
if ( null !== $mo_files ) {
|
||||||
|
return $mo_files;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cache_key = 'cached_mo_files_' . md5( $path );
|
||||||
|
$mo_files = wp_cache_get( $cache_key, 'translations' );
|
||||||
|
|
||||||
|
if ( false === $mo_files ) {
|
||||||
|
$mo_files = glob( $path . '*.mo' );
|
||||||
|
if ( false === $mo_files ) {
|
||||||
|
$mo_files = array();
|
||||||
|
}
|
||||||
|
wp_cache_set( $cache_key, $mo_files, 'translations' );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $mo_files;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidate the cache for .mo files.
|
||||||
|
*
|
||||||
|
* This function deletes the cache entries related to .mo files when triggered
|
||||||
|
* by specific actions, such as the completion of an upgrade process.
|
||||||
|
*
|
||||||
|
* @since 6.5.0
|
||||||
|
*
|
||||||
|
* @param WP_Upgrader $upgrader Unused. WP_Upgrader instance. In other contexts this might be a
|
||||||
|
* Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
|
||||||
|
* @param array $hook_extra {
|
||||||
|
* Array of bulk item update data.
|
||||||
|
*
|
||||||
|
* @type string $action Type of action. Default 'update'.
|
||||||
|
* @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
|
||||||
|
* @type bool $bulk Whether the update process is a bulk update. Default true.
|
||||||
|
* @type array $plugins Array of the basename paths of the plugins' main files.
|
||||||
|
* @type array $themes The theme slugs.
|
||||||
|
* @type array $translations {
|
||||||
|
* Array of translations update data.
|
||||||
|
*
|
||||||
|
* @type string $language The locale the translation is for.
|
||||||
|
* @type string $type Type of translation. Accepts 'plugin', 'theme', or 'core'.
|
||||||
|
* @type string $slug Text domain the translation is for. The slug of a theme/plugin or
|
||||||
|
* 'default' for core translations.
|
||||||
|
* @type string $version The version of a theme, plugin, or core.
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function invalidate_mo_files_cache( $upgrader, $hook_extra ) {
|
||||||
|
if ( 'translation' !== $hook_extra['type'] || array() === $hook_extra['translations'] ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$translation_types = array_unique( wp_list_pluck( $hook_extra['translations'], 'type' ) );
|
||||||
|
|
||||||
|
foreach ( $translation_types as $type ) {
|
||||||
|
switch ( $type ) {
|
||||||
|
case 'plugin':
|
||||||
|
wp_cache_delete( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/plugins/' ), 'translations' );
|
||||||
|
break;
|
||||||
|
case 'theme':
|
||||||
|
wp_cache_delete( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) . '/themes/' ), 'translations' );
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
wp_cache_delete( 'cached_mo_files_' . md5( trailingslashit( WP_LANG_DIR ) ), 'translations' );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns possible language directory paths for a given text domain.
|
* Returns possible language directory paths for a given text domain.
|
||||||
*
|
*
|
||||||
@ -156,7 +271,7 @@ class WP_Textdomain_Registry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the path to the language directory for the current locale.
|
* Gets the path to the language directory for the current domain and locale.
|
||||||
*
|
*
|
||||||
* Checks the plugins and themes language directories as well as any
|
* Checks the plugins and themes language directories as well as any
|
||||||
* custom directory set via {@see load_plugin_textdomain()} or {@see load_theme_textdomain()}.
|
* custom directory set via {@see load_plugin_textdomain()} or {@see load_theme_textdomain()}.
|
||||||
@ -175,13 +290,11 @@ class WP_Textdomain_Registry {
|
|||||||
$found_location = false;
|
$found_location = false;
|
||||||
|
|
||||||
foreach ( $locations as $location ) {
|
foreach ( $locations as $location ) {
|
||||||
if ( ! isset( $this->cached_mo_files[ $location ] ) ) {
|
$files = $this->get_language_files_from_path( $location );
|
||||||
$this->set_cached_mo_files( $location );
|
|
||||||
}
|
|
||||||
|
|
||||||
$path = "$location/$domain-$locale.mo";
|
$path = "$location/$domain-$locale.mo";
|
||||||
|
|
||||||
foreach ( $this->cached_mo_files[ $location ] as $mo_path ) {
|
foreach ( $files as $mo_path ) {
|
||||||
if (
|
if (
|
||||||
! in_array( $domain, $this->domains_with_translations, true ) &&
|
! in_array( $domain, $this->domains_with_translations, true ) &&
|
||||||
str_starts_with( str_replace( "$location/", '', $mo_path ), "$domain-" )
|
str_starts_with( str_replace( "$location/", '', $mo_path ), "$domain-" )
|
||||||
@ -215,21 +328,4 @@ class WP_Textdomain_Registry {
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads and caches all available MO files from a given directory.
|
|
||||||
*
|
|
||||||
* @since 6.1.0
|
|
||||||
*
|
|
||||||
* @param string $path Language directory path.
|
|
||||||
*/
|
|
||||||
private function set_cached_mo_files( $path ) {
|
|
||||||
$this->cached_mo_files[ $path ] = array();
|
|
||||||
|
|
||||||
$mo_files = glob( $path . '/*.mo' );
|
|
||||||
|
|
||||||
if ( $mo_files ) {
|
|
||||||
$this->cached_mo_files[ $path ] = $mo_files;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1398,15 +1398,21 @@ function translate_user_role( $name, $domain = 'default' ) {
|
|||||||
* @since 3.0.0
|
* @since 3.0.0
|
||||||
* @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter.
|
* @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter.
|
||||||
*
|
*
|
||||||
|
* @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
|
||||||
|
*
|
||||||
* @param string $dir A directory to search for language files.
|
* @param string $dir A directory to search for language files.
|
||||||
* Default WP_LANG_DIR.
|
* Default WP_LANG_DIR.
|
||||||
* @return string[] An array of language codes or an empty array if no languages are present.
|
* @return string[] An array of language codes or an empty array if no languages are present.
|
||||||
* Language codes are formed by stripping the .mo extension from the language file names.
|
* Language codes are formed by stripping the .mo extension from the language file names.
|
||||||
*/
|
*/
|
||||||
function get_available_languages( $dir = null ) {
|
function get_available_languages( $dir = null ) {
|
||||||
|
global $wp_textdomain_registry;
|
||||||
|
|
||||||
$languages = array();
|
$languages = array();
|
||||||
|
|
||||||
$lang_files = glob( ( is_null( $dir ) ? WP_LANG_DIR : $dir ) . '/*.mo' );
|
$path = is_null( $dir ) ? WP_LANG_DIR : $dir;
|
||||||
|
$lang_files = $wp_textdomain_registry->get_language_files_from_path( $path );
|
||||||
|
|
||||||
if ( $lang_files ) {
|
if ( $lang_files ) {
|
||||||
foreach ( $lang_files as $lang_file ) {
|
foreach ( $lang_files as $lang_file ) {
|
||||||
$lang_file = basename( $lang_file, '.mo' );
|
$lang_file = basename( $lang_file, '.mo' );
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '6.5-alpha-57286';
|
$wp_version = '6.5-alpha-57287';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
@ -380,6 +380,7 @@ $GLOBALS['wp_embed'] = new WP_Embed();
|
|||||||
* @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
|
* @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
|
||||||
*/
|
*/
|
||||||
$GLOBALS['wp_textdomain_registry'] = new WP_Textdomain_Registry();
|
$GLOBALS['wp_textdomain_registry'] = new WP_Textdomain_Registry();
|
||||||
|
$GLOBALS['wp_textdomain_registry']->init();
|
||||||
|
|
||||||
// Load multisite-specific files.
|
// Load multisite-specific files.
|
||||||
if ( is_multisite() ) {
|
if ( is_multisite() ) {
|
||||||
|
Loading…
Reference in New Issue
Block a user