Skip to content

Commit 79e020e

Browse files
committed
PHP 8.1 | load_script_textdomain(): fix passing null to non-nullable
The `$path` parameter has a default value of `null`, but would be passed onto the `untrailingslashit()` function without any input validation, even though the `untrailingslashit()` function explicitly only expects/supports a string input. Note: I'm explicitly not changing the `untrailingslashit()` function, but fixing this at the point where the invalid input is incorrectly (not) validated. Includes adding a dedicated unit test for this issue. This fix also allows to remove a couple of calls to `expectDeprecation()` in unrelated tests. Fixes: ``` 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 ```
1 parent 126faf0 commit 79e020e

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

src/wp-includes/l10n.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,9 @@ function load_script_textdomain( $handle, $domain = 'default', $path = null ) {
10651065
return false;
10661066
}
10671067

1068-
$path = untrailingslashit( $path );
1068+
if ( is_string( $path ) ) {
1069+
$path = untrailingslashit( $path );
1070+
}
10691071
$locale = determine_locale();
10701072

10711073
// If a path was given and the handle file exists simply return it.

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -721,16 +721,6 @@ public function test_wp_add_inline_script_before_after_concat_with_core_dependen
721721
$wp_scripts->base_url = '';
722722
$wp_scripts->do_concat = true;
723723

724-
if ( PHP_VERSION_ID >= 80100 ) {
725-
/*
726-
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
727-
* via hooked in filter functions until a more structural solution to the
728-
* "missing input validation" conundrum has been architected and implemented.
729-
*/
730-
$this->expectDeprecation();
731-
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
732-
}
733-
734724
$ver = get_bloginfo( 'version' );
735725
$suffix = wp_scripts_get_suffix();
736726
$expected = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core,jquery-migrate,regenerator-runtime,wp-polyfill,wp-dom-ready,wp-hooks&amp;ver={$ver}'></script>\n";
@@ -783,16 +773,6 @@ public function test_wp_add_inline_script_customize_dependency() {
783773
$wp_scripts->base_url = '';
784774
$wp_scripts->do_concat = true;
785775

786-
if ( PHP_VERSION_ID >= 80100 ) {
787-
/*
788-
* For the time being, ignoring PHP 8.1 "null to non-nullable" deprecations coming in
789-
* via hooked in filter functions until a more structural solution to the
790-
* "missing input validation" conundrum has been architected and implemented.
791-
*/
792-
$this->expectDeprecation();
793-
$this->expectDeprecationMessageMatches( '`Passing null to parameter \#[0-9]+ \(\$[^\)]+\) of type [^ ]+ is deprecated`' );
794-
}
795-
796776
$expected_tail = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n";
797777
$expected_tail .= "<script type='text/javascript' id='customize-dependency-js-after'>\n";
798778
$expected_tail .= "tryCustomizeDependency()\n";

tests/phpunit/tests/l10n/loadScriptTextdomain.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,17 @@ public function relative_path_from_cdn( $relative, $src ) {
131131

132132
return $relative;
133133
}
134+
135+
/**
136+
* @ticket 55656
137+
*/
138+
public function test_no_php81_notices_when_optional_params_not_passed() {
139+
$handle = 'test-example-root';
140+
$src = '/wp-includes/js/script.js';
141+
142+
wp_enqueue_script( $handle, $src );
143+
144+
$expected = file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' );
145+
$this->assertSame( $expected, load_script_textdomain( $handle ) );
146+
}
134147
}

0 commit comments

Comments
 (0)