-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( ']]>', ']]>', $content ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( ']]>', ']]>', $content ) ) . | ||
$content . | ||
'</div>' | ||
); | ||
} | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.This assumes
apiVersion: 2
, though, because of the use ofuseBlockProps
. Is that acceptable?There was a problem hiding this comment.
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.