Skip to content

Set featured image if none exists, based on adding other images#1998

Merged
swissspidy merged 28 commits intoamp-stories-reduxfrom
add/automatic-featured-image
Mar 28, 2019
Merged

Set featured image if none exists, based on adding other images#1998
swissspidy merged 28 commits intoamp-stories-reduxfrom
add/automatic-featured-image

Conversation

@kienstra
Copy link
Copy Markdown
Contributor

If no featured image exists, set it based on:

  • Adding a background image to a page block
  • In the Image block, selecting or uploading an image via the Media Library
  • In the Image block, clicking 'Upload' and uploading an image

Closes #1954

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.
@googlebot googlebot added the cla: yes Signed the Google CLA label Mar 20, 2019
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.
@swissspidy
Copy link
Copy Markdown
Collaborator

Should this include some logic to set the featured image only when the uploaded image is large enough?

@swissspidy
Copy link
Copy Markdown
Collaborator

For reference:

Each poster image should meet the recommended minimum size:

  • Portrait: 696px x 928px
  • Landscape: 928px x 696px
  • Square: 928px x 928px

@kienstra
Copy link
Copy Markdown
Contributor Author

Poster Image

Hi @swissspidy,
Good point, this PR should probably ensure that the image meets the minimum size.

… 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.
@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Mar 22, 2019

Image Dimensions Now Verified

Hi @swissspidy,
Good suggestion that this check for the minimum poster dimensions.

Based on the dimensions you shared, the following images are accepted:
https://lorempixel.com/669/928
https://lorempixel.com/928/669
https://lorempixel.com/928/928

While these dimensions won't be:
https://lorempixel.com/700/700
https://lorempixel.com/900/927

image-size-validation

As mentioned in Issue 1954,
this adds a notice on clicking 'Publish'
if there is no featured image.
@kienstra
Copy link
Copy Markdown
Contributor Author

Pre-publish Notice

There's now a notice on clicking 'Publish' when an AMP Story doesn't have a featured image:
featured-image-here

There was only a conflic in:
amp-story-editor-blocks.js
in the import statements.
/**
* Handles admin notices for AMP Stories.
*/
handleAMPStoryChange: function handleAMPStoryChange() {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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'.
@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Mar 22, 2019

Request For Another Review

Hi @swissspidy,
Thanks for your suggestions. Could you please review this again?

Here's the expected behavior:

  1. On adding an image in the following places, this sets it as the featured image if none exists:

a. The Image block's 'Media Library' or 'Upload' buttons:
image-block

b. The 'Select Media' button under'Background Media':

bg-media

  1. Moves the notice for no featured image from the top of the block editor to above the 'Featured Image' section in the Inspector:

featured-image-notice

  1. On clicking 'Publish' when there's no featured image, displays a notice in the pre-publish panel
    no-feat-image

@kienstra kienstra changed the title [WIP] Set featured image if none exists, based on adding other images Set featured image if none exists, based on adding other images Mar 22, 2019
}

// Conditionally set the uploaded image as the featured image.
const media = wp.data.select( 'core' ).getMedia( selectedBlock.attributes.id );
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I committed your suggestion.

*
* 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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Where is that apiFetch() call happening? Can we somehow avoid using delay() by awaiting the result of that or something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good question, I'll look at this more. The apiFetch() call happens when the MediaUploader calls createMediaFromFile().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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>
@googlebot
Copy link
Copy Markdown

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 cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: no Has not signed the Google CLA and removed cla: yes Signed the Google CLA labels Mar 22, 2019
@westonruter westonruter added cla: yes Signed the Google CLA and removed cla: no Has not signed the Google CLA labels Mar 22, 2019
@googlebot
Copy link
Copy Markdown

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.
@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Mar 23, 2019

Placeholder Poster Image

Hi @westonruter,

So to ensure that this doesn't happen, a placeholder image...should be shown

Sounds good, would you mind if I asked @dawidmlynarz to create an image for that?

@kienstra
Copy link
Copy Markdown
Contributor Author

Additional Poster Attributes

Hi @westonruter,

In addition to poster-portrait-src there should be attributes provided for poster-landscape-src and poster-square-src, per the docs.

Good point. How does c2381f2 look? The poster-square-src and poster-landscape-src attributes are now added:

poster-src

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.
@kienstra
Copy link
Copy Markdown
Contributor Author

Post Featured Image Notice

This adds the following to a normal post:

  1. A notice above the 'Featured Image' panel if it doesn't exist or is the wrong dimension
    featured-image-here

  2. A notice on clicking 'Publish' if the featured image doesn't exist or has the wrong width
    notice-publish

@kienstra
Copy link
Copy Markdown
Contributor Author

kienstra commented Mar 25, 2019

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 );
Copy link
Copy Markdown
Contributor Author

@kienstra kienstra Mar 25, 2019

Choose a reason for hiding this comment

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

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.
@kienstra
Copy link
Copy Markdown
Contributor Author

Request For Another Review

Hi @swissspidy,
Hope you're doing great.

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.
Copy link
Copy Markdown
Collaborator

@swissspidy swissspidy left a comment

Choose a reason for hiding this comment

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

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.

@swissspidy swissspidy merged commit 86892f6 into amp-stories-redux Mar 28, 2019
@swissspidy swissspidy deleted the add/automatic-featured-image branch March 28, 2019 14:27
@kienstra
Copy link
Copy Markdown
Contributor Author

Thanks, @swissspidy! You're right, we should document the fact that selecting a big enough image makes in the featured image (poster-portrait-src).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: no Has not signed the Google CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants