WP_Interactivity_API::state()publicWP 6.5.0

Gets and/or sets the initial state of an Interactivity API store for a given namespace.

If state for that store namespace already exists, it merges the new provided state with the existing one.

When no namespace is specified, it returns the state defined for the current value in the internal namespace stack during a process_directives call.

Method of the class: WP_Interactivity_API{}

No Hooks.

Returns

Array. The current state for the specified store namespace. This will be the updated state if a $state argument was provided.

Usage

$WP_Interactivity_API = new WP_Interactivity_API();
$WP_Interactivity_API->state( ?string $store_namespace, ?array $state ): array;
?string $store_namespace **
-
Default: null
?array $state **
-
Default: null

Changelog

Since 6.5.0 Introduced.
Since 6.6.0 The $store_namespace param is optional.

WP_Interactivity_API::state() code WP 6.8.1

public function state( ?string $store_namespace = null, ?array $state = null ): array {
	if ( ! $store_namespace ) {
		if ( $state ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'The namespace is required when state data is passed.' ),
				'6.6.0'
			);
			return array();
		}
		if ( null !== $store_namespace ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'The namespace should be a non-empty string.' ),
				'6.6.0'
			);
			return array();
		}
		if ( null === $this->namespace_stack ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'The namespace can only be omitted during directive processing.' ),
				'6.6.0'
			);
			return array();
		}

		$store_namespace = end( $this->namespace_stack );
	}
	if ( ! isset( $this->state_data[ $store_namespace ] ) ) {
		$this->state_data[ $store_namespace ] = array();
	}
	if ( is_array( $state ) ) {
		$this->state_data[ $store_namespace ] = array_replace_recursive(
			$this->state_data[ $store_namespace ],
			$state
		);
	}
	return $this->state_data[ $store_namespace ];
}