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

Interactivity API - Fix SSR if parsed block changes in a filter. #6245

Closed
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion src/wp-includes/interactivity-api/interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ function wp_interactivity_process_directives_of_interactive_blocks( array $parse

return $parsed_block;
}
add_filter( 'render_block_data', 'wp_interactivity_process_directives_of_interactive_blocks' );
/*
gziolo marked this conversation as resolved.
Show resolved Hide resolved
* Uses a priority of 20 to ensure that other filters can add additional attributes to
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
* $parsed_block before the processing starts.
*/
add_filter( 'render_block_data', 'wp_interactivity_process_directives_of_interactive_blocks', PHP_INT_MAX, 1 );
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

/**
* Retrieves the main WP_Interactivity_API instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @group interactivity-api
*/
class Tests_Interactivity_API_Functions extends WP_UnitTestCase {
class Tests_Interactivity_API_wpInteractivityAPIFunctions extends WP_UnitTestCase {
/**
* Set up.
*/
Expand Down Expand Up @@ -308,11 +308,46 @@ public function test_process_directives_only_process_the_root_interactive_blocks
';
$this->data_wp_test_processor_count = 0;
do_blocks( $post_content );
$this->assertEquals( 2, $this->data_wp_test_processor_count );
unregister_block_type( 'test/custom-directive-block' );
$this->assertEquals( 2, $this->data_wp_test_processor_count );
$directive_processors->setValue( null, $old_directive_processors );
}

/**
* Tests that directives are server side processing even if the $parsed_block variable is edited
* by a filter.
*
* @ticket 60743
*
* @covers ::wp_interactivity_process_directives_of_interactive_blocks
*/

swissspidy marked this conversation as resolved.
Show resolved Hide resolved
public function test_process_directives_when_block_is_filtered() {
register_block_type(
'test/custom-directive-block',
array(
'render_callback' => function () {
return '<input data-wp-interactive="nameSpace" ' . wp_interactivity_data_wp_context( array( 'text' => 'test' ) ) . ' data-wp-bind--value="context.text" />';
},
'supports' => array(
'interactivity' => true,
),
)
);
function test_render_block_data( $parsed_block ) {
$parsed_block['testKey'] = true;
return $parsed_block;
}
add_filter( 'render_block_data', 'test_render_block_data' );
$post_content = '<!-- wp:test/custom-directive-block /-->';
$processed_content = do_blocks( $post_content );
$processor = new WP_HTML_Tag_Processor( $processed_content );
$processor->next_tag( array( 'data-wp-interactive' => 'nameSpace' ) );
remove_filter( 'render_block_data', 'test_render_block_data' );
unregister_block_type( 'test/custom-directive-block' );
$this->assertEquals( 'test', $processor->get_attribute( 'value' ) );
}

/**
* Tests that wp_interactivity_data_wp_context function correctly converts different array
* structures to a JSON string.
Expand Down
Loading