| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/comment-author-avatar` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Renders the `core/comment-author-avatar` 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 Return the post comment's avatar. |
| 15 |
*/ |
| 16 |
function gutenberg_render_block_core_comment_author_avatar( $attributes, $content, $block ) { |
| 17 |
if ( ! isset( $block->context['commentId'] ) ) { |
| 18 |
return ''; |
| 19 |
} |
| 20 |
|
| 21 |
$comment = get_comment( $block->context['commentId'] ); |
| 22 |
if ( ! $comment ) { |
| 23 |
return ''; |
| 24 |
} |
| 25 |
|
| 26 |
// This is the only way to retrieve style and classes on different instances. |
| 27 |
$wrapper_attributes = WP_Block_Supports::get_instance()->apply_block_supports(); |
| 28 |
|
| 29 |
/** |
| 30 |
* We get the spacing attributes and transform the array provided into a string formatted for being applied as a style html tag. |
| 31 |
* Good candidate to be moved to a separate function in core. |
| 32 |
*/ |
| 33 |
$spacing_attributes = $attributes['style']['spacing'] ?? null; |
| 34 |
if ( isset( $spacing_attributes ) && ! empty( $spacing_attributes ) ) { |
| 35 |
$spacing_array = array(); |
| 36 |
foreach ( $spacing_attributes as $spacing_attribute_key => $spacing_attribute_value ) { |
| 37 |
foreach ( $spacing_attribute_value as $position_key => $position_value ) { |
| 38 |
$spacing_array[] = $spacing_attribute_key . '-' . $position_key . ': ' . $position_value; |
| 39 |
} |
| 40 |
} |
| 41 |
$spacing_string = implode( ';', $spacing_array ); |
| 42 |
} |
| 43 |
|
| 44 |
$width = $attributes['width'] ?? 96; |
| 45 |
$height = $attributes['height'] ?? 96; |
| 46 |
$styles = $wrapper_attributes['style'] ?? ''; |
| 47 |
$classes = $wrapper_attributes['class'] ?? ''; |
| 48 |
|
| 49 |
/* translators: %s: Author name. */ |
| 50 |
$alt = sprintf( __( '%s Avatar' ), $comment->comment_author ); |
| 51 |
|
| 52 |
$avatar_block = get_avatar( |
| 53 |
$comment, |
| 54 |
null, |
| 55 |
'', |
| 56 |
$alt, |
| 57 |
array( |
| 58 |
'height' => $height, |
| 59 |
'width' => $width, |
| 60 |
'extra_attr' => sprintf( 'style="%s"', $styles ), |
| 61 |
'class' => $classes, |
| 62 |
) |
| 63 |
); |
| 64 |
if ( isset( $spacing_attributes ) ) { |
| 65 |
return sprintf( '<div style="%1$s">%2$s</div>', esc_attr( $spacing_string ), $avatar_block ); |
| 66 |
} |
| 67 |
return sprintf( '<div>%s</div>', $avatar_block ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Registers the `core/comment-author-avatar` block on the server. |
| 72 |
*/ |
| 73 |
function gutenberg_register_block_core_comment_author_avatar() { |
| 74 |
register_block_type_from_metadata( |
| 75 |
__DIR__ . '/comment-author-avatar', |
| 76 |
array( |
| 77 |
'render_callback' => 'gutenberg_render_block_core_comment_author_avatar', |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
add_action( 'init', 'gutenberg_register_block_core_comment_author_avatar', 20 ); |
| 82 |
|