Skip to content

Commit

Permalink
Support data-wp-interactive="myPlugin"
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Feb 5, 2024
1 parent c848fe2 commit 6345e24
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
14 changes: 10 additions & 4 deletions src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,6 @@ private function data_wp_interactive_processor( WP_Interactivity_API_Directives_

// Tries to decode the `data-wp-interactive` attribute value.
$attribute_value = $p->get_attribute( 'data-wp-interactive' );
$decoded_json = is_string( $attribute_value ) && ! empty( $attribute_value )
? json_decode( $attribute_value, true )
: null;

/*
* Pushes the newly defined namespace or the current one if the
Expand All @@ -496,7 +493,16 @@ private function data_wp_interactive_processor( WP_Interactivity_API_Directives_
* independently of whether the previous `data-wp-interactive` definition
* contained a valid namespace.
*/
$namespace_stack[] = $decoded_json['namespace'] ?? end( $namespace_stack );
if ( is_string( $attribute_value ) && ! empty( $attribute_value ) ) {
$decoded_json = json_decode( $attribute_value, true );
if ( is_array( $decoded_json ) ) {
$namespace_stack[] = $decoded_json['namespace'] ?? end( $namespace_stack );
} else {
$namespace_stack[] = $attribute_value;
}
} else {
$namespace_stack[] = end( $namespace_stack );
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ private function process_directives( $html ) {

/**
* Tests that a default namespace is applied when using the
* `data-wp-interactive` directive.
* `data-wp-interactive` directive with a json object.
*
* @ticket 60356
*
* @covers ::process_directives
*/
public function test_wp_interactive_sets_a_default_namespace() {
public function test_wp_interactive_sets_a_default_namespace_with_object() {
$html = '
<div data-wp-interactive=\'{ "namespace": "myPlugin" }\'>
<div class="test" data-wp-bind--id="state.id">Text</div>
Expand All @@ -59,6 +59,24 @@ public function test_wp_interactive_sets_a_default_namespace() {
$this->assertEquals( 'some-id', $p->get_attribute( 'id' ) );
}

/**
* Tests that a default namespace is applied when using the
* `data-wp-interactive` directive with a string.
*
* @ticket 60356
*
* @covers ::process_directives
*/
public function test_wp_interactive_sets_a_default_namespace_with_string() {
$html = '
<div data-wp-interactive="myPlugin">
<div class="test" data-wp-bind--id="state.id">Text</div>
</div>
';
list($p) = $this->process_directives( $html );
$this->assertEquals( 'some-id', $p->get_attribute( 'id' ) );
}

/**
* Tests that the most recent `data-wp-interactive` directive replaces the
* previous default namespace.
Expand All @@ -83,14 +101,15 @@ public function test_wp_interactive_replaces_the_previous_default_namespace() {
}

/**
* Tests that a `data-wp-interactive` directive without a namespace does not
* replace the previously established default namespace.
* Tests that a `data-wp-interactive` directive with a json object that
* doesn't have a namespace property does not replace the previously
* established default namespace.
*
* @ticket 60356
*
* @covers ::process_directives
*/
public function test_wp_interactive_without_namespace_doesnt_replace_the_previous_default_namespace() {
public function test_wp_interactive_json_without_namespace_doesnt_replace_the_previous_default_namespace() {
$html = '
<div data-wp-interactive=\'{ "namespace": "myPlugin" }\'>
<div data-wp-interactive=\'{}\'>
Expand Down

0 comments on commit 6345e24

Please sign in to comment.