From 1723b8f148ca5e8096b6f36207ea613681e4daa8 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 18 Sep 2019 13:54:36 +0200 Subject: [PATCH] Detect current post more reliably in core/post-title and core/post-content renderers. --- packages/block-library/src/post-content/index.php | 14 +++++++++----- packages/block-library/src/post-title/index.php | 11 ++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 4ed0103e8229c1..1a36344795ffe9 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -8,19 +8,23 @@ /** * Renders the `core/post-content` block on the server. * + * @global WP_Query $wp_query WordPress query object. + * * @return string Returns the filtered post content of the current post. */ function render_block_core_post_content() { - // TODO: Without this temporary fix, an infinite loop can occur. - if ( is_admin() || defined( 'REST_REQUEST' ) ) { - return ''; - } + global $wp_query; - if ( ! in_the_loop() ) { + if ( isset( $wp_query ) && ! in_the_loop() ) { rewind_posts(); the_post(); } + // Ensure no infinite loop occurs. + if ( 'wp_template' === get_post_type() ) { + return ''; + } + return '
' . apply_filters( 'the_content', str_replace( ']]>', ']]>', get_the_content() ) ) . '
'; } diff --git a/packages/block-library/src/post-title/index.php b/packages/block-library/src/post-title/index.php index 3c8110a0af738a..ca8d8a49094df9 100644 --- a/packages/block-library/src/post-title/index.php +++ b/packages/block-library/src/post-title/index.php @@ -8,14 +8,23 @@ /** * Renders the `core/post-title` block on the server. * + * @global WP_Query $wp_query WordPress query object. + * * @return string Returns the filtered post title for the current post wrapped inside "h1" tags. */ function render_block_core_post_title() { - if ( ! in_the_loop() ) { + global $wp_query; + + if ( isset( $wp_query ) && ! in_the_loop() ) { rewind_posts(); the_post(); } + // Ensure no infinite loop occurs. + if ( 'wp_template' === get_post_type() ) { + return ''; + } + return the_title( '

', '

', false ); }