Skip to content

Commit

Permalink
Stop running shortcode block content through render_shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Oct 24, 2024
1 parent 37c250b commit 700adc5
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions mu-plugins/blocks/notice/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@ function init() {

foreach ( [ 'info', 'tip', 'alert', 'tutorial', 'warning' ] as $shortcode ) {
remove_shortcode( $shortcode );
add_shortcode( $shortcode, __NAMESPACE__ . '\render_callout_as_notice' );
add_shortcode( $shortcode, __NAMESPACE__ . '\render_shortcode' );
}
}

/**
* Display a callout using the notice block.
* Maps a shortcode type to a notice type.
*
* @param array|string $attr Callout shortcode attributes array or empty string.
* @param string $content Callout content.
* @param string $tag Callout type.
*
* @return string Callout output as HTML markup.
* @param string $type The shortcode type.
* @return string The corresponding notice type.
*/
function render_callout_as_notice( $attr, $content, $tag ) {
function map_shortcode_to_type( $type ) {
$shortcode_mapping = array(
'info' => 'info',
'tip' => 'tip',
Expand All @@ -45,7 +42,20 @@ function render_callout_as_notice( $attr, $content, $tag ) {
'warning' => 'warning',
);

$type = $shortcode_mapping[ $tag ] ?: 'tip';
return $shortcode_mapping[ $type ] ?: 'tip';
}

/**
* Display the callout shortcodes using the notice block.
*
* @param array|string $attr Shortcode attributes array or empty string.
* @param string $content Shortcode content.
* @param string $tag Shortcode name.
*
* @return string Shortcode output as HTML markup.
*/
function render_shortcode( $attr, $content, $tag ) {
$type = map_shortcode_to_type( $tag );

// Sanitize message content.
$content = wp_kses_post( $content );
Expand All @@ -54,6 +64,17 @@ function render_callout_as_notice( $attr, $content, $tag ) {
$content = apply_filters( 'the_content', $content );
remove_filter( 'o2_process_the_content', '__return_false', 1 );

return generate_notice_block_markup( $type, $content );
}

/**
* Generates and processes block markup for a notice.
*
* @param string $type The notice type.
* @param string $content The content to insert into the block.
* @return string The processed block markup.
*/
function generate_notice_block_markup( $type, $content ) {
// Create a unique placeholder for the content.
// Directly processing `$content` with `do_blocks` can lead to buggy layouts on make.wp.org.
// See https://github.com/WordPress/wporg-mu-plugins/pull/337#issuecomment-1819992059.
Expand All @@ -68,9 +89,7 @@ function render_callout_as_notice( $attr, $content, $tag ) {
EOT;

$processed_markup = do_blocks( $block_markup );
$final_markup = str_replace( $placeholder, $content, $processed_markup );

return $final_markup;
return str_replace( $placeholder, $content, $processed_markup );
}

/**
Expand All @@ -89,11 +108,12 @@ function render_callout_block_as_notice( $pre_render, $parsed_block ) {
// Extract the specific "callout-*" class and remove the "callout-" prefix
preg_match( '/\bcallout-([\w-]+)\b/', $callout_wrapper, $matches );
$tag = $matches[1] ?? 'tip';
$type = map_shortcode_to_type( $tag );

$content = '';
foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
$content .= render_block( $inner_block );
}

return render_callout_as_notice( '', $content, $tag );
return generate_notice_block_markup( $type, $content );
}

0 comments on commit 700adc5

Please sign in to comment.