From 12d8bca6f94f6df0f9507a4b377e0eb14fc59fc2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 8 Dec 2023 15:56:48 +0100 Subject: [PATCH 1/2] Post Content block: Allow Block Hooks child insertion --- .../block-library/src/post-content/index.php | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index dd84574fdea65..610ed53244a3f 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -54,9 +54,47 @@ function render_block_core_post_content( $attributes, $content, $block ) { $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) ); + // Allow `first_child` and `last_child` insertion. + // As the Post Content block simply renders the markup returned by the + // `get_the_content()` function (wrapped in a `
` block wrapper), + // Limitations: + // - Changes to anchor block by get_hooked_block_markup() aren't respected. + // - No $context available. + + $context = null; + $anchor_block_type = 'core/post-content'; + $anchor_block = array( + 'blockName' => $anchor_block_type, + 'attributes' => $attributes + ); + $hooked_blocks = get_hooked_blocks(); + + $first_child_markup = ''; + $hooked_block_types_first_child = isset( $hooked_blocks[ $anchor_block_type ]['first_child'] ) + ? $hooked_blocks[ $anchor_block_type ]['first_child'] + : array(); + + $hooked_block_types_first_child = apply_filters( 'hooked_block_types', $hooked_block_types_first_child, 'first_child', $anchor_block_type, $context ); + foreach ( $hooked_block_types_first_child as $hooked_block_type_first_child ) { + $first_child_markup .= get_hooked_block_markup( $anchor_block, $hooked_block_type_first_child ); + } + + // TODO: Should run `get_the_content` and apply `the_content` filter here. + + $last_child_markup = ''; + $hooked_block_types_last_child = isset( $hooked_blocks[ $anchor_block_type ]['last_child'] ) + ? $hooked_blocks[ $anchor_block_type ]['last_child'] + : array(); + + $hooked_block_types_last_child = apply_filters( 'hooked_block_types', $hooked_block_types_last_child, 'last_child', $anchor_block_type, $context ); + foreach ( $hooked_block_types_last_child as $hooked_block_type_last_child ) { + // $parent_block should be a reference. That's an array. Tricky. + $last_child_markup .= get_hooked_block_markup( $anchor_block, $hooked_block_type_last_child ); + } + return ( '
' . - $content . + $first_child_markup . $content . $last_child_markup . '
' ); } From 74439b5c1cda2df69b205f7ebf9a32f402f9d32a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 13 Dec 2023 17:57:27 +0100 Subject: [PATCH 2/2] Whitespace --- packages/block-library/src/post-content/index.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 610ed53244a3f..657800eb99a3c 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -61,15 +61,15 @@ function render_block_core_post_content( $attributes, $content, $block ) { // - Changes to anchor block by get_hooked_block_markup() aren't respected. // - No $context available. - $context = null; - $anchor_block_type = 'core/post-content'; - $anchor_block = array( + $context = null; + $anchor_block_type = 'core/post-content'; + $anchor_block = array( 'blockName' => $anchor_block_type, - 'attributes' => $attributes + 'attributes' => $attributes, ); - $hooked_blocks = get_hooked_blocks(); + $hooked_blocks = get_hooked_blocks(); - $first_child_markup = ''; + $first_child_markup = ''; $hooked_block_types_first_child = isset( $hooked_blocks[ $anchor_block_type ]['first_child'] ) ? $hooked_blocks[ $anchor_block_type ]['first_child'] : array(); @@ -81,7 +81,7 @@ function render_block_core_post_content( $attributes, $content, $block ) { // TODO: Should run `get_the_content` and apply `the_content` filter here. - $last_child_markup = ''; + $last_child_markup = ''; $hooked_block_types_last_child = isset( $hooked_blocks[ $anchor_block_type ]['last_child'] ) ? $hooked_blocks[ $anchor_block_type ]['last_child'] : array();