Edit File by line
/home/zeestwma/richards.../wp-inclu...
File: class-wp-textdomain-registry.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Locale API: WP_Textdomain_Registry class.
[2] Fix | Delete
*
[3] Fix | Delete
* This file uses rtrim() instead of untrailingslashit() and trailingslashit()
[4] Fix | Delete
* to avoid formatting.php dependency.
[5] Fix | Delete
*
[6] Fix | Delete
* @package WordPress
[7] Fix | Delete
* @subpackage i18n
[8] Fix | Delete
* @since 6.1.0
[9] Fix | Delete
*/
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Core class used for registering text domains.
[13] Fix | Delete
*
[14] Fix | Delete
* @since 6.1.0
[15] Fix | Delete
*/
[16] Fix | Delete
#[AllowDynamicProperties]
[17] Fix | Delete
class WP_Textdomain_Registry {
[18] Fix | Delete
/**
[19] Fix | Delete
* List of domains and all their language directory paths for each locale.
[20] Fix | Delete
*
[21] Fix | Delete
* @since 6.1.0
[22] Fix | Delete
*
[23] Fix | Delete
* @var array
[24] Fix | Delete
*/
[25] Fix | Delete
protected $all = array();
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* List of domains and their language directory path for the current (most recent) locale.
[29] Fix | Delete
*
[30] Fix | Delete
* @since 6.1.0
[31] Fix | Delete
*
[32] Fix | Delete
* @var array
[33] Fix | Delete
*/
[34] Fix | Delete
protected $current = array();
[35] Fix | Delete
[36] Fix | Delete
/**
[37] Fix | Delete
* List of domains and their custom language directory paths.
[38] Fix | Delete
*
[39] Fix | Delete
* @see load_plugin_textdomain()
[40] Fix | Delete
* @see load_theme_textdomain()
[41] Fix | Delete
*
[42] Fix | Delete
* @since 6.1.0
[43] Fix | Delete
*
[44] Fix | Delete
* @var array
[45] Fix | Delete
*/
[46] Fix | Delete
protected $custom_paths = array();
[47] Fix | Delete
[48] Fix | Delete
/**
[49] Fix | Delete
* Holds a cached list of available .mo files to improve performance.
[50] Fix | Delete
*
[51] Fix | Delete
* @since 6.1.0
[52] Fix | Delete
* @since 6.5.0 This property is no longer used.
[53] Fix | Delete
*
[54] Fix | Delete
* @var array
[55] Fix | Delete
*
[56] Fix | Delete
* @deprecated
[57] Fix | Delete
*/
[58] Fix | Delete
protected $cached_mo_files = array();
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Holds a cached list of domains with translations to improve performance.
[62] Fix | Delete
*
[63] Fix | Delete
* @since 6.2.0
[64] Fix | Delete
*
[65] Fix | Delete
* @var string[]
[66] Fix | Delete
*/
[67] Fix | Delete
protected $domains_with_translations = array();
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Initializes the registry.
[71] Fix | Delete
*
[72] Fix | Delete
* Hooks into the {@see 'upgrader_process_complete'} filter
[73] Fix | Delete
* to invalidate MO files caches.
[74] Fix | Delete
*
[75] Fix | Delete
* @since 6.5.0
[76] Fix | Delete
*/
[77] Fix | Delete
public function init() {
[78] Fix | Delete
add_action( 'upgrader_process_complete', array( $this, 'invalidate_mo_files_cache' ), 10, 2 );
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Returns the languages directory path for a specific domain and locale.
[83] Fix | Delete
*
[84] Fix | Delete
* @since 6.1.0
[85] Fix | Delete
*
[86] Fix | Delete
* @param string $domain Text domain.
[87] Fix | Delete
* @param string $locale Locale.
[88] Fix | Delete
*
[89] Fix | Delete
* @return string|false Languages directory path or false if there is none available.
[90] Fix | Delete
*/
[91] Fix | Delete
public function get( $domain, $locale ) {
[92] Fix | Delete
$path = $this->all[ $domain ][ $locale ] ?? $this->get_path_from_lang_dir( $domain, $locale );
[93] Fix | Delete
[94] Fix | Delete
/**
[95] Fix | Delete
* Filters the determined languages directory path for a specific domain and locale.
[96] Fix | Delete
*
[97] Fix | Delete
* @since 6.6.0
[98] Fix | Delete
*
[99] Fix | Delete
* @param string|false $path Languages directory path for the given domain and locale.
[100] Fix | Delete
* @param string $domain Text domain.
[101] Fix | Delete
* @param string $locale Locale.
[102] Fix | Delete
*/
[103] Fix | Delete
return apply_filters( 'lang_dir_for_domain', $path, $domain, $locale );
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
/**
[107] Fix | Delete
* Determines whether any MO file paths are available for the domain.
[108] Fix | Delete
*
[109] Fix | Delete
* This is the case if a path has been set for the current locale,
[110] Fix | Delete
* or if there is no information stored yet, in which case
[111] Fix | Delete
* {@see _load_textdomain_just_in_time()} will fetch the information first.
[112] Fix | Delete
*
[113] Fix | Delete
* @since 6.1.0
[114] Fix | Delete
*
[115] Fix | Delete
* @param string $domain Text domain.
[116] Fix | Delete
* @return bool Whether any MO file paths are available for the domain.
[117] Fix | Delete
*/
[118] Fix | Delete
public function has( $domain ) {
[119] Fix | Delete
return (
[120] Fix | Delete
isset( $this->current[ $domain ] ) ||
[121] Fix | Delete
empty( $this->all[ $domain ] ) ||
[122] Fix | Delete
in_array( $domain, $this->domains_with_translations, true )
[123] Fix | Delete
);
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
/**
[127] Fix | Delete
* Sets the language directory path for a specific domain and locale.
[128] Fix | Delete
*
[129] Fix | Delete
* Also sets the 'current' property for direct access
[130] Fix | Delete
* to the path for the current (most recent) locale.
[131] Fix | Delete
*
[132] Fix | Delete
* @since 6.1.0
[133] Fix | Delete
*
[134] Fix | Delete
* @param string $domain Text domain.
[135] Fix | Delete
* @param string $locale Locale.
[136] Fix | Delete
* @param string|false $path Language directory path or false if there is none available.
[137] Fix | Delete
*/
[138] Fix | Delete
public function set( $domain, $locale, $path ) {
[139] Fix | Delete
$this->all[ $domain ][ $locale ] = $path ? rtrim( $path, '/' ) . '/' : false;
[140] Fix | Delete
$this->current[ $domain ] = $this->all[ $domain ][ $locale ];
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Sets the custom path to the plugin's/theme's languages directory.
[145] Fix | Delete
*
[146] Fix | Delete
* Used by {@see load_plugin_textdomain()} and {@see load_theme_textdomain()}.
[147] Fix | Delete
*
[148] Fix | Delete
* @since 6.1.0
[149] Fix | Delete
*
[150] Fix | Delete
* @param string $domain Text domain.
[151] Fix | Delete
* @param string $path Language directory path.
[152] Fix | Delete
*/
[153] Fix | Delete
public function set_custom_path( $domain, $path ) {
[154] Fix | Delete
// If just-in-time loading was triggered before, reset the entry so it can be tried again.
[155] Fix | Delete
[156] Fix | Delete
if ( isset( $this->all[ $domain ] ) ) {
[157] Fix | Delete
$this->all[ $domain ] = array_filter( $this->all[ $domain ] );
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
if ( empty( $this->current[ $domain ] ) ) {
[161] Fix | Delete
unset( $this->current[ $domain ] );
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
$this->custom_paths[ $domain ] = rtrim( $path, '/' );
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
/**
[168] Fix | Delete
* Retrieves translation files from the specified path.
[169] Fix | Delete
*
[170] Fix | Delete
* Allows early retrieval through the {@see 'pre_get_mo_files_from_path'} filter to optimize
[171] Fix | Delete
* performance, especially in directories with many files.
[172] Fix | Delete
*
[173] Fix | Delete
* @since 6.5.0
[174] Fix | Delete
*
[175] Fix | Delete
* @param string $path The directory path to search for translation files.
[176] Fix | Delete
* @return array Array of translation file paths. Can contain .mo and .l10n.php files.
[177] Fix | Delete
*/
[178] Fix | Delete
public function get_language_files_from_path( $path ) {
[179] Fix | Delete
$path = rtrim( $path, '/' ) . '/';
[180] Fix | Delete
[181] Fix | Delete
/**
[182] Fix | Delete
* Filters the translation files retrieved from a specified path before the actual lookup.
[183] Fix | Delete
*
[184] Fix | Delete
* Returning a non-null value from the filter will effectively short-circuit
[185] Fix | Delete
* the MO files lookup, returning that value instead.
[186] Fix | Delete
*
[187] Fix | Delete
* This can be useful in situations where the directory contains a large number of files
[188] Fix | Delete
* and the default glob() function becomes expensive in terms of performance.
[189] Fix | Delete
*
[190] Fix | Delete
* @since 6.5.0
[191] Fix | Delete
*
[192] Fix | Delete
* @param null|array $files List of translation files. Default null.
[193] Fix | Delete
* @param string $path The path from which translation files are being fetched.
[194] Fix | Delete
*/
[195] Fix | Delete
$files = apply_filters( 'pre_get_language_files_from_path', null, $path );
[196] Fix | Delete
[197] Fix | Delete
if ( null !== $files ) {
[198] Fix | Delete
return $files;
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
$cache_key = md5( $path );
[202] Fix | Delete
$files = wp_cache_get( $cache_key, 'translation_files' );
[203] Fix | Delete
[204] Fix | Delete
if ( false === $files ) {
[205] Fix | Delete
$files = glob( $path . '*.mo' );
[206] Fix | Delete
if ( false === $files ) {
[207] Fix | Delete
$files = array();
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
$php_files = glob( $path . '*.l10n.php' );
[211] Fix | Delete
if ( is_array( $php_files ) ) {
[212] Fix | Delete
$files = array_merge( $files, $php_files );
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
wp_cache_set( $cache_key, $files, 'translation_files', HOUR_IN_SECONDS );
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
return $files;
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
/**
[222] Fix | Delete
* Invalidate the cache for .mo files.
[223] Fix | Delete
*
[224] Fix | Delete
* This function deletes the cache entries related to .mo files when triggered
[225] Fix | Delete
* by specific actions, such as the completion of an upgrade process.
[226] Fix | Delete
*
[227] Fix | Delete
* @since 6.5.0
[228] Fix | Delete
*
[229] Fix | Delete
* @param WP_Upgrader $upgrader Unused. WP_Upgrader instance. In other contexts this might be a
[230] Fix | Delete
* Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
[231] Fix | Delete
* @param array $hook_extra {
[232] Fix | Delete
* Array of bulk item update data.
[233] Fix | Delete
*
[234] Fix | Delete
* @type string $action Type of action. Default 'update'.
[235] Fix | Delete
* @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
[236] Fix | Delete
* @type bool $bulk Whether the update process is a bulk update. Default true.
[237] Fix | Delete
* @type array $plugins Array of the basename paths of the plugins' main files.
[238] Fix | Delete
* @type array $themes The theme slugs.
[239] Fix | Delete
* @type array $translations {
[240] Fix | Delete
* Array of translations update data.
[241] Fix | Delete
*
[242] Fix | Delete
* @type string $language The locale the translation is for.
[243] Fix | Delete
* @type string $type Type of translation. Accepts 'plugin', 'theme', or 'core'.
[244] Fix | Delete
* @type string $slug Text domain the translation is for. The slug of a theme/plugin or
[245] Fix | Delete
* 'default' for core translations.
[246] Fix | Delete
* @type string $version The version of a theme, plugin, or core.
[247] Fix | Delete
* }
[248] Fix | Delete
* }
[249] Fix | Delete
*/
[250] Fix | Delete
public function invalidate_mo_files_cache( $upgrader, $hook_extra ) {
[251] Fix | Delete
if (
[252] Fix | Delete
! isset( $hook_extra['type'] ) ||
[253] Fix | Delete
'translation' !== $hook_extra['type'] ||
[254] Fix | Delete
array() === $hook_extra['translations']
[255] Fix | Delete
) {
[256] Fix | Delete
return;
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
$translation_types = array_unique( wp_list_pluck( $hook_extra['translations'], 'type' ) );
[260] Fix | Delete
[261] Fix | Delete
foreach ( $translation_types as $type ) {
[262] Fix | Delete
switch ( $type ) {
[263] Fix | Delete
case 'plugin':
[264] Fix | Delete
wp_cache_delete( md5( WP_LANG_DIR . '/plugins/' ), 'translation_files' );
[265] Fix | Delete
break;
[266] Fix | Delete
case 'theme':
[267] Fix | Delete
wp_cache_delete( md5( WP_LANG_DIR . '/themes/' ), 'translation_files' );
[268] Fix | Delete
break;
[269] Fix | Delete
default:
[270] Fix | Delete
wp_cache_delete( md5( WP_LANG_DIR . '/' ), 'translation_files' );
[271] Fix | Delete
break;
[272] Fix | Delete
}
[273] Fix | Delete
}
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
/**
[277] Fix | Delete
* Returns possible language directory paths for a given text domain.
[278] Fix | Delete
*
[279] Fix | Delete
* @since 6.2.0
[280] Fix | Delete
*
[281] Fix | Delete
* @param string $domain Text domain.
[282] Fix | Delete
* @return string[] Array of language directory paths.
[283] Fix | Delete
*/
[284] Fix | Delete
private function get_paths_for_domain( $domain ) {
[285] Fix | Delete
$locations = array(
[286] Fix | Delete
WP_LANG_DIR . '/plugins',
[287] Fix | Delete
WP_LANG_DIR . '/themes',
[288] Fix | Delete
);
[289] Fix | Delete
[290] Fix | Delete
if ( isset( $this->custom_paths[ $domain ] ) ) {
[291] Fix | Delete
$locations[] = $this->custom_paths[ $domain ];
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
return $locations;
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
/**
[298] Fix | Delete
* Gets the path to the language directory for the current domain and locale.
[299] Fix | Delete
*
[300] Fix | Delete
* Checks the plugins and themes language directories as well as any
[301] Fix | Delete
* custom directory set via {@see load_plugin_textdomain()} or {@see load_theme_textdomain()}.
[302] Fix | Delete
*
[303] Fix | Delete
* @since 6.1.0
[304] Fix | Delete
*
[305] Fix | Delete
* @see _get_path_to_translation_from_lang_dir()
[306] Fix | Delete
*
[307] Fix | Delete
* @param string $domain Text domain.
[308] Fix | Delete
* @param string $locale Locale.
[309] Fix | Delete
* @return string|false Language directory path or false if there is none available.
[310] Fix | Delete
*/
[311] Fix | Delete
private function get_path_from_lang_dir( $domain, $locale ) {
[312] Fix | Delete
$locations = $this->get_paths_for_domain( $domain );
[313] Fix | Delete
[314] Fix | Delete
$found_location = false;
[315] Fix | Delete
[316] Fix | Delete
foreach ( $locations as $location ) {
[317] Fix | Delete
$files = $this->get_language_files_from_path( $location );
[318] Fix | Delete
[319] Fix | Delete
$mo_path = "$location/$domain-$locale.mo";
[320] Fix | Delete
$php_path = "$location/$domain-$locale.l10n.php";
[321] Fix | Delete
[322] Fix | Delete
foreach ( $files as $file_path ) {
[323] Fix | Delete
if (
[324] Fix | Delete
! in_array( $domain, $this->domains_with_translations, true ) &&
[325] Fix | Delete
str_starts_with( str_replace( "$location/", '', $file_path ), "$domain-" )
[326] Fix | Delete
) {
[327] Fix | Delete
$this->domains_with_translations[] = $domain;
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
if ( $file_path === $mo_path || $file_path === $php_path ) {
[331] Fix | Delete
$found_location = rtrim( $location, '/' ) . '/';
[332] Fix | Delete
break 2;
[333] Fix | Delete
}
[334] Fix | Delete
}
[335] Fix | Delete
}
[336] Fix | Delete
[337] Fix | Delete
if ( $found_location ) {
[338] Fix | Delete
$this->set( $domain, $locale, $found_location );
[339] Fix | Delete
[340] Fix | Delete
return $found_location;
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
/*
[344] Fix | Delete
* If no path is found for the given locale and a custom path has been set
[345] Fix | Delete
* using load_plugin_textdomain/load_theme_textdomain, use that one.
[346] Fix | Delete
*/
[347] Fix | Delete
if ( isset( $this->custom_paths[ $domain ] ) ) {
[348] Fix | Delete
$fallback_location = rtrim( $this->custom_paths[ $domain ], '/' ) . '/';
[349] Fix | Delete
$this->set( $domain, $locale, $fallback_location );
[350] Fix | Delete
return $fallback_location;
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
$this->set( $domain, $locale, false );
[354] Fix | Delete
[355] Fix | Delete
return false;
[356] Fix | Delete
}
[357] Fix | Delete
}
[358] Fix | Delete
[359] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function