Skip to content
Closed
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
53 changes: 35 additions & 18 deletions src/wp-includes/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,14 @@ function _block_template_render_title_tag() {
*
* @access private
* @since 5.8.0
* @since 6.4.0 Block template content now runs through a 'the_block_template_html' filter.
*
* @global string $_wp_current_template_content
* @global WP_Embed $wp_embed
* @global WP_Query $wp_query
* @global string $_wp_current_template_content
*
* @return string Block template markup.
*/
function get_the_block_template_html() {
global $_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 @@ -224,10 +223,35 @@ 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 HTML content.
*
* @since 6.4.0
*
* @param string $content The entire block template HTML content.
*/
$content = apply_filters( 'the_block_template_html', $content );
Copy link
Member

Choose a reason for hiding this comment

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

Another option would be to move this filter out from inside get_the_block_template_html() to instead apply inside of template-canvas.php on the entire HTML page:

diff --git a/src/wp-includes/template-canvas.php b/src/wp-includes/template-canvas.php
index 55f03267c8..f809b63e36 100644
--- a/src/wp-includes/template-canvas.php
+++ b/src/wp-includes/template-canvas.php
@@ -9,6 +9,20 @@
  * Get the template HTML.
  * This needs to run before <head> so that blocks can add scripts and styles in wp_head().
  */
+
+ob_start(
+	static function( $buffer ) {
+		/**
+		 * Filters the block template HTML content.
+		 *
+		 * @since 6.4.0
+		 *
+		 * @param string $content The entire block template HTML content.
+		 */
+		return apply_filters( 'the_block_template_html', $buffer );
+	}
+);
+
 $template_html = get_the_block_template_html();
 ?><!DOCTYPE html>
 <html <?php language_attributes(); ?>>

This would be a step toward implementing Core-43258/Core-58285. Granted, the filter would also need to apply for classic themes as well, and filters being added in this PR for block themes probably shouldn't then also apply to classic themes.

Copy link
Member Author

Choose a reason for hiding this comment

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

See my reply in #5188 (comment), I'm questioning what the benefit of that would be.

Copy link
Member

Choose a reason for hiding this comment

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

Benefits: #5188 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Based on the way @felixarntz implemented the filter, trying to move this higher in the execution order to run these after the whole template is generated could cause execution order problems. For example, blocks are being parsed as part of the filter in get_the_block_template_html(). It's important that some filters run prior to that parsing, and others run after. When blocks are parsed, they may enqueue scripts and styles to be printed in the head. I don't think we would be able to satisfy each of those requirements by a single filter that runs after the entire template-canvas.

I think adding an additional filter that would allow an output buffer to be implemented for the entire rendered content would be helpful, but doesn't necessarily negate the need to process template content prior to assembling the final HTML document.

Copy link
Member

Choose a reason for hiding this comment

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

As for a separate filter for the entire HTML document, I've proposed this as part of the full-page client-side navigation experiment in Gutenberg: WordPress/gutenberg#61212

$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>';
}

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

/*
* Most block themes omit the `core/query` and `core/post-template` blocks in their singular content templates.
Expand All @@ -246,20 +270,13 @@ function get_the_block_template_html() {
if ( is_singular() && 1 === $wp_query->post_count && have_posts() ) {
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 );
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 );
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 @@ -604,6 +604,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 );
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' );
add_filter( 'the_block_template_html', 'convert_smilies', 20 );

// 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 @@ -1822,6 +1822,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