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

Added toolbar dropdown action to set any image as feature image #65896

Merged
merged 18 commits into from
Nov 6, 2024
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
2 changes: 1 addition & 1 deletion packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "core/image",
"title": "Image",
"category": "media",
"usesContext": [ "allowResize", "imageCrop", "fixedHeight" ],
"usesContext": [ "allowResize", "imageCrop", "fixedHeight", "postId", "postType", "queryId" ],
"description": "Insert an image to make a visual statement.",
"keywords": [ "img", "photo", "picture" ],
"textdomain": "default",
Expand Down
40 changes: 39 additions & 1 deletion packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ import {
__experimentalUseBorderProps as useBorderProps,
__experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles,
privateApis as blockEditorPrivateApis,
BlockSettingsMenuControls,
} from '@wordpress/block-editor';
import { useEffect, useMemo, useState, useRef } from '@wordpress/element';
import { __, _x, sprintf, isRTL } from '@wordpress/i18n';
import { getFilename } from '@wordpress/url';
import { getBlockBindingsSource, switchToBlockType } from '@wordpress/blocks';
import { crop, overlayText, upload, chevronDown } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';
import { store as coreStore } from '@wordpress/core-data';
import { store as coreStore, useEntityProp } from '@wordpress/core-data';

/**
* Internal dependencies
Expand Down Expand Up @@ -893,6 +894,16 @@ export default function Image( {
const shadowProps = getShadowClassesAndStyles( attributes );
const isRounded = attributes.className?.includes( 'is-style-rounded' );

const { postType, postId, queryId } = context;
const isDescendentOfQueryLoop = Number.isFinite( queryId );

const [ , setFeaturedImage ] = useEntityProp(
'postType',
postType,
'featured_media',
postId
);

let img =
temporaryURL && hasImageErrored ? (
// Show a placeholder during upload when the blob URL can't be loaded. This can
Expand Down Expand Up @@ -1094,10 +1105,37 @@ export default function Image( {
);
}

/**
* Set the post's featured image with the current image.
*/
const setPostFeatureImage = () => {
setFeaturedImage( id );
up1512001 marked this conversation as resolved.
Show resolved Hide resolved
createSuccessNotice( __( 'Post featured image updated.' ), {
type: 'snackbar',
} );
};

const featuredImageControl = (
<BlockSettingsMenuControls>
{ ( { selectedClientIds } ) =>
selectedClientIds.length === 1 &&
! isDescendentOfQueryLoop &&
postId &&
id &&
clientId === selectedClientIds[ 0 ] && (
<MenuItem onClick={ setPostFeatureImage }>
{ __( 'Set featured image' ) }
</MenuItem>
)
}
</BlockSettingsMenuControls>
);

return (
<>
{ mediaReplaceFlow }
{ controls }
{ featuredImageControl }
{ img }

<Caption
Expand Down
Loading