WP_Interactivity_API::data_wp_bind_processor()privateWP 6.5.0

Processes the data-wp-bind directive.

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

Метод класса: WP_Interactivity_API{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// private - только в коде основоного (родительского) класса
$result = $this->data_wp_bind_processor( $p, $mode );
$p(WP_Interactivity_API_Directives_Processor) (обязательный)
The directives processor instance.
$mode(строка) (обязательный)
Whether the processing is entering or exiting the tag.

Список изменений

С версии 6.5.0 Введена.

Код WP_Interactivity_API::data_wp_bind_processor() WP 6.7.1

private function data_wp_bind_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) {
	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 );

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