diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index a4e54432d2aa2..8dce1f52ebf84 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -894,6 +894,14 @@ function _build_block_template_result_from_post( $post ) { } } + $hooked_blocks = get_hooked_blocks(); + if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template ); + $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template ); + $blocks = parse_blocks( $post->post_content ); + $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + } + return $template; } diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 52b20b3e55b26..0f6f9ff9c508d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -757,6 +757,44 @@ function get_hooked_blocks() { return $hooked_blocks; } +function insert_hooked_blocks( $relative_position, &$anchor_block, $hooked_blocks, $context ) { + $anchor_block_type = $anchor_block['blockName']; + $hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] ) + ? $hooked_blocks[ $anchor_block_type ][ $relative_position ] + : array(); + + $markup = ''; + /** + * Filters the list of hooked block types for a given anchor block type and relative position. + * + * @since 6.4.0 + * + * @param string[] $hooked_block_types The list of hooked block types. + * @param string $relative_position The relative position of the hooked blocks. + * Can be one of 'before', 'after', 'first_child', or 'last_child'. + * @param string $anchor_block_type The anchor block type. + * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. + */ + $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 ) { + if ( + ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) || + ! in_array( $hooked_block_type, $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) + ) { + $markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' ); + } + + // TODO: The following is only needed for the REST API endpoint. + // Ideally, the code should thus be moved into the controller. + if ( ! isset( $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] ) ) { + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'] = array(); + } + $anchor_block['attrs']['metadata']['ignoredHookedBlocks'][] = $hooked_block_type; + + } + return $markup; +} + /** * Returns a function that injects the theme attribute into, and hooked blocks before, a given block. * @@ -792,40 +830,10 @@ function make_before_block_visitor( $hooked_blocks, $context ) { if ( $parent_block && ! $prev ) { // Candidate for first-child insertion. - $relative_position = 'first_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(); - - /** - * Filters the list of hooked block types for a given anchor block type and relative position. - * - * @since 6.4.0 - * - * @param string[] $hooked_block_types The list of hooked block types. - * @param string $relative_position The relative position of the hooked blocks. - * Can be one of 'before', 'after', 'first_child', or 'last_child'. - * @param string $anchor_block_type The anchor block type. - * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. - */ - $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(), '' ); - } + $markup .= insert_hooked_blocks( 'first_child', $parent_block, $hooked_blocks, $context ); } - $relative_position = 'before'; - $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(), '' ); - } + $markup .= insert_hooked_blocks( 'before', $block, $hooked_blocks, $context ); return $markup; }; @@ -859,33 +867,11 @@ function make_after_block_visitor( $hooked_blocks, $context ) { * @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(), '' ); - } + $markup = insert_hooked_blocks( 'after', $block, $hooked_blocks, $context ); 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(), '' ); - } + $markup .= insert_hooked_blocks( 'last_child', $parent_block, $hooked_blocks, $context ); } return $markup; diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index 3ad400906945b..26d90c51b5529 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -43,10 +43,10 @@ function render_block_core_template_part( $attributes ) { if ( $template_part_post ) { // A published post might already exist if this template part was customized elsewhere // or if it's part of a customized template. - $content = $template_part_post->post_content; - $area_terms = get_the_terms( $template_part_post, 'wp_template_part_area' ); - if ( ! is_wp_error( $area_terms ) && false !== $area_terms ) { - $area = $area_terms[0]->name; + $block_template = _build_block_template_result_from_post( $template_part_post ); + $content = $block_template->content; + if ( isset( $block_template->area ) ) { + $area = $block_template->area; } /** * Fires when a block template part is loaded from a template post stored in the database. diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index b8ffb92e559b2..73bd1fd304a2f 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -243,10 +243,12 @@ class WP_Block_Type { * Attributes supported by every block. * * @since 6.0.0 + * @since 6.5.0 Added metadata global attribute. * @var array */ const GLOBAL_ATTRIBUTES = array( - 'lock' => array( 'type' => 'object' ), + 'lock' => array( 'type' => 'object' ), + 'metadata' => array( 'type' => 'object' ), ); /**