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

@@ -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' ) );
}
}