From 833d14c847dd68d0aa1dc9965d2e6f2d808cd39f Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 14 Mar 2024 08:57:48 +0000 Subject: [PATCH] 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 --- .../interactivity-api/interactivity-api.php | 10 ++++-- .../interactivity-api/interactivity-api.php | 35 ++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/interactivity-api/interactivity-api.php b/src/wp-includes/interactivity-api/interactivity-api.php index cc2c812047..b8f3e508d0 100644 --- a/src/wp-includes/interactivity-api/interactivity-api.php +++ b/src/wp-includes/interactivity-api/interactivity-api.php @@ -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. diff --git a/tests/phpunit/tests/interactivity-api/interactivity-api.php b/tests/phpunit/tests/interactivity-api/interactivity-api.php index 7d74fda8eb..951645b891 100644 --- a/tests/phpunit/tests/interactivity-api/interactivity-api.php +++ b/tests/phpunit/tests/interactivity-api/interactivity-api.php @@ -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 ' '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 = ''; + $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.