comment-reply-link.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/comment-reply-link` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/comment-reply-link` block on the server.
  9. *
  10. * @param array $attributes Block attributes.
  11. * @param string $content Block default content.
  12. * @param WP_Block $block Block instance.
  13. * @return string Return the post comment's reply link.
  14. */
  15. function render_block_core_comment_reply_link( $attributes, $content, $block ) {
  16. if ( ! isset( $block->context['commentId'] ) ) {
  17. return '';
  18. }
  19. $thread_comments = get_option( 'thread_comments' );
  20. if ( ! $thread_comments ) {
  21. return '';
  22. }
  23. $comment = get_comment( $block->context['commentId'] );
  24. if ( empty( $comment ) ) {
  25. return '';
  26. }
  27. $depth = 1;
  28. $max_depth = get_option( 'thread_comments_depth' );
  29. $parent_id = $comment->comment_parent;
  30. // Compute comment's depth iterating over its ancestors.
  31. while ( ! empty( $parent_id ) ) {
  32. $depth++;
  33. $parent_id = get_comment( $parent_id )->comment_parent;
  34. }
  35. $comment_reply_link = get_comment_reply_link(
  36. array(
  37. 'depth' => $depth,
  38. 'max_depth' => $max_depth,
  39. ),
  40. $comment
  41. );
  42. // Render nothing if the generated reply link is empty.
  43. if ( empty( $comment_reply_link ) ) {
  44. return;
  45. }
  46. $classes = '';
  47. if ( isset( $attributes['textAlign'] ) ) {
  48. $classes .= 'has-text-align-' . $attributes['textAlign'];
  49. }
  50. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  51. return sprintf(
  52. '<div %1$s>%2$s</div>',
  53. $wrapper_attributes,
  54. $comment_reply_link
  55. );
  56. }
  57. /**
  58. * Registers the `core/comment-reply-link` block on the server.
  59. */
  60. function register_block_core_comment_reply_link() {
  61. register_block_type_from_metadata(
  62. __DIR__ . '/comment-reply-link',
  63. array(
  64. 'render_callback' => 'render_block_core_comment_reply_link',
  65. )
  66. );
  67. }
  68. add_action( 'init', 'register_block_core_comment_reply_link' );