WP_Interactivity_API::filter_script_module_interactivity_data( array $data ): array

In this article

Set client-side interactivity data.

Description

Once in the browser, the state will be parsed and used to hydrate the client-side interactivity stores and the configuration will be available using a getConfig utility.

Parameters

$dataarrayrequired
Data to filter.

Return

array Data for the Interactivity API script module.

Source

public function filter_script_module_interactivity_data( array $data ): array {
	if (
		empty( $this->state_data ) &&
		empty( $this->config_data ) &&
		empty( $this->derived_state_closures )
	) {
		return $data;
	}

	$config = array();
	foreach ( $this->config_data as $key => $value ) {
		if ( ! empty( $value ) ) {
			$config[ $key ] = $value;
		}
	}
	if ( ! empty( $config ) ) {
		$data['config'] = $config;
	}

	$state = array();
	foreach ( $this->state_data as $key => $value ) {
		if ( ! empty( $value ) ) {
			$state[ $key ] = $value;
		}
	}
	if ( ! empty( $state ) ) {
		$data['state'] = $state;
	}

	$derived_props = array();
	foreach ( $this->derived_state_closures as $key => $value ) {
		if ( ! empty( $value ) ) {
			$derived_props[ $key ] = $value;
		}
	}
	if ( ! empty( $derived_props ) ) {
		$data['derivedStateClosures'] = $derived_props;
	}

	return $data;
}

Changelog

VersionDescription
6.9.0Serializes derived state props accessed during directive processing.
6.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.