From 106e6f7b8e0966b52d12df56025d78d3a40b3880 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Tue, 4 May 2021 11:51:48 +0100 Subject: [PATCH 1/3] Post Content: Prevent infinite recursion --- .../block-library/src/post-content/edit.js | 40 +++++++++++++++---- .../block-library/src/post-content/index.php | 33 ++++++++++++++- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index fcdb14198d7b1..88ee3d2098b63 100644 --- a/packages/block-library/src/post-content/edit.js +++ b/packages/block-library/src/post-content/edit.js @@ -7,7 +7,9 @@ import { useBlockProps, __experimentalUseInnerBlocksProps as useInnerBlocksProps, __experimentalUseEditorFeature as useEditorFeature, + __experimentalUseNoRecursiveRenders as useNoRecursiveRenders, store as blockEditorStore, + Warning, } from '@wordpress/block-editor'; import { useEntityBlockEditor } from '@wordpress/core-data'; @@ -55,19 +57,41 @@ function Placeholder() { ); } +function RecursionError() { + const blockProps = useBlockProps(); + return ( +
+ + { __( 'Block cannot be rendered inside itself.' ) } + +
+ ); +} + export default function PostContentEdit( { context: { postId: contextPostId, postType: contextPostType }, attributes, } ) { const { layout = {} } = attributes; + const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders( + contextPostId + ); - return contextPostId && contextPostType ? ( - - ) : ( - + if ( contextPostId && contextPostType && hasAlreadyRendered ) { + return ; + } + + return ( + + { contextPostId && contextPostType ? ( + + ) : ( + + ) } + ); } diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index d92bb8082e715..cc8a0a7b78221 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -14,25 +14,54 @@ * @return string Returns the filtered post content of the current post. */ function render_block_core_post_content( $attributes, $content, $block ) { + static $seen_ids = array(); + if ( ! isset( $block->context['postId'] ) ) { return ''; } + $post_id = $block->context['postId']; + + if ( isset( $seen_ids[ $post_id ] ) ) { + if ( ! is_admin() ) { + trigger_error( + sprintf( + // translators: %s are the block attributes. + __( 'Could not render Post Content block with the attributes: %s. Block cannot be rendered inside itself.' ), + wp_json_encode( $attributes ) + ), + E_USER_WARNING + ); + } + + $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]' ) : + ''; + } + + $seen_ids[ $post_id ] = true; + if ( ! in_the_loop() ) { the_post(); } - $content = get_the_content( null, false, $block->context['postId'] ); + $content = get_the_content( null, false, $post_id ); if ( empty( $content ) ) { + unset( $seen_ids[ $post_id ] ); return ''; } $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) ); + $content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ); + unset( $seen_ids[ $post_id ] ); return ( '
' . - apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ) . + $content . '
' ); } From 45e9f6967a3b8cf5afdf9b9c1b1923948bee2d91 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Tue, 4 May 2021 12:03:37 +0100 Subject: [PATCH 2/3] Report post ID instead of attributes in error message --- packages/block-library/src/post-content/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index cc8a0a7b78221..56e84992bf524 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -26,9 +26,9 @@ function render_block_core_post_content( $attributes, $content, $block ) { if ( ! is_admin() ) { trigger_error( sprintf( - // translators: %s are the block attributes. - __( 'Could not render Post Content block with the attributes: %s. Block cannot be rendered inside itself.' ), - wp_json_encode( $attributes ) + // 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 ); From bdd1f44c93a02ca61135ef79e5034770766704f6 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Tue, 4 May 2021 20:08:12 +0100 Subject: [PATCH 3/3] Reference documentation for filter 'the_content' --- packages/block-library/src/post-content/index.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/post-content/index.php b/packages/block-library/src/post-content/index.php index 56e84992bf524..253752d2acafc 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -56,7 +56,8 @@ function render_block_core_post_content( $attributes, $content, $block ) { } $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) ); - $content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ); + /** This filter is documented in wp-includes/post-template.php */ + $content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ); unset( $seen_ids[ $post_id ] ); return (