In `wp_generate_attachment_metadata()`, when an audio or video files contains upload-able image bits in its ID3 tags, only upload it if the image has not already been uploaded. Determine this by checking for a `_cover_hash` value in post meta that matches the `md5` representation of the bits.

This prevents uploading an album of 10 songs and subsequently uploading 10 copies of the same album cover.

Props GregLone for the new filter/filter docs: `'attachment_thumbnail_args'`.
Fixes #27573.
 


git-svn-id: https://develop.svn.wordpress.org/trunk@27863 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2014-03-30 20:53:21 +00:00
parent 149f36ed35
commit 2846655164
1 changed files with 50 additions and 21 deletions

View File

@ -134,6 +134,22 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
}
if ( $support && ! empty( $metadata['image']['data'] ) ) {
// check for existing cover
$hash = md5( $metadata['image']['data'] );
$posts = get_posts( array(
'fields' => 'ids',
'post_type' => 'attachment',
'post_mime_type' => $metadata['image']['mime'],
'post_status' => 'inherit',
'posts_per_page' => 1,
'meta_key' => '_cover_hash',
'meta_value' => $hash
) );
$exists = reset( $posts );
if ( ! empty( $exists ) ) {
update_post_meta( $attachment_id, '_thumbnail_id', $exists );
} else {
$ext = '.jpg';
switch ( $metadata['image']['mime'] ) {
case 'image/gif':
@ -146,17 +162,30 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
$basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
if ( false === $uploaded['error'] ) {
$attachment = array(
$image_attachment = array(
'post_mime_type' => $metadata['image']['mime'],
'post_type' => 'attachment',
'post_content' => '',
);
$sub_attachment_id = wp_insert_attachment( $attachment, $uploaded['file'] );
/**
* Filter the parameters for the attachment thumbnail creation.
*
* @since 3.9.0
*
* @param array $image_attachment An array of parameters to create the thumbnail.
* @param array $metadata Current attachment metadata.
* @param array $uploaded An array containing the thumbnail path and url.
*/
$image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded );
$sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] );
add_post_meta( $sub_attachment_id, '_cover_hash', $hash );
$attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] );
wp_update_attachment_metadata( $sub_attachment_id, $attach_data );
update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
}
}
}
// remove the blob of binary data from the array
if ( isset( $metadata['image']['data'] ) )