From 7abc29aafc028d17b24ea7a59bda41eea65d0e96 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 15 Jun 2021 16:51:54 +0000 Subject: [PATCH] Media: Make sure `wp_generate_attachment_metadata()` always returns an array. This matches the documentation for the filter of the same name. Previously, the function could return `false` for an audio or video attachment that does not exist in the local filesystem. Props Chouby, SergeyBiryukov. Fixes #52603. git-svn-id: https://develop.svn.wordpress.org/trunk@51162 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/image.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index b3b68560b4..5580ee5978 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -470,7 +470,7 @@ function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) { * * @param int $attachment_id Attachment Id to process. * @param string $file Filepath of the Attached image. - * @return mixed Metadata for attachment. + * @return array Metadata for attachment. */ function wp_generate_attachment_metadata( $attachment_id, $file ) { $attachment = get_post( $attachment_id ); @@ -490,6 +490,12 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); } + // wp_read_video_metadata() and wp_read_audio_metadata() return `false` if the attachment + // does not exist in the local filesystem, so make sure to convert the value to an array. + if ( ! is_array( $metadata ) ) { + $metadata = array(); + } + if ( $support && ! empty( $metadata['image']['data'] ) ) { // Check for existing cover. $hash = md5( $metadata['image']['data'] ); @@ -616,9 +622,7 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { } // Remove the blob of binary data from the array. - if ( $metadata ) { - unset( $metadata['image']['data'] ); - } + unset( $metadata['image']['data'] ); /** * Filters the generated attachment meta data.