wp/wp-includes/blocks/post-featured-image.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-featured-image` block.
       
     4  *
       
     5  * @package WordPress
       
     6  */
       
     7 
       
     8 /**
       
     9  * Renders the `core/post-featured-image` 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 featured image for the current post.
       
    15  */
       
    16 function render_block_core_post_featured_image( $attributes, $content, $block ) {
       
    17 	if ( ! isset( $block->context['postId'] ) ) {
       
    18 		return '';
       
    19 	}
       
    20 	$post_ID = $block->context['postId'];
       
    21 
       
    22 	$featured_image = get_the_post_thumbnail( $post_ID );
       
    23 	if ( ! $featured_image ) {
       
    24 		return '';
       
    25 	}
       
    26 
       
    27 	if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
       
    28 		$featured_image = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $featured_image );
       
    29 	}
       
    30 
       
    31 	$wrapper_attributes = get_block_wrapper_attributes();
       
    32 
       
    33 	return '<figure ' . $wrapper_attributes . '>' . $featured_image . '</figure>';
       
    34 }
       
    35 
       
    36 /**
       
    37  * Registers the `core/post-featured-image` block on the server.
       
    38  */
       
    39 function register_block_core_post_featured_image() {
       
    40 	register_block_type_from_metadata(
       
    41 		__DIR__ . '/post-featured-image',
       
    42 		array(
       
    43 			'render_callback' => 'render_block_core_post_featured_image',
       
    44 		)
       
    45 	);
       
    46 }
       
    47 add_action( 'init', 'register_block_core_post_featured_image' );