PluginProbe
Gutenberg / 22.3.0
Gutenberg v22.3.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / comment-author-avatar.php

comment-author-avatar.php in Gutenberg 22.3.0, at build/scripts/block-library/comment-author-avatar.php

82 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = isset( $attributes['style']['spacing'] ) ? $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 = isset( $attributes['width'] ) ? $attributes['width'] : 96;
45 $height = isset( $attributes['height'] ) ? $attributes['height'] : 96;
46 $styles = isset( $wrapper_attributes['style'] ) ? $wrapper_attributes['style'] : '';
47 $classes = isset( $wrapper_attributes['class'] ) ? $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="%1s"', $styles ),
61 'class' => $classes,
62 )
63 );
64 if ( isset( $spacing_attributes ) ) {
65 return sprintf( '<div style="%1s">%2s</div>', esc_attr( $spacing_string ), $avatar_block );
66 }
67 return sprintf( '<div>%1s</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