| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/post-author-biography` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/post-author-biography` block on the server. |
| 10 |
* |
| 11 |
* @since 6.0.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 biography block. |
| 17 |
*/ |
| 18 |
function gutenberg_render_block_core_post_author_biography( $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_biography = get_the_author_meta( 'description', $author_id ); |
| 30 |
if ( empty( $author_biography ) ) { |
| 31 |
return ''; |
| 32 |
} |
| 33 |
|
| 34 |
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; |
| 35 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); |
| 36 |
|
| 37 |
return sprintf( '<div %1$s>', $wrapper_attributes ) . $author_biography . '</div>'; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Registers the `core/post-author-biography` block on the server. |
| 42 |
* |
| 43 |
* @since 6.0.0 |
| 44 |
*/ |
| 45 |
function gutenberg_register_block_core_post_author_biography() { |
| 46 |
register_block_type_from_metadata( |
| 47 |
__DIR__ . '/post-author-biography', |
| 48 |
array( |
| 49 |
'render_callback' => 'gutenberg_render_block_core_post_author_biography', |
| 50 |
) |
| 51 |
); |
| 52 |
} |
| 53 |
add_action( 'init', 'gutenberg_register_block_core_post_author_biography', 20 ); |
| 54 |
|