wp/wp-includes/blocks/post-date.php
changeset 18 be944660c56a
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
       
     1 <?php
       
     2 /**
       
     3  * Server-side rendering of the `core/post-date` block.
       
     4  *
       
     5  * @package WordPress
       
     6  */
       
     7 
       
     8 /**
       
     9  * Renders the `core/post-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 Returns the filtered post date for the current post wrapped inside "time" tags.
       
    15  */
       
    16 function render_block_core_post_date( $attributes, $content, $block ) {
       
    17 	if ( ! isset( $block->context['postId'] ) ) {
       
    18 		return '';
       
    19 	}
       
    20 
       
    21 	$post_ID            = $block->context['postId'];
       
    22 	$align_class_name   = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
       
    23 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
       
    24 	$formatted_date     = get_the_date( isset( $attributes['format'] ) ? $attributes['format'] : '', $post_ID );
       
    25 	if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
       
    26 		$formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date );
       
    27 	}
       
    28 
       
    29 	return sprintf(
       
    30 		'<div %1$s><time datetime="%2$s">%3$s</time></div>',
       
    31 		$wrapper_attributes,
       
    32 		get_the_date( 'c', $post_ID ),
       
    33 		$formatted_date
       
    34 	);
       
    35 }
       
    36 
       
    37 /**
       
    38  * Registers the `core/post-date` block on the server.
       
    39  */
       
    40 function register_block_core_post_date() {
       
    41 	register_block_type_from_metadata(
       
    42 		__DIR__ . '/post-date',
       
    43 		array(
       
    44 			'render_callback' => 'render_block_core_post_date',
       
    45 		)
       
    46 	);
       
    47 }
       
    48 add_action( 'init', 'register_block_core_post_date' );