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


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref