Skip to content

Commit

Permalink
I18N: Change how WP_Textdomain_Registry stores the default language…
Browse files Browse the repository at this point in the history
…s path.

`WP_Textdomain_Registry` was introduced in [53874] to store text domains and their language directory paths, addressing issues with just-in-time loading of textdomains when using locale switching and when using`load_*_textdomain()` functions.

Said change has inadvertently caused a performance regression exactly when using`load_*_textdomain()`, which still often is the case, where the cached information was not further used or even overridden.

This change addresses that issue by storing the default languages paths in a separate way, while at the same time making `WP_Textdomain_Registry` easier to maintain and adding new tests to catch future regressions.

Props flixos90, spacedmonkey, ocean90, SergeyBiryukov, costdev.
Fixes #39210.

git-svn-id: https://develop.svn.wordpress.org/trunk@54669 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
swissspidy committed Oct 24, 2022
1 parent a5463b4 commit 799d7dc
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 83 deletions.
123 changes: 63 additions & 60 deletions src/wp-includes/class-wp-textdomain-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ class WP_Textdomain_Registry {
*/
protected $current = array();

/**
* List of domains and their custom language directory paths.
*
* @see load_plugin_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.
*
Expand All @@ -42,7 +54,7 @@ class WP_Textdomain_Registry {
protected $cached_mo_files;

/**
* Returns the MO file path for a specific domain and locale.
* Returns the languages directory path for a specific domain and locale.
*
* @since 6.1.0
*
Expand All @@ -62,33 +74,21 @@ public function get( $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
* {@see _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 ! empty( $this->all[ $domain ] );
}

/**
* Returns the current (most recent) MO file path for a specific domain.
*
* @since 6.1.0
*
* @param string $domain Text domain.
* @return string|false Current MO file path or false if there is none available.
*/
public function get_current( $domain ) {
if ( isset( $this->current[ $domain ] ) ) {
return $this->current[ $domain ];
}

return false;
return ! empty( $this->current[ $domain ] ) || empty( $this->all[ $domain ] );
}

/**
* Sets the MO file path for a specific domain and locale.
* Sets the language 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.
Expand All @@ -105,81 +105,84 @@ public function set( $domain, $locale, $path ) {
}

/**
* Resets the registry state.
* Sets the custom path to the plugin's/theme's languages directory.
*
* @since 6.1.0
* Used by {@see load_plugin_textdomain()} and {@see load_theme_textdomain()}.
*
* @param string $domain Text domain.
* @param string $path Language directory path.
*/
public function reset() {
$this->cached_mo_files = null;
$this->all = array();
$this->current = array();
public function set_custom_path( $domain, $path ) {
$this->custom_paths[ $domain ] = untrailingslashit( $path );
}

/**
* Gets the path to a translation file in the languages directory for the current locale.
* Gets the path to the language directory for the current 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()}.
*
* @since 6.1.0
*
* @see _get_path_to_translation_from_lang_dir()
*
* @param string $domain Text domain.
* @param string $locale Locale.
* @return string|false MO file path or false if there is none available.
* @return string|false Language directory path or false if there is none available.
*/
private function get_path_from_lang_dir( $domain, $locale ) {
if ( null === $this->cached_mo_files ) {
$this->set_cached_mo_files();
$locations = array(
WP_LANG_DIR . '/plugins',
WP_LANG_DIR . '/themes',
);

if ( isset( $this->custom_paths[ $domain ] ) ) {
$locations[] = $this->custom_paths[ $domain ];
}

$mofile = "{$domain}-{$locale}.mo";
$mofile = "$domain-$locale.mo";

foreach ( $locations as $location ) {
if ( ! isset( $this->cached_mo_files[ $location ] ) ) {
$this->set_cached_mo_files( $location );
}

$path = WP_LANG_DIR . '/plugins/' . $mofile;
$path = $location . '/' . $mofile;

if ( in_array( $path, $this->cached_mo_files, true ) ) {
$path = WP_LANG_DIR . '/plugins/';
$this->set( $domain, $locale, $path );
if ( in_array( $path, $this->cached_mo_files[ $location ], true ) ) {
$this->set( $domain, $locale, $location );

return $path;
return trailingslashit( $location );
}
}

$path = WP_LANG_DIR . '/themes/' . $mofile;
if ( in_array( $path, $this->cached_mo_files, true ) ) {
$path = WP_LANG_DIR . '/themes/';
// If no path is found for the given locale and a custom path has been set
// using load_plugin_textdomain/load_theme_textdomain, use that one.
if ( 'en_US' !== $locale && isset( $this->custom_paths[ $domain ] ) ) {
$path = trailingslashit( $this->custom_paths[ $domain ] );
$this->set( $domain, $locale, $path );

return $path;
}

// If no path is found for the given locale, check if an entry for the default
// en_US locale exists. This is the case when e.g. using load_plugin_textdomain
// with a custom path.
if ( 'en_US' !== $locale && isset( $this->all[ $domain ]['en_US'] ) ) {
$this->set( $domain, $locale, $this->all[ $domain ]['en_US'] );
return $this->all[ $domain ]['en_US'];
}

$this->set( $domain, $locale, false );

return false;
}

/**
* Reads and caches all available MO files from the plugins and themes language directories.
* Reads and caches all available MO files from a given directory.
*
* @since 6.1.0
*
* @param string $path Language directory path.
*/
protected function set_cached_mo_files() {
$this->cached_mo_files = array();

$locations = array(
WP_LANG_DIR . '/plugins',
WP_LANG_DIR . '/themes',
);
private function set_cached_mo_files( $path ) {
$this->cached_mo_files[ $path ] = array();

foreach ( $locations as $location ) {
$mo_files = glob( $location . '/*.mo' );
$mo_files = glob( $path . '/*.mo' );

if ( $mo_files ) {
$this->cached_mo_files = array_merge( $this->cached_mo_files, $mo_files );
}
if ( $mo_files ) {
$this->cached_mo_files[ $path ] = $mo_files;
}
}
}
8 changes: 4 additions & 4 deletions src/wp-includes/l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path
$path = WP_PLUGIN_DIR;
}

$wp_textdomain_registry->set( $domain, $locale, $path );
$wp_textdomain_registry->set_custom_path( $domain, $path );

return load_textdomain( $domain, $path . '/' . $mofile, $locale );
}
Expand Down Expand Up @@ -968,7 +968,7 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {

$path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' );

$wp_textdomain_registry->set( $domain, $locale, $path );
$wp_textdomain_registry->set_custom_path( $domain, $path );

return load_textdomain( $domain, $path . '/' . $mofile, $locale );
}
Expand Down Expand Up @@ -1016,7 +1016,7 @@ function load_theme_textdomain( $domain, $path = false ) {
$path = get_template_directory();
}

$wp_textdomain_registry->set( $domain, $locale, $path );
$wp_textdomain_registry->set_custom_path( $domain, $path );

return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale );
}
Expand Down Expand Up @@ -1265,7 +1265,7 @@ function _load_textdomain_just_in_time( $domain ) {
return false;
}

if ( $wp_textdomain_registry->has( $domain ) && ! $wp_textdomain_registry->get_current( $domain ) ) {
if ( ! $wp_textdomain_registry->has( $domain ) ) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/l10n/loadTextdomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public function set_up() {
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
global $wp_textdomain_registry;

$wp_textdomain_registry->reset();
$wp_textdomain_registry = new WP_Textdomain_Registry();
}

public function tear_down() {

/** @var WP_Textdomain_Registry $wp_textdomain_registry */
global $wp_textdomain_registry;

$wp_textdomain_registry->reset();
$wp_textdomain_registry = new WP_Textdomain_Registry();

parent::tear_down();
}
Expand Down
37 changes: 26 additions & 11 deletions tests/phpunit/tests/l10n/loadTextdomainJustInTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Tests_L10n_LoadTextdomainJustInTime extends WP_UnitTestCase {
protected $orig_theme_dir;
protected $theme_root;
protected static $user_id;
private $locale_count;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$user_id = $factory->user->create(
Expand All @@ -24,7 +23,6 @@ public function set_up() {

$this->theme_root = DIR_TESTDATA . '/themedir1';
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
$this->locale_count = 0;

// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root );
Expand All @@ -37,7 +35,7 @@ public function set_up() {
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
global $wp_textdomain_registry;

$wp_textdomain_registry->reset();
$wp_textdomain_registry = new WP_Textdomain_Registry();
}

public function tear_down() {
Expand All @@ -48,7 +46,7 @@ public function tear_down() {
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
global $wp_textdomain_registry;

$wp_textdomain_registry->reset();
$wp_textdomain_registry = new WP_Textdomain_Registry();

parent::tear_down();
}
Expand Down Expand Up @@ -262,23 +260,40 @@ public function test_theme_translation_with_user_locale() {
public function test_get_locale_is_called_only_once_per_textdomain() {
$textdomain = 'foo-bar-baz';

add_filter( 'locale', array( $this, '_filter_locale_count' ) );
$filter = new MockAction();
add_filter( 'locale', array( $filter, 'filter' ) );

__( 'Foo', $textdomain );
__( 'Bar', $textdomain );
__( 'Baz', $textdomain );
__( 'Foo Bar', $textdomain );
__( 'Foo Bar Baz', $textdomain );

remove_filter( 'locale', array( $this, '_filter_locale_count' ) );

$this->assertFalse( is_textdomain_loaded( $textdomain ) );
$this->assertSame( 1, $this->locale_count );
$this->assertSame( 1, $filter->get_call_count() );
}

public function _filter_locale_count( $locale ) {
++$this->locale_count;
/**
* @ticket 37997
* @ticket 39210
*
* @covers ::_load_textdomain_just_in_time
*/
public function test_get_locale_is_called_only_once_per_textdomain_with_custom_lang_dir() {
load_plugin_textdomain( 'custom-internationalized-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

$textdomain = 'custom-internationalized-plugin';

return $locale;
$filter = new MockAction();
add_filter( 'locale', array( $filter, 'filter' ) );

__( 'Foo', $textdomain );
__( 'Bar', $textdomain );
__( 'Baz', $textdomain );
__( 'Foo Bar', $textdomain );
__( 'Foo Bar Baz', $textdomain );

$this->assertFalse( is_textdomain_loaded( $textdomain ) );
$this->assertSame( 1, $filter->get_call_count() );
}
}
14 changes: 8 additions & 6 deletions tests/phpunit/tests/l10n/wpLocaleSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function set_up() {
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
global $wp_textdomain_registry;

$wp_textdomain_registry->reset();
$wp_textdomain_registry = new WP_Textdomain_Registry();
}

public function tear_down() {
Expand All @@ -36,7 +36,7 @@ public function tear_down() {
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
global $wp_textdomain_registry;

$wp_textdomain_registry->reset();
$wp_textdomain_registry = new WP_Textdomain_Registry();

parent::tear_down();
}
Expand Down Expand Up @@ -478,11 +478,12 @@ public function test_switch_reloads_plugin_translations_outside_wp_lang_dir() {

require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php';

$registry_value = $wp_textdomain_registry->get( 'custom-internationalized-plugin', determine_locale() );

$actual = custom_i18n_plugin_test();

switch_to_locale( 'es_ES' );

$registry_value = $wp_textdomain_registry->get( 'custom-internationalized-plugin', determine_locale() );

switch_to_locale( 'de_DE' );

$actual_de_de = custom_i18n_plugin_test();
Expand Down Expand Up @@ -517,11 +518,12 @@ public function test_switch_reloads_theme_translations_outside_wp_lang_dir() {

require_once get_stylesheet_directory() . '/functions.php';

$registry_value = $wp_textdomain_registry->get( 'custom-internationalized-theme', determine_locale() );

$actual = custom_i18n_theme_test();

switch_to_locale( 'es_ES' );

$registry_value = $wp_textdomain_registry->get( 'custom-internationalized-theme', determine_locale() );

switch_to_locale( 'de_DE' );

$actual_de_de = custom_i18n_theme_test();
Expand Down
Loading

0 comments on commit 799d7dc

Please sign in to comment.