WP_Interactivity_API::state( string $store_namespace = null, array $state = null ): array

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

Description

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.

Parameters

$store_namespacestringoptional
The unique store namespace identifier.

Default:null

$statearrayoptional
The array that will be merged with the existing state for the specified store namespace.

Default:null

Return

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

Source

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 ];
}

Changelog

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

User Contributed Notes

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