diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 07:57:26 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 07:57:26 +0000 |
commit | 30883c26bdceb9eaf32c8d4a1b0c1bce223b5226 (patch) | |
tree | 39a02e2aeb21ab5b7923c6f5757d66d55b708912 /wp-includes/class-wp-textdomain-registry.php | |
parent | Adding upstream version 6.4.3+dfsg1. (diff) | |
download | wordpress-30883c26bdceb9eaf32c8d4a1b0c1bce223b5226.tar.xz wordpress-30883c26bdceb9eaf32c8d4a1b0c1bce223b5226.zip |
Adding upstream version 6.5+dfsg1.upstream/6.5+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'wp-includes/class-wp-textdomain-registry.php')
-rw-r--r-- | wp-includes/class-wp-textdomain-registry.php | 157 |
1 files changed, 132 insertions, 25 deletions
diff --git a/wp-includes/class-wp-textdomain-registry.php b/wp-includes/class-wp-textdomain-registry.php index 99d72eb..fe6d04b 100644 --- a/wp-includes/class-wp-textdomain-registry.php +++ b/wp-includes/class-wp-textdomain-registry.php @@ -51,8 +51,11 @@ class WP_Textdomain_Registry { * Holds a cached list of available .mo files to improve performance. * * @since 6.1.0 + * @since 6.5.0 This property is no longer used. * * @var array + * + * @deprecated */ protected $cached_mo_files = array(); @@ -66,6 +69,18 @@ class WP_Textdomain_Registry { 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. * * @since 6.1.0 @@ -135,6 +150,115 @@ class WP_Textdomain_Registry { } /** + * Retrieves translation 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 translation files. + * @return array Array of translation file paths. Can contain .mo and .l10n.php files. + */ + public function get_language_files_from_path( $path ) { + $path = rtrim( $path, '/' ) . '/'; + + /** + * Filters the translation 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 $files List of translation files. Default null. + * @param string $path The path from which translation files are being fetched. + **/ + $files = apply_filters( 'pre_get_language_files_from_path', null, $path ); + + if ( null !== $files ) { + return $files; + } + + $cache_key = md5( $path ); + $files = wp_cache_get( $cache_key, 'translation_files' ); + + if ( false === $files ) { + $files = glob( $path . '*.mo' ); + if ( false === $files ) { + $files = array(); + } + + $php_files = glob( $path . '*.l10n.php' ); + if ( is_array( $php_files ) ) { + $files = array_merge( $files, $php_files ); + } + + wp_cache_set( $cache_key, $files, 'translation_files', HOUR_IN_SECONDS ); + } + + return $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. + * } + * } + */ + public function invalidate_mo_files_cache( $upgrader, $hook_extra ) { + if ( + ! isset( $hook_extra['type'] ) || + '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( md5( WP_LANG_DIR . '/plugins/' ), 'translation_files' ); + break; + case 'theme': + wp_cache_delete( md5( WP_LANG_DIR . '/themes/' ), 'translation_files' ); + break; + default: + wp_cache_delete( md5( WP_LANG_DIR . '/' ), 'translation_files' ); + break; + } + } + } + + /** * Returns possible language directory paths for a given text domain. * * @since 6.2.0 @@ -156,7 +280,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 * custom directory set via {@see load_plugin_textdomain()} or {@see load_theme_textdomain()}. @@ -175,22 +299,22 @@ class WP_Textdomain_Registry { $found_location = false; foreach ( $locations as $location ) { - if ( ! isset( $this->cached_mo_files[ $location ] ) ) { - $this->set_cached_mo_files( $location ); - } + $files = $this->get_language_files_from_path( $location ); - $path = "$location/$domain-$locale.mo"; + $mo_path = "$location/$domain-$locale.mo"; + $php_path = "$location/$domain-$locale.l10n.php"; - foreach ( $this->cached_mo_files[ $location ] as $mo_path ) { + foreach ( $files as $file_path ) { if ( ! in_array( $domain, $this->domains_with_translations, true ) && - str_starts_with( str_replace( "$location/", '', $mo_path ), "$domain-" ) + str_starts_with( str_replace( "$location/", '', $file_path ), "$domain-" ) ) { $this->domains_with_translations[] = $domain; } - if ( $mo_path === $path ) { + if ( $file_path === $mo_path || $file_path === $php_path ) { $found_location = rtrim( $location, '/' ) . '/'; + break 2; } } } @@ -215,21 +339,4 @@ class WP_Textdomain_Registry { 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; - } - } } |