From 50331c4681bc39e7bd20e451e7a9affa89240674 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 5 Nov 2026 13:35:48 +0400 Subject: [PATCH 1/2] Interactivity API: Remove directive bind directives during server processing. Server processing now removes `data-wp-bind--data-wp-*` directives from output and reports incorrect usage. A binding must not set, replace or remove another Interactivity API directive attribute. The check runs on the attribute name before the suffix and unique ID checks, so bindings with a unique ID are removed as well. Follows r65228, which gave event handler bindings the same treatment. See #66452. --- .../class-wp-interactivity-api.php | 18 ++- .../wpInteractivityAPI-wp-bind.php | 113 ++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php index d8f11e2d05fe3..357ffc11a0227 100644 --- a/src/wp-includes/interactivity-api/class-wp-interactivity-api.php +++ b/src/wp-includes/interactivity-api/class-wp-interactivity-api.php @@ -1153,7 +1153,7 @@ private function data_wp_context_processor( WP_Interactivity_API_Directives_Proc * @since 7.1.0 An object is resolved to whatever it serializes to for the client, a number is formatted by the * JSON encoder, and a value which cannot be sent to the client is rejected rather than passed to * WP_HTML_Tag_Processor::set_attribute(). - * @since 7.2.0 Event handler bind directives are removed. + * @since 7.2.0 Event handler and directive bind directives are removed. * * @param WP_Interactivity_API_Directives_Processor $p The directives processor instance. * @param string $mode Whether the processing is entering or exiting the tag. @@ -1161,6 +1161,7 @@ private function data_wp_context_processor( WP_Interactivity_API_Directives_Proc private function data_wp_bind_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ): void { if ( 'enter' === $mode ) { $event_handler_prefix = 'data-wp-bind--on'; + $directive_prefix = 'data-wp-bind--data-wp-'; $entries = $this->get_directive_entries( $p, 'bind' ); foreach ( $entries as $entry ) { // Binding event handler attributes is not supported. @@ -1178,6 +1179,21 @@ private function data_wp_bind_processor( WP_Interactivity_API_Directives_Process continue; } + // Binding Interactivity API directive attributes is not supported. + if ( str_starts_with( $entry['attribute_name'], $directive_prefix ) ) { + $p->remove_attribute( $entry['attribute_name'] ); + _doing_it_wrong( + __METHOD__, + sprintf( + /* translators: %s: The directive, e.g. data-wp-bind--data-wp-context. */ + __( 'Binding Interactivity API directive attributes is not supported, so "%s" was removed.' ), + esc_html( $entry['attribute_name'] ) + ), + '7.2.0' + ); + continue; + } + if ( empty( $entry['suffix'] ) || null !== $entry['unique_id'] ) { continue; } diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php index 5d6306827faf1..30f412eed2e7b 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php @@ -1037,4 +1037,117 @@ public function test_wp_bind_removes_event_handler_binding_but_processes_valid_b $new_html = $this->interactivity->process_directives( $html ); $this->assertEqualHTML( '', $new_html ); } + + /** + * Data provider for directive bindings. + * + * @return array Data provider. + */ + public static function data_directive_bindings(): array { + return array( + 'interactive directive' => array( 'attribute' => 'data-wp-bind--data-wp-interactive' ), + 'context directive' => array( 'attribute' => 'data-wp-bind--data-wp-context' ), + 'text directive' => array( 'attribute' => 'data-wp-bind--data-wp-text' ), + 'bind directive' => array( 'attribute' => 'data-wp-bind--data-wp-bind--id' ), + 'context directive with an empty unique ID' => array( 'attribute' => 'data-wp-bind--data-wp-context---' ), + 'context directive with a unique ID' => array( 'attribute' => 'data-wp-bind--data-wp-context---unique-id' ), + ); + } + + /** + * Tests that `data-wp-bind` removes bindings to directive attributes. + * + * @ticket 66452 + * + * @covers ::process_directives + * + * @dataProvider data_directive_bindings + * + * @expectedIncorrectUsage WP_Interactivity_API::data_wp_bind_processor + * + * @param non-empty-string $attribute Directive attribute name. + */ + public function test_wp_bind_removes_directive_bindings( string $attribute ): void { + $html = ""; + $new_html = $this->interactivity->process_directives( $html ); + $this->assertEqualHTML( '', $new_html ); + } + + /** + * Tests that `data-wp-bind` removes a binding to a directive attribute + * written in uppercase. + * + * @ticket 66452 + * + * @covers ::process_directives + * + * @expectedIncorrectUsage WP_Interactivity_API::data_wp_bind_processor + */ + public function test_wp_bind_removes_uppercase_directive_binding(): void { + $html = '
'; + $new_html = $this->interactivity->process_directives( $html ); + $this->assertEqualHTML( '
', $new_html ); + } + + /** + * Tests that a binding to a directive attribute neither replaces nor + * removes an existing directive attribute on the element. + * + * @ticket 66452 + * + * @covers ::process_directives + * + * @expectedIncorrectUsage WP_Interactivity_API::data_wp_bind_processor + */ + public function test_wp_bind_leaves_existing_directive_attributes_unchanged(): void { + $html = '
Text
'; + $new_html = $this->interactivity->process_directives( $html ); + $this->assertEqualHTML( '
Text
', $new_html ); + } + + /** + * Tests that `data-wp-bind` removes a directive binding but still + * processes valid bind directives on the same element. + * + * @ticket 66452 + * + * @covers ::process_directives + * + * @expectedIncorrectUsage WP_Interactivity_API::data_wp_bind_processor + */ + public function test_wp_bind_removes_directive_binding_but_processes_valid_binds(): void { + $html = ''; + $new_html = $this->interactivity->process_directives( $html ); + $this->assertEqualHTML( '', $new_html ); + } + + /** + * Data provider for attribute names which resemble directives but are not. + * + * @return array Data provider. + */ + public static function data_non_directive_attribute_names(): array { + return array( + 'data-wpx prefix' => array( 'attribute' => 'data-wpx' ), + 'data-foo' => array( 'attribute' => 'data-foo' ), + 'aria-label' => array( 'attribute' => 'aria-label' ), + ); + } + + /** + * Tests that attributes whose names resemble directives are still bound. + * + * @ticket 66452 + * + * @covers ::process_directives + * + * @dataProvider data_non_directive_attribute_names + * + * @param non-empty-string $attribute Attribute name. + */ + public function test_wp_bind_sets_attributes_resembling_directives( string $attribute ): void { + $html = "
Text
"; + list($p) = $this->process_directives( $html ); + $this->assertSame( 'some-id', $p->get_attribute( $attribute ) ); + } } From 83bd0cf37cc91aa21e4a6b27d9caf6a27e5a8bb1 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 5 Nov 2026 13:58:19 +0400 Subject: [PATCH 2/2] Tests: Keep only the directive binding removal test. --- .../wpInteractivityAPI-wp-bind.php | 78 ------------------- 1 file changed, 78 deletions(-) diff --git a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php index 30f412eed2e7b..bb43f11dbfc96 100644 --- a/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php +++ b/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-bind.php @@ -1072,82 +1072,4 @@ public function test_wp_bind_removes_directive_bindings( string $attribute ): vo $new_html = $this->interactivity->process_directives( $html ); $this->assertEqualHTML( '', $new_html ); } - - /** - * Tests that `data-wp-bind` removes a binding to a directive attribute - * written in uppercase. - * - * @ticket 66452 - * - * @covers ::process_directives - * - * @expectedIncorrectUsage WP_Interactivity_API::data_wp_bind_processor - */ - public function test_wp_bind_removes_uppercase_directive_binding(): void { - $html = '
'; - $new_html = $this->interactivity->process_directives( $html ); - $this->assertEqualHTML( '
', $new_html ); - } - - /** - * Tests that a binding to a directive attribute neither replaces nor - * removes an existing directive attribute on the element. - * - * @ticket 66452 - * - * @covers ::process_directives - * - * @expectedIncorrectUsage WP_Interactivity_API::data_wp_bind_processor - */ - public function test_wp_bind_leaves_existing_directive_attributes_unchanged(): void { - $html = '
Text
'; - $new_html = $this->interactivity->process_directives( $html ); - $this->assertEqualHTML( '
Text
', $new_html ); - } - - /** - * Tests that `data-wp-bind` removes a directive binding but still - * processes valid bind directives on the same element. - * - * @ticket 66452 - * - * @covers ::process_directives - * - * @expectedIncorrectUsage WP_Interactivity_API::data_wp_bind_processor - */ - public function test_wp_bind_removes_directive_binding_but_processes_valid_binds(): void { - $html = ''; - $new_html = $this->interactivity->process_directives( $html ); - $this->assertEqualHTML( '', $new_html ); - } - - /** - * Data provider for attribute names which resemble directives but are not. - * - * @return array Data provider. - */ - public static function data_non_directive_attribute_names(): array { - return array( - 'data-wpx prefix' => array( 'attribute' => 'data-wpx' ), - 'data-foo' => array( 'attribute' => 'data-foo' ), - 'aria-label' => array( 'attribute' => 'aria-label' ), - ); - } - - /** - * Tests that attributes whose names resemble directives are still bound. - * - * @ticket 66452 - * - * @covers ::process_directives - * - * @dataProvider data_non_directive_attribute_names - * - * @param non-empty-string $attribute Attribute name. - */ - public function test_wp_bind_sets_attributes_resembling_directives( string $attribute ): void { - $html = "
Text
"; - list($p) = $this->process_directives( $html ); - $this->assertSame( 'some-id', $p->get_attribute( $attribute ) ); - } }