From 156edf736e09eff42f05ce9e326ab151c9577c49 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Thu, 9 Feb 2023 21:39:59 -0600 Subject: [PATCH] Add aspect-ratio to post featured image block (#47854) --- docs/reference-guides/core-blocks.md | 2 +- lib/compat/wordpress-6.2/blocks.php | 1 + .../image-editor/aspect-ratio-dropdown.js | 1 + .../src/post-featured-image/block.json | 3 + .../post-featured-image/dimension-controls.js | 66 ++++++++++++++++++- .../src/post-featured-image/edit.js | 24 +++++-- .../src/post-featured-image/index.php | 34 +++++++--- 7 files changed, 113 insertions(+), 18 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 0eb9894a07b760..22171c32d60881 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -555,7 +555,7 @@ Display a post's featured image. ([Source](https://github.com/WordPress/gutenber - **Name:** core/post-featured-image - **Category:** theme - **Supports:** align (center, full, left, right, wide), anchor, color (~~background~~, ~~text~~), spacing (margin, padding), ~~html~~ -- **Attributes:** customGradient, customOverlayColor, dimRatio, gradient, height, isLink, linkTarget, overlayColor, rel, scale, sizeSlug, width +- **Attributes:** aspectRatio, customGradient, customOverlayColor, dimRatio, gradient, height, isLink, linkTarget, overlayColor, rel, scale, sizeSlug, width ## Post Navigation Link diff --git a/lib/compat/wordpress-6.2/blocks.php b/lib/compat/wordpress-6.2/blocks.php index c409e4212cf0d4..f432f6a41af8f7 100644 --- a/lib/compat/wordpress-6.2/blocks.php +++ b/lib/compat/wordpress-6.2/blocks.php @@ -21,6 +21,7 @@ function gutenberg_safe_style_attrs_6_2( $attrs ) { $attrs[] = 'left'; $attrs[] = 'z-index'; $attrs[] = 'box-shadow'; + $attrs[] = 'aspect-ratio'; return $attrs; } diff --git a/packages/block-editor/src/components/image-editor/aspect-ratio-dropdown.js b/packages/block-editor/src/components/image-editor/aspect-ratio-dropdown.js index bc4ce5b904c324..c2e8a2cefe4773 100644 --- a/packages/block-editor/src/components/image-editor/aspect-ratio-dropdown.js +++ b/packages/block-editor/src/components/image-editor/aspect-ratio-dropdown.js @@ -54,6 +54,7 @@ export default function AspectRatioDropdown( { toggleProps } ) { } } value={ aspect } aspectRatios={ [ + // All ratios should be mirrored in PostFeaturedImage in @wordpress/block-library { title: __( 'Original' ), aspect: defaultAspect, diff --git a/packages/block-library/src/post-featured-image/block.json b/packages/block-library/src/post-featured-image/block.json index e6c8c3bfbbd3f9..c6007785cd82ac 100644 --- a/packages/block-library/src/post-featured-image/block.json +++ b/packages/block-library/src/post-featured-image/block.json @@ -11,6 +11,9 @@ "type": "boolean", "default": false }, + "aspectRatio": { + "type": "string" + }, "width": { "type": "string" }, diff --git a/packages/block-library/src/post-featured-image/dimension-controls.js b/packages/block-library/src/post-featured-image/dimension-controls.js index 3d88de9e652604..da66e7f4bda52d 100644 --- a/packages/block-library/src/post-featured-image/dimension-controls.js +++ b/packages/block-library/src/post-featured-image/dimension-controls.js @@ -49,7 +49,7 @@ const scaleHelp = { const DimensionControls = ( { clientId, - attributes: { width, height, scale, sizeSlug }, + attributes: { aspectRatio, width, height, scale, sizeSlug }, setAttributes, imageSizeOptions = [], } ) => { @@ -72,6 +72,68 @@ const DimensionControls = ( { const scaleLabel = _x( 'Scale', 'Image scaling options' ); return ( + !! aspectRatio } + label={ __( 'Aspect ratio' ) } + onDeselect={ () => setAttributes( { aspectRatio: undefined } ) } + resetAllFilter={ () => ( { + aspectRatio: undefined, + } ) } + isShownByDefault={ true } + panelId={ clientId } + > + + setAttributes( { aspectRatio: nextAspectRatio } ) + } + /> + !! height } @@ -116,7 +178,7 @@ const DimensionControls = ( { units={ units } /> - { !! height && ( + { ( height || aspectRatio ) && ( !! scale && scale !== DEFAULT_SCALE } label={ scaleLabel } diff --git a/packages/block-library/src/post-featured-image/edit.js b/packages/block-library/src/post-featured-image/edit.js index 59c12cf5efb0c3..f378a14f5c9d1b 100644 --- a/packages/block-library/src/post-featured-image/edit.js +++ b/packages/block-library/src/post-featured-image/edit.js @@ -50,8 +50,16 @@ export default function PostFeaturedImageEdit( { context: { postId, postType: postTypeSlug, queryId }, } ) { const isDescendentOfQueryLoop = Number.isFinite( queryId ); - const { isLink, height, width, scale, sizeSlug, rel, linkTarget } = - attributes; + const { + isLink, + aspectRatio, + height, + width, + scale, + sizeSlug, + rel, + linkTarget, + } = attributes; const [ featuredImage, setFeaturedImage ] = useEntityProp( 'postType', postTypeSlug, @@ -89,7 +97,7 @@ export default function PostFeaturedImageEdit( { } ) ); const blockProps = useBlockProps( { - style: { width, height }, + style: { width, height, aspectRatio }, } ); const borderProps = useBorderProps( attributes ); @@ -101,7 +109,10 @@ export default function PostFeaturedImageEdit( { borderProps.className ) } withIllustration={ true } - style={ borderProps.style } + style={ { + ...blockProps.style, + ...borderProps.style, + } } > { content } @@ -218,8 +229,9 @@ export default function PostFeaturedImageEdit( { const label = __( 'Add a featured image' ); const imageStyles = { ...borderProps.style, - height, - objectFit: height && scale, + height: ( !! aspectRatio && '100%' ) || height, + width: !! aspectRatio && '100%', + objectFit: !! ( height || aspectRatio ) && scale, }; /** diff --git a/packages/block-library/src/post-featured-image/index.php b/packages/block-library/src/post-featured-image/index.php index 8da66accd3032c..6cb4110ee000e6 100644 --- a/packages/block-library/src/post-featured-image/index.php +++ b/packages/block-library/src/post-featured-image/index.php @@ -42,11 +42,20 @@ function render_block_core_post_featured_image( $attributes, $content, $block ) } } - if ( ! empty( $attributes['height'] ) ) { - $extra_styles = "height:{$attributes['height']};"; - if ( ! empty( $attributes['scale'] ) ) { - $extra_styles .= "object-fit:{$attributes['scale']};"; - } + $extra_styles = ''; + + // Aspect ratio with a height set needs to override the default width/height. + if ( ! empty( $attributes['aspectRatio'] ) ) { + $extra_styles .= 'width:100%;height:100%;'; + } elseif ( ! empty( $attributes['height'] ) ) { + $extra_styles .= "height:{$attributes['height']};"; + } + + if ( ! empty( $attributes['scale'] ) ) { + $extra_styles .= "object-fit:{$attributes['scale']};"; + } + + if ( ! empty( $extra_styles ) ) { $attr['style'] = empty( $attr['style'] ) ? $extra_styles : $attr['style'] . $extra_styles; } @@ -71,12 +80,19 @@ function render_block_core_post_featured_image( $attributes, $content, $block ) $featured_image = $featured_image . $overlay_markup; } - $width = ! empty( $attributes['width'] ) ? esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';' : ''; - $height = ! empty( $attributes['height'] ) ? esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';' : ''; - if ( ! $height && ! $width ) { + $aspect_ratio = ! empty( $attributes['aspectRatio'] ) + ? esc_attr( safecss_filter_attr( 'aspect-ratio:' . $attributes['aspectRatio'] ) ) . ';' + : ''; + $width = ! empty( $attributes['width'] ) + ? esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';' + : ''; + $height = ! empty( $attributes['height'] ) + ? esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';' + : ''; + if ( ! $height && ! $width && ! $aspect_ratio ) { $wrapper_attributes = get_block_wrapper_attributes(); } else { - $wrapper_attributes = get_block_wrapper_attributes( array( 'style' => $width . $height ) ); + $wrapper_attributes = get_block_wrapper_attributes( array( 'style' => $aspect_ratio . $width . $height ) ); } return "
{$featured_image}
"; }