I18N: Avoid PHP notices for relative URLs in load_script_textdomain().

Props hellofromTonya, SeBsZ, archon810, nourma, justinahinon, SergeyBiryukov.
Fixes #49145.

git-svn-id: https://develop.svn.wordpress.org/trunk@49639 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-11-17 20:50:21 +00:00
parent 8372bbdfa5
commit 1a338a859b
2 changed files with 118 additions and 59 deletions

View File

@ -1040,7 +1040,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = null ) {
// If the host is the same or it's a relative URL.
if (
( ! isset( $content_url['path'] ) || strpos( $src_url['path'], $content_url['path'] ) === 0 ) &&
( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] )
( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
) {
// Make the src relative the specific plugin or theme.
if ( isset( $content_url['path'] ) ) {
@ -1057,7 +1057,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = null ) {
$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'] )
( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
) {
// Make the src relative the specific plugin.
if ( isset( $plugins_url['path'] ) ) {
@ -1072,7 +1072,7 @@ function load_script_textdomain( $handle, $domain = 'default', $path = null ) {
$relative = array_slice( $relative, 1 ); // Remove <plugin name>.
$relative = implode( '/', $relative );
} elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
} elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
if ( ! isset( $site_url['path'] ) ) {
$relative = trim( $src_url['path'], '/' );
} elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {

View File

@ -5,8 +5,121 @@
* @group i18n
*/
class Tests_L10n_loadScriptTextdomain extends WP_UnitTestCase {
public function site_url_subdirectory( $site_url ) {
return $site_url . '/wp';
/**
* @ticket 45528
* @ticket 46336
* @ticket 46387
* @ticket 49145
*
* @dataProvider data_test_resolve_relative_path
*/
public function test_resolve_relative_path( $translation_path, $handle, $src, $textdomain, $filter = array() ) {
if ( ! empty( $filter ) ) {
add_filter( $filter[0], $filter[1], 10, isset( $filter[2] ) ? $filter[2] : 1 );
}
wp_enqueue_script( $handle, $src, array(), null );
$expected = file_get_contents( DIR_TESTDATA . $translation_path );
$this->assertSame( $expected, load_script_textdomain( $handle, $textdomain, DIR_TESTDATA . '/languages' ) );
}
public function data_test_resolve_relative_path() {
return array(
// @ticket 45528
array(
'/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
'test-example-root',
'/wp-includes/js/script.js',
'default',
),
// Assets on a CDN.
array(
'/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
'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 ),
),
// Test for WordPress installs in a subdirectory.
array(
'/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
'test-example-subdir',
'/wp/wp-includes/js/script.js',
'default',
array(
'site_url',
function ( $site_url ) {
return $site_url . '/wp';
},
),
),
// @ticket 46336
array(
'/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
'plugin-example-1',
'https://plugins.example.com/my-plugin/js/script.js',
'internationalized-plugin',
array(
'plugins_url',
function () {
return 'https://plugins.example.com';
},
),
),
// @ticket 46387
array(
'/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
'plugin-example-2',
'https://content.example.com/plugins/my-plugin/js/script.js',
'internationalized-plugin',
array(
'content_url',
function () {
return 'https://content.example.com';
},
),
),
// @ticket 49145
array(
'/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
'test-when-no-content_url-host',
'https://content.example.com/plugins/my-plugin/js/script.js',
'internationalized-plugin',
array(
'content_url',
function () {
return '/';
},
),
),
// @ticket 49145
array(
'/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
'test-when-no-plugins_url-host',
'https://plugins.example.com/my-plugin/js/script.js',
'internationalized-plugin',
array(
'plugins_url',
function () {
return '/';
},
),
),
// @ticket 49145
array(
'/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
'test-when-no-site_url-host',
'/wp/wp-includes/js/script.js',
'default',
array(
'site_url',
function () {
return '/wp';
},
),
),
);
}
public function relative_path_from_cdn( $relative, $src ) {
@ -16,58 +129,4 @@ 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
*/
public function test_resolve_relative_path() {
$json_translations = file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' );
wp_enqueue_script( 'test-example-root', '/wp-includes/js/script.js', array(), null );
$this->assertSame( $json_translations, load_script_textdomain( 'test-example-root', 'default', DIR_TESTDATA . '/languages' ) );
// Assets on a CDN.
add_filter( 'load_script_textdomain_relative_path', array( $this, 'relative_path_from_cdn' ), 10, 2 );
wp_enqueue_script( 'test-example-cdn', 'https://my-cdn.com/wordpress/wp-includes/js/script.js', array(), null );
$this->assertSame( $json_translations, load_script_textdomain( 'test-example-cdn', 'default', DIR_TESTDATA . '/languages' ) );
remove_filter( 'load_script_textdomain_relative_path', array( $this, 'relative_path_from_cdn' ) );
// Test for WordPress installs in a subdirectory.
add_filter( 'site_url', array( $this, 'site_url_subdirectory' ) );
wp_enqueue_script( 'test-example-subdir', '/wp/wp-includes/js/script.js', array(), null );
$this->assertSame( $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->assertSame( $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->assertSame( $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' ) );
}
}