Make WordPress Core

Changeset 57841


Ignore:
Timestamp:
03/15/2024 12:10:49 PM (2 months ago)
Author:
swissspidy
Message:

Interactivity API: Do not print state if it’s an empty array.

This prunes stores and configurations that are empty arrays, as stores are expected to be JSON objects.
By not printing empty configurations, less redundant data is serialized into the HTML.

Props jonsurrell, luisherranz, darerodz, gziolo, swissspidy.
Fixes #60761.

Location:
trunk
Files:
2 edited

Legend:

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

    r57835 r57841  
    141141     */
    142142    public function print_client_interactivity_data() {
    143         $store      = array();
    144         $has_state  = ! empty( $this->state_data );
    145         $has_config = ! empty( $this->config_data );
    146 
    147         if ( $has_state || $has_config ) {
    148             if ( $has_config ) {
    149                 $store['config'] = $this->config_data;
    150             }
    151             if ( $has_state ) {
    152                 $store['state'] = $this->state_data;
    153             }
     143        if ( empty( $this->state_data ) && empty( $this->config_data ) ) {
     144            return;
     145        }
     146
     147        $interactivity_data = array();
     148
     149        $config = array();
     150        foreach ( $this->config_data as $key => $value ) {
     151            if ( ! empty( $value ) ) {
     152                $config[ $key ] = $value;
     153            }
     154        }
     155        if ( ! empty( $config ) ) {
     156            $interactivity_data['config'] = $config;
     157        }
     158
     159        $state = array();
     160        foreach ( $this->state_data as $key => $value ) {
     161            if ( ! empty( $value ) ) {
     162                $state[ $key ] = $value;
     163            }
     164        }
     165        if ( ! empty( $state ) ) {
     166            $interactivity_data['state'] = $state;
     167        }
     168
     169        if ( ! empty( $interactivity_data ) ) {
    154170            wp_print_inline_script_tag(
    155171                wp_json_encode(
    156                     $store,
     172                    $interactivity_data,
    157173                    JSON_HEX_TAG | JSON_HEX_AMP
    158174                ),
  • trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php

    r57649 r57841  
    237237    }
    238238
    239 
     239    /**
     240     * Tests that empty state objects are pruned from printed data.
     241     *
     242     * @ticket 60761
     243     *
     244     * @covers ::print_client_interactivity_data
     245     */
     246    public function test_state_not_printed_when_empty_array() {
     247        $this->interactivity->state( 'pluginWithEmptyState_prune', array() );
     248        $this->interactivity->state( 'pluginWithState_include', array( 'value' => 'excellent' ) );
     249        $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
     250        $expected       = <<<'SCRIPT_TAG'
     251<script type="application/json" id="wp-interactivity-data">
     252{"state":{"pluginWithState_include":{"value":"excellent"}}}
     253</script>
     254
     255SCRIPT_TAG;
     256
     257        $this->assertSame( $expected, $printed_script );
     258    }
     259
     260    /**
     261     * Tests that data consisting of only empty state objects is not printed.
     262     *
     263     * @ticket 60761
     264     *
     265     * @covers ::print_client_interactivity_data
     266     */
     267    public function test_state_not_printed_when_only_empty_arrays() {
     268        $this->interactivity->state( 'pluginWithEmptyState_prune', array() );
     269        $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
     270        $this->assertSame( '', $printed_script );
     271    }
     272
     273    /**
     274     * Tests that nested empty state objects are printed correctly.
     275     *
     276     * @ticket 60761
     277     *
     278     * @covers ::print_client_interactivity_data
     279     */
     280    public function test_state_printed_correctly_with_nested_empty_array() {
     281        $this->interactivity->state( 'myPlugin', array( 'emptyArray' => array() ) );
     282        $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
     283        $expected       = <<<'SCRIPT_TAG'
     284<script type="application/json" id="wp-interactivity-data">
     285{"state":{"myPlugin":{"emptyArray":[]}}}
     286</script>
     287
     288SCRIPT_TAG;
     289
     290        $this->assertSame( $expected, $printed_script );
     291    }
     292
     293    /**
     294     * Tests that empty config objects are pruned from printed data.
     295     *
     296     * @ticket 60761
     297     *
     298     * @covers ::print_client_interactivity_data
     299     */
     300    public function test_config_not_printed_when_empty_array() {
     301        $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() );
     302        $this->interactivity->config( 'pluginWithConfig_include', array( 'value' => 'excellent' ) );
     303        $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
     304        $expected       = <<<'SCRIPT_TAG'
     305<script type="application/json" id="wp-interactivity-data">
     306{"config":{"pluginWithConfig_include":{"value":"excellent"}}}
     307</script>
     308
     309SCRIPT_TAG;
     310
     311        $this->assertSame( $expected, $printed_script );
     312    }
     313
     314    /**
     315     * Tests that data consisting of only empty config objects is not printed.
     316     *
     317     * @ticket 60761
     318     *
     319     * @covers ::print_client_interactivity_data
     320     */
     321    public function test_config_not_printed_when_only_empty_arrays() {
     322        $this->interactivity->config( 'pluginWithEmptyConfig_prune', array() );
     323        $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
     324        $this->assertSame( '', $printed_script );
     325    }
     326
     327    /**
     328     * Tests that nested empty config objects are printed correctly.
     329     *
     330     * @ticket 60761
     331     *
     332     * @covers ::print_client_interactivity_data
     333     */
     334    public function test_config_printed_correctly_with_nested_empty_array() {
     335        $this->interactivity->config( 'myPlugin', array( 'emptyArray' => array() ) );
     336        $printed_script = get_echo( array( $this->interactivity, 'print_client_interactivity_data' ) );
     337        $expected       = <<<'SCRIPT_TAG'
     338<script type="application/json" id="wp-interactivity-data">
     339{"config":{"myPlugin":{"emptyArray":[]}}}
     340</script>
     341
     342SCRIPT_TAG;
     343
     344        $this->assertSame( $expected, $printed_script );
     345    }
    240346
    241347    /**
Note: See TracChangeset for help on using the changeset viewer.