WP_Interactivity_API::data_wp_class_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array $context_stack, array $namespace_stack )

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Processes the data-wp-class directive.

Description

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

Parameters

$pWP_Interactivity_API_Directives_Processorrequired
The directives processor instance.
$modestringrequired
Whether the processing is entering or exiting the tag.
$context_stackarrayrequired
The reference to the context stack.
$namespace_stackarrayrequired
The reference to the store namespace stack.

Source

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

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

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