From aac5aaf762a873a8331a0d714e4a550bffadd782 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 30 Jun 2021 10:45:07 +0300 Subject: [PATCH] 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 1dc1f19163849..12ee435e88f5b 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 3f6629f90ac44..ff0fc8809d16d 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 e7229c558a75a..abac410913d00 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 4e0aaff72edec..271aca8721aa4 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.