Нет описания

comment-template.php 3.3KB

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