Make WordPress Core

09/28/2022 05:31:53 PM (21 months ago)

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.


  • 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


Follow-up to [44169], [44607], [51968].

Props jrf, ocean90, Chouby, swissspidy, lovor, iviweb, meysamnorouzi, DarkoG, oneearth27, SergeyBiryukov.
Fixes #55967. See #55656.

1 edited


  • trunk/tests/phpunit/tests/l10n/loadScriptTextdomain.php

    r53866 r54349  
    132132        return $relative;
    133133    }
     135    /**
     136     * Tests that PHP 8.1 "passing null to non-nullable" deprecation notice
     137     * is not thrown when passing the default `$path` to untrailingslashit() in the function.
     138     *
     139     * The notice that we should not see:
     140     * `Deprecated: rtrim(): Passing null to parameter #1 ($string) of type string is deprecated`.
     141     *
     142     * @ticket 55967
     143     */
     144    public function test_does_not_throw_deprecation_notice_for_rtrim_with_default_parameters() {
     145        $handle = 'test-example-root';
     146        $src    = '/wp-includes/js/script.js';
     148        wp_enqueue_script( $handle, $src );
     150        $expected = file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' );
     151        $this->assertSame( $expected, load_script_textdomain( $handle ) );
     152    }
Note: See TracChangeset for help on using the changeset viewer.