← All changes
|
build/scripts/block-library/post-featured-image.php
+28
-23
23.0.1
→
trunk
View file →
| @@ -39,18 +39,31 @@ | ||
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | $extra_styles = ''; |
| 43 | + $has_width = array_key_exists( 'width', $attributes ) && null !== $attributes['width'] && '' !== $attributes['width']; | |
| 44 | + $has_height = array_key_exists( 'height', $attributes ) && null !== $attributes['height'] && '' !== $attributes['height']; | |
| 43 | 45 | |
| 44 | - // Aspect ratio with a height set needs to override the default width/height. | |
| 45 | 46 | if ( ! empty( $attributes['aspectRatio'] ) ) { |
| 46 | - $extra_styles .= 'width:100%;height:100%;'; | |
| 47 | - } elseif ( ! empty( $attributes['height'] ) ) { | |
| 48 | - $extra_styles .= "height:{$attributes['height']};"; | |
| 47 | + // If there's no non-default value set, let the image's width and height attributes determine its intrinsic aspect ratio. | |
| 48 | + if ( 'auto' !== $attributes['aspectRatio'] ) { | |
| 49 | + $extra_styles .= esc_attr( safecss_filter_attr( 'aspect-ratio:' . $attributes['aspectRatio'] ) ) . ';'; | |
| 50 | + } | |
| 51 | + $extra_styles .= 'width:100%;'; | |
| 49 | 52 | } |
| 50 | 53 | |
| 54 | + if ( $has_height ) { | |
| 55 | + $extra_styles .= esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';'; | |
| 56 | + } elseif ( $has_width ) { | |
| 57 | + $extra_styles .= 'height:auto;'; | |
| 58 | + } | |
| 59 | + | |
| 60 | + if ( $has_width ) { | |
| 61 | + $extra_styles .= esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';'; | |
| 62 | + } | |
| 63 | + | |
| 51 | 64 | if ( ! empty( $attributes['scale'] ) ) { |
| 52 | - $extra_styles .= "object-fit:{$attributes['scale']};"; | |
| 65 | + $extra_styles .= esc_attr( safecss_filter_attr( 'object-fit:' . $attributes['scale'] ) ) . ';'; | |
| 53 | 66 | } |
| 54 | 67 | if ( ! empty( $attributes['style']['shadow'] ) ) { |
| 55 | 68 | $shadow_styles = gutenberg_style_engine_get_styles( array( 'shadow' => $attributes['style']['shadow'] ) ); |
| 56 | 69 | |
| @@ -89,8 +102,15 @@ | ||
| 89 | 102 | $tag_html->next_tag(); |
| 90 | 103 | foreach ( $processor->get_attribute_names_with_prefix( '' ) as $name ) { |
| 91 | 104 | $tag_html->set_attribute( $name, $processor->get_attribute( $name ) ); |
| 92 | 105 | } |
| 106 | + if ( ! empty( $attr['style'] ) ) { | |
| 107 | + $existing_style = $tag_html->get_attribute( 'style' ); | |
| 108 | + $style = is_string( $existing_style ) && '' !== $existing_style | |
| 109 | + ? rtrim( $existing_style, ';' ) . ';' . $attr['style'] | |
| 110 | + : $attr['style']; | |
| 111 | + $tag_html->set_attribute( 'style', $style ); | |
| 112 | + } | |
| 93 | 113 | $featured_image = $tag_html->get_updated_html(); |
| 94 | 114 | } |
| 95 | 115 | } |
| 96 | 116 | |
| @@ -100,15 +120,13 @@ | ||
| 100 | 120 | |
| 101 | 121 | if ( $is_link ) { |
| 102 | 122 | $link_target = $attributes['linkTarget']; |
| 103 | 123 | $rel = ! empty( $attributes['rel'] ) ? 'rel="' . esc_attr( $attributes['rel'] ) . '"' : ''; |
| 104 | - $height = ! empty( $attributes['height'] ) ? 'style="' . esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . '"' : ''; | |
| 105 | 124 | $featured_image = sprintf( |
| 106 | - '<a href="%1$s" target="%2$s" %3$s %4$s>%5$s%6$s</a>', | |
| 107 | - get_the_permalink( $post_ID ), | |
| 125 | + '<a href="%1$s" target="%2$s" %3$s>%4$s%5$s</a>', | |
| 126 | + esc_url( get_the_permalink( $post_ID ) ), | |
| 108 | 127 | esc_attr( $link_target ), |
| 109 | 128 | $rel, |
| 110 | - $height, | |
| 111 | 129 | $featured_image, |
| 112 | 130 | $overlay_markup |
| 113 | 131 | ); |
| 114 | 132 | } else { |
| @@ -114,22 +132,9 @@ | ||
| 114 | 132 | } else { |
| 115 | 133 | $featured_image = $featured_image . $overlay_markup; |
| 116 | 134 | } |
| 117 | 135 | |
| 118 | - $aspect_ratio = ! empty( $attributes['aspectRatio'] ) | |
| 119 | - ? esc_attr( safecss_filter_attr( 'aspect-ratio:' . $attributes['aspectRatio'] ) ) . ';' | |
| 120 | - : ''; | |
| 121 | - $width = ! empty( $attributes['width'] ) | |
| 122 | - ? esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';' | |
| 123 | - : ''; | |
| 124 | - $height = ! empty( $attributes['height'] ) | |
| 125 | - ? esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';' | |
| 126 | - : ''; | |
| 127 | - if ( ! $height && ! $width && ! $aspect_ratio ) { | |
| 128 | - $wrapper_attributes = get_block_wrapper_attributes(); | |
| 129 | - } else { | |
| 130 | - $wrapper_attributes = get_block_wrapper_attributes( array( 'style' => $aspect_ratio . $width . $height ) ); | |
| 131 | - } | |
| 136 | + $wrapper_attributes = get_block_wrapper_attributes(); | |
| 132 | 137 | return "<figure {$wrapper_attributes}>{$featured_image}</figure>"; |
| 133 | 138 | } |
| 134 | 139 | |
| 135 | 140 | /** |