<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[54349] trunk: Code Modernization: Use correct default value for JavaScript translations path.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/54349">54349</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/54349","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-09-28 17:31:53 +0000 (Wed, 28 Sep 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/1">#1</a> ($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 <a href="https://core.trac.wordpress.org/changeset/44169">[44169]</a>, <a href="https://core.trac.wordpress.org/changeset/44607">[44607]</a>, <a href="https://core.trac.wordpress.org/changeset/51968">[51968]</a>.

Props jrf, ocean90, Chouby, swissspidy, lovor, iviweb, meysamnorouzi, DarkoG, oneearth27, SergeyBiryukov.
Fixes <a href="https://core.trac.wordpress.org/ticket/55967">#55967</a>. See <a href="https://core.trac.wordpress.org/ticket/55656">#55656</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesl10nphp">trunk/src/wp-includes/l10n.php</a></li>
<li><a href="#trunktestsphpunittestsdependenciesscriptsphp">trunk/tests/phpunit/tests/dependencies/scripts.php</a></li>
<li><a href="#trunktestsphpunittestsl10nloadScriptTextdomainphp">trunk/tests/phpunit/tests/l10n/loadScriptTextdomain.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesl10nphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/l10n.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/l10n.php    2022-09-28 14:57:55 UTC (rev 54348)
+++ trunk/src/wp-includes/l10n.php      2022-09-28 17:31:53 UTC (rev 54349)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1058,7 +1058,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return string|false The translated strings in JSON encoding on success,
</span><span class="cx" style="display: block; padding: 0 10px">  *                      false if the script textdomain could not be loaded.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function load_script_textdomain( $handle, $domain = 'default', $path = null ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         $wp_scripts = wp_scripts();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1065,7 +1065,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $path   = untrailingslashit( $path );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_string( $path ) ) {
+               $path = untrailingslashit( $path );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $locale = determine_locale();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // If a path was given and the handle file exists simply return it.
</span></span></pre></div>
<a id="trunktestsphpunittestsdependenciesscriptsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/dependencies/scripts.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/dependencies/scripts.php        2022-09-28 14:57:55 UTC (rev 54348)
+++ trunk/tests/phpunit/tests/dependencies/scripts.php  2022-09-28 17:31:53 UTC (rev 54349)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -721,16 +721,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_scripts->base_url  = '';
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_scripts->do_concat = true;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                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`' );
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $ver       = get_bloginfo( 'version' );
</span><span class="cx" style="display: block; padding: 0 10px">                $suffix    = wp_scripts_get_suffix();
</span><span class="cx" style="display: block; padding: 0 10px">                $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";
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -783,16 +773,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_scripts->base_url  = '';
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_scripts->do_concat = true;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                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`' );
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $expected_tail  = "<script type='text/javascript' src='/customize-dependency.js' id='customize-dependency-js'></script>\n";
</span><span class="cx" style="display: block; padding: 0 10px">                $expected_tail .= "<script type='text/javascript' id='customize-dependency-js-after'>\n";
</span><span class="cx" style="display: block; padding: 0 10px">                $expected_tail .= "tryCustomizeDependency()\n";
</span></span></pre></div>
<a id="trunktestsphpunittestsl10nloadScriptTextdomainphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/l10n/loadScriptTextdomain.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/l10n/loadScriptTextdomain.php   2022-09-28 14:57:55 UTC (rev 54348)
+++ trunk/tests/phpunit/tests/l10n/loadScriptTextdomain.php     2022-09-28 17:31:53 UTC (rev 54349)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -131,4 +131,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $relative;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * 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 ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>