Skip to content

Commit

Permalink
I18N: Fix script languages path on Multisite.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
swissspidy committed Oct 21, 2024
1 parent 61d6019 commit 0d9c56e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/wp-includes/l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/l10n/loadScriptTextdomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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/' ) );
}
Expand Down

0 comments on commit 0d9c56e

Please sign in to comment.