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,27 +134,56 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
} }
if ( $support && ! empty( $metadata['image']['data'] ) ) { if ( $support && ! empty( $metadata['image']['data'] ) ) {
$ext = '.jpg'; // check for existing cover
switch ( $metadata['image']['mime'] ) { $hash = md5( $metadata['image']['data'] );
case 'image/gif': $posts = get_posts( array(
$ext = '.gif'; 'fields' => 'ids',
break; 'post_type' => 'attachment',
case 'image/png': 'post_mime_type' => $metadata['image']['mime'],
$ext = '.png'; 'post_status' => 'inherit',
break; 'posts_per_page' => 1,
} 'meta_key' => '_cover_hash',
$basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; 'meta_value' => $hash
$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); ) );
if ( false === $uploaded['error'] ) { $exists = reset( $posts );
$attachment = array(
'post_mime_type' => $metadata['image']['mime'], if ( ! empty( $exists ) ) {
'post_type' => 'attachment', update_post_meta( $attachment_id, '_thumbnail_id', $exists );
'post_content' => '', } else {
); $ext = '.jpg';
$sub_attachment_id = wp_insert_attachment( $attachment, $uploaded['file'] ); switch ( $metadata['image']['mime'] ) {
$attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); case 'image/gif':
wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); $ext = '.gif';
update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); break;
case 'image/png':
$ext = '.png';
break;
}
$basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
if ( false === $uploaded['error'] ) {
$image_attachment = array(
'post_mime_type' => $metadata['image']['mime'],
'post_type' => 'attachment',
'post_content' => '',
);
/**
* 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 );
}
} }
} }