mirror of
git://develop.git.wordpress.org/
synced 2025-02-07 08:04:27 +01:00
Code Modernization: Use correct default value for JavaScript translations path.
The `$path` parameter of `load_script_textdomain()` had a default value of `null`, but would be passed onto the `untrailingslashit()` function without any input validation, even though the latter explicitly only expects/supports a string input. This commit changes the default value for `$path` to an empty string, and adds an `is_string()` check before passing the value to `untrailingslashit()` to fix the issue at the point where the invalid input is incorrectly (not) validated. Note: Changing the `untrailingslashit()` function is outside the scope of this commit. Includes: * Adding a dedicated unit test for this issue. * Correcting the default value for `$path` from `null` to an empty string in a few related methods and functions: * `WP_Dependency::set_translations()` * `WP_Scripts::set_translations()` * `wp_set_script_translations()` * `load_script_textdomain()` This fix also allows to remove a couple of calls to `expectDeprecation()` in unrelated tests. Fixes an error when running the test suite: {{{ 4) Tests_Dependencies_Scripts::test_wp_external_wp_i18n_print_order rtrim(): Passing null to parameter #1 ($string) of type string is deprecated /var/www/src/wp-includes/formatting.php:2782 /var/www/src/wp-includes/l10n.php:1068 /var/www/src/wp-includes/class.wp-scripts.php:605 /var/www/src/wp-includes/class.wp-scripts.php:320 /var/www/src/wp-includes/class.wp-dependencies.php:136 /var/www/src/wp-includes/functions.wp-scripts.php:109 /var/www/tests/phpunit/tests/dependencies/scripts.php:1505 /var/www/tests/phpunit/includes/utils.php:436 /var/www/tests/phpunit/tests/dependencies/scripts.php:1507 /var/www/vendor/bin/phpunit:123 }}} Follow-up to [44169], [44607], [51968]. Props jrf, ocean90, Chouby, swissspidy, lovor, iviweb, meysamnorouzi, DarkoG, oneearth27, SergeyBiryukov. Fixes #55967. See #55656. git-svn-id: https://develop.svn.wordpress.org/trunk@54349 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
7fed5bec3c
commit
d6bae0ceda
@ -1058,14 +1058,17 @@ function load_child_theme_textdomain( $domain, $path = false ) {
|
||||
* @return string|false The translated strings in JSON encoding on success,
|
||||
* false if the script textdomain could not be loaded.
|
||||
*/
|
||||
function load_script_textdomain( $handle, $domain = 'default', $path = null ) {
|
||||
function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
|
||||
$wp_scripts = wp_scripts();
|
||||
|
||||
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = untrailingslashit( $path );
|
||||
if ( is_string( $path ) ) {
|
||||
$path = untrailingslashit( $path );
|
||||
}
|
||||
|
||||
$locale = determine_locale();
|
||||
|
||||
// If a path was given and the handle file exists simply return it.
|
||||
|
@ -721,16 +721,6 @@ JS;
|
||||
$wp_scripts->base_url = '';
|
||||
$wp_scripts->do_concat = true;
|
||||
|
||||
if ( PHP_VERSION_ID >= 80100 ) {
|
||||
/*
|
||||
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
|
||||
* via hooked in filter functions until a more structural solution to the
|
||||
* "missing input validation" conundrum has been architected and implemented.
|
||||
*/
|
||||
$this->expectDeprecation();
|
||||
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
|
||||
}
|
||||
|
||||
$ver = get_bloginfo( 'version' );
|
||||
$suffix = wp_scripts_get_suffix();
|
||||
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&load%5Bchunk_0%5D=jquery-core,jquery-migrate,regenerator-runtime,wp-polyfill,wp-dom-ready,wp-hooks&ver={$ver}'></script>\n";
|
||||
@ -783,16 +773,6 @@ JS;
|
||||
$wp_scripts->base_url = '';
|
||||
$wp_scripts->do_concat = true;
|
||||
|
||||
if ( PHP_VERSION_ID >= 80100 ) {
|
||||
/*
|
||||
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
|
||||
* via hooked in filter functions until a more structural solution to the
|
||||
* "missing input validation" conundrum has been architected and implemented.
|
||||
*/
|
||||
$this->expectDeprecation();
|
||||
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
|
||||
}
|
||||
|
||||
$expected_tail = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n";
|
||||
$expected_tail .= "<script type='text/javascript' id='customize-dependency-js-after'>\n";
|
||||
$expected_tail .= "tryCustomizeDependency()\n";
|
||||
|
@ -131,4 +131,23 @@ class Tests_L10n_LoadScriptTextdomain extends WP_UnitTestCase {
|
||||
|
||||
return $relative;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that PHP 8.1 "passing null to non-nullable" deprecation notice
|
||||
* is not thrown when passing the default `$path` to untrailingslashit() in the function.
|
||||
*
|
||||
* The notice that we should not see:
|
||||
* `Deprecated: rtrim(): Passing null to parameter #1 ($string) of type string is deprecated`.
|
||||
*
|
||||
* @ticket 55967
|
||||
*/
|
||||
public function test_does_not_throw_deprecation_notice_for_rtrim_with_default_parameters() {
|
||||
$handle = 'test-example-root';
|
||||
$src = '/wp-includes/js/script.js';
|
||||
|
||||
wp_enqueue_script( $handle, $src );
|
||||
|
||||
$expected = file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' );
|
||||
$this->assertSame( $expected, load_script_textdomain( $handle ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user