Make WordPress Core


Ignore:
Timestamp:
05/29/2024 11:55:27 AM (13 months ago)
Author:
gziolo
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/interactivity-api/interactivity-api.php

    r57826 r58234  
    77 * @since 6.5.0
    88 */
    9 
    10 /**
    11  * Processes the directives on the rendered HTML of the interactive blocks.
    12  *
    13  * This processes only one root interactive block at a time because the
    14  * rendered HTML of that block contains the rendered HTML of all its inner
    15  * blocks, including any interactive block. It does so by ignoring all the
    16  * interactive inner blocks until the root interactive block is processed.
    17  *
    18  * @since 6.5.0
    19  *
    20  * @param array $parsed_block The parsed block.
    21  * @return array The same parsed block.
    22  */
    23 function wp_interactivity_process_directives_of_interactive_blocks( array $parsed_block ): array {
    24     static $root_interactive_block = null;
    25 
    26     /*
    27      * Checks whether a root interactive block is already annotated for
    28      * processing, and if it is, it ignores the subsequent ones.
    29      */
    30     if ( null === $root_interactive_block ) {
    31         $block_name = $parsed_block['blockName'];
    32         $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
    33 
    34         if (
    35             isset( $block_name ) &&
    36             ( ( isset( $block_type->supports['interactivity'] ) && true === $block_type->supports['interactivity'] ) ||
    37             ( isset( $block_type->supports['interactivity']['interactive'] ) && true === $block_type->supports['interactivity']['interactive'] ) )
    38         ) {
    39             // Annotates the root interactive block for processing.
    40             $root_interactive_block = array( $block_name, $parsed_block );
    41 
    42             /*
    43              * Adds a filter to process the root interactive block once it has
    44              * finished rendering.
    45              */
    46             $process_interactive_blocks = static function ( string $content, array $parsed_block ) use ( &$root_interactive_block, &$process_interactive_blocks ): string {
    47                 // Checks whether the current block is the root interactive block.
    48                 list($root_block_name, $root_parsed_block) = $root_interactive_block;
    49                 if ( $root_block_name === $parsed_block['blockName'] && $parsed_block === $root_parsed_block ) {
    50                     // The root interactive blocks has finished rendering, process it.
    51                     $content = wp_interactivity_process_directives( $content );
    52                     // Removes the filter and reset the root interactive block.
    53                     remove_filter( 'render_block_' . $parsed_block['blockName'], $process_interactive_blocks );
    54                     $root_interactive_block = null;
    55                 }
    56                 return $content;
    57             };
    58 
    59             /*
    60              * Uses a priority of 100 to ensure that other filters can add additional
    61              * directives before the processing starts.
    62              */
    63             add_filter( 'render_block_' . $block_name, $process_interactive_blocks, 100, 2 );
    64         }
    65     }
    66 
    67     return $parsed_block;
    68 }
    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 );
    749
    7510/**
Note: See TracChangeset for help on using the changeset viewer.