Skip to content

Commit

Permalink
Update block_template_part to expand shortcodes in template parts
Browse files Browse the repository at this point in the history
Add set of typical actions run on `the_content` to the `block_template_part` function.
This replicate the behavior of the template part block's render method.
  • Loading branch information
petitphp committed Sep 8, 2023
1 parent 3f9bc4e commit a117a89
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,20 @@ function block_template_part( $part ) {
if ( ! $template_part || empty( $template_part->content ) ) {
return;
}
echo do_blocks( $template_part->content );

// Run through the actions that are typically taken on the_content.
$content = do_blocks( $template_part->content );
$content = wptexturize( $content );
$content = convert_smilies( $content );
$content = shortcode_unautop( $content );
$content = wp_filter_content_tags( $content );
$content = do_shortcode( $content );

// Handle embeds for block template parts.
global $wp_embed;
$content = $wp_embed->autoembed( $content );

echo $content;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- wp:group {"tagName":"header","layout":{"type":"flex","flexWrap":"wrap"}} -->
<header id="masthead" class="wp-block-group">
<!-- wp:shortcode -->
[test_shortcode]
<!-- /wp:shortcode -->
</header>
<!-- /wp:group -->
24 changes: 24 additions & 0 deletions tests/phpunit/tests/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Tests_Block_Template_Utils extends WP_UnitTestCase {

const TEST_THEME = 'block-theme';

const TEST_SHORTCODE = 'test_shortcode';

private static $template_post;
private static $template_part_post;

Expand Down Expand Up @@ -340,4 +342,26 @@ public function test_wp_generate_block_templates_export_file() {
}
$this->assertTrue( $has_html_files, 'contains at least one html file' );
}

/**
* Should expand shortcodes in block template part.
*
* @ticket 56780
* @covers ::block_template_part
*/
public function test_block_template_part_render_shortcode() {
add_shortcode( self::TEST_SHORTCODE, array( $this, 'render_test_shortcode' ) );

ob_start();
block_template_part( 'template-part-shortcode' );
$generated_content = ob_get_clean();

remove_shortcode( self::TEST_SHORTCODE );

$this->assertStringContainsString( 'test_shortcode_rendered', $generated_content );
}

public function render_test_shortcode() {
return 'test_shortcode_rendered';
}
}

0 comments on commit a117a89

Please sign in to comment.