Skip to content

Image block lightbox: PHP notices when an image with lightbox is deleted #55347

Closed

Description

@afercia

Description

Discovered because of an edge case scenario but can be reproduced with a normal flow users can take in the WP admin.

Here:

if ( isset( $block['attrs']['id'] ) ) {
$img_uploaded_src = wp_get_attachment_url( $block['attrs']['id'] );
$img_metadata = wp_get_attachment_metadata( $block['attrs']['id'] );
$img_width = $img_metadata['width'];
$img_height = $img_metadata['height'];
} else {

we are trying to access the width and height array offsets that may not exist. In fact, wp_get_attachment_metadata can return false 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:

  • The image was still visible, both in the Editor and on the front end, because the image path was unchanged.
  • The image actual id was new, although it stayed unchanged in the block markuo.
  • As such, 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

  • Create a post.
  • Add an image from the Media Library and set it to open in the lightbox.
  • Publish and view the front end.
  • Observe everything works as expected.
  • Go to the Media Library.
  • Delete the image that you previously added in the post.
  • View the previous post in the front end, refresh the page if necessary.
  • The image will be 'broken', and that is expected.
  • However these PHP notices are not expected:
PHP Notice:  Trying to access array offset on value of type bool in /var/www/src/wp-content/plugins/gutenberg/build/block-library/blocks/image.php on line 174

PHP Notice:  Trying to access array offset on value of type bool in /var/www/src/wp-content/plugins/gutenberg/build/block-library/blocks/image.php on line 175

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

Activity

  1. added
    [Type] Bug
    [Block] Image
    on Oct 13, 2023
  2. changed the title [-]Image block lighbox: PHP notices when an image with lighbox is deleted[/-] [+]Image block lightbox: PHP notices when an image with lightbox is deleted[/+] on Oct 13, 2023
  3. afercia commented on Oct 13, 2023

    @afercia
    ContributorAuthor

    Given this bug is already in Core trunk, this should be fixed before the 6.4 release.

  4. moved this to Needs Dev / Todo in WordPress 6.4 Editor Taskson Oct 13, 2023
  5. added
    [Status] In Progress
    on Oct 15, 2023
  6. moved this from Needs Dev / Todo to Done in WordPress 6.4 Editor Taskson Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Block] Image[Status] In Progress[Type] Bug

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions