WP_Interactivity_API::data_wp_class_processor()privateWP 6.5.0

Processes the data-wp-class directive.

It adds or removes CSS classes in the current HTML element based on the evaluation of its associated references.

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_class_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_class_processor() code WP 6.5.4

private function data_wp_class_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
	if ( 'enter' === $mode ) {
		$all_class_directives = $p->get_attribute_names_with_prefix( 'data-wp-class--' );

		foreach ( $all_class_directives as $attribute_name ) {
			list( , $class_name ) = $this->extract_prefix_and_suffix( $attribute_name );
			if ( empty( $class_name ) ) {
				return;
			}

			$attribute_value = $p->get_attribute( $attribute_name );
			$result          = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) );

			if ( $result ) {
				$p->add_class( $class_name );
			} else {
				$p->remove_class( $class_name );
			}
		}
	}
}