From d4551e5d7823d018bccf5cb7ce5f4f8187a7136b Mon Sep 17 00:00:00 2001
From: jrfnl <github_nospam@adviesenzo.nl>
Date: Fri, 18 Nov 2016 03:15:31 -0500
Subject: [PATCH] Improve 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 ++++++++++++++++++----
.../tests/l10n/loadTextdomainJustInTime.php | 2 +
tests/phpunit/tests/l10n/localeSwitcher.php | 2 +
4 files changed, 64 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
|
b
|
class WP_Locale_Switcher {
|
| 228 | 228 | * @param string $locale The locale to change to. |
| 229 | 229 | */ |
| 230 | 230 | private function change_locale( $locale ) { |
| | 231 | // Reset translation availability information. |
| | 232 | _get_path_to_translation( null, true ); |
| | 233 | |
| 231 | 234 | $this->load_translations( $locale ); |
| 232 | 235 | |
| 233 | 236 | $GLOBALS['wp_locale'] = new WP_Locale(); |
diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php
index 35f3582..4fbb6af 100644
|
a
|
b
|
function load_child_theme_textdomain( $domain, $path = false ) {
|
| 827 | 827 | * the translation file from `wp-content/languages`, removing the need |
| 828 | 828 | * to call load_plugin_texdomain() or load_theme_texdomain(). |
| 829 | 829 | * |
| 830 | | * Holds a cached list of available .mo files to improve performance. |
| 831 | | * |
| 832 | 830 | * @since 4.6.0 |
| 833 | 831 | * @access private |
| 834 | 832 | * |
| … |
… |
function load_child_theme_textdomain( $domain, $path = false ) {
|
| 841 | 839 | function _load_textdomain_just_in_time( $domain ) { |
| 842 | 840 | global $l10n_unloaded; |
| 843 | 841 | |
| 844 | | $l10n_unloaded = (array) $l10n_unloaded; |
| 845 | | |
| 846 | | static $cached_mofiles = null; |
| | 842 | $l10n_unloaded = (array) $l10n_unloaded; |
| | 843 | $translation_path = _get_path_to_translation( $domain ); |
| 847 | 844 | |
| 848 | 845 | // Short-circuit if domain is 'default' which is reserved for core. |
| 849 | | if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) { |
| | 846 | if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) || false === $translation_path ) { |
| 850 | 847 | return false; |
| 851 | 848 | } |
| 852 | 849 | |
| | 850 | return load_textdomain( $domain, $translation_path ); |
| | 851 | } |
| | 852 | |
| | 853 | /** |
| | 854 | * Get the path to a translation file in the `wp-content/languages` directory. |
| | 855 | * |
| | 856 | * Caches the retrieved results internally. |
| | 857 | * |
| | 858 | * @since 4.7.0 |
| | 859 | * @access private |
| | 860 | * |
| | 861 | * @see _load_textdomain_just_in_time() |
| | 862 | * |
| | 863 | * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
| | 864 | * @param bool $reset Whether to reset the internal cache. Used by the switch to locale functionality. |
| | 865 | * @return string|false The path to the translation file or false if no translation file was found. |
| | 866 | */ |
| | 867 | function _get_path_to_translation( $domain, $reset = false ) { |
| | 868 | static $available_translations = array(); |
| | 869 | |
| | 870 | if ( true === $reset ) { |
| | 871 | $available_translations = array(); |
| | 872 | } |
| | 873 | |
| | 874 | if ( ! isset( $available_translations[ $domain ] ) ) { |
| | 875 | $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain ); |
| | 876 | } |
| | 877 | |
| | 878 | return $available_translations[ $domain ]; |
| | 879 | } |
| | 880 | |
| | 881 | /** |
| | 882 | * Get the path to a translation file in the languages directory for loading |
| | 883 | * a textdomain just in time. |
| | 884 | * |
| | 885 | * Holds a cached list of available .mo files to improve performance. |
| | 886 | * |
| | 887 | * @since 4.7.0 |
| | 888 | * @access private |
| | 889 | * |
| | 890 | * @see _get_path_to_translation() |
| | 891 | * |
| | 892 | * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
| | 893 | * @return string|false The path to the translation file or false if no translation file was found. |
| | 894 | */ |
| | 895 | function _get_path_to_translation_from_lang_dir( $domain ) { |
| | 896 | static $cached_mofiles = null; |
| | 897 | |
| 853 | 898 | if ( null === $cached_mofiles ) { |
| 854 | 899 | $cached_mofiles = array(); |
| 855 | 900 | |
| … |
… |
function _load_textdomain_just_in_time( $domain ) {
|
| 869 | 914 | $locale = is_admin() ? get_user_locale() : get_locale(); |
| 870 | 915 | $mofile = "{$domain}-{$locale}.mo"; |
| 871 | 916 | |
| 872 | | if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) { |
| 873 | | return load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ); |
| | 917 | $path = WP_LANG_DIR . '/plugins/' . $mofile; |
| | 918 | if ( in_array( $path, $cached_mofiles ) ) { |
| | 919 | return $path; |
| 874 | 920 | } |
| 875 | 921 | |
| 876 | | if ( in_array( WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles ) ) { |
| 877 | | return load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ); |
| | 922 | $path = WP_LANG_DIR . '/themes/' . $mofile; |
| | 923 | if ( in_array( $path, $cached_mofiles ) ) { |
| | 924 | return $path; |
| 878 | 925 | } |
| 879 | 926 | |
| 880 | 927 | return false; |
diff --git a/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php b/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php
index f0d7b55..c1d99ec 100644
|
a
|
b
|
class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase {
|
| 31 | 31 | unset( $GLOBALS['wp_themes'] ); |
| 32 | 32 | unset( $GLOBALS['l10n'] ); |
| 33 | 33 | unset( $GLOBALS['l10n_unloaded'] ); |
| | 34 | _get_path_to_translation( null, true ); |
| 34 | 35 | } |
| 35 | 36 | |
| 36 | 37 | public function tearDown() { |
| … |
… |
class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase {
|
| 42 | 43 | unset( $GLOBALS['wp_themes'] ); |
| 43 | 44 | unset( $GLOBALS['l10n'] ); |
| 44 | 45 | unset( $GLOBALS['l10n_unloaded'] ); |
| | 46 | _get_path_to_translation( null, true ); |
| 45 | 47 | |
| 46 | 48 | parent::tearDown(); |
| 47 | 49 | } |
diff --git a/tests/phpunit/tests/l10n/localeSwitcher.php b/tests/phpunit/tests/l10n/localeSwitcher.php
index 262f8b1..41492bd 100644
|
a
|
b
|
class Tests_Locale_Switcher extends WP_UnitTestCase {
|
| 24 | 24 | |
| 25 | 25 | unset( $GLOBALS['l10n'] ); |
| 26 | 26 | unset( $GLOBALS['l10n_unloaded'] ); |
| | 27 | _get_path_to_translation( null, true ); |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | public function tearDown() { |
| 30 | 31 | unset( $GLOBALS['l10n'] ); |
| 31 | 32 | unset( $GLOBALS['l10n_unloaded'] ); |
| | 33 | _get_path_to_translation( null, true ); |
| 32 | 34 | |
| 33 | 35 | parent::tearDown(); |
| 34 | 36 | } |