From 0e6dc6a72cf1ad0c3e4a8b28626538097221c5cc Mon Sep 17 00:00:00 2001 From: Nik Tsekouras Date: Thu, 1 Jul 2021 12:41:36 +0300 Subject: [PATCH] [Block Library]: Less warnings when blocks try to render themselves. (#33032) * Less warnings when blocks try to render themeselves. * rename util to gutenberg_is_rest_api_request * Log only if is `is_debug` and not in admin or REST request * remove log entirely --- packages/block-library/src/block/index.php | 11 ----------- .../block-library/src/post-content/index.php | 16 ++++------------ .../block-library/src/template-part/index.php | 12 +----------- 3 files changed, 5 insertions(+), 34 deletions(-) diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index 3613680e9e5159..8b0227f43c45fd 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -25,17 +25,6 @@ function render_block_core_block( $attributes ) { } if ( isset( $seen_refs[ $attributes['ref'] ] ) ) { - if ( ! is_admin() ) { - trigger_error( - sprintf( - // translators: %s is the user-provided title of the reusable block. - __( 'Could not render Reusable Block %s. Block cannot be rendered inside itself.' ), - $reusable_block->post_title - ), - E_USER_WARNING - ); - } - // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent // is set in `wp_debug_mode()`. $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 4c0c4965647f11..400e3068dfa0a9 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -23,19 +23,11 @@ function render_block_core_post_content( $attributes, $content, $block ) { $post_id = $block->context['postId']; if ( isset( $seen_ids[ $post_id ] ) ) { - if ( ! is_admin() ) { - trigger_error( - sprintf( - // translators: %s is a post ID (integer). - __( 'Could not render Post Content block with post ID: %s. Block cannot be rendered inside itself.' ), - $post_id - ), - E_USER_WARNING - ); - } - + // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent + // is set in `wp_debug_mode()`. $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; + return $is_debug ? // translators: Visible only in the front end, this warning takes the place of a faulty block. __( '[block rendering halted]' ) : @@ -44,7 +36,7 @@ function render_block_core_post_content( $attributes, $content, $block ) { $seen_ids[ $post_id ] = true; - if ( ! in_the_loop() ) { + if ( ! in_the_loop() && have_posts() ) { the_post(); } diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index c210d1a123be69..4f52b0f6d1738a 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -73,21 +73,11 @@ function render_block_core_template_part( $attributes ) { } if ( isset( $seen_ids[ $template_part_id ] ) ) { - if ( ! is_admin() ) { - trigger_error( - sprintf( - // translators: %s are the block attributes. - __( 'Could not render Template Part block with the attributes: %s. Block cannot be rendered inside itself.' ), - wp_json_encode( $attributes ) - ), - E_USER_WARNING - ); - } - // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent // is set in `wp_debug_mode()`. $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; + return $is_debug ? // translators: Visible only in the front end, this warning takes the place of a faulty block. __( '[block rendering halted]' ) :