Make WordPress Core


Ignore:
Timestamp:
09/19/2023 12:34:50 PM (3 years ago)
Author:
Bernhard Reiter
Message:

Blocks: Implement block insertion functions.

For #59313, we need to implement functions to insert a given parsed block into another parsed block's inner blocks, and to prepend and append to that array, respectively.

We will use those functions in combination with traverse_and_serialize_blocks (see #59327) to implement automatic insertion of hooked blocks into block templates and patterns.

Props gziolo.
Fixes #59385.

File:
1 edited

Legend:

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

    r56610 r56618  
    762762
    763763/**
     764 * Insert a parsed block into a parent block's inner blocks.
     765 *
     766 * Given a parsed block, a block index, and a chunk index, insert another parsed block
     767 * into the parent block at the given indices.
     768 *
     769 * Note that the this mutates the parent block by inserting into the parent's `innerBlocks`
     770 * array, and by updating the parent's `innerContent` array accordingly.
     771 *
     772 * @since 6.4.0
     773 *
     774 * @param array $parent_block   The parent block.
     775 * @param int   $block_index    The index specifying the insertion position among the parent block's inner blocks.
     776 * @param int   $chunk_index    The index specifying the insertion position among the parent block's inner content chunks.
     777 * @param array $inserted_block The block to insert.
     778 * @return void
     779 */
     780function insert_inner_block( &$parent_block, $block_index, $chunk_index, $inserted_block ) {
     781    array_splice( $parent_block['innerBlocks'], $block_index, 0, array( $inserted_block ) );
     782
     783    /*
     784     * Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`)
     785     * when rendering blocks, we also need to insert a value (`null`, to mark a block
     786     * location) into that array.
     787     */
     788    array_splice( $parent_block['innerContent'], $chunk_index, 0, array( null ) );
     789}
     790
     791/**
     792 * Prepend a parsed block to a parent block's inner blocks.
     793 *
     794 * Given a parsed block, prepend another parsed block to the parent block's inner blocks.
     795 *
     796 * Note that the this mutates the parent block by inserting into the parent's `innerBlocks`
     797 * array, and by updating the parent's `innerContent` array accordingly.
     798 *
     799 * @since 6.4.0
     800 *
     801 * @param array  $parent_block   The parent block.
     802 * @param array  $inserted_block The block to insert.
     803 * @return void
     804 */
     805function prepend_inner_block( &$parent_block, $inserted_block ) {
     806    $chunk_index = 0;
     807    for ( $index = 0; $index < count( $parent_block['innerContent'] ); $index++ ) {
     808        if ( is_null( $parent_block['innerContent'][ $index ] ) ) {
     809            $chunk_index = $index;
     810            break;
     811        }
     812    }
     813    insert_inner_block( $parent_block, 0, $chunk_index, $inserted_block );
     814}
     815
     816/**
     817 * Append a parsed block to a parent block's inner blocks.
     818 *
     819 * Given a parsed block, append another parsed block to the parent block's inner blocks.
     820 *
     821 * Note that the this mutates the parent block by inserting into the parent's `innerBlocks`
     822 * array, and by updating the parent's `innerContent` array accordingly.
     823 *
     824 * @since 6.4.0
     825 *
     826 * @param array  $parent_block   The parent block.
     827 * @param array  $inserted_block The block to insert.
     828 * @return void
     829 */
     830function append_inner_block( &$parent_block, $inserted_block ) {
     831    $chunk_index = count( $parent_block['innerContent'] );
     832    for ( $index = count( $parent_block['innerContent'] ); $index > 0; $index-- ) {
     833        if ( is_null( $parent_block['innerContent'][ $index - 1 ] ) ) {
     834            $chunk_index = $index;
     835            break;
     836        }
     837    }
     838    insert_inner_block( $parent_block, count( $parent_block['innerBlocks'] ), $chunk_index, $inserted_block );
     839}
     840
     841/**
    764842 * Given an array of attributes, returns a string in the serialized attributes
    765843 * format prepared for post content.
Note: See TracChangeset for help on using the changeset viewer.