From bf346251a3dc2f9a8c845da05ac33852d5ab7fee Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 15 Mar 2024 12:12:09 +0000 Subject: [PATCH] =?UTF-8?q?Interactivity=20API:=20Do=20not=20print=20state?= =?UTF-8?q?=20if=20it=E2=80=99s=20an=20empty=20array.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. Built from https://develop.svn.wordpress.org/trunk@57841 git-svn-id: http://core.svn.wordpress.org/trunk@57342 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-interactivity-api.php | 34 ++++++++++++++----- wp-includes/version.php | 2 +- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/wp-includes/interactivity-api/class-wp-interactivity-api.php b/wp-includes/interactivity-api/class-wp-interactivity-api.php index 98876eaf8e..3db539aae6 100644 --- a/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -140,20 +140,36 @@ final class WP_Interactivity_API { * @since 6.5.0 */ public function print_client_interactivity_data() { - $store = array(); - $has_state = ! empty( $this->state_data ); - $has_config = ! empty( $this->config_data ); + if ( empty( $this->state_data ) && empty( $this->config_data ) ) { + return; + } - if ( $has_state || $has_config ) { - if ( $has_config ) { - $store['config'] = $this->config_data; + $interactivity_data = array(); + + $config = array(); + foreach ( $this->config_data as $key => $value ) { + if ( ! empty( $value ) ) { + $config[ $key ] = $value; } - if ( $has_state ) { - $store['state'] = $this->state_data; + } + if ( ! empty( $config ) ) { + $interactivity_data['config'] = $config; + } + + $state = array(); + foreach ( $this->state_data as $key => $value ) { + if ( ! empty( $value ) ) { + $state[ $key ] = $value; } + } + if ( ! empty( $state ) ) { + $interactivity_data['state'] = $state; + } + + if ( ! empty( $interactivity_data ) ) { wp_print_inline_script_tag( wp_json_encode( - $store, + $interactivity_data, JSON_HEX_TAG | JSON_HEX_AMP ), array( diff --git a/wp-includes/version.php b/wp-includes/version.php index e51fdcd612..f5db149896 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.6-alpha-57840'; +$wp_version = '6.6-alpha-57841'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.