WP_Interactivity_API::data_wp_interactive_processor()privateWP 6.5.0

Processes the data-wp-interactive directive.

It adds the default store namespace defined in the directive value to the stack so that it's available for the nested interactivity elements.

Method of the class: WP_Interactivity_API{}

No Hooks.

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->data_wp_interactive_processor( $p, $mode, $context_stack, $namespace_stack );
$p(WP_Interactivity_API_Directives_Processor) (required)
The directives processor instance.
$mode(string) (required)
Whether the processing is entering or exiting the tag.
$context_stack(array) (required)
The reference to the context stack.
$namespace_stack(array) (required)
The reference to the store namespace stack.

Changelog

Since 6.5.0 Introduced.

WP_Interactivity_API::data_wp_interactive_processor() code WP 6.5.4

private function data_wp_interactive_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
	// When exiting tags, it removes the last namespace from the stack.
	if ( 'exit' === $mode ) {
		array_pop( $namespace_stack );
		return;
	}

	// Tries to decode the `data-wp-interactive` attribute value.
	$attribute_value = $p->get_attribute( 'data-wp-interactive' );

	/*
	 * Pushes the newly defined namespace or the current one if the
	 * `data-wp-interactive` definition was invalid or does not contain a
	 * namespace. It does so because the function pops out the current namespace
	 * from the stack whenever it finds a `data-wp-interactive`'s closing tag,
	 * independently of whether the previous `data-wp-interactive` definition
	 * contained a valid namespace.
	 */
	$new_namespace = null;
	if ( is_string( $attribute_value ) && ! empty( $attribute_value ) ) {
		$decoded_json = json_decode( $attribute_value, true );
		if ( is_array( $decoded_json ) ) {
			$new_namespace = $decoded_json['namespace'] ?? null;
		} else {
			$new_namespace = $attribute_value;
		}
	}
	$namespace_stack[] = ( $new_namespace && 1 === preg_match( '/^([\w\-_\/]+)/', $new_namespace ) )
		? $new_namespace
		: end( $namespace_stack );
}