From 824028054ce1ee55f01444378e584f48b40ad5e0 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 28 Jun 2021 14:29:02 +0300 Subject: [PATCH 1/4] Less warnings when blocks try to render themeselves. --- lib/utils.php | 20 +++++++++++++++++++ packages/block-library/src/block/index.php | 2 +- .../block-library/src/post-content/index.php | 4 ++-- .../block-library/src/template-part/index.php | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/utils.php b/lib/utils.php index 12ee435e88f5b0..0c1f5943e21692 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -137,3 +137,23 @@ function gutenberg_experimental_to_kebab_case( $string ) { return strtolower( implode( '-', $matches[0] ) ); //phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase } + + +/** + * Returns true if the request is a non-legacy REST API request. + * + * Legacy REST requests should still run some extra code for backwards compatibility. + * + * @todo: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061. + * + * @return bool + */ +function is_rest_api_request() { + if ( empty( $_SERVER['REQUEST_URI'] ) ) { + return false; + } + + $rest_prefix = trailingslashit( rest_get_url_prefix() ); + $is_rest_api_request = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix ) ); + return $is_rest_api_request; +} diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index 3613680e9e5159..576e8d5e8819a6 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -25,7 +25,7 @@ function render_block_core_block( $attributes ) { } if ( isset( $seen_refs[ $attributes['ref'] ] ) ) { - if ( ! is_admin() ) { + if ( ! is_admin() && ! is_rest_api_request() ) { trigger_error( sprintf( // translators: %s is the user-provided title of the reusable block. diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 4c0c4965647f11..4683615e198f7a 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -23,7 +23,7 @@ function render_block_core_post_content( $attributes, $content, $block ) { $post_id = $block->context['postId']; if ( isset( $seen_ids[ $post_id ] ) ) { - if ( ! is_admin() ) { + if ( ! is_admin() && ! is_rest_api_request() ) { trigger_error( sprintf( // translators: %s is a post ID (integer). @@ -44,7 +44,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..5b416300f1384e 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -73,7 +73,7 @@ function render_block_core_template_part( $attributes ) { } if ( isset( $seen_ids[ $template_part_id ] ) ) { - if ( ! is_admin() ) { + if ( ! is_admin() && ! is_rest_api_request() ) { trigger_error( sprintf( // translators: %s are the block attributes. From 69e68ce2b12dd8c30075af97c58e29ac59bf5065 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 29 Jun 2021 08:06:03 +0300 Subject: [PATCH 2/4] rename util to gutenberg_is_rest_api_request --- lib/utils.php | 2 +- packages/block-library/src/block/index.php | 2 +- packages/block-library/src/post-content/index.php | 2 +- packages/block-library/src/template-part/index.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils.php b/lib/utils.php index 0c1f5943e21692..1dc1f191638491 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -148,7 +148,7 @@ function gutenberg_experimental_to_kebab_case( $string ) { * * @return bool */ -function is_rest_api_request() { +function gutenberg_is_rest_api_request() { if ( empty( $_SERVER['REQUEST_URI'] ) ) { return false; } diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index 576e8d5e8819a6..3f6629f90ac447 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -25,7 +25,7 @@ function render_block_core_block( $attributes ) { } if ( isset( $seen_refs[ $attributes['ref'] ] ) ) { - if ( ! is_admin() && ! is_rest_api_request() ) { + if ( ! is_admin() && ! gutenberg_is_rest_api_request() ) { trigger_error( sprintf( // translators: %s is the user-provided title of the reusable block. diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 4683615e198f7a..e7229c558a75ae 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -23,7 +23,7 @@ function render_block_core_post_content( $attributes, $content, $block ) { $post_id = $block->context['postId']; if ( isset( $seen_ids[ $post_id ] ) ) { - if ( ! is_admin() && ! is_rest_api_request() ) { + if ( ! is_admin() && ! gutenberg_is_rest_api_request() ) { trigger_error( sprintf( // translators: %s is a post ID (integer). diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 5b416300f1384e..4e0aaff72edec7 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -73,7 +73,7 @@ function render_block_core_template_part( $attributes ) { } if ( isset( $seen_ids[ $template_part_id ] ) ) { - if ( ! is_admin() && ! is_rest_api_request() ) { + if ( ! is_admin() && ! gutenberg_is_rest_api_request() ) { trigger_error( sprintf( // translators: %s are the block attributes. From 7d7ee8fb96c07038edda275cc4c2d90ab343336b Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 30 Jun 2021 10:45:07 +0300 Subject: [PATCH 3/4] Log only if is `is_debug` and not in admin or REST request --- lib/utils.php | 20 ------------ packages/block-library/src/block/index.php | 28 ++++++++--------- .../block-library/src/post-content/index.php | 31 ++++++++++--------- .../block-library/src/template-part/index.php | 29 ++++++++--------- 4 files changed, 46 insertions(+), 62 deletions(-) diff --git a/lib/utils.php b/lib/utils.php index 1dc1f191638491..12ee435e88f5b0 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -137,23 +137,3 @@ function gutenberg_experimental_to_kebab_case( $string ) { return strtolower( implode( '-', $matches[0] ) ); //phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase } - - -/** - * Returns true if the request is a non-legacy REST API request. - * - * Legacy REST requests should still run some extra code for backwards compatibility. - * - * @todo: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061. - * - * @return bool - */ -function gutenberg_is_rest_api_request() { - if ( empty( $_SERVER['REQUEST_URI'] ) ) { - return false; - } - - $rest_prefix = trailingslashit( rest_get_url_prefix() ); - $is_rest_api_request = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix ) ); - return $is_rest_api_request; -} diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index 3f6629f90ac447..ff0fc8809d16d8 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -25,26 +25,26 @@ function render_block_core_block( $attributes ) { } if ( isset( $seen_refs[ $attributes['ref'] ] ) ) { - if ( ! is_admin() && ! gutenberg_is_rest_api_request() ) { - 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 && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; - return $is_debug ? + if ( $is_debug ) { + if ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { + 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 + ); + } // translators: Visible only in the front end, this warning takes the place of a faulty block. - __( '[block rendering halted]' ) : - ''; + return __( '[block rendering halted]' ); + } + return; } if ( 'publish' !== $reusable_block->post_status || ! empty( $reusable_block->post_password ) ) { diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index e7229c558a75ae..abac410913d003 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -23,23 +23,26 @@ function render_block_core_post_content( $attributes, $content, $block ) { $post_id = $block->context['postId']; if ( isset( $seen_ids[ $post_id ] ) ) { - if ( ! is_admin() && ! gutenberg_is_rest_api_request() ) { - 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 ? + + if ( $is_debug ) { + if ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { + 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 + ); + } // translators: Visible only in the front end, this warning takes the place of a faulty block. - __( '[block rendering halted]' ) : - ''; + return __( '[block rendering halted]' ); + } + return; } $seen_ids[ $post_id ] = true; diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 4e0aaff72edec7..271aca8721aa41 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -73,25 +73,26 @@ function render_block_core_template_part( $attributes ) { } if ( isset( $seen_ids[ $template_part_id ] ) ) { - if ( ! is_admin() && ! gutenberg_is_rest_api_request() ) { - 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 ? + + if ( $is_debug ) { + if ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { + 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 + ); + } // translators: Visible only in the front end, this warning takes the place of a faulty block. - __( '[block rendering halted]' ) : - ''; + return __( '[block rendering halted]' ); + } + return; } // Run through the actions that are typically taken on the_content. From 726f53869384cbdc0ce5541ad9a1670e33506d7c Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 1 Jul 2021 08:38:28 +0300 Subject: [PATCH 4/4] remove log entirely --- packages/block-library/src/block/index.php | 17 +++-------------- .../block-library/src/post-content/index.php | 17 +++-------------- .../block-library/src/template-part/index.php | 17 +++-------------- 3 files changed, 9 insertions(+), 42 deletions(-) diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index ff0fc8809d16d8..8b0227f43c45fd 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -30,21 +30,10 @@ function render_block_core_block( $attributes ) { $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; - if ( $is_debug ) { - if ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { - 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 - ); - } + return $is_debug ? // translators: Visible only in the front end, this warning takes the place of a faulty block. - return __( '[block rendering halted]' ); - } - return; + __( '[block rendering halted]' ) : + ''; } if ( 'publish' !== $reusable_block->post_status || ! empty( $reusable_block->post_password ) ) { diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index abac410913d003..400e3068dfa0a9 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -28,21 +28,10 @@ function render_block_core_post_content( $attributes, $content, $block ) { $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; - if ( $is_debug ) { - if ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { - 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 - ); - } + return $is_debug ? // translators: Visible only in the front end, this warning takes the place of a faulty block. - return __( '[block rendering halted]' ); - } - return; + __( '[block rendering halted]' ) : + ''; } $seen_ids[ $post_id ] = true; diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 271aca8721aa41..4f52b0f6d1738a 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -78,21 +78,10 @@ function render_block_core_template_part( $attributes ) { $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; - if ( $is_debug ) { - if ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { - 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 - ); - } + return $is_debug ? // translators: Visible only in the front end, this warning takes the place of a faulty block. - return __( '[block rendering halted]' ); - } - return; + __( '[block rendering halted]' ) : + ''; } // Run through the actions that are typically taken on the_content.