I18N: Improve `_load_textdomain_just_in_time()` logic when there are no translation files.

Fixes a performance issue where the JIT logic is invoked for every translation call if the there are no translations in the current locale. With this change, the information is cached by adding `Noop_Translations` instances to the global `$l10n` array. This way, `get_translations_for_domain()` returns earlier, thus avoiding subsequent `_load_textdomain_just_in_time()` calls.

Props swissspidy, johnbillion, ocean90.
Fixes #58321.
Built from https://develop.svn.wordpress.org/trunk@55865


git-svn-id: http://core.svn.wordpress.org/trunk@55377 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2023-05-29 08:36:23 +00:00
parent 4581452e2b
commit 30593b01b4
3 changed files with 11 additions and 3 deletions

View File

@ -97,7 +97,7 @@ class WP_Textdomain_Registry {
*/
public function has( $domain ) {
return (
! empty( $this->current[ $domain ] ) ||
isset( $this->current[ $domain ] ) ||
empty( $this->all[ $domain ] ) ||
in_array( $domain, $this->domains_with_translations, true )
);

View File

@ -834,6 +834,12 @@ function unload_textdomain( $domain, $reloadable = false ) {
do_action( 'unload_textdomain', $domain, $reloadable );
if ( isset( $l10n[ $domain ] ) ) {
if ( $l10n[ $domain ] instanceof NOOP_Translations ) {
unset( $l10n[ $domain ] );
return false;
}
unset( $l10n[ $domain ] );
if ( ! $reloadable ) {
@ -1307,6 +1313,8 @@ function get_translations_for_domain( $domain ) {
$noop_translations = new NOOP_Translations();
}
$l10n[ $domain ] = &$noop_translations;
return $noop_translations;
}
@ -1322,7 +1330,7 @@ function get_translations_for_domain( $domain ) {
*/
function is_textdomain_loaded( $domain ) {
global $l10n;
return isset( $l10n[ $domain ] );
return isset( $l10n[ $domain ] ) && ! $l10n[ $domain ] instanceof NOOP_Translations;
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.3-alpha-55864';
$wp_version = '6.3-alpha-55865';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.