| 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 gutenberg_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['className'] ) ? array( $attributes['className'] ) : array(), |
| 34 |
isset( $attributes['itemsJustification'] ) ? array( 'items-justified-' . $attributes['itemsJustification'] ) : array(), |
| 35 |
isset( $attributes['textAlign'] ) ? array( 'has-text-align-' . $attributes['textAlign'] ) : array() |
| 36 |
); |
| 37 |
|
| 38 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
| 39 |
|
| 40 |
return sprintf( '<div %1$s>', $wrapper_attributes ) . |
| 41 |
( ! empty( $attributes['showAvatar'] ) ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) . |
| 42 |
'<div class="wp-block-post-author__content">' . |
| 43 |
( ! empty( $byline ) ? '<p class="wp-block-post-author__byline">' . $byline . '</p>' : '' ) . |
| 44 |
'<p class="wp-block-post-author__name">' . get_the_author_meta( 'display_name', $author_id ) . '</p>' . |
| 45 |
( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description', $author_id ) . '</p>' : '' ) . |
| 46 |
'</div>' . |
| 47 |
'</div>'; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Registers the `core/post-author` block on the server. |
| 52 |
*/ |
| 53 |
function gutenberg_register_block_core_post_author() { |
| 54 |
register_block_type_from_metadata( |
| 55 |
__DIR__ . '/post-author', |
| 56 |
array( |
| 57 |
'render_callback' => 'gutenberg_render_block_core_post_author', |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
add_action( 'init', 'gutenberg_register_block_core_post_author', 20 ); |
| 62 |
|