make_after_block_visitor

The timeline below displays how wordpress function make_after_block_visitor has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Returns a function that injects the hooked blocks after a given block.
 *
 * The returned function can be used as `$post_callback` argument to `traverse_and_serialize_block(s)`,
 * where it will append the markup for any blocks hooked `after` the given block and as its parent's
 * `last_child`, respectively.
 *
 * This function is meant for internal use only.
 *
 * @since 6.4.0
 * @since 6.5.0 Added $callback argument.
 * @access private
 *
 * @param array                           $hooked_blocks An array of blocks hooked to another block.
 * @param WP_Block_Template|WP_Post|array $context       A block template, template part, `wp_navigation` post object,
 *                                                       or pattern that the blocks belong to.
 * @param callable                        $callback      A function that will be called for each block to generate
 *                                                       the markup for a given list of blocks that are hooked to it.
 *                                                       Default: 'insert_hooked_blocks'.
 * @return callable A function that returns the serialized markup for the given block,
 *                  including the markup for any hooked blocks after it.
 */
function make_after_block_visitor($hooked_blocks, $context, $callback = 'insert_hooked_blocks')
{
    /**
     * Injects hooked blocks after the given block, and returns the serialized markup.
     *
     * Append the markup for any blocks hooked `after` the given block and as its parent's
     * `last_child`, respectively, to the serialized markup for the given block.
     *
     * @param array $block        The block to inject the hooked blocks after. Passed by reference.
     * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
     * @param array $next         The next sibling block of the given block. Default null.
     * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
     */
    return function (&$block, &$parent_block = null, $next = null) use ($hooked_blocks, $context, $callback) {
        $markup = call_user_func_array($callback, array(&$block, 'after', $hooked_blocks, $context));
        if ($parent_block && !$next) {
            // Candidate for last-child insertion.
            $markup .= call_user_func_array($callback, array(&$parent_block, 'last_child', $hooked_blocks, $context));
        }
        return $markup;
    };
}

WordPress Version: 6.4

/**
 * Returns a function that injects the hooked blocks after a given block.
 *
 * The returned function can be used as `$post_callback` argument to `traverse_and_serialize_block(s)`,
 * where it will append the markup for any blocks hooked `after` the given block and as its parent's
 * `last_child`, respectively.
 *
 * This function is meant for internal use only.
 *
 * @since 6.4.0
 * @access private
 *
 * @param array                   $hooked_blocks An array of blocks hooked to another block.
 * @param WP_Block_Template|array $context       A block template, template part, or pattern that the blocks belong to.
 * @return callable A function that returns the serialized markup for the given block,
 *                  including the markup for any hooked blocks after it.
 */
function make_after_block_visitor($hooked_blocks, $context)
{
    /**
     * Injects hooked blocks after the given block, and returns the serialized markup.
     *
     * Append the markup for any blocks hooked `after` the given block and as its parent's
     * `last_child`, respectively, to the serialized markup for the given block.
     *
     * @param array $block        The block to inject the hooked blocks after. Passed by reference.
     * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
     * @param array $next         The next sibling block of the given block. Default null.
     * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
     */
    return function (&$block, &$parent_block = null, $next = null) use ($hooked_blocks, $context) {
        $markup = '';
        $relative_position = 'after';
        $anchor_block_type = $block['blockName'];
        $hooked_block_types = isset($hooked_blocks[$anchor_block_type][$relative_position]) ? $hooked_blocks[$anchor_block_type][$relative_position] : array();
        /** This filter is documented in wp-includes/blocks.php */
        $hooked_block_types = apply_filters('hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context);
        foreach ($hooked_block_types as $hooked_block_type) {
            $markup .= get_comment_delimited_block_content($hooked_block_type, array(), '');
        }
        if ($parent_block && !$next) {
            // Candidate for last-child insertion.
            $relative_position = 'last_child';
            $anchor_block_type = $parent_block['blockName'];
            $hooked_block_types = isset($hooked_blocks[$anchor_block_type][$relative_position]) ? $hooked_blocks[$anchor_block_type][$relative_position] : array();
            /** This filter is documented in wp-includes/blocks.php */
            $hooked_block_types = apply_filters('hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context);
            foreach ($hooked_block_types as $hooked_block_type) {
                $markup .= get_comment_delimited_block_content($hooked_block_type, array(), '');
            }
        }
        return $markup;
    };
}