WP_Interactivity_API::data_wp_bind_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-bind directive.

Description

It updates or removes the bound attributes based on the evaluation of its associated reference.

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_bind_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
	if ( 'enter' === $mode ) {
		$all_bind_directives = $p->get_attribute_names_with_prefix( 'data-wp-bind--' );

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

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

			if (
				null !== $result &&
				(
					false !== $result ||
					( strlen( $bound_attribute ) > 5 && '-' === $bound_attribute[4] )
				)
			) {
				/*
				 * If the result of the evaluation is a boolean and the attribute is
				 * `aria-` or `data-, convert it to a string "true" or "false". It
				 * follows the exact same logic as Preact because it needs to
				 * replicate what Preact will later do in the client:
				 * https://github.com/preactjs/preact/blob/ea49f7a0f9d1ff2c98c0bdd66aa0cbc583055246/src/diff/props.js#L131C24-L136
				 */
				if (
					is_bool( $result ) &&
					( strlen( $bound_attribute ) > 5 && '-' === $bound_attribute[4] )
				) {
					$result = $result ? 'true' : 'false';
				}
				$p->set_attribute( $bound_attribute, $result );
			} else {
				$p->remove_attribute( $bound_attribute );
			}
		}
	}
}

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

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