WordPress.org

Make WordPress Core

Opened 2 weeks ago

Closed 12 days ago

Last modified 4 days ago

#54464 closed defect (bug) (fixed)

Twenty Twenty-One get_attachment_image_attributes_image_attributes not checking if variable isset properly

Reported by: wetah Owned by: audrasjb
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: Bundled Theme Keywords: good-first-bug has-patch commit
Focuses: template Cc:

Description

The twenty_twenty_one_get_attachment_image_attributes function, added to the filter wp_get_attachment_image_attributes in inc/template-functions.php incorrectly checks the existence of some variables in this piece of code:

<?php
$meta = wp_get_attachment_metadata( $attachment->ID );
if ( $meta['width'] && $meta['height'] ) {
    $width  = (int) $meta['width'];
    $height = (int) $meta['height'];
}

This causes an Undefined index: width on line 460 error if the result from the function does not set any value to $meta. I believe something like this should be enough:

<?php
$meta = wp_get_attachment_metadata( $attachment->ID );
if ( isset($meta['width']) && isset($meta['height']) ) {
    $width  = (int) $meta['width'];
    $height = (int) $meta['height'];
}

Attachments (1)

54464.diff (774 bytes) - added by hasanuzzamanshamim 13 days ago.
Patch created for width & height validation

Download all attachments as: .zip

Change History (7)

#1 @SergeyBiryukov
2 weeks ago

  • Keywords needs-patch good-first-bug added
  • Milestone changed from Awaiting Review to 5.9

@hasanuzzamanshamim
13 days ago

Patch created for width & height validation

#2 @hasanuzzamanshamim
13 days ago

  • Keywords has-patch added; needs-patch removed

#3 @mukesh27
12 days ago

  • Keywords commit added

Thanks for the patch!

Mark as commit

#4 @audrasjb
12 days ago

  • Owner set to audrasjb
  • Status changed from new to reviewing

Self-assigning for final review then commit consideration

#5 @audrasjb
12 days ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 52217:

Twenty Twenty-One: Prevent notice thrown in twenty_twenty_one_get_attachment_image_attributes().

Calling twenty_twenty_one_get_attachment_image_attributes() was causing an Undefined index: width|height notice to be thrown when the result from wp_get_attachment_metadata() does not set any value to $meta. This change adds an isset() check to prevent it.

Props wetah, hasanuzzamanshamim.
Fixes #54464.

#6 @audrasjb
4 days ago

#54526 was marked as a duplicate.

Note: See TracTickets for help on using tickets.