Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/wp-includes/class-wp-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading