I18N: Add support for custom WP_PLUGIN_URL in load_script_textdomain().

Plugins may not be on the same host/path as the rest of the content. To support loading translations for this setup check if the script source matches `plugins_url()`.
Also fixes an undefined index notice when a custom content URL has no path.

Props odminstudios, ocean90.
Fixes #46336, #46387.

git-svn-id: https://develop.svn.wordpress.org/trunk@45685 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling 2019-07-27 12:43:56 +00:00
parent 160fc055da
commit 84b977945c
2 changed files with 58 additions and 3 deletions

View File

@ -948,20 +948,43 @@ function load_script_textdomain( $handle, $domain = 'default', $path = null ) {
$src_url = wp_parse_url( $src );
$content_url = wp_parse_url( content_url() );
$plugins_url = wp_parse_url( plugins_url() );
$site_url = wp_parse_url( site_url() );
// If the host is the same or it's a relative URL.
if (
strpos( $src_url['path'], $content_url['path'] ) === 0 &&
( ! isset( $content_url['path'] ) || strpos( $src_url['path'], $content_url['path'] ) === 0 ) &&
( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] )
) {
// Make the src relative the specific plugin or theme.
$relative = trim( substr( $src_url['path'], strlen( $content_url['path'] ) ), '/' );
if ( isset( $content_url['path'] ) ) {
$relative = substr( $src_url['path'], strlen( $content_url['path'] ) );
} else {
$relative = $src_url['path'];
}
$relative = trim( $relative, '/' );
$relative = explode( '/', $relative );
$languages_path = WP_LANG_DIR . '/' . $relative[0];
$relative = array_slice( $relative, 2 );
$relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
$relative = implode( '/', $relative );
} elseif (
( ! isset( $plugins_url['path'] ) || strpos( $src_url['path'], $plugins_url['path'] ) === 0 ) &&
( ! isset( $src_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
) {
// Make the src relative the specific plugin.
if ( isset( $plugins_url['path'] ) ) {
$relative = substr( $src_url['path'], strlen( $plugins_url['path'] ) );
} else {
$relative = $src_url['path'];
}
$relative = trim( $relative, '/' );
$relative = explode( '/', $relative );
$languages_path = WP_LANG_DIR . '/plugins';
$relative = array_slice( $relative, 1 ); // Remove <plugin name>.
$relative = implode( '/', $relative );
} elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
if ( ! isset( $site_url['path'] ) ) {

View File

@ -17,6 +17,14 @@ class Tests_L10n_loadScriptTextdomain extends WP_UnitTestCase {
return $relative;
}
public function plugins_url_custom_domain() {
return 'https://plugins.example.com';
}
public function content_url_custom_domain_with_no_path() {
return 'https://content.example.com';
}
/**
* @ticket 45528
*/
@ -38,4 +46,28 @@ class Tests_L10n_loadScriptTextdomain extends WP_UnitTestCase {
$this->assertEquals( $json_translations, load_script_textdomain( 'test-example-subdir', 'default', DIR_TESTDATA . '/languages' ) );
remove_filter( 'site_url', array( $this, 'site_url_subdirectory' ) );
}
/**
* @ticket 46336
*/
public function test_resolve_relative_path_custom_plugins_url() {
$json_translations = file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' );
add_filter( 'plugins_url', array( $this, 'plugins_url_custom_domain' ) );
wp_enqueue_script( 'plugin-example-1', 'https://plugins.example.com/my-plugin/js/script.js', array(), null );
$this->assertEquals( $json_translations, load_script_textdomain( 'plugin-example-1', 'internationalized-plugin', DIR_TESTDATA . '/languages' ) );
remove_filter( 'plugins_url', array( $this, 'plugins_url_custom_domain' ) );
}
/**
* @ticket 46387
*/
public function test_resolve_relative_path_custom_content_url() {
$json_translations = file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' );
add_filter( 'content_url', array( $this, 'content_url_custom_domain_with_no_path' ) );
wp_enqueue_script( 'plugin-example-2', 'https://content.example.com/plugins/my-plugin/js/script.js', array(), null );
$this->assertEquals( $json_translations, load_script_textdomain( 'plugin-example-2', 'internationalized-plugin', DIR_TESTDATA . '/languages' ) );
remove_filter( 'content_url', array( $this, 'content_url_custom_domain_with_no_path' ) );
}
}