Interactivity API: Increase hook priority for processing directives.

Use a priority of 100 to ensure that other filters can add additional directives before the processing starts.
This way, directives will be processed even if the $parsed_block variable is edited by a filter.

Reviewed by gziolo.
Merges [57826] to the to the 6.5 branch.

Props cbravobernal, swissspidy, flixos90, joemcgill, gziolo.
Fixes #60743.

git-svn-id: https://develop.svn.wordpress.org/branches/6.5@57830 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2024-03-14 08:57:48 +00:00
parent 893bf88e25
commit 833d14c847
2 changed files with 41 additions and 4 deletions

View File

@ -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' );
/*
* 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.

View File

@ -308,11 +308,44 @@ class Tests_Interactivity_API_Functions extends WP_UnitTestCase {
';
$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.