From 0d9c56edfb94eba08eeb7a2a43d5614542a097ea Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 21 Oct 2024 14:11:56 +0000 Subject: [PATCH] I18N: Fix script languages path on Multisite. This is a follow-up to [59126] (itself a follow-up to [57922]), which caused a regression when determining the right path when loading script translations. Props swissspidy, themes-1, staurand. Fixes #62016. git-svn-id: https://develop.svn.wordpress.org/trunk@59264 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/l10n.php | 10 ++++++---- tests/phpunit/tests/l10n/loadScriptTextdomain.php | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index e4acf40b38ee0..bee3581618181 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -1151,6 +1151,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) { $content_url = wp_parse_url( content_url() ); $plugins_url = wp_parse_url( plugins_url() ); $site_url = wp_parse_url( site_url() ); + $theme_root = get_theme_root(); // If the host is the same or it's a relative URL. if ( @@ -1167,12 +1168,13 @@ function load_script_textdomain( $handle, $domain = 'default', $path = '' ) { $relative = explode( '/', $relative ); /* - * Ensure correct languages path when using a custom `WP_PLUGIN_DIR` / `WP_PLUGIN_URL` configuration. + * Ensure correct languages path when using a custom `WP_PLUGIN_DIR` / `WP_PLUGIN_URL` configuration, + * a custom theme root, and/or using Multisite with subdirectories. * See https://core.trac.wordpress.org/ticket/60891 and https://core.trac.wordpress.org/ticket/62016. */ - $plugins_dir = array_slice( explode( '/', $plugins_url['path'] ), 2 ); - $plugins_dir = trim( $plugins_dir[0], '/' ); - $dirname = $plugins_dir === $relative[0] ? 'plugins' : 'themes'; + + $theme_dir = array_slice( explode( '/', $theme_root ), -1 ); + $dirname = $theme_dir[0] === $relative[0] ? 'themes' : 'plugins'; $languages_path = WP_LANG_DIR . '/' . $dirname; diff --git a/tests/phpunit/tests/l10n/loadScriptTextdomain.php b/tests/phpunit/tests/l10n/loadScriptTextdomain.php index 1f5fde9eec03b..bc028fbfa5e4d 100644 --- a/tests/phpunit/tests/l10n/loadScriptTextdomain.php +++ b/tests/phpunit/tests/l10n/loadScriptTextdomain.php @@ -28,7 +28,7 @@ public function test_resolve_relative_path( $translation_path, $handle, $src, $t $this->assertSame( $expected, load_script_textdomain( $handle, $textdomain, DIR_TESTDATA . '/languages' ) ); } - public function data_resolve_relative_path() { + public static function data_resolve_relative_path() { return array( // @ticket 45528 array( @@ -43,7 +43,7 @@ public function data_resolve_relative_path() { 'test-example-cdn', 'https://my-cdn.com/wordpress/wp-includes/js/script.js', 'default', - array( 'load_script_textdomain_relative_path', array( $this, 'relative_path_from_cdn' ), 2 ), + array( 'load_script_textdomain_relative_path', array( __CLASS__, 'relative_path_from_cdn' ), 2 ), ), // Test for WordPress installs in a subdirectory. array( @@ -146,7 +146,7 @@ static function () { ); } - public function relative_path_from_cdn( $relative, $src ) { + public static function relative_path_from_cdn( $relative, $src ) { if ( 0 === strpos( $src, 'https://my-cdn.com/wordpress/' ) ) { return substr( $src, strlen( 'https://my-cdn.com/wordpress/' ) ); }