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

Enable image size for featured images in templates #46848

Merged
merged 1 commit into from
Jan 18, 2023
Merged
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
72 changes: 45 additions & 27 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function getMediaSourceUrlBySizeSlug( media, slug ) {
);
}

function PostFeaturedImageDisplay( {
export default function PostFeaturedImageEdit( {
clientId,
attributes,
setAttributes,
Expand Down Expand Up @@ -167,6 +167,11 @@ function PostFeaturedImageDisplay( {
</>
);
let image;

/**
* A post featured image block placed in a query loop
* does not have image replacement or upload options.
*/
if ( ! featuredImage && isDescendentOfQueryLoop ) {
return (
<>
Expand All @@ -183,13 +188,46 @@ function PostFeaturedImageDisplay( {
);
}

/**
* A post featured image placed in a block template, outside a query loop,
* does not have a postId and will always be a placeholder image.
* It does not have image replacement, upload, or link options.
*/
if ( ! featuredImage && ! postId ) {
return (
<>
<DimensionControls
clientId={ clientId }
attributes={ attributes }
setAttributes={ setAttributes }
imageSizeOptions={ imageSizeOptions }
/>
<div { ...blockProps }>
{ placeholder() }
<Overlay
attributes={ attributes }
setAttributes={ setAttributes }
clientId={ clientId }
/>
</div>
</>
);
}

const label = __( 'Add a featured image' );
const imageStyles = {
...borderProps.style,
height,
objectFit: height && scale,
};

/**
* When the post featured image block is placed in a context where:
* - It has a postId (for example in a single post)
* - It is not inside a query loop
* - It has no image assigned yet
* Then display the placeholder with the image upload option.
*/
if ( ! featuredImage ) {
image = (
<MediaPlaceholder
Expand Down Expand Up @@ -236,6 +274,12 @@ function PostFeaturedImageDisplay( {
);
}

/**
* When the post featured image block:
* - Has an image assigned
* - Is not inside a query loop
* Then display the image and the image replacement option.
*/
return (
<>
{ controls }
Expand Down Expand Up @@ -266,29 +310,3 @@ function PostFeaturedImageDisplay( {
</>
);
}

export default function PostFeaturedImageEdit( props ) {
const blockProps = useBlockProps();
const borderProps = useBorderProps( props.attributes );

if ( ! props.context?.postId ) {
return (
<div { ...blockProps }>
<Placeholder
className={ classnames(
'block-editor-media-placeholder',
borderProps.className
) }
withIllustration={ true }
style={ borderProps.style }
/>
<Overlay
attributes={ props.attributes }
setAttributes={ props.setAttributes }
clientId={ props.clientId }
/>
</div>
);
}
return <PostFeaturedImageDisplay { ...props } />;
}