Image block lightbox: PHP notices when an image with lightbox is deleted #55347
Labels
[Block] Image
Affects the Image Block
[Status] In Progress
Tracking issues with work in progress
[Type] Bug
An existing feature does not function as intended
Description
Discovered because of an edge case scenario but can be reproduced with a normal flow users can take in the WP admin.
Here:
gutenberg/packages/block-library/src/image/index.php
Lines 170 to 175 in e21581b
we are trying to access the width and height array offsets that may not exist. In fact,
wp_get_attachment_metadata
can returnfalse
instead of an array.On my local development environment, I had to re-import all my attachments via the WordPress importer and the theme unit test data becasue attachments got deleted while running PHP unit tests. By reimporting them, the post attachments got a new ID. Their path in the upload directory was unchanged, because I previously uploaded them in this same month:
/wp-content/uploads/2023/10/
Testing a post with an image block that opens in the lightbox:
wp_get_attachment_metadata
returned false, triggering the PHP notices.A more common scenario that users can potentially trigger by just using the WP admin is when deelting an image from the Media Library. See testing instructione below.
Seems to me that as a good practice, we should always check if an array item is set before trying to access it. Even though it's only a PHP notice that would probably be not visible on production, the code should be more safeguarder.
Step-by-step reproduction instructions
Aside:
when refreshing the post in the Editor, you will get the additional, usual, PHP warning
Cannot modify header information - headers already sent
Screenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
The text was updated successfully, but these errors were encountered: