Skip to content

Commit

Permalink
Added toggle control to set any image as feature image if no feature …
Browse files Browse the repository at this point in the history
…image is set for post (WordPress#65896)

* feature: created toggle control to set content image as feature image if no feature image is set

* Fix: docs build

* revert: php changes for feature image

* feature: created toolbar control to set image as feature image is post don't have feature image set

* feature: created block settings & added proper notices for success message

* update: added required block context for feature image control

* update: set feature image control as per suggestion

* remove: unnessary isFeature image attribute

* rename: feature image control

* Fix: query loop issue

* Fix: typo in set featured image

* update as per suggestions

* Fix: minor feedback

* Fix: minor issue
  • Loading branch information
up1512001 authored Nov 6, 2024
1 parent fe2dcb6 commit 84048f8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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 );
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

0 comments on commit 84048f8

Please sign in to comment.