From 5b6555a3845818624a8dab93d57325e11e1c04e1 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 1 Sep 2023 17:32:16 +0000 Subject: [PATCH] Editor: Ensure main query loop is entered for singular content in block themes. Block themes currently lack the means to trigger the main query loop for singular content, since they cannot reasonably use the `core/query` and `core/post-template` blocks which are intended only for displaying a list of posts. So far, the missing main query loop on singular block templates has been worked around by enforcing the loop in certain `core/post-*` blocks, which however causes other bugs. This changeset ensures that the main query loop is still started for singular block theme templates, by wrapping the entire template into the loop, which will by definition only have a single cycle as it only encompasses a single post. This is currently the most reliable solution, since even if there were blocks to properly trigger the main query loop on singular content, it would be unrealistic to expect all existing block themes to update their templates accordingly. It may be revisited in the future. Props gziolo, youknowriad, joemcgill, costdev, mukesh27, flixos90. Fixes #58154. See #59225, #58027. Built from https://develop.svn.wordpress.org/trunk@56507 git-svn-id: http://core.svn.wordpress.org/trunk@56019 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-template.php | 29 ++++++++++++++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/wp-includes/block-template.php b/wp-includes/block-template.php index 794aec9120..ffed0e0436 100644 --- a/wp-includes/block-template.php +++ b/wp-includes/block-template.php @@ -210,12 +210,12 @@ function _block_template_render_title_tag() { * * @global string $_wp_current_template_content * @global WP_Embed $wp_embed + * @global WP_Query $wp_query * * @return string Block template markup. */ function get_the_block_template_html() { - global $_wp_current_template_content; - global $wp_embed; + global $_wp_current_template_content, $wp_embed, $wp_query; if ( ! $_wp_current_template_content ) { if ( is_user_logged_in() ) { @@ -228,7 +228,30 @@ function get_the_block_template_html() { $content = $wp_embed->autoembed( $content ); $content = shortcode_unautop( $content ); $content = do_shortcode( $content ); - $content = do_blocks( $content ); + + /* + * Most block themes omit the `core/query` and `core/post-template` blocks in their singular content templates. + * While this technically still works since singular content templates are always for only one post, it results in + * the main query loop never being entered which causes bugs in core and the plugin ecosystem. + * + * The workaround below ensures that the loop is started even for those singular templates. The while loop will by + * definition only go through a single iteration, i.e. `do_blocks()` is only called once. Additional safeguard + * checks are included to ensure the main query loop has not been tampered with and really only encompasses a + * single post. + * + * Even if the block template contained a `core/query` and `core/post-template` block referencing the main query + * loop, it would not cause errors since it would use a cloned instance and go through the same loop of a single + * post, within the actual main query loop. + */ + if ( is_singular() && 1 === $wp_query->post_count && have_posts() ) { + while ( have_posts() ) { + the_post(); + $content = do_blocks( $content ); + } + } else { + $content = do_blocks( $content ); + } + $content = wptexturize( $content ); $content = convert_smilies( $content ); $content = wp_filter_content_tags( $content, 'template' ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 803d27af47..34ec942cb1 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56506'; +$wp_version = '6.4-alpha-56507'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.