diff --git a/packages/block-library/src/post-content/edit.js b/packages/block-library/src/post-content/edit.js index fcdb14198d7b10..88ee3d2098b63d 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 d92bb8082e7151..253752d2acafc6 100644 --- a/packages/block-library/src/post-content/index.php +++ b/packages/block-library/src/post-content/index.php @@ -14,25 +14,55 @@ * @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 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 + ); + } + + $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' ) ); + /** This filter is documented in wp-includes/post-template.php */ + $content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ); + unset( $seen_ids[ $post_id ] ); return ( '
' . - apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ) . + $content . '
' ); }