-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Introduce 'editor.PostFeaturedImage.imageSize' filter for Featured Image #8196
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me but since this is about backward compat and featured images are important: it'd be nice to add a test for this.
const postLabel = get( postType, [ 'labels' ], {} ); | ||
|
||
let mediaWidth, | ||
mediaHeight, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's up with the indentation here? If these have to be on their own lines it might just be nicer to have separate let
declarations. 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these have to be on their own lines it might just be nicer to have separate
let
declarations
Is there an eslint for this coding standard?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, but there aren't any instances of:
let foo,
bar,
foobar;
in the code when I looked for it. Conversely, there are several instances of multiple-variable let
declarations:
https://github.com/WordPress/gutenberg/blob/master/packages/date/src/index.js#L285
https://github.com/WordPress/gutenberg/blob/master/packages/components/src/toggle-control/index.js#L35
https://github.com/WordPress/gutenberg/blob/master/packages/components/src/higher-order/with-focus-outside/test/index.js#L17
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My implicit point was: if it's coding style and there's an eslint for it, then it's enforceable. If there isn't a lint for it, then it's a bit perfectionist and detracts from the code review process.
Regardless, I've updated in da643a1
mediaSourceUrl; | ||
if ( media ) { | ||
let mediaSize = 'post-thumbnail'; | ||
mediaSize = applyFilters( 'editor.PostFeaturedImage.imageSize', mediaSize, media.id, currentPostId ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this could just be const mediaSize = applyFilters( 'editor.PostFeaturedImage.imageSize', 'post-thumbnail', media.id, currentPostId );
, then you can ditch the previous line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -163,6 +163,20 @@ var withDataAlign = wp.compose.createHigherOrderComponent( function( BlockListBl | |||
wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-data-align', withDataAlign ); | |||
``` | |||
|
|||
### `editor.PostFeaturedImage.imageSize` | |||
|
|||
Used to modify the image size displayed in the Post Featured Image component. It defaults to `'post-thumbnail'`, and will fail back to the `full` image size when the specified image size doesn't exist on the media object. It's modeled after the `admin_post_thumbnail_size` filter in the Classic Editor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "exist on the media object" should be "exist in the media object".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide a reference I can follow for this test? |
I would think an e2e test that checked for the output of the cover-image after this filter is applied would be the trick. Not sure if there's much prior art as our tests are still a bit slim... it could be that https://github.com/WordPress/gutenberg/blob/master/test/e2e/specs/hooks-api.test.js has examples of how to make hooks work. I'd imagine what we want here is to test the cover image's output before/after the hook is applied by inserting a cover image block and checking its |
This PR impacts the |
Ok. Given the nature of the change, and relative complexity of mocking the test, I'd like to pass on tests for now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not big on how often we punt on tests; they are a fair amount of effort, especially when there are fewer tests for prior art to draw on, but that means less confidence in our code and in refactors.
The code here is fine, so I'm approving this PR, but we hit regressions in nearly every release and I think the only way to stop playing catch-up is to start writing tests alongside code.
That's not particular to this PR though, it's a systemic issue. No reason to single this PR out on it.
🚢
Thanks @tofumatt. I agree generally with your sentiment about tests; I think the likelihood of tests being written increases with the ease of writing them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to integrate the existing admin_post_thumbnail_size
filter and pass its value to Gutenberg?
In the case of similar settings which have some PHP based setup we use editor settings to expose them to the client:
https://github.com/WordPress/gutenberg/blob/master/lib/client-assets.php#L1247-L1248
It isn't a perfect approach as noticed by @aduth. However, it helps us to avoid duplicating existing logic. Should we refactor the code merged in this PR to use editor setting or something similar? I think @youknowriad had an idea of extracting media
module which could keep those kinds of data.
@@ -163,6 +163,20 @@ var withDataAlign = wp.compose.createHigherOrderComponent( function( BlockListBl | |||
wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-data-align', withDataAlign ); | |||
``` | |||
|
|||
### `editor.PostFeaturedImage.imageSize` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This documentation should be located in a different place. It has no connection with extending blocks in the current shape.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, can you provide an alternative suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should create a new doc file (extending-editor.md
?) in the same folder and start adding there all editor related hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to create a new JavaScript-based filter, instead of reusing the existing PHP one, because the filter's return value can change based on post state. If Gutenberg were to use the value of the existing This is the same rationale described in #3315 (comment) |
Description
Adds a
editor.PostFeaturedImage.imageSize
filter for the image size displayed in the Post Featured Image component. Modeling the behavior of the existingadmin_post_thumbnail_size
filter [ref], it defaults to'post-thumbnail'
as the default image size and fails back to the full image size when the specified image size doesn't exist.It appears the current behavior was defined in #894 (71f1d3a) without any specific discussion about media sizes.
Fixes #7505
How has this been tested?
Open a new Gutenberg post and assign a featured image.
Add the following filter in the Console:
Screenshots
Types of changes
Enhancement.
Checklist: