Interactivity API: Move directive processing to `WP_Block` class

Integrates the directives processing into the WP_Block class. It removes the overhead of running additional hooks when rendering blocks and simplifies the way we detect whether the directive processing should run on an interactive region of the produced final HTML for the blocks.

Introduces `interactivity_process_directives` filter to offer a way to opt out from directives processing. It's needed in Gutenberg: https://github.com/WordPress/gutenberg/pull/62095.

Props gziolo, cbravobernal.
Fixes #61185.


Built from https://develop.svn.wordpress.org/trunk@58234


git-svn-id: http://core.svn.wordpress.org/trunk@57697 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo 2024-05-29 11:57:08 +00:00
parent b5894d595e
commit b23d47efe7
4 changed files with 49 additions and 66 deletions

View File

@ -408,6 +408,29 @@ class WP_Block {
*/
public function render( $options = array() ) {
global $post;
/*
* There can be only one root interactive block at a time because the rendered HTML of that block contains
* the rendered HTML of all its inner blocks, including any interactive block.
*/
static $root_interactive_block = null;
/**
* Filters whether Interactivity API should process directives.
*
* @since 6.6.0
*
* @param bool $enabled Whether the directives processing is enabled.
*/
$interactivity_process_directives_enabled = apply_filters( 'interactivity_process_directives', true );
if (
$interactivity_process_directives_enabled && null === $root_interactive_block && (
( isset( $this->block_type->supports['interactivity'] ) && true === $this->block_type->supports['interactivity'] ) ||
! empty( $this->block_type->supports['interactivity']['interactive'] )
)
) {
$root_interactive_block = $this;
}
$options = wp_parse_args(
$options,
array(
@ -533,6 +556,12 @@ class WP_Block {
*/
$block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this );
if ( $root_interactive_block === $this ) {
// The root interactive block has finished rendering. Time to process directives.
$block_content = wp_interactivity_process_directives( $block_content );
$root_interactive_block = null;
}
return $block_content;
}
}

View File

@ -6318,3 +6318,22 @@ function wp_render_elements_support( $block_content, $block ) {
_deprecated_function( __FUNCTION__, '6.6.0', 'wp_render_elements_class_name' );
return $block_content;
}
/**
* Processes the directives on the rendered HTML of the interactive blocks.
*
* This processes only one root interactive block at a time because the
* rendered HTML of that block contains the rendered HTML of all its inner
* blocks, including any interactive block. It does so by ignoring all the
* interactive inner blocks until the root interactive block is processed.
*
* @since 6.5.0
* @deprecated 6.6.0
*
* @param array $parsed_block The parsed block.
* @return array The same parsed block.
*/
function wp_interactivity_process_directives_of_interactive_blocks( array $parsed_block ): array {
_deprecated_function( __FUNCTION__, '6.6.0' );
return $parsed_block;
}

View File

@ -7,71 +7,6 @@
* @since 6.5.0
*/
/**
* Processes the directives on the rendered HTML of the interactive blocks.
*
* This processes only one root interactive block at a time because the
* rendered HTML of that block contains the rendered HTML of all its inner
* blocks, including any interactive block. It does so by ignoring all the
* interactive inner blocks until the root interactive block is processed.
*
* @since 6.5.0
*
* @param array $parsed_block The parsed block.
* @return array The same parsed block.
*/
function wp_interactivity_process_directives_of_interactive_blocks( array $parsed_block ): array {
static $root_interactive_block = null;
/*
* Checks whether a root interactive block is already annotated for
* processing, and if it is, it ignores the subsequent ones.
*/
if ( null === $root_interactive_block ) {
$block_name = $parsed_block['blockName'];
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
if (
isset( $block_name ) &&
( ( isset( $block_type->supports['interactivity'] ) && true === $block_type->supports['interactivity'] ) ||
( isset( $block_type->supports['interactivity']['interactive'] ) && true === $block_type->supports['interactivity']['interactive'] ) )
) {
// Annotates the root interactive block for processing.
$root_interactive_block = array( $block_name, $parsed_block );
/*
* Adds a filter to process the root interactive block once it has
* finished rendering.
*/
$process_interactive_blocks = static function ( string $content, array $parsed_block ) use ( &$root_interactive_block, &$process_interactive_blocks ): string {
// Checks whether the current block is the root interactive block.
list($root_block_name, $root_parsed_block) = $root_interactive_block;
if ( $root_block_name === $parsed_block['blockName'] && $parsed_block === $root_parsed_block ) {
// The root interactive blocks has finished rendering, process it.
$content = wp_interactivity_process_directives( $content );
// Removes the filter and reset the root interactive block.
remove_filter( 'render_block_' . $parsed_block['blockName'], $process_interactive_blocks );
$root_interactive_block = null;
}
return $content;
};
/*
* 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, 100, 2 );
}
}
return $parsed_block;
}
/*
* 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

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.6-alpha-58233';
$wp_version = '6.6-alpha-58234';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.