Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix server processing of wp-context getting out of sync #55436

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '
<div data-wp-context=\'{ "my-key": "some-value" }\'>
<div data-wp-context=\'{ "wrong_json_object: }\'>
</div>
</div>
';
$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()
);
}
}
Loading