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

Refresh: Implement 'the_block_template_html' filter for the block template #6533

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions src/wp-includes/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,14 @@ function _block_template_render_title_tag() {
*
* @access private
* @since 5.8.0
* @since 6.6.0 Block template content now runs through a 'the_block_template_html' filter.
*
* @global string $_wp_current_template_id
* @global string $_wp_current_template_content
* @global WP_Embed $wp_embed WordPress Embed object.
* @global WP_Query $wp_query WordPress Query object.
* @global string $_wp_current_template_content
*
* @return string Block template markup.
*/
function get_the_block_template_html() {
global $_wp_current_template_id, $_wp_current_template_content, $wp_embed, $wp_query;
global $_wp_current_template_content;

if ( ! $_wp_current_template_content ) {
if ( is_user_logged_in() ) {
Expand All @@ -225,10 +223,36 @@ function get_the_block_template_html() {
return;
}

$content = $wp_embed->run_shortcode( $_wp_current_template_content );
$content = $wp_embed->autoembed( $content );
$content = shortcode_unautop( $content );
$content = do_shortcode( $content );
$content = $_wp_current_template_content;

/**
* Filters the block template content.
*
* @since 6.6.0
*
* @param string $content The entire block template content.
*/
$content = apply_filters( 'the_block_template_html', $content );
$content = str_replace( ']]>', ']]>', $content );

// Wrap block template in .wp-site-blocks to allow for specific descendant styles
// (e.g. `.wp-site-blocks > *`).
return '<div class="wp-site-blocks">' . $content . '</div>';
}

/**
* Parses dynamic blocks from the block template and re-renders them.
*
* @since 6.6.0
*
* @global WP_Query $wp_query
* @global string $_wp_current_template_id
*
* @param string $content Block template content.
* @return string Updated block template content.
*/
function do_block_template_blocks( $content ) {
global $wp_query, $_wp_current_template_id;

/*
* Most block themes omit the `core/query` and `core/post-template` blocks in their singular content templates.
Expand Down Expand Up @@ -257,20 +281,13 @@ function get_the_block_template_html() {
) {
while ( have_posts() ) {
the_post();

$content = do_blocks( $content );
}
} else {
$content = do_blocks( $content );
return $content;
}

$content = wptexturize( $content );
$content = convert_smilies( $content );
$content = wp_filter_content_tags( $content, 'template' );
$content = str_replace( ']]>', ']]&gt;', $content );

// Wrap block template in .wp-site-blocks to allow for specific descendant styles
// (e.g. `.wp-site-blocks > *`).
return '<div class="wp-site-blocks">' . $content . '</div>';
return do_blocks( $content );
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/wp-includes/class-wp-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class WP_Embed {
*/
public function __construct() {
// Hack to get the [embed] shortcode to run before wpautop().
add_filter( 'the_block_template_html', array( $this, 'run_shortcode' ), 6 );
Copy link
Member

Choose a reason for hiding this comment

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

If we go with my other suggestion on adjusting the shortcode filter priorities, we could align this:

Suggested change
add_filter( 'the_block_template_html', array( $this, 'run_shortcode' ), 6 );
add_filter( 'the_block_template_html', array( $this, 'run_shortcode' ), 8 );

add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
add_filter( 'widget_text_content', array( $this, 'run_shortcode' ), 8 );
add_filter( 'widget_block_content', array( $this, 'run_shortcode' ), 8 );
Expand All @@ -38,6 +39,7 @@ public function __construct() {
add_shortcode( 'embed', '__return_false' );

// Attempts to embed all URLs in a post.
add_filter( 'the_block_template_html', array( $this, 'autoembed' ), 6 );
Copy link
Member

Choose a reason for hiding this comment

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

If we go with my other suggestion on adjusting the shortcode filter priorities, we could align this:

Suggested change
add_filter( 'the_block_template_html', array( $this, 'autoembed' ), 6 );
add_filter( 'the_block_template_html', array( $this, 'autoembed' ), 8 );

add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
add_filter( 'widget_text_content', array( $this, 'autoembed' ), 8 );
add_filter( 'widget_block_content', array( $this, 'autoembed' ), 8 );
Expand Down
8 changes: 8 additions & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,14 @@
// Global styles custom CSS.
add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles_custom_css' );

// Block template.
add_filter( 'the_block_template_html', 'shortcode_unautop', 7 );
add_filter( 'the_block_template_html', 'do_shortcode', 8 );
Comment on lines +607 to +608
Copy link
Member

@felixarntz felixarntz May 13, 2024

Choose a reason for hiding this comment

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

While this maintains the original order of the function, I wonder whether we could align this with the filter order execution on the other similar scenarios, e.g. on the_content. On those, shortcode_unautop uses the default 10 and do_shortcode uses 11.

I know this comment is a bit opposite of my other comment, but I'm purely throwing it out there for consideration. I wonder why it was even decided in the first place to use a different order here.

On another note: If we used 10 and 11 here, it would also allow to use 8 for the filters in the WP_Embed class, which is another place to make things more consistent.

Suggested change
add_filter( 'the_block_template_html', 'shortcode_unautop', 7 );
add_filter( 'the_block_template_html', 'do_shortcode', 8 );
add_filter( 'the_block_template_html', 'shortcode_unautop' );
add_filter( 'the_block_template_html', 'do_shortcode', 11 );

add_filter( 'the_block_template_html', 'do_block_template_blocks', 9 );
add_filter( 'the_block_template_html', 'wptexturize' );
add_filter( 'the_block_template_html', 'wp_filter_content_tags' );
Comment on lines +610 to +611
Copy link
Member

Choose a reason for hiding this comment

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

Instead of having the order of these two purely defined by the order of these two add_filter() calls, I think we should use priority 12 on wp_filter_content_tags, also for consistency with how it is intentionally done elsewhere as of 6560f46.

Suggested change
add_filter( 'the_block_template_html', 'wptexturize' );
add_filter( 'the_block_template_html', 'wp_filter_content_tags' );
add_filter( 'the_block_template_html', 'wptexturize' );
add_filter( 'the_block_template_html', 'wp_filter_content_tags', 12 );

add_filter( 'the_block_template_html', 'convert_smilies', 20 );
Comment on lines +611 to +612
Copy link
Member

Choose a reason for hiding this comment

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

In the original code that is being changed here, convert_smilies() was run before wp_filter_content_tags(). While I'm not opposed to use the same priority as convert_smilies uses elsewhere, we should make sure using a different order here wasn't an intentional decision (and there is a good chance it wasn't).

But if we don't feel strongly, it would be safer to leave out the priority on convert_smilies() so that the original order is maintained.

Suggested change
add_filter( 'the_block_template_html', 'wp_filter_content_tags' );
add_filter( 'the_block_template_html', 'convert_smilies', 20 );
add_filter( 'the_block_template_html', 'wp_filter_content_tags', 12 );
add_filter( 'the_block_template_html', 'convert_smilies' );


// Block supports, and other styles parsed and stored in the Style Engine.
add_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' );
add_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 );
Expand Down
5 changes: 5 additions & 0 deletions src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,11 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
function wp_filter_content_tags( $content, $context = null ) {
if ( null === $context ) {
$context = current_filter();

// For backward compatibility, continue using the 'template' context for the block template.
if ( 'the_block_template_html' === $context ) {
$context = 'template';
}
}

$add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context );
Expand Down
Loading