comment-template.php 3.6 KB

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