Make WordPress Core

Changeset 27127


Ignore:
Timestamp:
02/07/2014 10:40:32 PM (11 years ago)
Author:
wonderboymusic
Message:

Introduce maybe_regenerate_attachment_metadata( $attachment ). On the 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.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/media.php

    r27036 r27127  
    26372637    elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'audio/' ) ):
    26382638
     2639        maybe_regenerate_attachment_metadata( $post );
     2640
    26392641        echo wp_audio_shortcode( array( 'src' => $att_url ) );
    26402642
    26412643    elseif ( $attachment_id && 0 === strpos( $post->post_mime_type, 'video/' ) ):
     2644
     2645        maybe_regenerate_attachment_metadata( $post );
    26422646
    26432647        $meta = wp_get_attachment_metadata( $attachment_id );
  • trunk/src/wp-includes/media.php

    r27097 r27127  
    21952195    return empty( $gallery['src'] ) ? array() : $gallery['src'];
    21962196}
     2197
     2198/**
     2199 * If an attachment is missing its metadata, try to regenerate it
     2200 *
     2201 * @param post $attachment Post object.
     2202 */
     2203function maybe_regenerate_attachment_metadata( $attachment ) {
     2204    if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id = (int) $attachment->ID ) ) {
     2205        return;
     2206    }
     2207
     2208    $file = get_attached_file( $attachment_id );
     2209    $meta = wp_get_attachment_metadata( $attachment_id );
     2210    if ( empty( $meta ) && file_exists( $file ) ) {
     2211        $_meta = get_post_meta( $attachment_id );
     2212        $regeneration_lock = 'wp_regenerating_' . $attachment_id;
     2213        if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $regeneration_lock ) ) {
     2214            set_transient( $regeneration_lock, $file );
     2215            wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
     2216            delete_transient( $regeneration_lock );
     2217        }
     2218    }
     2219}
Note: See TracChangeset for help on using the changeset viewer.