wp/wp-includes/blocks/comment-template.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
--- a/wp/wp-includes/blocks/comment-template.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/blocks/comment-template.php	Fri Sep 05 18:40:08 2025 +0200
@@ -8,6 +8,8 @@
 /**
  * Function that recursively renders a list of nested comments.
  *
+ * @since 6.3.0 Changed render_block_context priority to `1`.
+ *
  * @global int $comment_depth
  *
  * @param WP_Comment[] $comments        The array of comments.
@@ -16,6 +18,8 @@
  */
 function block_core_comment_template_render_comments( $comments, $block ) {
 	global $comment_depth;
+	$thread_comments       = get_option( 'thread_comments' );
+	$thread_comments_depth = get_option( 'thread_comments_depth' );
 
 	if ( empty( $comment_depth ) ) {
 		$comment_depth = 1;
@@ -23,13 +27,29 @@
 
 	$content = '';
 	foreach ( $comments as $comment ) {
+		$comment_id           = $comment->comment_ID;
+		$filter_block_context = static function ( $context ) use ( $comment_id ) {
+			$context['commentId'] = $comment_id;
+			return $context;
+		};
 
-		$block_content = ( new WP_Block(
-			$block->parsed_block,
-			array(
-				'commentId' => $comment->comment_ID,
-			)
-		) )->render( array( 'dynamic' => false ) );
+		/*
+		 * We set commentId context through the `render_block_context` filter so
+		 * that dynamically inserted blocks (at `render_block` filter stage)
+		 * will also receive that context.
+		 *
+		 * Use an early priority to so that other 'render_block_context' filters
+		 * have access to the values.
+		 */
+		add_filter( 'render_block_context', $filter_block_context, 1 );
+
+		/*
+		 * We construct a new WP_Block instance from the parsed block so that
+		 * it'll receive any changes made by the `render_block_data` filter.
+		 */
+		$block_content = ( new WP_Block( $block->parsed_block ) )->render( array( 'dynamic' => false ) );
+
+		remove_filter( 'render_block_context', $filter_block_context, 1 );
 
 		$children = $comment->get_children();
 
@@ -46,26 +66,34 @@
 
 		// If the comment has children, recurse to create the HTML for the nested
 		// comments.
-		if ( ! empty( $children ) ) {
-			$comment_depth += 1;
-			$inner_content  = block_core_comment_template_render_comments(
-				$children,
-				$block
-			);
-			$block_content .= sprintf( '<ol>%1$s</ol>', $inner_content );
-			$comment_depth -= 1;
+		if ( ! empty( $children ) && ! empty( $thread_comments ) ) {
+			if ( $comment_depth < $thread_comments_depth ) {
+				++$comment_depth;
+				$inner_content  = block_core_comment_template_render_comments(
+					$children,
+					$block
+				);
+				$block_content .= sprintf( '<ol>%1$s</ol>', $inner_content );
+				--$comment_depth;
+			} else {
+				$block_content .= block_core_comment_template_render_comments(
+					$children,
+					$block
+				);
+			}
 		}
 
 		$content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content );
 	}
 
 	return $content;
-
 }
 
 /**
  * Renders the `core/comment-template` block on the server.
  *
+ * @since 6.0.0
+ *
  * @param array    $attributes Block attributes.
  * @param string   $content    Block default content.
  * @param WP_Block $block      Block instance.
@@ -110,6 +138,8 @@
 
 /**
  * Registers the `core/comment-template` block on the server.
+ *
+ * @since 6.0.0
  */
 function register_block_core_comment_template() {
 	register_block_type_from_metadata(