WordPress.org

Make WordPress Core

Ticket #26825: 26825.3.diff

File 26825.3.diff, 1.9 KB (added by wonderboymusic, 6 years ago)
  • src/wp-admin/includes/media.php

     
    26362636        <?php
    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/' ) ):
    26422644
     2645                maybe_regenerate_attachment_metadata( $post );
     2646
    26432647                $meta = wp_get_attachment_metadata( $attachment_id );
    26442648                $w = ! empty( $meta['width'] ) ? min( $meta['width'], 600 ) : 0;
    26452649                $h = 0;
  • src/wp-includes/media.php

     
    21942194        $gallery = get_post_gallery( $post, false );
    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}
     2220 No newline at end of file