Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket 56780 #3851

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
18 changes: 17 additions & 1 deletion src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1158,14 +1158,30 @@ function get_block_file_template( $id, $template_type = 'wp_template' ) {
*
* @since 5.9.0
*
* @global WP_Embed $wp_embed An instance of WP_Embed for embedding rich media.
*
* @param string $part The block template part to print. Use "header" or "footer".
*/
function block_template_part( $part ) {
global $wp_embed;

$template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixarntz Do you have any idea how this will effect fetch priority or other image

$content = do_shortcode( $content );

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

echo $content;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- wp:group {"tagName":"header","layout":{"type":"flex","flexWrap":"wrap"}} -->
<header id="masthead" class="wp-block-group">
<h4>wptexturize</h4>
<p>'cause today's effort makes it worth tomorrow's "holiday"...</p>
<h4>convert_smilies</h4>
<p>:)</p>
<h4>shortcode_unautop (shortcode wrapped in paragraph)</h4>
<p>[test_shortcode]_unautop</p>
<h4>wp_filter_content_tags</h4>
<img width="408" height="287" src="https://placekitten.com/408/287">
<h4>do_shortcode</h4>
<!-- wp:shortcode -->
[test_shortcode]_shortcode_block
<!-- /wp:shortcode -->
<h4>$wp_embed->autoembed</h4>
https://youtube.com/shorts/_H4vwkGvWjE?feature=share
</header>
<!-- /wp:group -->
99 changes: 99 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 @@ -523,4 +525,101 @@ public function test_wp_generate_block_templates_export_file() {
}
$this->assertTrue( $has_html_files, 'contains at least one html file' );
}

/**
* Tests that block_template_part() does shortcodes.
*
* @ticket 56780
*
* @covers ::block_template_part
*/
public function test_block_template_part_should_do_shortcodes() {
add_shortcode(
self::TEST_SHORTCODE,
static function () {
return 'test_shortcode_rendered';
}
);

$generated_content = get_echo( 'block_template_part', array( 'template-part-shortcode' ) );
remove_shortcode( self::TEST_SHORTCODE );

$this->assertStringContainsString(
'test_shortcode_rendered_unautop',
$generated_content,
'Should render shortcodes in paragraph tags'
);

$this->assertStringContainsString(
'test_shortcode_rendered_shortcode_block',
$generated_content,
'Should render shortcodes in shortcode blocks'
);
}

/**
* Tests that block_template_part() includes new browser and HTML
* technologies that may not have existed at the time of post creation.
*
* @ticket 56780
*
* @covers ::block_template_part
*/
public function test_block_template_part_should_include_new_browser_and_html_technologies() {
$this->assertStringContainsString(
'<img loading="lazy" decoding="async" width="408" height="287" src="https://placekitten.com/408/287">',
get_echo( 'block_template_part', array( 'template-part-shortcode' ) )
);
}

/**
* Tests that block_template_part() converts smilies.
*
* @ticket 56780
*
* @covers ::block_template_part
*/
public function test_block_template_part_should_convert_smilies() {
$this->assertStringContainsString(
'🙂',
get_echo( 'block_template_part', array( 'template-part-shortcode' ) )
);
}

/**
* Tests that block_template_part() auto-embeds videos.
*
* @ticket 56780
*
* @covers ::block_template_part
*/
public function test_block_template_part_should_auto_embed_videos() {
$generated_content = get_echo( 'block_template_part', array( 'template-part-shortcode' ) );

$this->assertStringContainsString(
'<iframe',
$generated_content,
'Should replace Youtube link with an iframe'
);

$this->assertStringContainsString(
'src="https://www.youtube.com/embed/_H4vwkGvWjE',
$generated_content,
'Should use the correct src for the iframe'
);
}

/**
* Tests that block_template_part() should use formatted entities.
*
* @ticket 56780
*
* @covers ::block_template_part
*/
public function test_block_template_part_should_use_formatted_entities() {
$this->assertStringContainsString(
'&#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221;&#8230;',
get_echo( 'block_template_part', array( 'template-part-shortcode' ) )
);
}
}