diff --git a/lib/experimental/interactivity-api/class-wp-directive-context.php b/lib/experimental/interactivity-api/class-wp-directive-context.php index 3fc47b2604316..4276eddca20ac 100644 --- a/lib/experimental/interactivity-api/class-wp-directive-context.php +++ b/lib/experimental/interactivity-api/class-wp-directive-context.php @@ -65,9 +65,10 @@ public function get_context() { * @return void */ public function set_context( $context ) { - if ( $context ) { - array_push( $this->stack, array_replace_recursive( $this->get_context(), $context ) ); - } + array_push( + $this->stack, + array_replace_recursive( $this->get_context(), $context ) + ); } /** diff --git a/lib/experimental/interactivity-api/directives/wp-context.php b/lib/experimental/interactivity-api/directives/wp-context.php index 5e3c5a140b2b0..bf3014c5772d7 100644 --- a/lib/experimental/interactivity-api/directives/wp-context.php +++ b/lib/experimental/interactivity-api/directives/wp-context.php @@ -25,8 +25,8 @@ function gutenberg_interactivity_process_wp_context( $tags, $context ) { $new_context = json_decode( $value, true ); if ( null === $new_context ) { - // Invalid JSON defined in the directive. - return; + // If the JSON is not valid, we still add an empty array to the stack. + $new_context = array(); } $context->set_context( $new_context ); diff --git a/phpunit/experimental/interactivity-api/directives/wp-context-test.php b/phpunit/experimental/interactivity-api/directives/wp-context-test.php index 0f36f38ba837d..d3f44a5e4fd81 100644 --- a/phpunit/experimental/interactivity-api/directives/wp-context-test.php +++ b/phpunit/experimental/interactivity-api/directives/wp-context-test.php @@ -73,4 +73,55 @@ public function test_directive_doesnt_throw_on_malformed_context_objects() { $context->get_context() ); } + + public function test_directive_keeps_working_after_malformed_context_objects() { + $context = new WP_Directive_Context(); + + $markup = ' +
+
+
+
+ '; + $tags = new WP_HTML_Tag_Processor( $markup ); + + // Parent div. + $tags->next_tag( array( 'tag_closers' => 'visit' ) ); + gutenberg_interactivity_process_wp_context( $tags, $context ); + + $this->assertSame( + array( 'my-key' => 'some-value' ), + $context->get_context() + ); + + // Children div. + $tags->next_tag( array( 'tag_closers' => 'visit' ) ); + gutenberg_interactivity_process_wp_context( $tags, $context ); + + // Still the same context. + $this->assertSame( + array( 'my-key' => 'some-value' ), + $context->get_context() + ); + + // Closing children div. + $tags->next_tag( array( 'tag_closers' => 'visit' ) ); + gutenberg_interactivity_process_wp_context( $tags, $context ); + + // Still the same context. + $this->assertSame( + array( 'my-key' => 'some-value' ), + $context->get_context() + ); + + // Closing parent div. + $tags->next_tag( array( 'tag_closers' => 'visit' ) ); + gutenberg_interactivity_process_wp_context( $tags, $context ); + + // Now the context is empty. + $this->assertSame( + array(), + $context->get_context() + ); + } }