WP_Interactivity_API::data_wp_style_processor()privateWP 6.5.0

Processes the data-wp-style directive.

It updates the style attribute value of 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_style_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_style_processor() code WP 6.5.4

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

		foreach ( $all_style_attributes as $attribute_name ) {
			list( , $style_property ) = $this->extract_prefix_and_suffix( $attribute_name );
			if ( empty( $style_property ) ) {
				continue;
			}

			$directive_attribute_value = $p->get_attribute( $attribute_name );
			$style_property_value      = $this->evaluate( $directive_attribute_value, end( $namespace_stack ), end( $context_stack ) );
			$style_attribute_value     = $p->get_attribute( 'style' );
			$style_attribute_value     = ( $style_attribute_value && ! is_bool( $style_attribute_value ) ) ? $style_attribute_value : '';

			/*
			 * Checks first if the style property is not falsy and the style
			 * attribute value is not empty because if it is, it doesn't need to
			 * update the attribute value.
			 */
			if ( $style_property_value || $style_attribute_value ) {
				$style_attribute_value = $this->merge_style_property( $style_attribute_value, $style_property, $style_property_value );
				/*
				 * If the style attribute value is not empty, it sets it. Otherwise,
				 * it removes it.
				 */
				if ( ! empty( $style_attribute_value ) ) {
					$p->set_attribute( 'style', $style_attribute_value );
				} else {
					$p->remove_attribute( 'style' );
				}
			}
		}
	}
}