web-stories-wp
web-stories-wp copied to clipboard
Editor: incorrect poster dimensions being returned
Bug Description
Lucky found this obscure bug where the checklist in the editor shows a warning about incorrect poster dimensions, even though the poster definitely has the right size.
I tracked this down to some unexpected behavior in WordPress that boils down to honoring $content_width
.
This only happens in the admin and not when accessing the REST API in an other way.
Here's how it works:
- In the editor we preload REST API responses, such as the one for the current story (which is handled by
Stories_Controller
) -
\Google\Web_Stories\REST_API\Stories_Controller::get_story_poster()
callswp_get_attachment_image_src()
-
wp_get_attachment_image_src()
in turn callsimage_downsize
-
image_downsize
in turn callsimage_constrain_size_for_editor
-
image_constrain_size_for_editor
then reduces the width and height because we're in the admin (is_admin()
is true because of the REST API preloading)
To work around this, I think we have to call image_get_intermediate_size()
to get the actual size ourselves, and if that returns false
(because the size does not exist), get the original size from wp_get_attachment_metadata()
.
But there might be other options.
Notes:
- This same issue theoretically exists in
\Google\Web_Stories\Model\Story::load_from_post()
- We could just use the
Story
model inStories_Controller
, so we can delete the redundantget_story_poster
method there
Aside:
I find it unexpected that wp_get_attachment_image_src()
honors $content_width
like this. Maybe worth opening a Trac ticket?
Expected Behaviour
There should be no incorrect warning.
Steps to Reproduce
- Activate Twenty Twenty theme
- Create a new story and add a few pages
- Add a poster image by uploading a new image that needs to be cropped & crop it
- Save the story
- Reload the page
- Notice the checklist warning about incorrect poster dimensions
Screenshots
Additional Context
- Plugin Version:
- WordPress Version:
- Operating System:
- Browser:
We have a filter in image_constrain_size_for_editor
called editor_max_image_size
. I wonder if this can be used the expected results.
Maybe worth opening a Trac ticket?
I think it is a good call.
Hmm I guess we could do something like this with that filter:
add_filter( 'editor_max_image_size', $constrain_dimensions );
$poster_src = wp_get_attachment_image_src( $thumbnail_id, Image_Sizes::POSTER_PORTRAIT_IMAGE_SIZE );
remove_filter( 'editor_max_image_size', $constrain_dimensions );
That seems to work
I am trying to think of a solution that would work with BC in mind. I was thinking about adding a filter before the preload and remove after preload is finished. Which might fix other issues in the future.
So basically doing that at a higher level (before the preload) vs. within the controller?
So basically doing that at a higher level (before the preload) vs. within the controller?
Yeah, I would have done it in edit-story.php.
I suppose that could work