wp/wp-includes/blocks/post-author-name.php
changeset 21 48c4eec2b7e6
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     1 <?php
       
     2 /**
       
     3  * Server-side rendering of the `core/post-author-name` block.
       
     4  *
       
     5  * @package WordPress
       
     6  */
       
     7 
       
     8 /**
       
     9  * Renders the `core/post-author-name` block on the server.
       
    10  *
       
    11  * @since 6.2.0
       
    12  *
       
    13  * @param  array    $attributes Block attributes.
       
    14  * @param  string   $content    Block default content.
       
    15  * @param  WP_Block $block      Block instance.
       
    16  * @return string Returns the rendered post author name block.
       
    17  */
       
    18 function render_block_core_post_author_name( $attributes, $content, $block ) {
       
    19 	if ( isset( $block->context['postId'] ) ) {
       
    20 		$author_id = get_post_field( 'post_author', $block->context['postId'] );
       
    21 	} else {
       
    22 		$author_id = get_query_var( 'author' );
       
    23 	}
       
    24 
       
    25 	if ( empty( $author_id ) ) {
       
    26 		return '';
       
    27 	}
       
    28 
       
    29 	$author_name = get_the_author_meta( 'display_name', $author_id );
       
    30 	if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
       
    31 		$author_name = sprintf( '<a href="%1$s" target="%2$s" class="wp-block-post-author-name__link">%3$s</a>', get_author_posts_url( $author_id ), esc_attr( $attributes['linkTarget'] ), $author_name );
       
    32 	}
       
    33 
       
    34 	$classes = array();
       
    35 	if ( isset( $attributes['textAlign'] ) ) {
       
    36 		$classes[] = 'has-text-align-' . $attributes['textAlign'];
       
    37 	}
       
    38 	if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
       
    39 		$classes[] = 'has-link-color';
       
    40 	}
       
    41 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
       
    42 
       
    43 	return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $author_name );
       
    44 }
       
    45 
       
    46 /**
       
    47  * Registers the `core/post-author-name` block on the server.
       
    48  *
       
    49  * @since 6.2.0
       
    50  */
       
    51 function register_block_core_post_author_name() {
       
    52 	register_block_type_from_metadata(
       
    53 		__DIR__ . '/post-author-name',
       
    54 		array(
       
    55 			'render_callback' => 'render_block_core_post_author_name',
       
    56 		)
       
    57 	);
       
    58 }
       
    59 add_action( 'init', 'register_block_core_post_author_name' );