WP_Interactivity_API::data_wp_context_processor()privateWP 6.5.0

Processes the data-wp-context directive.

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

Метод класса: WP_Interactivity_API{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// private - только в коде основоного (родительского) класса
$result = $this->data_wp_context_processor( $p, $mode );
$p(WP_Interactivity_API_Directives_Processor) (обязательный)
The directives processor instance.
$mode(строка) (обязательный)
Whether the processing is entering or exiting the tag.

Список изменений

С версии 6.5.0 Введена.

Код WP_Interactivity_API::data_wp_context_processor() WP 6.7.1

private function data_wp_context_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) {
	// When exiting tags, it removes the last context from the stack.
	if ( 'exit' === $mode ) {
		array_pop( $this->context_stack );
		return;
	}

	$attribute_value = $p->get_attribute( 'data-wp-context' );
	$namespace_value = end( $this->namespace_stack );

	// Separates the namespace from the context JSON object.
	list( $namespace_value, $decoded_json ) = is_string( $attribute_value ) && ! empty( $attribute_value )
		? $this->extract_directive_value( $attribute_value, $namespace_value )
		: array( $namespace_value, null );

	/*
	 * If there is a namespace, it adds a new context to the stack merging the
	 * previous context with the new one.
	 */
	if ( is_string( $namespace_value ) ) {
		$this->context_stack[] = array_replace_recursive(
			end( $this->context_stack ) !== false ? end( $this->context_stack ) : array(),
			array( $namespace_value => is_array( $decoded_json ) ? $decoded_json : array() )
		);
	} else {
		/*
		 * If there is no namespace, it pushes the current context to the stack.
		 * It needs to do so because the function pops out the current context
		 * from the stack whenever it finds a `data-wp-context`'s closing tag.
		 */
		$this->context_stack[] = end( $this->context_stack );
	}
}