Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Content: Prevent infinite recursion #31455

Merged
merged 3 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -55,19 +57,41 @@ function Placeholder() {
);
}

function RecursionError() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, maybe something that useNoRecursiveRenders should also expose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, interesting. Btw, I made it a separate component because otherwise some of the ref-juggling hooks inside useBlockProps — specifically, useScrollIntoView — throw errors.

something that useNoRecursiveRenders should also expose?

This assumes apiVersion: 2, though, because of the use of useBlockProps. Is that acceptable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to confirm why useScrollIntoView causes issues, maybe it was called too late, React is very strict about all the rules of hooks? I don't mind having it in its own component though. I mostly curious to confirm it isn't some sort of bug with the hook.

const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<Warning>
{ __( 'Block cannot be rendered inside itself.' ) }
</Warning>
</div>
);
}

export default function PostContentEdit( {
context: { postId: contextPostId, postType: contextPostType },
attributes,
} ) {
const { layout = {} } = attributes;
const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders(
contextPostId
);

return contextPostId && contextPostType ? (
<Content
postType={ contextPostType }
postId={ contextPostId }
layout={ layout }
/>
) : (
<Placeholder />
if ( contextPostId && contextPostType && hasAlreadyRendered ) {
return <RecursionError />;
}

return (
<RecursionProvider>
{ contextPostId && contextPostType ? (
<Content
postType={ contextPostType }
postId={ contextPostId }
layout={ layout }
/>
) : (
<Placeholder />
) }
</RecursionProvider>
);
}
33 changes: 31 additions & 2 deletions packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
);
}

$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( ']]>', ']]&gt;', $content ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to add a comment with a reference to where the filter is documented. It's a common pattern used in WordPress core and this file is going to be soon exposed there as well.

Completely unrelated to this PR but it should be nice to fix here since it isn't that much work.

unset( $seen_ids[ $post_id ] );

return (
'<div ' . $wrapper_attributes . '>' .
apply_filters( 'the_content', str_replace( ']]>', ']]&gt;', $content ) ) .
$content .
'</div>'
);
}
Expand Down