From c43783bad967169fcb0cd41b0b8a82f6c28baf0a Mon Sep 17 00:00:00 2001 From: Sukhendu Sekhar Guria Date: Thu, 18 Jun 2026 18:14:45 +0530 Subject: [PATCH] fix(script-loader): avoid false inline entries --- src/wp-includes/class-wp-scripts.php | 3 +- tests/phpunit/tests/dependencies/scripts.php | 32 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-scripts.php b/src/wp-includes/class-wp-scripts.php index 6f633d465bb2c..ba14b448ac880 100644 --- a/src/wp-includes/class-wp-scripts.php +++ b/src/wp-includes/class-wp-scripts.php @@ -525,7 +525,8 @@ public function add_inline_script( $handle, $data, $position = 'after' ) { $position = 'before'; } - $script = (array) $this->get_data( $handle, $position ); + $script = $this->get_data( $handle, $position ); + $script = false === $script ? array() : (array) $script; $script[] = $data; return $this->add_data( $handle, $position, $script ); diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 41c9673915b93..85da7fe7e83f8 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -230,6 +230,38 @@ public function data_provider_delayed_strategies() { ); } + /** + * Tests that inline scripts do not include a false entry when no data exists yet. + * + * @ticket 52320 + * @dataProvider data_inline_script_positions + * + * @param string $position Inline script position. + */ + public function test_add_inline_script_does_not_store_false_for_empty_existing_data( $position ) { + $handle = 'test-inline-script-' . $position; + + wp_register_script( $handle, '/test.js', array(), null ); + wp_add_inline_script( $handle, 'console.log( "test" );', $position ); + + $this->assertSame( + array( 'console.log( "test" );' ), + wp_scripts()->get_data( $handle, $position ) + ); + } + + /** + * Data provider for inline script positions. + * + * @return array[] Inline script positions. + */ + public function data_inline_script_positions() { + return array( + 'before' => array( 'before' ), + 'after' => array( 'after' ), + ); + } + /** * Tests that inline scripts in the `after` position, attached to delayed main scripts, remain unaffected. *