wp/wp-includes/blocks/comment-reply-link.php
changeset 19 3d72ae0968f4
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
       
     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  * @param array    $attributes Block attributes.
       
    12  * @param string   $content    Block default content.
       
    13  * @param WP_Block $block      Block instance.
       
    14  * @return string Return the post comment's reply link.
       
    15  */
       
    16 function render_block_core_comment_reply_link( $attributes, $content, $block ) {
       
    17 	if ( ! isset( $block->context['commentId'] ) ) {
       
    18 		return '';
       
    19 	}
       
    20 
       
    21 	$thread_comments = get_option( 'thread_comments' );
       
    22 	if ( ! $thread_comments ) {
       
    23 		return '';
       
    24 	}
       
    25 
       
    26 	$comment = get_comment( $block->context['commentId'] );
       
    27 	if ( empty( $comment ) ) {
       
    28 		return '';
       
    29 	}
       
    30 
       
    31 	$depth     = 1;
       
    32 	$max_depth = get_option( 'thread_comments_depth' );
       
    33 	$parent_id = $comment->comment_parent;
       
    34 
       
    35 	// Compute comment's depth iterating over its ancestors.
       
    36 	while ( ! empty( $parent_id ) ) {
       
    37 		$depth++;
       
    38 		$parent_id = get_comment( $parent_id )->comment_parent;
       
    39 	}
       
    40 
       
    41 	$comment_reply_link = get_comment_reply_link(
       
    42 		array(
       
    43 			'depth'     => $depth,
       
    44 			'max_depth' => $max_depth,
       
    45 		),
       
    46 		$comment
       
    47 	);
       
    48 
       
    49 	// Render nothing if the generated reply link is empty.
       
    50 	if ( empty( $comment_reply_link ) ) {
       
    51 		return;
       
    52 	}
       
    53 
       
    54 	$classes = '';
       
    55 	if ( isset( $attributes['textAlign'] ) ) {
       
    56 		$classes .= 'has-text-align-' . $attributes['textAlign'];
       
    57 	}
       
    58 
       
    59 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
       
    60 
       
    61 	return sprintf(
       
    62 		'<div %1$s>%2$s</div>',
       
    63 		$wrapper_attributes,
       
    64 		$comment_reply_link
       
    65 	);
       
    66 }
       
    67 
       
    68 /**
       
    69  * Registers the `core/comment-reply-link` block on the server.
       
    70  */
       
    71 function register_block_core_comment_reply_link() {
       
    72 	register_block_type_from_metadata(
       
    73 		__DIR__ . '/comment-reply-link',
       
    74 		array(
       
    75 			'render_callback' => 'render_block_core_comment_reply_link',
       
    76 		)
       
    77 	);
       
    78 }
       
    79 
       
    80 add_action( 'init', 'register_block_core_comment_reply_link' );