wp/wp-includes/blocks/comment-date.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-date` block.
       
     4  *
       
     5  * @package WordPress
       
     6  */
       
     7 
       
     8 /**
       
     9  * Renders the `core/comment-date` 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 date.
       
    15  */
       
    16 function render_block_core_comment_date( $attributes, $content, $block ) {
       
    17 	if ( ! isset( $block->context['commentId'] ) ) {
       
    18 		return '';
       
    19 	}
       
    20 
       
    21 	$comment = get_comment( $block->context['commentId'] );
       
    22 	if ( empty( $comment ) ) {
       
    23 		return '';
       
    24 	}
       
    25 
       
    26 	$classes = '';
       
    27 
       
    28 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
       
    29 	$formatted_date     = get_comment_date(
       
    30 		isset( $attributes['format'] ) ? $attributes['format'] : '',
       
    31 		$comment
       
    32 	);
       
    33 	$link               = get_comment_link( $comment );
       
    34 
       
    35 	if ( ! empty( $attributes['isLink'] ) ) {
       
    36 		$formatted_date = sprintf( '<a href="%1s">%2s</a>', esc_url( $link ), $formatted_date );
       
    37 	}
       
    38 
       
    39 	return sprintf(
       
    40 		'<div %1$s><time datetime="%2$s">%3$s</time></div>',
       
    41 		$wrapper_attributes,
       
    42 		esc_attr( get_comment_date( 'c', $comment ) ),
       
    43 		$formatted_date
       
    44 	);
       
    45 }
       
    46 
       
    47 /**
       
    48  * Registers the `core/comment-date` block on the server.
       
    49  */
       
    50 function register_block_core_comment_date() {
       
    51 	register_block_type_from_metadata(
       
    52 		__DIR__ . '/comment-date',
       
    53 		array(
       
    54 			'render_callback' => 'render_block_core_comment_date',
       
    55 		)
       
    56 	);
       
    57 }
       
    58 add_action( 'init', 'register_block_core_comment_date' );