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

Media Text Block: Image size option for featured image #67464

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
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
25 changes: 16 additions & 9 deletions packages/block-library/src/media-text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function getImageSourceUrlBySizeSlug( image, slug ) {
}

function attributesFromMedia( {
attributes: { linkDestination, href },
attributes: { linkDestination, href, mediaSizeSlug },
setAttributes,
} ) {
return ( media ) => {
Expand All @@ -76,6 +76,7 @@ function attributesFromMedia( {
mediaLink: undefined,
href: undefined,
focalPoint: undefined,
useFeaturedImage: false,
} );
return;
}
Expand All @@ -101,11 +102,10 @@ function attributesFromMedia( {
}

if ( mediaType === 'image' ) {
// Try the "large" size URL, falling back to the "full" size URL below.
// Get the URL for the selected image size, falling back to the full size.
src =
media.sizes?.large?.url ||
// eslint-disable-next-line camelcase
media.media_details?.sizes?.large?.source_url;
media.sizes?.[ mediaSizeSlug ]?.url ||
media?.media_details?.sizes?.[ mediaSizeSlug ]?.source_url;
}

let newHref = href;
Expand Down Expand Up @@ -174,7 +174,8 @@ function MediaTextEdit( {
);

const featuredImageURL = useFeaturedImage
? featuredImageMedia?.source_url
? featuredImageMedia?.media_details?.sizes?.[ mediaSizeSlug ]
?.source_url ?? featuredImageMedia?.source_url
: '';
const featuredImageAlt = useFeaturedImage
? featuredImageMedia?.alt_text
Expand Down Expand Up @@ -261,11 +262,17 @@ function MediaTextEdit( {
setAttributes( { verticalAlignment: alignment } );
};

const currentImageMedia = useFeaturedImage ? featuredImageMedia : image;
const imageSizeOptions = imageSizes
.filter( ( { slug } ) => getImageSourceUrlBySizeSlug( image, slug ) )
.filter( ( { slug } ) =>
getImageSourceUrlBySizeSlug( currentImageMedia, slug )
)
.map( ( { name, slug } ) => ( { value: slug, label: name } ) );
const updateImage = ( newMediaSizeSlug ) => {
const newUrl = getImageSourceUrlBySizeSlug( image, newMediaSizeSlug );
const newUrl = getImageSourceUrlBySizeSlug(
currentImageMedia,
newMediaSizeSlug
);

if ( ! newUrl ) {
return null;
Expand Down Expand Up @@ -408,7 +415,7 @@ function MediaTextEdit( {
/>
</ToolsPanelItem>
) }
{ mediaType === 'image' && ! useFeaturedImage && (
{ mediaType === 'image' && (
<ResolutionTool
value={ mediaSizeSlug }
options={ imageSizeOptions }
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/media-text/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function render_block_core_media_text( $attributes, $content ) {
update_post_thumbnail_cache();
}

$current_featured_image = get_the_post_thumbnail_url();
$current_featured_image = get_the_post_thumbnail_url( null, $attributes['mediaSizeSlug'] ?? null );
if ( ! $current_featured_image ) {
return $content;
}
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/media-text/media-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ function MediaContainer( props, ref ) {
}
mediaId={ mediaId }
toggleUseFeaturedImage={ toggleUseFeaturedImage }
useFeaturedImage={ useFeaturedImage }
/>
{ ( mediaTypeRenderers[ mediaType ] || noop )() }
{ isTemporaryMedia && <Spinner /> }
Expand Down
18 changes: 10 additions & 8 deletions packages/block-library/src/media-text/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function save( { attributes } ) {
href,
linkTarget,
rel,
useFeaturedImage,
} = attributes;
const mediaSizeSlug = attributes.mediaSizeSlug || DEFAULT_MEDIA_SIZE_SLUG;
const newRel = ! rel ? undefined : rel;
Expand All @@ -46,14 +47,15 @@ export default function save( { attributes } ) {
? imageFillStyles( mediaUrl, focalPoint )
: {};

let image = mediaUrl ? (
<img
src={ mediaUrl }
alt={ mediaAlt }
className={ imageClasses || null }
style={ positionStyles }
/>
) : null;
let image =
! useFeaturedImage && mediaUrl ? (
<img
src={ mediaUrl }
alt={ mediaAlt }
className={ imageClasses || null }
style={ positionStyles }
/>
) : null;

if ( href ) {
image = (
Expand Down
Loading