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 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
10 changes: 7 additions & 3 deletions src/wp-includes/interactivity-api/interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,20 @@ function wp_interactivity_process_directives_of_interactive_blocks( array $parse
};

/*
* Uses a priority of 20 to ensure that other filters can add additional
* Uses a priority of 100 to ensure that other filters can add additional
* directives before the processing starts.
*/
add_filter( 'render_block_' . $block_name, $process_interactive_blocks, 20, 2 );
add_filter( 'render_block_' . $block_name, $process_interactive_blocks, 100, 2 );
}
}

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 100 to ensure that other filters can add additional attributes to
* $parsed_block before the processing starts.
*/
add_filter( 'render_block_data', 'wp_interactivity_process_directives_of_interactive_blocks', 100, 1 );

/**
* Retrieves the main WP_Interactivity_API instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,44 @@ 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
*/
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