Skip to content

Commit

Permalink
Introduce maybe_regenerate_attachment_metadata( $attachment ). On t…
Browse files Browse the repository at this point in the history
…he Edit Media screen, call it for Audio and Video files.

The functions checks if the item is lacking metadata altogether. If a video or audio file was uploaded prior to 3.6, it does not have any metadata. This tries to fix it. Implements locking via a transient to protect against this running in parallel with another request.

This is the minimum viable product for #26825, but leaving the ticket open unless this function needs to be called in other places.

See #26825.


Built from https://develop.svn.wordpress.org/trunk@27127


git-svn-id: http://core.svn.wordpress.org/trunk@26994 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
staylor committed Feb 7, 2014
1 parent f1fff8a commit 03dda3b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions wp-admin/includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2636,10 +2636,14 @@ function edit_form_image_editor( $post ) {
<?php
elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'audio/' ) ):

maybe_regenerate_attachment_metadata( $post );

echo wp_audio_shortcode( array( 'src' => $att_url ) );

elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'video/' ) ):

maybe_regenerate_attachment_metadata( $post );

$meta = wp_get_attachment_metadata( $attachment_id );
$w = ! empty( $meta['width'] ) ? min( $meta['width'], 600 ) : 0;
$h = 0;
Expand Down
23 changes: 23 additions & 0 deletions wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -2194,3 +2194,26 @@ function get_post_gallery_images( $post = 0 ) {
$gallery = get_post_gallery( $post, false );
return empty( $gallery['src'] ) ? array() : $gallery['src'];
}

/**
* If an attachment is missing its metadata, try to regenerate it
*
* @param post $attachment Post object.
*/
function maybe_regenerate_attachment_metadata( $attachment ) {
if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id = (int) $attachment->ID ) ) {
return;
}

$file = get_attached_file( $attachment_id );
$meta = wp_get_attachment_metadata( $attachment_id );
if ( empty( $meta ) && file_exists( $file ) ) {
$_meta = get_post_meta( $attachment_id );
$regeneration_lock = 'wp_regenerating_' . $attachment_id;
if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $regeneration_lock ) ) {
set_transient( $regeneration_lock, $file );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
delete_transient( $regeneration_lock );
}
}
}

0 comments on commit 03dda3b

Please sign in to comment.