Make WordPress Core

Changeset 63579


Ignore:
Timestamp:
09/10/2026 10:48:20 AM (23 hours ago)
Author:
jonsurrell
Message:

Script Loader: Avoid a false entry in inline script data.

WP_Scripts::get_data() returns false when a handle has no before or after data, and casting that to an array produced array( false ). The first inline script added for a handle was therefore stored after an unused false entry. Missing inline script data is now treated as an empty array.

Developed in: https://github.com/WordPress/wordpress-develop/pull/12217

Follow-up to r36633.

Props vanyukov, sukhendu2002, jonsurrell, apermo.
Fixes #52320.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-scripts.php

    r63440 r63579  
    528528                }
    529529
    530                 $script   = (array) $this->get_data( $handle, $position );
     530                $script   = $this->get_data( $handle, $position );
     531                $script   = false === $script ? array() : (array) $script;
    531532                $script[] = $data;
    532533
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r62957 r63579  
    228228                        'defer' => array( 'defer' ),
    229229                        'async' => array( 'async' ),
     230                );
     231        }
     232
     233        /**
     234         * Tests that inline scripts do not include a false entry when no data exists yet.
     235         *
     236         * @ticket 52320
     237         * @dataProvider data_inline_script_positions
     238         *
     239         * @param string $position Inline script position.
     240         */
     241        public function test_add_inline_script_does_not_store_false_for_empty_existing_data( $position ): void {
     242                $handle = 'test-inline-script-' . $position;
     243
     244                wp_register_script( $handle, '/test.js', array(), null );
     245                wp_add_inline_script( $handle, 'console.log( "test" );', $position );
     246
     247                $this->assertSame(
     248                        array( 'console.log( "test" );' ),
     249                        wp_scripts()->get_data( $handle, $position )
     250                );
     251        }
     252
     253        /**
     254         * Data provider for inline script positions.
     255         *
     256         * @return array<string, array{0: string}> Inline script positions.
     257         */
     258        public function data_inline_script_positions(): array {
     259                return array(
     260                        'before' => array( 'before' ),
     261                        'after'  => array( 'after' ),
    230262                );
    231263        }
Note: See TracChangeset for help on using the changeset viewer.