Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1235,14 +1235,15 @@ 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.
*/
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.
Expand All @@ -1260,6 +1261,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1037,4 +1037,39 @@ public function test_wp_bind_removes_event_handler_binding_but_processes_valid_b
$new_html = $this->interactivity->process_directives( $html );
$this->assertEqualHTML( '<button data-wp-bind--id="myPlugin::state.id" id="some-id">Text</button>', $new_html );
}

/**
* Data provider for directive bindings.
*
* @return array<non-empty-string, array{ attribute: non-empty-string }> Data provider.
*/
public static function data_directive_bindings(): array {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static function data_directive_bindings(): array {
public static function data_wp_bind_removes_directive_bindings(): array {

Per handbook, single use data providers ought to be named for the their test with a modified prefix.

🔢 This will require a change in the docblock too.

  1. Also, the data provider ought to sit immediately below the test, rather than above. This will look a bit messy as an inline suggestion, so I'll leave it for you.

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 = "<button att-before {$attribute}=\"myPlugin::state.id\" att-after>Text</button>";
$new_html = $this->interactivity->process_directives( $html );
$this->assertEqualHTML( '<button att-before att-after>Text</button>', $new_html );
}
}
Loading