Skip to content

Commit

Permalink
Log only if is is_debug and not in admin or REST request
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Jun 30, 2021
1 parent 35eca19 commit aac5aaf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 62 deletions.
20 changes: 0 additions & 20 deletions lib/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
28 changes: 14 additions & 14 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <strong>%s</strong>. 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 <strong>%s</strong>. 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 ) ) {
Expand Down
31 changes: 17 additions & 14 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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: <code>%s</code>. 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: <code>%s</code>. 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;
Expand Down
29 changes: 15 additions & 14 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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: <code>%s</code>. 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: <code>%s</code>. 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.
Expand Down

0 comments on commit aac5aaf

Please sign in to comment.