Skip to content

Commit 0d456e2

Browse files
committed
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. Props cbravobernal, swissspidy, flixos90, joemcgill, gziolo. Fixes #60743. git-svn-id: https://develop.svn.wordpress.org/trunk@57826 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4793768 commit 0d456e2

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/wp-includes/interactivity-api/interactivity-api.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,20 @@ function wp_interactivity_process_directives_of_interactive_blocks( array $parse
5757
};
5858

5959
/*
60-
* Uses a priority of 20 to ensure that other filters can add additional
60+
* Uses a priority of 100 to ensure that other filters can add additional
6161
* directives before the processing starts.
6262
*/
63-
add_filter( 'render_block_' . $block_name, $process_interactive_blocks, 20, 2 );
63+
add_filter( 'render_block_' . $block_name, $process_interactive_blocks, 100, 2 );
6464
}
6565
}
6666

6767
return $parsed_block;
6868
}
69-
add_filter( 'render_block_data', 'wp_interactivity_process_directives_of_interactive_blocks' );
69+
/*
70+
* Uses a priority of 100 to ensure that other filters can add additional attributes to
71+
* $parsed_block before the processing starts.
72+
*/
73+
add_filter( 'render_block_data', 'wp_interactivity_process_directives_of_interactive_blocks', 100, 1 );
7074

7175
/**
7276
* Retrieves the main WP_Interactivity_API instance.

tests/phpunit/tests/interactivity-api/wpInteractivityAPIFunctions.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,44 @@ public function test_process_directives_only_process_the_root_interactive_blocks
308308
';
309309
$this->data_wp_test_processor_count = 0;
310310
do_blocks( $post_content );
311-
$this->assertEquals( 2, $this->data_wp_test_processor_count );
312311
unregister_block_type( 'test/custom-directive-block' );
312+
$this->assertEquals( 2, $this->data_wp_test_processor_count );
313313
$directive_processors->setValue( null, $old_directive_processors );
314314
}
315315

316+
/**
317+
* Tests that directives are server side processing even if the $parsed_block variable is edited by a filter.
318+
*
319+
* @ticket 60743
320+
*
321+
* @covers ::wp_interactivity_process_directives_of_interactive_blocks
322+
*/
323+
public function test_process_directives_when_block_is_filtered() {
324+
register_block_type(
325+
'test/custom-directive-block',
326+
array(
327+
'render_callback' => function () {
328+
return '<input data-wp-interactive="nameSpace" ' . wp_interactivity_data_wp_context( array( 'text' => 'test' ) ) . ' data-wp-bind--value="context.text" />';
329+
},
330+
'supports' => array(
331+
'interactivity' => true,
332+
),
333+
)
334+
);
335+
function test_render_block_data( $parsed_block ) {
336+
$parsed_block['testKey'] = true;
337+
return $parsed_block;
338+
}
339+
add_filter( 'render_block_data', 'test_render_block_data' );
340+
$post_content = '<!-- wp:test/custom-directive-block /-->';
341+
$processed_content = do_blocks( $post_content );
342+
$processor = new WP_HTML_Tag_Processor( $processed_content );
343+
$processor->next_tag( array( 'data-wp-interactive' => 'nameSpace' ) );
344+
remove_filter( 'render_block_data', 'test_render_block_data' );
345+
unregister_block_type( 'test/custom-directive-block' );
346+
$this->assertEquals( 'test', $processor->get_attribute( 'value' ) );
347+
}
348+
316349
/**
317350
* Tests that wp_interactivity_data_wp_context function correctly converts different array
318351
* structures to a JSON string.

0 commit comments

Comments
 (0)