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