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. Reviewed by gziolo. Merges [57841] to the to the 6.5 branch. Props jonsurrell, luisherranz, darerodz, gziolo, swissspidy. Fixes #60761. Built from https://develop.svn.wordpress.org/branches/6.5@57843 git-svn-id: http://core.svn.wordpress.org/branches/6.5@57344 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
2d39c954a2
commit
dede0d6b09
|
@ -140,20 +140,36 @@ final class WP_Interactivity_API {
|
||||||
* @since 6.5.0
|
* @since 6.5.0
|
||||||
*/
|
*/
|
||||||
public function print_client_interactivity_data() {
|
public function print_client_interactivity_data() {
|
||||||
$store = array();
|
if ( empty( $this->state_data ) && empty( $this->config_data ) ) {
|
||||||
$has_state = ! empty( $this->state_data );
|
return;
|
||||||
$has_config = ! empty( $this->config_data );
|
}
|
||||||
|
|
||||||
if ( $has_state || $has_config ) {
|
$interactivity_data = array();
|
||||||
if ( $has_config ) {
|
|
||||||
$store['config'] = $this->config_data;
|
$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_print_inline_script_tag(
|
||||||
wp_json_encode(
|
wp_json_encode(
|
||||||
$store,
|
$interactivity_data,
|
||||||
JSON_HEX_TAG | JSON_HEX_AMP
|
JSON_HEX_TAG | JSON_HEX_AMP
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '6.5-RC2-57838';
|
$wp_version = '6.5-RC2-57843';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue