Make WordPress Core


Ignore:
Timestamp:
09/21/2023 04:16:05 PM (9 months ago)
Author:
Bernhard Reiter
Message:

Blocks: Implement automatic block insertion into Block Hooks.

Block Hooks allow a third-party block to specify a position relative to a given block into which it will then be automatically inserted (e.g. a "Like" button block can ask to be inserted after the Post Content block, or an eCommerce shopping cart block can ask to be inserted after the Navigation block).

The underlying idea is to provide an extensibility mechanism for Block Themes, in analogy to WordPress' Hooks concept that has allowed extending Classic Themes through filters and actions.

The two core tenets for Block Hooks are:

  1. Insertion into the frontend should happen right after a plugin containing a hooked block is activated (i.e. the user isn't required to insert the block manually in the editor first); similarly, disabling the plugin should remove the hooked block from the frontend.
  2. The user has the ultimate power to customize that automatic insertion: The hooked block is also visible in the editor, and the user's decision to persist, dismiss (i.e. remove), customize, or move it will be respected (and reflected on the frontend).

To account for both tenets, the tradeoff was made to limit automatic block insertion to unmodified templates (and template parts, respectively). The reason for this is that the simplest way of storing the information whether a block has been persisted to (or dismissed from) a given template (or part) is right in the template markup.

To accommodate for that tradeoff, UI controls (toggles) are being added to increase visibility of hooked blocks, and to allow for their later insertion into templates (or parts) that already have been modified by the user.

For hooked blocks to appear both in the frontend and in the editor (see tenet number 2), they need to be inserted into both the frontend markup and the REST API (templates and patterns endpoints) equally. As a consequence, this means that automatic insertion couldn't (only) be implemented at block render stage, as for the editor, the serialized (but unrendered) markup needs to be modified.

Furthermore, hooked blocks also have to be inserted into block patterns. Since practically no filters exist for the patterns registry, this has to be done in the registry's get_registered and get_all_registered methods.

Props gziolo.
Fixes #59313.

File:
1 edited

Legend:

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

    r56644 r56649  
    752752    $hooked_blocks = array();
    753753    foreach ( $block_types as $block_type ) {
     754        if ( ! property_exists( $block_type, 'block_hooks' ) || ! is_array( $block_type->block_hooks ) ) {
     755            continue;
     756        }
    754757        foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
    755758            if ( $anchor_block_type === $name ) {
     
    759762    }
    760763    return $hooked_blocks;
     764}
     765
     766/**
     767 * Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
     768 *
     769 * The returned function can be used as `$pre_callback` argument to `traverse_and_serialize_block(s)`,
     770 * where it will inject the `theme` attribute into all Template Part blocks, and prepend the markup for
     771 * any blocks hooked `before` the given block and as its parent's `first_child`, respectively.
     772 *
     773 * @since 6.4.0
     774 * @access private
     775 *
     776 * @param WP_Block_Template|array $context A block template, template part, or pattern that the blocks belong to.
     777 * @return callable A function that returns the serialized markup for the given block,
     778 *                  including the markup for any hooked blocks before it.
     779 */
     780function make_before_block_visitor( $context ) {
     781    /**
     782     * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup.
     783     *
     784     * If the current block is a Template Part block, inject the `theme` attribute.
     785     * Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's
     786     * `first_child`, respectively, to the serialized markup for the given block.
     787     *
     788     * @param array $block  The block to inject the theme attribute into, and hooked blocks before.
     789     * @param array $parent The parent block of the given block.
     790     * @param array $prev   The previous sibling block of the given block.
     791     * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
     792     */
     793    return function( &$block, $parent = null, $prev = null ) use ( $context ) {
     794        _inject_theme_attribute_in_template_part_block( $block );
     795
     796        $markup = '';
     797
     798        if ( $parent && ! $prev ) {
     799            // Candidate for first-child insertion.
     800            $hooked_blocks_for_parent = get_hooked_blocks( $parent['blockName'] );
     801            foreach ( $hooked_blocks_for_parent as $hooked_block_type => $relative_position ) {
     802                if ( 'first_child' === $relative_position ) {
     803                    $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
     804                    /** This filter is documented in wp-includes/blocks.php */
     805                    $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $parent, $context );
     806                }
     807            }
     808        }
     809
     810        $hooked_blocks = get_hooked_blocks( $block['blockName'] );
     811        foreach ( $hooked_blocks as $hooked_block_type => $relative_position ) {
     812            if ( 'before' === $relative_position ) {
     813                $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
     814                /**
     815                 * Filters the serialized markup of a hooked block.
     816                 *
     817                 * @since 6.4.0
     818                 *
     819                 * @param string                  $hooked_block_markup The serialized markup of the hooked block.
     820                 * @param string                  $hooked_block_type   The type of the hooked block.
     821                 * @param string                  $relative_position   The relative position of the hooked block.
     822                 *                                                     Can be one of 'before', 'after', 'first_child', or 'last_child'.
     823                 * @param array                   $block               The anchor block.
     824                 * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
     825                 */
     826                $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $block, $context );
     827            }
     828        }
     829
     830        return $markup;
     831    };
     832}
     833
     834/**
     835 * Returns a function that injects the hooked blocks after a given block.
     836 *
     837 * The returned function can be used as `$post_callback` argument to `traverse_and_serialize_block(s)`,
     838 * where it will append the markup for any blocks hooked `after` the given block and as its parent's
     839 * `last_child`, respectively.
     840 *
     841 * @since 6.4.0
     842 * @access private
     843 *
     844 * @param WP_Block_Template|array $context A block template, template part, or pattern that the blocks belong to.
     845 * @return callable A function that returns the serialized markup for the given block,
     846 *                  including the markup for any hooked blocks after it.
     847 */
     848function make_after_block_visitor( $context ) {
     849    /**
     850     * Injects hooked blocks after the given block, and returns the serialized markup.
     851     *
     852     * Append the markup for any blocks hooked `after` the given block and as its parent's
     853     * `last_child`, respectively, to the serialized markup for the given block.
     854     *
     855     * @param array $block  The block to inject the hooked blocks after.
     856     * @param array $parent The parent block of the given block.
     857     * @param array $next   The next sibling block of the given block.
     858     * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
     859     */
     860    return function( &$block, $parent = null, $next = null ) use ( $context ) {
     861        $markup = '';
     862
     863        $hooked_blocks = get_hooked_blocks( $block['blockName'] );
     864        foreach ( $hooked_blocks as $hooked_block_type => $relative_position ) {
     865            if ( 'after' === $relative_position ) {
     866                $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
     867                /** This filter is documented in wp-includes/blocks.php */
     868                $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $block, $context );
     869            }
     870        }
     871
     872        if ( $parent && ! $next ) {
     873            // Candidate for last-child insertion.
     874            $hooked_blocks_for_parent = get_hooked_blocks( $parent['blockName'] );
     875            foreach ( $hooked_blocks_for_parent as $hooked_block_type => $relative_position ) {
     876                if ( 'last_child' === $relative_position ) {
     877                    $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' );
     878                    /** This filter is documented in wp-includes/blocks.php */
     879                    $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $parent, $context );
     880                }
     881            }
     882        }
     883
     884        return $markup;
     885    };
    761886}
    762887
Note: See TracChangeset for help on using the changeset viewer.