WP_Interactivity_API::data_wp_context_processor()
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.
Method of the class: WP_Interactivity_API{}
No Hooks.
Returns
null
. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->data_wp_context_processor( $p, $mode );
- $p(WP_Interactivity_API_Directives_Processor) (required)
- The directives processor instance.
- $mode(string) (required)
- Whether the processing is entering or exiting the tag.
Changelog
Since 6.5.0 | Introduced. |
WP_Interactivity_API::data_wp_context_processor() WP Interactivity API::data wp context processor code WP 6.8.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 ); } }