From 30593b01b4a61d76e068f25a6e1ffd94ca971128 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 29 May 2023 08:36:23 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-textdomain-registry.php | 2 +- wp-includes/l10n.php | 10 +++++++++- wp-includes/version.php | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/wp-includes/class-wp-textdomain-registry.php b/wp-includes/class-wp-textdomain-registry.php index 4564a6c0bf..fc3994ad15 100644 --- a/wp-includes/class-wp-textdomain-registry.php +++ b/wp-includes/class-wp-textdomain-registry.php @@ -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 ) ); diff --git a/wp-includes/l10n.php b/wp-includes/l10n.php index 080007b869..a0c7bba903 100644 --- a/wp-includes/l10n.php +++ b/wp-includes/l10n.php @@ -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; } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 50dc350fb9..9f1b619e6b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.