Set featured image if none exists, based on adding other images#1998
Set featured image if none exists, based on adding other images#1998swissspidy merged 28 commits intoamp-stories-reduxfrom
Conversation
But only if there isn't a featured image. @todo: consider this for the image block.
On selecting an image in the Image block's Media Library, check if there's a featured image. If not, set this as the featured image. This doesn't apply to the 'Upload' or 'Insert from URL' buttons.
When clicking 'Upload' and uploading an image, set it as the featured image if there is none.
There's now a handler for the MediaUpload component, so that it sets the featured image if there is none. So this logic isn't needed anymore.
When setting the featured image via the Imge block or the Story page's 'Background Media' control, hide the admin notice that there is no featured image.
Mainly copy the logic from amp-block-validation.js for the module with-featured-image-notice.js.
|
Should this include some logic to set the featured image only when the uploaded image is large enough? |
|
For reference: Each poster image should meet the recommended minimum size:
|
|
Poster Image Hi @swissspidy, |
… featured image As Pascal mentioned, it'll be good to ensure that an image has the correct poster image dimensions before automatically setting it as the featured image.
|
Image Dimensions Now Verified Hi @swissspidy, Based on the dimensions you shared, the following images are accepted: While these dimensions won't be: |
As mentioned in Issue 1954, this adds a notice on clicking 'Publish' if there is no featured image.
There was only a conflic in: amp-story-editor-blocks.js in the import statements.
| /** | ||
| * Handles admin notices for AMP Stories. | ||
| */ | ||
| handleAMPStoryChange: function handleAMPStoryChange() { |
There was a problem hiding this comment.
This notice at the top of the entire editor is replaced by a notice above the Featured Image control in with-featured-image-notice.js.
One is a typo, using 'not' instead of 'no'. The other is a change from 'display' to 'this displays'.
|
Request For Another Review Hi @swissspidy, Here's the expected behavior:
a. The Image block's 'Media Library' or 'Upload' buttons: b. The 'Select Media' button under'Background Media':
|
| } | ||
|
|
||
| // Conditionally set the uploaded image as the featured image. | ||
| const media = wp.data.select( 'core' ).getMedia( selectedBlock.attributes.id ); |
There was a problem hiding this comment.
| const media = wp.data.select( 'core' ).getMedia( selectedBlock.attributes.id ); | |
| const media = select( 'core' ).getMedia( selectedBlock.attributes.id ); |
You already import select at the top 🙂
| * | ||
| * If there's no featured image, set this image as the featured image. | ||
| * This component unmounts right after uploading an image. | ||
| * But this needs to be delayed, as there's an apiFetch() call that gets the id. |
There was a problem hiding this comment.
Where is that apiFetch() call happening? Can we somehow avoid using delay() by awaiting the result of that or something?
There was a problem hiding this comment.
Good question, I'll look at this more. The apiFetch() call happens when the MediaUploader calls createMediaFromFile().
There was a problem hiding this comment.
7a3df1a prevents using delay() by instead filtering BlockEdit.
As Pascal mentioned, it already imports '@wordpress/data` at the top Co-Authored-By: kienstra <kienstraryan@gmail.com>
|
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the ℹ️ Googlers: Go here for more info. |
|
A Googler has manually verified that the CLAs look good. (Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.) ℹ️ Googlers: Go here for more info. |
I introduced a regression where this didn't work anymore, as it uses attributes.mediaId, instead of attributes.id
As Weston suggested, accept the dimensions as parameters. This will allow it to be used for the post type of 'post' also.
As Weston mentioned, these should also be included. So set these if they exist.
|
Placeholder Poster Image Hi @westonruter,
Sounds good, would you mind if I asked @dawidmlynarz to create an image for that? |
|
Additional Poster Attributes Hi @westonruter,
Good point. How does c2381f2 look? The |
Abstract this into the function getFeaturedImageNotice() this can be used for the post featured image, also.
Using the logic for AMP stories, add a notice to normal posts if the featured image has a width less than 1200px.
Pass different arguments, as the post requires that a featured image have a width of 1200px.
Including adding JSDoc tags, and spaces before and after brackets.
This might be the cause of a failed Travis build.
|
Also Adds Size Validation To AMP Story This also adds the invalid size notices shown above to AMP stories. Before, there were only notices if the featured images didn't exist at all. |
There were some functions that had no JSDocs, so add some.
| * @return {boolean} Whether the media has the minimum dimensions. | ||
| */ | ||
| const hasMinimumFeaturedImageWidth = ( media ) => { | ||
| return ( media.width && media.width >= 1200 ); |
There was a problem hiding this comment.
If it's alright, I used an entirely different function for normal posts. This function for normal posts is simpler than the function to check that an AMP Story's featured image has the right dimensions.
These have dimensions of 696 and 928, so update the image sizes.
Add a simple .jpg for the poster-portrait-src, in case there is no featured image. There will still be notices in the block editor, but at least the AMP Story will display. Before, it didn't display at all with no featured image.
|
Request For Another Review Hi @swissspidy, When you have a chance, could you please review this PR again? The main changes lately are adding notices if the featured image sizes are wrong in AMP Stories and posts, and a fallback image if there's no featured image. Thanks, Pascal! And congratulations on speaking at WordCamp Europe. |
It’s not actually a UI component, just a string, which is better suited here.
It’s not actually a UI component, just a boolean callback, which is better suited here.
swissspidy
left a comment
There was a problem hiding this comment.
Let's ship it! 🚢
I still have to get used to the fact that any big enough image I add to the story suddenly ends up as the featured image. We should document this well.
The other really minor thing is that the default fallback image does not translate well. One does never really see it while writing anyway, so I guess it's fine.
Further polishing can be done in separate PRs if needed.
|
Thanks, @swissspidy! You're right, we should document the fact that selecting a big enough image makes in the featured image (poster-portrait-src). |









If no featured image exists, set it based on:
Closes #1954