avatar.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/avatar` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/avatar` block on the server.
  9. *
  10. * @param array $attributes Block attributes.
  11. * @param string $content Block default content.
  12. * @param WP_Block $block Block instance.
  13. * @return string Return the avatar.
  14. */
  15. function render_block_core_avatar( $attributes, $content, $block ) {
  16. $size = isset( $attributes['size'] ) ? $attributes['size'] : 96;
  17. $wrapper_attributes = get_block_wrapper_attributes();
  18. $image_styles = array();
  19. // Add border width styles.
  20. $has_border_width = ! empty( $attributes['style']['border']['width'] );
  21. if ( $has_border_width ) {
  22. $border_width = $attributes['style']['border']['width'];
  23. $image_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) );
  24. }
  25. // Add border radius styles.
  26. $has_border_radius = ! empty( $attributes['style']['border']['radius'] );
  27. if ( $has_border_radius ) {
  28. $border_radius = $attributes['style']['border']['radius'];
  29. if ( is_array( $border_radius ) ) {
  30. // Apply styles for individual corner border radii.
  31. foreach ( $border_radius as $key => $value ) {
  32. if ( null !== $value ) {
  33. $name = _wp_to_kebab_case( $key );
  34. // Add shared styles for individual border radii.
  35. $border_style = sprintf(
  36. 'border-%s-radius: %s;',
  37. esc_attr( $name ),
  38. esc_attr( $value )
  39. );
  40. $image_styles[] = $border_style;
  41. }
  42. }
  43. } else {
  44. $border_style = sprintf( 'border-radius: %s;', esc_attr( $border_radius ) );
  45. $image_styles[] = $border_style;
  46. }
  47. }
  48. // Add border color styles.
  49. $has_border_color = ! empty( $attributes['style']['border']['color'] );
  50. if ( $has_border_color ) {
  51. $border_color = $attributes['style']['border']['color'];
  52. $image_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );
  53. }
  54. // Add border style (solid, dashed, dotted ).
  55. $has_border_style = ! empty( $attributes['style']['border']['style'] );
  56. if ( $has_border_style ) {
  57. $border_style = $attributes['style']['border']['style'];
  58. $image_styles[] = sprintf( 'border-style: %s;', esc_attr( $border_style ) );
  59. }
  60. // Add border classes to the avatar image for both custom colors and palette colors.
  61. $image_classes = '';
  62. if ( $has_border_color || isset( $attributes['borderColor'] ) ) {
  63. $image_classes .= 'has-border-color';
  64. }
  65. if ( isset( $attributes['borderColor'] ) ) {
  66. $image_classes .= ' has-' . $attributes['borderColor'] . '-border-color';
  67. }
  68. if ( ! isset( $block->context['commentId'] ) ) {
  69. $author_id = isset( $attributes['userId'] ) ? $attributes['userId'] : get_post_field( 'post_author', $block->context['postId'] );
  70. $author_name = get_the_author_meta( 'display_name', $author_id );
  71. // translators: %s is the Author name.
  72. $alt = sprintf( __( '%s Avatar' ), $author_name );
  73. $avatar_block = get_avatar(
  74. $author_id,
  75. $size,
  76. '',
  77. $alt,
  78. array(
  79. 'extra_attr' => isset( $image_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $image_styles ) ) ) : '',
  80. 'class' => "wp-block-avatar__image $image_classes ",
  81. )
  82. );
  83. if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
  84. $label = '';
  85. if ( '_blank' === $attributes['linkTarget'] ) {
  86. // translators: %s is the Author name.
  87. $label = 'aria-label="' . sprintf( esc_attr__( '(%s author archive, opens in a new tab)' ), $author_name ) . '"';
  88. }
  89. // translators: %1$s: Author archive link. %2$s: Link target. %3$s Aria label. %4$s Avatar image.
  90. $avatar_block = sprintf( '<a href="%1$s" target="%2$s" %3$s class="wp-block-avatar__link">%4$s</a>', get_author_posts_url( $author_id ), esc_attr( $attributes['linkTarget'] ), $label, $avatar_block );
  91. }
  92. return sprintf( '<div %1s>%2s</div>', $wrapper_attributes, $avatar_block );
  93. }
  94. $comment = get_comment( $block->context['commentId'] );
  95. if ( ! $comment ) {
  96. return '';
  97. }
  98. /* translators: %s is the Comment Author name */
  99. $alt = sprintf( __( '%s Avatar' ), $comment->comment_author );
  100. $avatar_block = get_avatar(
  101. $comment,
  102. $size,
  103. '',
  104. $alt,
  105. array(
  106. 'extra_attr' => isset( $image_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $image_styles ) ) ) : '',
  107. 'class' => "wp-block-avatar__image $image_classes",
  108. )
  109. );
  110. if ( isset( $attributes['isLink'] ) && $attributes['isLink'] && isset( $comment->comment_author_url ) && '' !== $comment->comment_author_url ) {
  111. $label = '';
  112. if ( '_blank' === $attributes['linkTarget'] ) {
  113. // translators: %s is the Comment Author name.
  114. $label = 'aria-label="' . sprintf( esc_attr__( '(%s website link, opens in a new tab)' ), $comment->comment_author ) . '"';
  115. }
  116. // translators: %1$s: Comment Author website link. %2$s: Link target. %3$s Aria label. %4$s Avatar image.
  117. $avatar_block = sprintf( '<a href="%1$s" target="%2$s" %3$s class="wp-block-avatar__link">%4$s</a>', esc_url( $comment->comment_author_url ), esc_attr( $attributes['linkTarget'] ), $label, $avatar_block );
  118. }
  119. return sprintf( '<div %1s>%2s</div>', $wrapper_attributes, $avatar_block );
  120. }
  121. /**
  122. * Registers the `core/avatar` block on the server.
  123. */
  124. function register_block_core_avatar() {
  125. register_block_type_from_metadata(
  126. __DIR__ . '/avatar',
  127. array(
  128. 'render_callback' => 'render_block_core_avatar',
  129. )
  130. );
  131. }
  132. add_action( 'init', 'register_block_core_avatar' );