6 */ |
6 */ |
7 |
7 |
8 /** |
8 /** |
9 * Renders the `core/post-author` block on the server. |
9 * Renders the `core/post-author` block on the server. |
10 * |
10 * |
|
11 * @since 5.9.0 |
|
12 * |
11 * @param array $attributes Block attributes. |
13 * @param array $attributes Block attributes. |
12 * @param string $content Block default content. |
14 * @param string $content Block default content. |
13 * @param WP_Block $block Block instance. |
15 * @param WP_Block $block Block instance. |
14 * @return string Returns the rendered author block. |
16 * @return string Returns the rendered author block. |
15 */ |
17 */ |
16 function render_block_core_post_author( $attributes, $content, $block ) { |
18 function render_block_core_post_author( $attributes, $content, $block ) { |
17 if ( ! isset( $block->context['postId'] ) ) { |
19 if ( ! isset( $block->context['postId'] ) ) { |
18 return ''; |
20 $author_id = get_query_var( 'author' ); |
|
21 } else { |
|
22 $author_id = get_post_field( 'post_author', $block->context['postId'] ); |
19 } |
23 } |
20 |
24 |
21 $author_id = get_post_field( 'post_author', $block->context['postId'] ); |
|
22 if ( empty( $author_id ) ) { |
25 if ( empty( $author_id ) ) { |
23 return ''; |
26 return ''; |
24 } |
27 } |
25 |
28 |
26 $avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar( |
29 $avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar( |
27 $author_id, |
30 $author_id, |
28 $attributes['avatarSize'] |
31 $attributes['avatarSize'] |
29 ) : null; |
32 ) : null; |
30 |
33 |
|
34 $link = get_author_posts_url( $author_id ); |
|
35 $author_name = get_the_author_meta( 'display_name', $author_id ); |
|
36 if ( ! empty( $attributes['isLink'] && ! empty( $attributes['linkTarget'] ) ) ) { |
|
37 $author_name = sprintf( '<a href="%1$s" target="%2$s">%3$s</a>', esc_url( $link ), esc_attr( $attributes['linkTarget'] ), $author_name ); |
|
38 } |
|
39 |
31 $byline = ! empty( $attributes['byline'] ) ? $attributes['byline'] : false; |
40 $byline = ! empty( $attributes['byline'] ) ? $attributes['byline'] : false; |
32 $classes = array_merge( |
41 $classes = array(); |
33 isset( $attributes['itemsJustification'] ) ? array( 'items-justified-' . $attributes['itemsJustification'] ) : array(), |
42 if ( isset( $attributes['itemsJustification'] ) ) { |
34 isset( $attributes['textAlign'] ) ? array( 'has-text-align-' . $attributes['textAlign'] ) : array() |
43 $classes[] = 'items-justified-' . $attributes['itemsJustification']; |
35 ); |
44 } |
|
45 if ( isset( $attributes['textAlign'] ) ) { |
|
46 $classes[] = 'has-text-align-' . $attributes['textAlign']; |
|
47 } |
|
48 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
|
49 $classes[] = 'has-link-color'; |
|
50 } |
36 |
51 |
37 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
52 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
38 |
53 |
39 return sprintf( '<div %1$s>', $wrapper_attributes ) . |
54 return sprintf( '<div %1$s>', $wrapper_attributes ) . |
40 ( ! empty( $attributes['showAvatar'] ) ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) . |
55 ( ! empty( $attributes['showAvatar'] ) ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) . |
41 '<div class="wp-block-post-author__content">' . |
56 '<div class="wp-block-post-author__content">' . |
42 ( ! empty( $byline ) ? '<p class="wp-block-post-author__byline">' . wp_kses_post( $byline ) . '</p>' : '' ) . |
57 ( ! 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>' . |
58 '<p class="wp-block-post-author__name">' . $author_name . '</p>' . |
44 ( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description', $author_id ) . '</p>' : '' ) . |
59 ( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description', $author_id ) . '</p>' : '' ) . |
45 '</div>' . |
60 '</div>' . |
46 '</div>'; |
61 '</div>'; |
47 } |
62 } |
48 |
63 |
49 /** |
64 /** |
50 * Registers the `core/post-author` block on the server. |
65 * Registers the `core/post-author` block on the server. |
|
66 * |
|
67 * @since 5.9.0 |
51 */ |
68 */ |
52 function register_block_core_post_author() { |
69 function register_block_core_post_author() { |
53 register_block_type_from_metadata( |
70 register_block_type_from_metadata( |
54 __DIR__ . '/post-author', |
71 __DIR__ . '/post-author', |
55 array( |
72 array( |