wp/wp-includes/blocks/post-author.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/post-author` block.
       
     4  *
       
     5  * @package WordPress
       
     6  */
       
     7 
       
     8 /**
       
     9  * Renders the `core/post-author` 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 rendered author block.
       
    15  */
       
    16 function render_block_core_post_author( $attributes, $content, $block ) {
       
    17 	if ( ! isset( $block->context['postId'] ) ) {
       
    18 		return '';
       
    19 	}
       
    20 
       
    21 	$author_id = get_post_field( 'post_author', $block->context['postId'] );
       
    22 	if ( empty( $author_id ) ) {
       
    23 		return '';
       
    24 	}
       
    25 
       
    26 	$avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar(
       
    27 		$author_id,
       
    28 		$attributes['avatarSize']
       
    29 	) : null;
       
    30 
       
    31 	$byline  = ! empty( $attributes['byline'] ) ? $attributes['byline'] : false;
       
    32 	$classes = array_merge(
       
    33 		isset( $attributes['itemsJustification'] ) ? array( 'items-justified-' . $attributes['itemsJustification'] ) : array(),
       
    34 		isset( $attributes['textAlign'] ) ? array( 'has-text-align-' . $attributes['textAlign'] ) : array()
       
    35 	);
       
    36 
       
    37 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
       
    38 
       
    39 	return sprintf( '<div %1$s>', $wrapper_attributes ) .
       
    40 	( ! empty( $attributes['showAvatar'] ) ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) .
       
    41 	'<div class="wp-block-post-author__content">' .
       
    42 	( ! empty( $byline ) ? '<p class="wp-block-post-author__byline">' . wp_kses_post( $byline ) . '</p>' : '' ) .
       
    43 	'<p class="wp-block-post-author__name">' . get_the_author_meta( 'display_name', $author_id ) . '</p>' .
       
    44 	( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description', $author_id ) . '</p>' : '' ) .
       
    45 	'</div>' .
       
    46 	'</div>';
       
    47 }
       
    48 
       
    49 /**
       
    50  * Registers the `core/post-author` block on the server.
       
    51  */
       
    52 function register_block_core_post_author() {
       
    53 	register_block_type_from_metadata(
       
    54 		__DIR__ . '/post-author',
       
    55 		array(
       
    56 			'render_callback' => 'render_block_core_post_author',
       
    57 		)
       
    58 	);
       
    59 }
       
    60 add_action( 'init', 'register_block_core_post_author' );