17 if ( ! isset( $block->context['postId'] ) ) { |
17 if ( ! isset( $block->context['postId'] ) ) { |
18 return ''; |
18 return ''; |
19 } |
19 } |
20 $post_ID = $block->context['postId']; |
20 $post_ID = $block->context['postId']; |
21 |
21 |
22 $featured_image = get_the_post_thumbnail( $post_ID ); |
22 $size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail'; |
|
23 $post_title = trim( strip_tags( get_the_title( $post_ID ) ) ); |
|
24 $featured_image = get_the_post_thumbnail( $post_ID, $size_slug, array( 'alt' => $post_title ) ); |
23 if ( ! $featured_image ) { |
25 if ( ! $featured_image ) { |
24 return ''; |
26 return ''; |
25 } |
27 } |
26 |
28 $wrapper_attributes = get_block_wrapper_attributes(); |
27 if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { |
29 if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { |
28 $featured_image = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $featured_image ); |
30 $featured_image = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $featured_image ); |
29 } |
31 } |
30 |
32 |
31 $wrapper_attributes = get_block_wrapper_attributes(); |
33 $has_width = ! empty( $attributes['width'] ); |
|
34 $has_height = ! empty( $attributes['height'] ); |
|
35 if ( ! $has_height && ! $has_width ) { |
|
36 return "<figure $wrapper_attributes>$featured_image</figure>"; |
|
37 } |
32 |
38 |
33 return '<figure ' . $wrapper_attributes . '>' . $featured_image . '</figure>'; |
39 if ( $has_width ) { |
|
40 $wrapper_attributes = get_block_wrapper_attributes( array( 'style' => "width:{$attributes['width']};" ) ); |
|
41 } |
|
42 |
|
43 if ( $has_height ) { |
|
44 $image_styles = "height:{$attributes['height']};"; |
|
45 if ( ! empty( $attributes['scale'] ) ) { |
|
46 $image_styles .= "object-fit:{$attributes['scale']};"; |
|
47 } |
|
48 $featured_image = str_replace( 'src=', 'style="' . esc_attr( $image_styles ) . '" src=', $featured_image ); |
|
49 } |
|
50 |
|
51 return "<figure $wrapper_attributes>$featured_image</figure>"; |
34 } |
52 } |
35 |
53 |
36 /** |
54 /** |
37 * Registers the `core/post-featured-image` block on the server. |
55 * Registers the `core/post-featured-image` block on the server. |
38 */ |
56 */ |