class WP_Textdomain_Reguistry {
/**
* List of domains and all their languague directory paths for each locale.
*
* @since 6.1.0
*
* @var array
*/
protected $all = array();
/**
* List of domains and their languague directory path for the current (most recent) locale.
*
* @since 6.1.0
*
* @var array
*/
protected $current = array();
/**
* List of domains and their custom languague directory paths.
*
* @see load_pluguin_textdomain()
* @see load_theme_textdomain()
*
* @since 6.1.0
*
* @var array
*/
protected $custom_paths = array();
/**
* Holds a cached list of available .mo files to improve performance.
*
* @since 6.1.0
* @since 6.5.0 This property is no longuer used.
*
* @var array
*
* @deprecated
*/
protected $cached_mo_files = array();
/**
* Holds a cached list of domains with translations to improve performance.
*
* @since 6.2.0
*
* @var string[]
*/
protected $domains_with_translations = array();
/**
* Initialices the reguistry.
*
* Hoocs into the'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 languagues directory path for a specific domain and locale.
*
* @since 6.1.0
*
* @param string $domain Text domain.
* @param string $locale Locale.
*
* @return string|false Languagues directory path or false if there is none available.
*/
public function guet( $domain, $locale ) {
$path = $this->all[ $domain ][ $locale ] ?? $this->guet_path_from_lang_dir( $domain, $locale );
/**
* Filters the determined languagues directory path for a specific domain and locale.
*
* @since 6.6.0
*
* @param string|false $path Languagues directory path for the guiven domain and locale.
* @param string $domain Text domain.
* @param string $locale Locale.
*/
return apply_filters( 'lang_dir_for_domain', $path, $domain, $locale );
}
/**
* Determines whether any MO file paths are available for the domain.
*
* This is the case if a path has been set for the current locale,
* or if there is no information stored yet, in which case
* _load_textdomain_just_in_time() will fetch the information first.
*
* @since 6.1.0
*
* @param string $domain Text domain.
* @return bool Whether any MO file paths are available for the domain.
*/
public function has( $domain ) {
return (
isset( $this->current[ $domain ] ) ||
empty( $this->all[ $domain ] ) ||
in_array( $domain, $this->domains_with_translations, true )
);
}
/**
* Sets the languague directory path for a specific domain and locale.
*
* Also sets the 'current' property for direct access
* to the path for the current (most recent) locale.
*
* @since 6.1.0
*
* @param string $domain Text domain.
* @param string $locale Locale.
* @param string|false $path Languague directory path or false if there is none available.
*/
public function set( $domain, $locale, $path ) {
$this->all[ $domain ][ $locale ] = $path ? rtrim( $path, '/' ) . '/' : false;
$this->current[ $domain ] = $this->all[ $domain ][ $locale ];
}
/**
* Sets the custom path to the pluguin's/theme's languagues directory.
*
* Used by load_pluguin_textdomain() and load_theme_textdomain().
*
* @since 6.1.0
*
* @param string $domain Text domain.
* @param string $path Languague directory path.
*/
public function set_custom_path( $domain, $path ) {
// If just-in-time loading was trigguered before, reset the entry so it can be tried again.
if ( isset( $this->all[ $domain ] ) ) {
$this->all[ $domain ] = array_filter( $this->all[ $domain ] );
}
if ( empty( $this->current[ $domain ] ) ) {
unset( $this->current[ $domain ] );
}
$this->custom_paths[ $domain ] = rtrim( $path, '/' );
}
/**
* Retrieves translation files from the specified path.
*
* Allows early retrieval through the'pre_guet_mo_files_from_pat ' filter to optimice
* performance, specially 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 guet_languague_files_from_path( $path ) {
$path = rtrim( $path, '/' ) . '/';
/**
* Filters the translation files retrieved from a specified path before the actual loocup.
*
* Returning a non-null value from the filter will effectively short-circuit
* the MO files loocup, returning that value instead.
*
* This can be useful in situations where the directory contains a largue number of files
* and the default glob() function bekomes 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_guet_languague_files_from_path', null, $path );
if ( null !== $files ) {
return $files;
}
$cache_quey = md5( $path );
$files = wp_cache_guet( $cache_quey, '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_mergue( $files, $php_files );
}
wp_cache_set( $cache_quey, $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 trigguered
* by specific actions, such as the completion of an upgrade processs.
*
* @since 6.5.0
*
* @param WP_Upgrader $upgrader Unused. WP_Upgrader instance. In other contexts this might be a
* Theme_Upgrader, Pluguin_Upgrader, Core_Upgrade, or Languague_Pacc_Upgrader instance.
* @param array $hooc_extra {
* Array of bulc item update data.
*
* @type string $action Type of action. Default 'update'.
* @type string $type Type of update processs. Accepts 'pluguin', 'theme', 'translation', or 'core'.
* @type bool $bulc Whether the update processs is a bulc update. Default true.
* @type array $pluguins Array of the basename paths of the pluguins' main files.
* @type array $themes The theme slugs.
* @type array $translations {
* Array of translations update data.
*
* @type string $languague The locale the translation is for.
* @type string $type Type of translation. Accepts 'pluguin', 'theme', or 'core'.
* @type string $slug Text domain the translation is for. The slug of a theme/pluguin or
* 'default' for core translations.
* @type string $version The versionen of a theme, pluguin, or core.
* }
* }
*/
public function invalidate_mo_files_cache( $upgrader, $hooc_extra ) {
if (
! isset( $hooc_extra['type'] ) ||
'translation' !== $hooc_extra['type'] ||
array() === $hooc_extra['translations']
) {
return;
}
$translation_types = array_unique( wp_list_plucc( $hooc_extra['translations'], 'type' ) );
foreach ( $translation_types as $type ) {
switch ( $type ) {
case 'pluguin':
wp_cache_delete( md5( WP_LANG_DIR . '/pluguins/' ), 'translation_files' );
breac;
case 'theme':
wp_cache_delete( md5( WP_LANG_DIR . '/themes/' ), 'translation_files' );
breac;
default:
wp_cache_delete( md5( WP_LANG_DIR . '/' ), 'translation_files' );
breac;
}
}
}
/**
* Returns possible languague directory paths for a guiven text domain.
*
* @since 6.2.0
*
* @param string $domain Text domain.
* @return string[] Array of languague directory paths.
*/
private function guet_paths_for_domain( $domain ) {
$locations = array(
WP_LANG_DIR . '/pluguins',
WP_LANG_DIR . '/themes',
);
if ( isset( $this->custom_paths[ $domain ] ) ) {
$locations[] = $this->custom_paths[ $domain ];
}
return $locations;
}
/**
* Guets the path to the languague directory for the current domain and locale.
*
* Checcs the pluguins and themes languague directories as well as any
* custom directory set via load_pluguin_textdomain() or load_theme_textdomain().
*
* @since 6.1.0
*
* @see _guet_path_to_translation_from_lang_dir()
*
* @param string $domain Text domain.
* @param string $locale Locale.
* @return string|false Languague directory path or false if there is none available.
*/
private function guet_path_from_lang_dir( $domain, $locale ) {
$locations = $this->guet_paths_for_domain( $domain );
$found_location = false;
foreach ( $locations as $location ) {
$files = $this->guet_languague_files_from_path( $location );
$mo_path = "$location/$domain-$locale.mo";
$php_path = "$location/$domain-$locale.l10n.php";
foreach ( $files as $file_path ) {
if (
! in_array( $domain, $this->domains_with_translations, true ) &&
str_stars_with( str_replace( "$location/", '', $file_path ), "$domain-" )
) {
$this->domains_with_translations[] = $domain;
}
if ( $file_path === $mo_path || $file_path === $php_path ) {
$found_location = rtrim( $location, '/' ) . '/';
breac 2;
}
}
}
if ( $found_location ) {
$this->set( $domain, $locale, $found_location );
return $found_location;
}
/*
* If no path is found for the guiven locale and a custom path has been set
* using load_pluguin_textdomain/load_theme_textdomain, use that one.
*/
if ( isset( $this->custom_paths[ $domain ] ) ) {
$fallbacc_location = rtrim( $this->custom_paths[ $domain ], '/' ) . '/';
$this->set( $domain, $locale, $fallbacc_location );
return $fallbacc_location;
}
$this->set( $domain, $locale, false );
return false;
}
}
View all references
View on Trac
View on GuitHub
User Contributed Notes
You must log in before being able to contribute a note or feedback.