| @@ -7,13 +7,23 @@ | ||
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * Function that recursively renders a list of nested comments. |
| 10 | 10 | * |
| 11 | - * @param WP_Comment[] $comments The array of comments. | |
| 11 | + * @global int $comment_depth | |
| 12 | + * | |
| 13 | + * @param WP_Comment[] $comments The array of comments. | |
| 12 | 14 | * @param WP_Block $block Block instance. |
| 13 | 15 | * @return string |
| 14 | 16 | */ |
| 15 | 17 | function gutenberg_block_core_comment_template_render_comments( $comments, $block ) { |
| 18 | + global $comment_depth; | |
| 19 | + $thread_comments = get_option( 'thread_comments' ); | |
| 20 | + $thread_comments_depth = get_option( 'thread_comments_depth' ); | |
| 21 | + | |
| 22 | + if ( empty( $comment_depth ) ) { | |
| 23 | + $comment_depth = 1; | |
| 24 | + } | |
| 25 | + | |
| 16 | 26 | $content = ''; |
| 17 | 27 | foreach ( $comments as $comment ) { |
| 18 | 28 | |
| 19 | 29 | $block_content = ( new WP_Block( |
| @@ -24,23 +34,43 @@ | ||
| 24 | 34 | ) )->render( array( 'dynamic' => false ) ); |
| 25 | 35 | |
| 26 | 36 | $children = $comment->get_children(); |
| 27 | 37 | |
| 38 | + /* | |
| 39 | + * We need to create the CSS classes BEFORE recursing into the children. | |
| 40 | + * This is because comment_class() uses globals like `$comment_alt` | |
| 41 | + * and `$comment_thread_alt` which are order-sensitive. | |
| 42 | + * | |
| 43 | + * The `false` parameter at the end means that we do NOT want the function | |
| 44 | + * to `echo` the output but to return a string. | |
| 45 | + * See https://developer.wordpress.org/reference/functions/comment_class/#parameters. | |
| 46 | + */ | |
| 47 | + $comment_classes = comment_class( '', $comment->comment_ID, $comment->comment_post_ID, false ); | |
| 48 | + | |
| 28 | 49 | // If the comment has children, recurse to create the HTML for the nested |
| 29 | 50 | // comments. |
| 30 | - if ( ! empty( $children ) ) { | |
| 31 | - $inner_content = gutenberg_block_core_comment_template_render_comments( | |
| 32 | - $children, | |
| 33 | - $block | |
| 34 | - ); | |
| 35 | - $block_content .= sprintf( '<ol>%1$s</ol>', $inner_content ); | |
| 51 | + if ( ! empty( $children ) && ! empty( $thread_comments ) ) { | |
| 52 | + if ( $comment_depth < $thread_comments_depth ) { | |
| 53 | + ++$comment_depth; | |
| 54 | + $inner_content = gutenberg_block_core_comment_template_render_comments( | |
| 55 | + $children, | |
| 56 | + $block | |
| 57 | + ); | |
| 58 | + $block_content .= sprintf( '<ol>%1$s</ol>', $inner_content ); | |
| 59 | + --$comment_depth; | |
| 60 | + } else { | |
| 61 | + $inner_content = gutenberg_block_core_comment_template_render_comments( | |
| 62 | + $children, | |
| 63 | + $block | |
| 64 | + ); | |
| 65 | + $block_content .= sprintf( $inner_content ); | |
| 66 | + } | |
| 36 | 67 | } |
| 37 | 68 | |
| 38 | - $content .= '<li>' . $block_content . '</li>'; | |
| 69 | + $content .= sprintf( '<li id="comment-%1$s" %2$s>%3$s</li>', $comment->comment_ID, $comment_classes, $block_content ); | |
| 39 | 70 | } |
| 40 | 71 | |
| 41 | 72 | return $content; |
| 42 | - | |
| 43 | 73 | } |
| 44 | 74 | |
| 45 | 75 | /** |
| 46 | 76 | * Renders the `core/comment-template` block on the server. |
| @@ -57,8 +87,12 @@ | ||
| 57 | 87 | if ( empty( $block->context['postId'] ) ) { |
| 58 | 88 | return ''; |
| 59 | 89 | } |
| 60 | 90 | |
| 91 | + if ( post_password_required( $block->context['postId'] ) ) { | |
| 92 | + return; | |
| 93 | + } | |
| 94 | + | |
| 61 | 95 | $comment_query = new WP_Comment_Query( |
| 62 | 96 | build_comment_query_vars_from_block( $block ) |
| 63 | 97 | ); |
| 64 | 98 | |
| @@ -65,8 +99,14 @@ | ||
| 65 | 99 | // Get an array of comments for the current post. |
| 66 | 100 | $comments = $comment_query->get_comments(); |
| 67 | 101 | if ( count( $comments ) === 0 ) { |
| 68 | 102 | return ''; |
| 103 | + } | |
| 104 | + | |
| 105 | + $comment_order = get_option( 'comment_order' ); | |
| 106 | + | |
| 107 | + if ( 'desc' === $comment_order ) { | |
| 108 | + $comments = array_reverse( $comments ); | |
| 69 | 109 | } |
| 70 | 110 | |
| 71 | 111 | $wrapper_attributes = get_block_wrapper_attributes(); |
| 72 | 112 | |