From 39f91adcd53c761137ae0cfbdba2a29b4a947187 Mon Sep 17 00:00:00 2001
Date: Sat, 12 Nov 2016 01:55:57 +0100
Subject: [PATCH] Proof of concept for improving the performance of the
 `_load_textdomain_just_in_time()` function.

---
 src/wp-includes/class-wp-locale-switcher.php |  3 ++
 src/wp-includes/l10n.php                     | 67 +++++++++++++++++++++++-----
 2 files changed, 60 insertions(+), 10 deletions(-)

diff --git a/src/wp-includes/class-wp-locale-switcher.php b/src/wp-includes/class-wp-locale-switcher.php
index 3d288c8..4ccffab 100644
--- a/src/wp-includes/class-wp-locale-switcher.php
+++ b/src/wp-includes/class-wp-locale-switcher.php
@@ -228,6 +228,9 @@ class WP_Locale_Switcher {
 	 * @param string $locale The locale to change to.
 	 */
 	private function change_locale( $locale ) {
+		// Reset translation availability information.
+		_get_path_to_translation( null, true );
+
 		$this->load_translations( $locale );
 
 		$GLOBALS['wp_locale'] = new WP_Locale();
diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php
index 39346c4..cbd7985 100644
--- a/src/wp-includes/l10n.php
+++ b/src/wp-includes/l10n.php
@@ -827,8 +827,6 @@ function load_child_theme_textdomain( $domain, $path = false ) {
  * the translation file from `wp-content/languages`, removing the need
  * to call load_plugin_texdomain() or load_theme_texdomain().
  *
- * Holds a cached list of available .mo files to improve performance.
- *
  * @since 4.6.0
  * @access private
  *
@@ -841,15 +839,62 @@ function load_child_theme_textdomain( $domain, $path = false ) {
 function _load_textdomain_just_in_time( $domain ) {
 	global $l10n_unloaded;
 
-	$l10n_unloaded = (array) $l10n_unloaded;
-
-	static $cached_mofiles = null;
+	$l10n_unloaded    = (array) $l10n_unloaded;
+	$translation_path = _get_path_to_translation( $domain );
 
 	// Short-circuit if domain is 'default' which is reserved for core.
-	if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
+	if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) || false === $translation_path ) {
 		return false;
 	}
 
+	return load_textdomain( $domain, $translation_path );
+}
+
+/**
+ * Get the path to a translation file in the `wp-content/languages` directory.
+ *
+ * Caches the retrieved results internally.
+ *
+ * @since 4.7.0
+ * @access private
+ *
+ * @see _load_textdomain_just_in_time()
+ *
+ * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @param bool   $reset  Whether to reset the internal cache. Used by the switch to locale functionality.
+ * @return string|false The path to the translation file or false if no translation file was found.
+ */
+function _get_path_to_translation( $domain, $reset = false ) {
+	static $available_translations = array();
+
+	if ( true === $reset ) {
+		$available_translations = array();
+	}
+
+	if ( ! isset( $available_translations[ $domain ] ) ) {
+		$available_translations[ $domain ] = _get_path_to_glotpress_translation( $domain );
+	}
+
+	return $available_translations[ $domain ];
+}
+
+/**
+ * Get the path to a translation file in the languages directory for loading
+ * a textdomain just in time.
+ *
+ * Holds a cached list of available .mo files to improve performance.
+ *
+ * @since 4.7.0
+ * @access private
+ *
+ * @see _get_path_to_translation()
+ *
+ * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @return string|false The path to the translation file or false if no translation file was found.
+ */
+function _get_path_to_glotpress_translation( $domain ) {
+	static $cached_mofiles = null;
+
 	if ( null === $cached_mofiles ) {
 		$cached_mofiles = array();
 
@@ -868,12 +913,14 @@ function _load_textdomain_just_in_time( $domain ) {
 	$locale = is_admin() ? get_user_locale() : get_locale();
 	$mofile = "{$domain}-{$locale}.mo";
 
-	if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) {
-		return load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile );
+	$path = WP_LANG_DIR . '/plugins/' . $mofile;
+	if ( in_array( $path, $cached_mofiles ) ) {
+		return $path;
 	}
 
-	if ( in_array( WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles ) ) {
-		return load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile );
+	$path = WP_LANG_DIR . '/themes/' . $mofile;
+	if ( in_array( $path, $cached_mofiles ) ) {
+		return $path;
 	}
 
 	return false;
-- 
1.9.5.msysgit.0

