Skip to content

Commit

Permalink
Revert "Revert "Comment template: Call comment_class() before recur…
Browse files Browse the repository at this point in the history
…sing into child comments (#40455)""

This reverts commit 922abd3.
  • Loading branch information
ockham committed Apr 25, 2022
1 parent 922abd3 commit f36c69a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/block-library/src/comment-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/**
* Function that recursively renders a list of nested comments.
*
* @global int $comment_depth
*
* @param WP_Comment[] $comments The array of comments.
* @param WP_Block $block Block instance.
* @return string
Expand All @@ -31,6 +33,17 @@ function block_core_comment_template_render_comments( $comments, $block ) {

$children = $comment->get_children();

/*
* We need to create the CSS classes BEFORE recursing into the children.
* This is because comment_class() uses globals like `$comment_alt`
* and `$comment_thread_alt` which are order-sensitive.
*
* The `false` parameter at the end means that we do NOT want the function
* to `echo` the output but to return a string.
* See https://developer.wordpress.org/reference/functions/comment_class/#parameters.
*/
$comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false );

// If the comment has children, recurse to create the HTML for the nested
// comments.
if ( ! empty( $children ) ) {
Expand All @@ -43,10 +56,6 @@ function block_core_comment_template_render_comments( $comments, $block ) {
$comment_depth -= 1;
}

// The `false` parameter at the end means that we do NOT want the function to `echo` the output but to return a string.
// See https://developer.wordpress.org/reference/functions/comment_class/#parameters.
$comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false );

$content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content );
}

Expand Down

0 comments on commit f36c69a

Please sign in to comment.