Skip to content

Commit

Permalink
Implement image size option for featured image in cover block (#67273)
Browse files Browse the repository at this point in the history
Co-authored-by: akasunil <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: carolinan <[email protected]>
  • Loading branch information
4 people authored Dec 2, 2024
1 parent 4775e70 commit c5b33b8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ function CoverEdit( {
select( coreStore ).getMedia( featuredImage, { context: 'view' } ),
[ featuredImage ]
);
const mediaUrl = media?.source_url;
const mediaUrl =
media?.media_details?.sizes?.[ sizeSlug ]?.source_url ??
media?.source_url;

// User can change the featured image outside of the block, but we still
// need to update the block when that happens. This effect should only
Expand Down Expand Up @@ -451,6 +453,7 @@ function CoverEdit( {
toggleUseFeaturedImage={ toggleUseFeaturedImage }
updateDimRatio={ onUpdateDimRatio }
onClearMedia={ onClearMedia }
featuredImage={ media }
/>
);

Expand Down
13 changes: 10 additions & 3 deletions packages/block-library/src/cover/edit/inspector-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export default function CoverInspectorControls( {
coverRef,
currentSettings,
updateDimRatio,
featuredImage,
} ) {
const {
useFeaturedImage,
Expand Down Expand Up @@ -132,8 +133,12 @@ export default function CoverInspectorControls( {
[ id, isImageBackground ]
);

const currentBackgroundImage = useFeaturedImage ? featuredImage : image;

function updateImage( newSizeSlug ) {
const newUrl = image?.media_details?.sizes?.[ newSizeSlug ]?.source_url;
const newUrl =
currentBackgroundImage?.media_details?.sizes?.[ newSizeSlug ]
?.source_url;
if ( ! newUrl ) {
return null;
}
Expand All @@ -146,7 +151,9 @@ export default function CoverInspectorControls( {

const imageSizeOptions = imageSizes
?.filter(
( { slug } ) => image?.media_details?.sizes?.[ slug ]?.source_url
( { slug } ) =>
currentBackgroundImage?.media_details?.sizes?.[ slug ]
?.source_url
)
?.map( ( { name, slug } ) => ( { value: slug, label: name } ) );

Expand Down Expand Up @@ -321,7 +328,7 @@ export default function CoverInspectorControls( {
/>
</ToolsPanelItem>
) }
{ ! useFeaturedImage && !! imageSizeOptions?.length && (
{ !! imageSizeOptions?.length && (
<ResolutionTool
value={ sizeSlug }
onChange={ updateImage }
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/cover/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ function render_block_core_cover( $attributes, $content ) {
$attr['style'] = 'object-position:' . $object_position . ';';
}

$image = get_the_post_thumbnail( null, 'post-thumbnail', $attr );
$image = get_the_post_thumbnail( null, $attributes['sizeSlug'] ?? 'post-thumbnail', $attr );
} else {
if ( in_the_loop() ) {
update_post_thumbnail_cache();
}
$current_featured_image = get_the_post_thumbnail_url();
$current_featured_image = get_the_post_thumbnail_url( null, $attributes['sizeSlug'] ?? null );
if ( ! $current_featured_image ) {
return $content;
}
Expand Down

0 comments on commit c5b33b8

Please sign in to comment.