[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> avatar.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/avatar` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/avatar` 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 Return the avatar.
  17   */
  18  function render_block_core_avatar( $attributes, $content, $block ) {
  19      $size               = $attributes['size'] ?? 96;
  20      $wrapper_attributes = get_block_wrapper_attributes();
  21      $border_attributes  = get_block_core_avatar_border_attributes( $attributes );
  22      $shadow_styles      = wp_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ?? null ) );
  23  
  24      // Class gets passed through `esc_attr` via `get_avatar`.
  25      $image_classes = ! empty( $border_attributes['class'] )
  26          ? "wp-block-avatar__image {$border_attributes['class']}"
  27          : 'wp-block-avatar__image';
  28  
  29      // Unlike class, `get_avatar` doesn't filter the styles via `esc_attr`.
  30      // The style engine does pass the border and shadow styles through
  31      // `safecss_filter_attr` however.
  32      $image_css    = ( $border_attributes['style'] ?? '' ) . ( $shadow_styles['css'] ?? '' );
  33      $image_styles = ! empty( $image_css )
  34          ? sprintf( ' style="%s"', esc_attr( $image_css ) )
  35          : '';
  36  
  37      if ( ! isset( $block->context['commentId'] ) ) {
  38          if ( isset( $attributes['userId'] ) ) {
  39              $author_id = $attributes['userId'];
  40          } elseif ( isset( $block->context['postId'] ) ) {
  41              $author_id = get_post_field( 'post_author', $block->context['postId'] );
  42          } else {
  43              $author_id = get_query_var( 'author' );
  44          }
  45  
  46          if ( empty( $author_id ) ) {
  47              return '';
  48          }
  49  
  50          $author_name = get_the_author_meta( 'display_name', $author_id );
  51          // translators: %s: Author name.
  52          $alt          = sprintf( __( '%s Avatar' ), $author_name );
  53          $avatar_block = get_avatar(
  54              $author_id,
  55              $size,
  56              '',
  57              $alt,
  58              array(
  59                  'extra_attr' => $image_styles,
  60                  'class'      => $image_classes,
  61              )
  62          );
  63          if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
  64              $label = '';
  65              if ( '_blank' === $attributes['linkTarget'] ) {
  66                  // translators: %s is the Author name.
  67                  $label = 'aria-label="' . esc_attr( sprintf( __( '(%s author archive, opens in a new tab)' ), $author_name ) ) . '"';
  68              }
  69              // translators: 1: Author archive link. 2: Link target. %3$s Aria label. %4$s Avatar image.
  70              $avatar_block = sprintf( '<a href="%1$s" target="%2$s" %3$s class="wp-block-avatar__link">%4$s</a>', esc_url( get_author_posts_url( $author_id ) ), esc_attr( $attributes['linkTarget'] ), $label, $avatar_block );
  71          }
  72          return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $avatar_block );
  73      }
  74      $comment = get_comment( $block->context['commentId'] );
  75      if ( ! $comment ) {
  76          return '';
  77      }
  78      /* translators: %s: Author name. */
  79      $alt          = sprintf( __( '%s Avatar' ), $comment->comment_author );
  80      $avatar_block = get_avatar(
  81          $comment,
  82          $size,
  83          '',
  84          $alt,
  85          array(
  86              'extra_attr' => $image_styles,
  87              'class'      => $image_classes,
  88          )
  89      );
  90      if ( isset( $attributes['isLink'] ) && $attributes['isLink'] && isset( $comment->comment_author_url ) && '' !== $comment->comment_author_url ) {
  91          $label = '';
  92          if ( '_blank' === $attributes['linkTarget'] ) {
  93              // translators: %s: Comment author name.
  94              $label = 'aria-label="' . esc_attr( sprintf( __( '(%s website link, opens in a new tab)' ), $comment->comment_author ) ) . '"';
  95          }
  96          $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 );
  97      }
  98      return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $avatar_block );
  99  }
 100  
 101  /**
 102   * Generates class names and styles to apply the border support styles for
 103   * the Avatar block.
 104   *
 105   * @since 6.3.0
 106   *
 107   * @param array $attributes The block attributes.
 108   * @return array The border-related classnames and styles for the block.
 109   */
 110  function get_block_core_avatar_border_attributes( $attributes ) {
 111      $border_styles = array();
 112      $sides         = array( 'top', 'right', 'bottom', 'left' );
 113  
 114      // Border radius.
 115      if ( isset( $attributes['style']['border']['radius'] ) ) {
 116          $border_styles['radius'] = $attributes['style']['border']['radius'];
 117      }
 118  
 119      // Border style.
 120      if ( isset( $attributes['style']['border']['style'] ) ) {
 121          $border_styles['style'] = $attributes['style']['border']['style'];
 122      }
 123  
 124      // Border width.
 125      if ( isset( $attributes['style']['border']['width'] ) ) {
 126          $border_styles['width'] = $attributes['style']['border']['width'];
 127      }
 128  
 129      // Border color.
 130      $preset_color           = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
 131      $custom_color           = $attributes['style']['border']['color'] ?? null;
 132      $border_styles['color'] = $preset_color ? $preset_color : $custom_color;
 133  
 134      // Individual border styles e.g. top, left etc.
 135      foreach ( $sides as $side ) {
 136          $border                 = $attributes['style']['border'][ $side ] ?? null;
 137          $border_styles[ $side ] = array(
 138              'color' => $border['color'] ?? null,
 139              'style' => $border['style'] ?? null,
 140              'width' => $border['width'] ?? null,
 141          );
 142      }
 143  
 144      $styles     = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
 145      $attributes = array();
 146      if ( ! empty( $styles['classnames'] ) ) {
 147          $attributes['class'] = $styles['classnames'];
 148      }
 149      if ( ! empty( $styles['css'] ) ) {
 150          $attributes['style'] = $styles['css'];
 151      }
 152      return $attributes;
 153  }
 154  
 155  /**
 156   * Registers the `core/avatar` block on the server.
 157   *
 158   * @since 6.0.0
 159   */
 160  function register_block_core_avatar() {
 161      register_block_type_from_metadata(
 162          __DIR__ . '/avatar',
 163          array(
 164              'render_callback' => 'render_block_core_avatar',
 165          )
 166      );
 167  }
 168  add_action( 'init', 'register_block_core_avatar' );


Generated : Sun Oct 25 08:20:35 2026 Cross-referenced by PHPXref