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.
Built from https://develop.svn.wordpress.org/trunk@51162


git-svn-id: http://core.svn.wordpress.org/trunk@50771 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2021-06-15 16:52:56 +00:00
parent 870a494015
commit 4803207ba1
2 changed files with 9 additions and 5 deletions

View File

@ -470,7 +470,7 @@ function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
* *
* @param int $attachment_id Attachment Id to process. * @param int $attachment_id Attachment Id to process.
* @param string $file Filepath of the Attached image. * @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 ) { function wp_generate_attachment_metadata( $attachment_id, $file ) {
$attachment = get_post( $attachment_id ); $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' ); $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'] ) ) { if ( $support && ! empty( $metadata['image']['data'] ) ) {
// Check for existing cover. // Check for existing cover.
$hash = md5( $metadata['image']['data'] ); $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. // 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. * Filters the generated attachment meta data.

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.8-beta1-51161'; $wp_version = '5.8-beta1-51162';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.