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

Post Featured Image: Add size selector #38044

Merged
merged 6 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,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), color (~~background~~, ~~text~~), spacing (margin, padding), ~~html~~
- **Attributes:** height, isLink, scale, width
- **Attributes:** height, isLink, scale, sizeSlug, width

## Post Navigation Link

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/post-featured-image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"scale": {
"type": "string",
"default": "cover"
},
"sizeSlug": {
"type": "string"
}
},
"usesContext": [ "postId", "postType", "queryId" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __, _x } from '@wordpress/i18n';
import {
SelectControl,
__experimentalUnitControl as UnitControl,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
Expand Down Expand Up @@ -32,6 +33,7 @@ const SCALE_OPTIONS = (
);

const DEFAULT_SCALE = 'cover';
const DEFAULT_SIZE = 'full';

const scaleHelp = {
cover: __(
Expand All @@ -47,8 +49,9 @@ const scaleHelp = {

const DimensionControls = ( {
clientId,
attributes: { width, height, scale },
attributes: { width, height, scale, sizeSlug },
setAttributes,
imageSizeOptions = [],
} ) => {
const defaultUnits = [ 'px', '%', 'vw', 'em', 'rem' ];
const units = useCustomUnits( {
Expand Down Expand Up @@ -113,6 +116,29 @@ const DimensionControls = ( {
units={ units }
/>
</ToolsPanelItem>
{ !! imageSizeOptions.length && (
<ToolsPanelItem
hasValue={ () => !! sizeSlug && sizeSlug !== DEFAULT_SIZE }
label={ __( 'Size' ) }
onDeselect={ () =>
setAttributes( { sizeSlug: undefined } )
}
resetAllFilter={ () => ( {
sizeSlug: undefined,
} ) }
isShownByDefault={ true }
panelId={ clientId }
>
<SelectControl
label={ __( 'Size' ) }
value={ sizeSlug || DEFAULT_SIZE }
options={ imageSizeOptions }
onChange={ ( nextSizeSlug ) =>
setAttributes( { sizeSlug: nextSizeSlug } )
}
/>
</ToolsPanelItem>
) }
{ !! height && (
<ToolsPanelItem
hasValue={ () => !! scale && scale !== DEFAULT_SCALE }
Expand Down
28 changes: 25 additions & 3 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
MediaPlaceholder,
MediaReplaceFlow,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { upload } from '@wordpress/icons';
Expand Down Expand Up @@ -46,14 +47,20 @@ const placeholderChip = (
</div>
);

function getMediaSourceUrlBySizeSlug( media, slug ) {
return (
media?.media_details?.sizes?.[ slug ]?.source_url || media?.source_url
);
}

function PostFeaturedImageDisplay( {
clientId,
attributes,
setAttributes,
context: { postId, postType, queryId },
} ) {
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const { isLink, height, width, scale } = attributes;
const { isLink, height, width, scale, sizeSlug } = attributes;
const [ featuredImage, setFeaturedImage ] = useEntityProp(
'postType',
postType,
Expand All @@ -67,6 +74,20 @@ function PostFeaturedImageDisplay( {
select( coreStore ).getMedia( featuredImage, { context: 'view' } ),
[ featuredImage ]
);
const mediaUrl = getMediaSourceUrlBySizeSlug( media, sizeSlug );

const imageSizes = useSelect(
( select ) => select( blockEditorStore ).getSettings().imageSizes,
[]
);
const imageSizeOptions = imageSizes
.filter( ( { slug } ) => {
return media?.media_details?.sizes?.[ slug ]?.source_url;
} )
.map( ( { name, slug } ) => ( {
value: slug,
label: name,
} ) );
Comment on lines +83 to +90
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably memoize this value, but I'm not 100% convinced that it won't be premature optimization.


const blockProps = useBlockProps( {
style: { width, height },
Expand Down Expand Up @@ -98,6 +119,7 @@ function PostFeaturedImageDisplay( {
clientId={ clientId }
attributes={ attributes }
setAttributes={ setAttributes }
imageSizeOptions={ imageSizeOptions }
/>
<InspectorControls>
<PanelBody title={ __( 'Link settings' ) }>
Expand Down Expand Up @@ -156,7 +178,7 @@ function PostFeaturedImageDisplay( {
placeholderChip
) : (
<img
src={ media.source_url }
src={ mediaUrl }
alt={ media.alt_text || __( 'Featured image' ) }
style={ { height, objectFit: height && scale } }
/>
Expand All @@ -170,7 +192,7 @@ function PostFeaturedImageDisplay( {
<BlockControls group="other">
<MediaReplaceFlow
mediaId={ featuredImage }
mediaURL={ media.source_url }
mediaURL={ mediaUrl }
allowedTypes={ ALLOWED_MEDIA_TYPES }
accept="image/*"
onSelect={ onSelectImage }
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
}
$post_ID = $block->context['postId'];

$featured_image = get_the_post_thumbnail( $post_ID );
$sizeSlug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to fall back to post-thumbnail, so this change doesn't break current behavior.

$featured_image = get_the_post_thumbnail( $post_ID, $sizeSlug );
if ( ! $featured_image ) {
return '';
}
Expand Down