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 */
18function 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 if ( isset( $block->context['postType'] ) && ! post_type_supports( $block->context['postType'], 'author' ) ) {
30 return '';
31 }
32
33 $author_name = get_the_author_meta( 'display_name', $author_id );
34 if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
35 $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 );
36 }
37
38 $classes = array();
39 if ( isset( $attributes['textAlign'] ) ) {
40 $classes[] = 'has-text-align-' . $attributes['textAlign'];
41 }
42 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
43 $classes[] = 'has-link-color';
44 }
45 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
46
47 return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $author_name );
48}
49
50/**
51 * Registers the `core/post-author-name` block on the server.
52 *
53 * @since 6.2.0
54 */
55function register_block_core_post_author_name() {
56 register_block_type_from_metadata(
57 __DIR__ . '/post-author-name',
58 array(
59 'render_callback' => 'render_block_core_post_author_name',
60 )
61 );
62}
63add_action( 'init', 'register_block_core_post_author_name' );
64