From 8fafc3acae2a1808115084ec5e8b82a05375e67e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 14 Mar 2024 09:04:00 +0000 Subject: [PATCH] I18N: Improve translation file cache group & expiration. Adds an explicit 1 hour expiration for the translation file cache introduced in [57287] / #58919. This prevents stale caches when a site does not use the regular way of installing language packs, for example when an atomic filesystem is involved. Also configures the `translation_files` group as a global cache group on multisite. Props dd32. Fixes #60764. git-svn-id: https://develop.svn.wordpress.org/trunk@57831 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-textdomain-registry.php | 12 ++++----- src/wp-includes/load.php | 1 + .../tests/l10n/wpTextdomainRegistry.php | 26 +++++++++---------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/class-wp-textdomain-registry.php b/src/wp-includes/class-wp-textdomain-registry.php index 113ef3bd65..fe6d04b0a1 100644 --- a/src/wp-includes/class-wp-textdomain-registry.php +++ b/src/wp-includes/class-wp-textdomain-registry.php @@ -183,8 +183,8 @@ class WP_Textdomain_Registry { return $files; } - $cache_key = 'cached_mo_files_' . md5( $path ); - $files = wp_cache_get( $cache_key, 'translations' ); + $cache_key = md5( $path ); + $files = wp_cache_get( $cache_key, 'translation_files' ); if ( false === $files ) { $files = glob( $path . '*.mo' ); @@ -197,7 +197,7 @@ class WP_Textdomain_Registry { $files = array_merge( $files, $php_files ); } - wp_cache_set( $cache_key, $files, 'translations' ); + wp_cache_set( $cache_key, $files, 'translation_files', HOUR_IN_SECONDS ); } return $files; @@ -246,13 +246,13 @@ class WP_Textdomain_Registry { foreach ( $translation_types as $type ) { switch ( $type ) { case 'plugin': - wp_cache_delete( 'cached_mo_files_' . md5( WP_LANG_DIR . '/plugins/' ), 'translations' ); + wp_cache_delete( md5( WP_LANG_DIR . '/plugins/' ), 'translation_files' ); break; case 'theme': - wp_cache_delete( 'cached_mo_files_' . md5( WP_LANG_DIR . '/themes/' ), 'translations' ); + wp_cache_delete( md5( WP_LANG_DIR . '/themes/' ), 'translation_files' ); break; default: - wp_cache_delete( 'cached_mo_files_' . md5( WP_LANG_DIR . '/' ), 'translations' ); + wp_cache_delete( md5( WP_LANG_DIR . '/' ), 'translation_files' ); break; } } diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 40947a0b20..b7bde142ec 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -876,6 +876,7 @@ function wp_start_object_cache() { 'site-queries', 'site-transient', 'theme_files', + 'translation_files', 'rss', 'users', 'user-queries', diff --git a/tests/phpunit/tests/l10n/wpTextdomainRegistry.php b/tests/phpunit/tests/l10n/wpTextdomainRegistry.php index dd9688e947..0344fe6caf 100644 --- a/tests/phpunit/tests/l10n/wpTextdomainRegistry.php +++ b/tests/phpunit/tests/l10n/wpTextdomainRegistry.php @@ -19,10 +19,10 @@ class Tests_L10n_wpTextdomainRegistry extends WP_UnitTestCase { } public function tear_down() { - wp_cache_delete( 'cached_mo_files_' . md5( WP_LANG_DIR . '/foobar/' ), 'translations' ); - wp_cache_delete( 'cached_mo_files_' . md5( WP_LANG_DIR . '/plugins/' ), 'translations' ); - wp_cache_delete( 'cached_mo_files_' . md5( WP_LANG_DIR . '/themes/' ), 'translations' ); - wp_cache_delete( 'cached_mo_files_' . md5( WP_LANG_DIR . '/' ), 'translations' ); + wp_cache_delete( md5( WP_LANG_DIR . '/foobar/' ), 'translation_files' ); + wp_cache_delete( md5( WP_LANG_DIR . '/plugins/' ), 'translation_files' ); + wp_cache_delete( md5( WP_LANG_DIR . '/themes/' ), 'translation_files' ); + wp_cache_delete( md5( WP_LANG_DIR . '/' ), 'translation_files' ); parent::tear_down(); } @@ -49,7 +49,7 @@ class Tests_L10n_wpTextdomainRegistry extends WP_UnitTestCase { 'Custom path for textdomain not returned' ); $this->assertNotFalse( - wp_cache_get( 'cached_mo_files_' . md5( WP_LANG_DIR . '/bar/' ), 'translations' ), + wp_cache_get( md5( WP_LANG_DIR . '/bar/' ), 'translation_files' ), 'List of files in custom path not cached' ); } @@ -89,10 +89,10 @@ class Tests_L10n_wpTextdomainRegistry extends WP_UnitTestCase { $this->instance->get_language_files_from_path( WP_LANG_DIR . '/themes/' ); $this->instance->get_language_files_from_path( WP_LANG_DIR . '/' ); - $this->assertNotFalse( wp_cache_get( 'cached_mo_files_' . md5( WP_LANG_DIR . '/plugins/' ), 'translations' ) ); - $this->assertNotFalse( wp_cache_get( 'cached_mo_files_' . md5( WP_LANG_DIR . '/themes/' ), 'translations' ) ); - $this->assertNotFalse( wp_cache_get( 'cached_mo_files_' . md5( WP_LANG_DIR . '/foobar/' ), 'translations' ) ); - $this->assertNotFalse( wp_cache_get( 'cached_mo_files_' . md5( WP_LANG_DIR . '/' ), 'translations' ) ); + $this->assertNotFalse( wp_cache_get( md5( WP_LANG_DIR . '/plugins/' ), 'translation_files' ) ); + $this->assertNotFalse( wp_cache_get( md5( WP_LANG_DIR . '/themes/' ), 'translation_files' ) ); + $this->assertNotFalse( wp_cache_get( md5( WP_LANG_DIR . '/foobar/' ), 'translation_files' ) ); + $this->assertNotFalse( wp_cache_get( md5( WP_LANG_DIR . '/' ), 'translation_files' ) ); } /** @@ -103,7 +103,7 @@ class Tests_L10n_wpTextdomainRegistry extends WP_UnitTestCase { $result = $this->instance->get_language_files_from_path( WP_LANG_DIR . '/plugins/' ); remove_filter( 'pre_get_language_files_from_path', '__return_empty_array' ); - $cache = wp_cache_get( 'cached_mo_files_' . md5( WP_LANG_DIR . '/plugins/' ), 'translations' ); + $cache = wp_cache_get( md5( WP_LANG_DIR . '/plugins/' ), 'translation_files' ); $this->assertEmpty( $result ); $this->assertFalse( $cache ); @@ -144,9 +144,9 @@ class Tests_L10n_wpTextdomainRegistry extends WP_UnitTestCase { ) ); - $this->assertFalse( wp_cache_get( 'cached_mo_files_' . md5( WP_LANG_DIR . '/plugins/' ), 'translations' ) ); - $this->assertFalse( wp_cache_get( 'cached_mo_files_' . md5( WP_LANG_DIR . '/themes/' ), 'translations' ) ); - $this->assertFalse( wp_cache_get( 'cached_mo_files_' . md5( WP_LANG_DIR . '/' ), 'translations' ) ); + $this->assertFalse( wp_cache_get( md5( WP_LANG_DIR . '/plugins/' ), 'translation_files' ) ); + $this->assertFalse( wp_cache_get( md5( WP_LANG_DIR . '/themes/' ), 'translation_files' ) ); + $this->assertFalse( wp_cache_get( md5( WP_LANG_DIR . '/' ), 'translation_files' ) ); } public function data_domains_locales() {