From 1c86f5761733eaf5add5d4eaf381843208d4b95b Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:31:54 +1300 Subject: [PATCH] Filter callout block render and render a notice block Closes https://github.com/WordPress/wordpress.org/issues/374 --- mu-plugins/blocks/notice/index.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/mu-plugins/blocks/notice/index.php b/mu-plugins/blocks/notice/index.php index ebc9fb755..20cba36c1 100644 --- a/mu-plugins/blocks/notice/index.php +++ b/mu-plugins/blocks/notice/index.php @@ -9,6 +9,8 @@ // Run after `WPorg_Handbook_Callout_Boxes` registers the shortcodes. add_action( 'init', __NAMESPACE__ . '\init', 11 ); +add_filter( 'pre_render_block', __NAMESPACE__ . '\render_callout_block_as_notice', 11, 2 ); + /** * Registers the block using the metadata loaded from the `block.json` file. * Behind the scenes, it registers also all assets so they can be enqueued @@ -70,3 +72,28 @@ function render_shortcode( $attr, $content, $tag ) { return $final_markup; } + +/** + * Renders a callout block as a notice. + * + * @param string|null $pre_render The pre-rendered content or null. + * @param array $parsed_block The parsed block array. + * @return string|null The rendered notice or the original pre-render value. + */ +function render_callout_block_as_notice( $pre_render, $parsed_block ) { + if ( is_admin() || 'wporg/callout' !== $parsed_block['blockName'] ) { + return $pre_render; + } + + $callout_wrapper = $parsed_block['innerHTML']; + // Extract the specific "callout-*" class and remove the "callout-" prefix + preg_match( '/\bcallout-([\w-]+)\b/', $callout_wrapper, $matches ); + $tag = $matches[1] ?? 'info'; + + $content = ''; + foreach ( $parsed_block['innerBlocks'] as $inner_block ) { + $content .= render_block( $inner_block ); + } + + return render_shortcode( null, $content, $tag ); +}