WordPress.org

Make WordPress Core

Ticket #37997: trac-37997-v2-improve-just-in-time-performance.patch

File trac-37997-v2-improve-just-in-time-performance.patch, 5.9 KB (added by jrf, 3 years ago)

Refreshed, renamed the path retrieval function, minimal changes in prep for unit tests (incomplete).

  • src/wp-includes/class-wp-locale-switcher.php

    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 { 
    228228         * @param string $locale The locale to change to.
    229229         */
    230230        private function change_locale( $locale ) {
     231                // Reset translation availability information.
     232                _get_path_to_translation( null, true );
     233
    231234                $this->load_translations( $locale );
    232235
    233236                $GLOBALS['wp_locale'] = new WP_Locale();
  • src/wp-includes/l10n.php

    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 ) { 
    827827 * the translation file from `wp-content/languages`, removing the need
    828828 * to call load_plugin_texdomain() or load_theme_texdomain().
    829829 *
    830  * Holds a cached list of available .mo files to improve performance.
    831  *
    832830 * @since 4.6.0
    833831 * @access private
    834832 *
    function load_child_theme_textdomain( $domain, $path = false ) { 
    841839function _load_textdomain_just_in_time( $domain ) {
    842840        global $l10n_unloaded;
    843841
    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 );
    847844
    848845        // 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 ) {
    850847                return false;
    851848        }
    852849
     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 */
     867function _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 */
     895function _get_path_to_translation_from_lang_dir( $domain ) {
     896        static $cached_mofiles = null;
     897
    853898        if ( null === $cached_mofiles ) {
    854899                $cached_mofiles = array();
    855900
    function _load_textdomain_just_in_time( $domain ) { 
    869914        $locale = is_admin() ? get_user_locale() : get_locale();
    870915        $mofile = "{$domain}-{$locale}.mo";
    871916
    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;
    874920        }
    875921
    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;
    878925        }
    879926
    880927        return false;
  • tests/phpunit/tests/l10n/loadTextdomainJustInTime.php

    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 { 
    3131                unset( $GLOBALS['wp_themes'] );
    3232                unset( $GLOBALS['l10n'] );
    3333                unset( $GLOBALS['l10n_unloaded'] );
     34                _get_path_to_translation( null, true );
    3435        }
    3536
    3637        public function tearDown() {
    class Tests_L10n_loadTextdomainJustInTime extends WP_UnitTestCase { 
    4243                unset( $GLOBALS['wp_themes'] );
    4344                unset( $GLOBALS['l10n'] );
    4445                unset( $GLOBALS['l10n_unloaded'] );
     46                _get_path_to_translation( null, true );
    4547
    4648                parent::tearDown();
    4749        }
  • tests/phpunit/tests/l10n/localeSwitcher.php

    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 { 
    2424
    2525                unset( $GLOBALS['l10n'] );
    2626                unset( $GLOBALS['l10n_unloaded'] );
     27                _get_path_to_translation( null, true );
    2728        }
    2829
    2930        public function tearDown() {
    3031                unset( $GLOBALS['l10n'] );
    3132                unset( $GLOBALS['l10n_unloaded'] );
     33                _get_path_to_translation( null, true );
    3234
    3335                parent::tearDown();
    3436        }