Make WordPress Core

Opened 3 months ago

Last modified 3 months ago

#60480 assigned defect (bug)

Warning: Undefined array key "file" in .../wp-includes/media.php on line 1723

Reported by: shanemac10's profile shanemac10 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version: 6.4.3
Component: Import Keywords:
Focuses: Cc:

Description

When running Tools > Import > Run Importer, then selecting "Download and import file attachments" on the import options, the media (what appears to be a native Featured Image in my case) does not import, and displays this following message for each failed media import:

Warning: Undefined array key "file" in .../wp-includes/media.php on line 1723

When looking into the matter, it appears in wp_img_tag_add_srcset_and_sizes_attr on line 2154 and 2155 where wp_get_attachment_metadata is called, which may return false, then it's sent directly into wp_image_add_srcset_and_sizes, where it (on 1723) tests a preg_match on it without testing if $image_meta is an array or if key_exists, etc.

2154  $image_meta = wp_get_attachment_metadata( $attachment_id ); // <--- returns array|false
2155  return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id );
1723  if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) // <--- $image_meta may be false

Get blame says this code has been around for over 4 years.
https://github.com/WordPress/wordpress-develop/blame/6.4/src/wp-includes/media.php#L2138-L2159

My scenario is importing a custom post type (that is identical to generic post type) with only WYSIWYG content and a featured image, to a local dev site on my personal computer from a public site (w/ full DNS with SSL cert), so there should not be an issue with fetching the images I believe. Either way, this error is a bug, and needs a way to fail more gracefully if the issue is else where in the Import process.

Change History (1)

#1 @shanemac10
3 months ago

As an additional note: I made the following change to the code, and re-ran the import. The images did not import, and there were no errors printed either.

Added...

($image_meta['file'] ?? false) &&

Like so...

1723. if (($image_meta['file'] ?? false) && preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash )
Note: See TracTickets for help on using tickets.