Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover Block: Image size option for featured image #67273

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading