| 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 |
* @since 5.9.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 author block. |
| 17 |
*/ |
| 18 |
function gutenberg_render_block_core_post_author( $attributes, $content, $block ) { |
| 19 |
if ( ! isset( $block->context['postId'] ) ) { |
| 20 |
$author_id = get_query_var( 'author' ); |
| 21 |
} else { |
| 22 |
$author_id = get_post_field( 'post_author', $block->context['postId'] ); |
| 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 |
$avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar( |
| 34 |
$author_id, |
| 35 |
$attributes['avatarSize'] |
| 36 |
) : null; |
| 37 |
|
| 38 |
$link = get_author_posts_url( $author_id ); |
| 39 |
$author_name = get_the_author_meta( 'display_name', $author_id ); |
| 40 |
if ( ! empty( $attributes['isLink'] && ! empty( $attributes['linkTarget'] ) ) ) { |
| 41 |
$author_name = sprintf( '<a href="%1$s" target="%2$s">%3$s</a>', esc_url( $link ), esc_attr( $attributes['linkTarget'] ), $author_name ); |
| 42 |
} |
| 43 |
|
| 44 |
$byline = ! empty( $attributes['byline'] ) ? $attributes['byline'] : false; |
| 45 |
$classes = array(); |
| 46 |
if ( isset( $attributes['itemsJustification'] ) ) { |
| 47 |
$classes[] = 'items-justified-' . $attributes['itemsJustification']; |
| 48 |
} |
| 49 |
if ( isset( $attributes['textAlign'] ) ) { |
| 50 |
$classes[] = 'has-text-align-' . $attributes['textAlign']; |
| 51 |
} |
| 52 |
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
| 53 |
$classes[] = 'has-link-color'; |
| 54 |
} |
| 55 |
|
| 56 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
| 57 |
|
| 58 |
return sprintf( '<div %1$s>', $wrapper_attributes ) . |
| 59 |
( ! empty( $attributes['showAvatar'] ) ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) . |
| 60 |
'<div class="wp-block-post-author__content">' . |
| 61 |
( ! empty( $byline ) ? '<p class="wp-block-post-author__byline">' . wp_kses_post( $byline ) . '</p>' : '' ) . |
| 62 |
'<p class="wp-block-post-author__name">' . $author_name . '</p>' . |
| 63 |
( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description', $author_id ) . '</p>' : '' ) . |
| 64 |
'</div>' . |
| 65 |
'</div>'; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Registers the `core/post-author` block on the server. |
| 70 |
* |
| 71 |
* @since 5.9.0 |
| 72 |
*/ |
| 73 |
function gutenberg_register_block_core_post_author() { |
| 74 |
register_block_type_from_metadata( |
| 75 |
__DIR__ . '/post-author', |
| 76 |
array( |
| 77 |
'render_callback' => 'gutenberg_render_block_core_post_author', |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
add_action( 'init', 'gutenberg_register_block_core_post_author', 20 ); |
| 82 |
|