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