WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/15/2019 01:01:48 AM (8 months ago)
Author:
azaozz
Message:

Save progress of intermediate image creation after upload. First run.

  • Introduces wp_get_missing_image_subsizes() and wp_update_image_subsizes() to generate image sub-sizes that are missing or were not created after the upload.
  • Adds a way to display errors that happened while creating sub-sizes.
  • Introduces wp_create_image_subsizes() intended for use after an image was uploaded. It saves/updates the image metadata immediately after each sub-size is created. This fixes the (long standing) problem when some of the sub-size image files were created but there was a timeout or an error and the metadata was not saved. Until now such uploads were considered "failed" which usually resulted in the user trying to upload the same image again, creating even more "orphan" image files.

Note that the patch also includes some unrelated WPCS fixes.

Props mikeschroder, azaozz.
See #40439.

File:
1 edited

Legend:

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

    r45270 r45538  
    1818function wp_get_additional_image_sizes() {
    1919    global $_wp_additional_image_sizes;
     20
    2021    if ( ! $_wp_additional_image_sizes ) {
    2122        $_wp_additional_image_sizes = array();
    2223    }
     24
    2325    return $_wp_additional_image_sizes;
    2426}
     
    6769        $max_width  = $size[0];
    6870        $max_height = $size[1];
    69     } elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
     71    } elseif ( $size === 'thumb' || $size === 'thumbnail' ) {
    7072        $max_width  = intval( get_option( 'thumbnail_size_w' ) );
    7173        $max_height = intval( get_option( 'thumbnail_size_h' ) );
     
    7577            $max_height = 96;
    7678        }
    77     } elseif ( $size == 'medium' ) {
     79    } elseif ( $size === 'medium' ) {
    7880        $max_width  = intval( get_option( 'medium_size_w' ) );
    7981        $max_height = intval( get_option( 'medium_size_h' ) );
    8082
    81     } elseif ( $size == 'medium_large' ) {
     83    } elseif ( $size === 'medium_large' ) {
    8284        $max_width  = intval( get_option( 'medium_large_size_w' ) );
    8385        $max_height = intval( get_option( 'medium_large_size_h' ) );
     
    8688            $max_width = min( intval( $content_width ), $max_width );
    8789        }
    88     } elseif ( $size == 'large' ) {
     90    } elseif ( $size === 'large' ) {
    8991        /*
    9092         * We're inserting a large size image into the editor. If it's a really
     
    9597        $max_width  = intval( get_option( 'large_size_w' ) );
    9698        $max_height = intval( get_option( 'large_size_h' ) );
     99
    97100        if ( intval( $content_width ) > 0 ) {
    98101            $max_width = min( intval( $content_width ), $max_width );
    99102        }
    100     } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
     103    } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) {
    101104        $max_width  = intval( $_wp_additional_image_sizes[ $size ]['width'] );
    102105        $max_height = intval( $_wp_additional_image_sizes[ $size ]['height'] );
     
    196199     *                               Default 'medium'.
    197200     */
    198     if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) {
     201    $out = apply_filters( 'image_downsize', false, $id, $size );
     202
     203    if ( $out ) {
    199204        return $out;
    200205    }
     
    202207    $img_url          = wp_get_attachment_url( $id );
    203208    $meta             = wp_get_attachment_metadata( $id );
    204     $width            = $height = 0;
     209    $width            = 0;
     210    $height           = 0;
    205211    $is_intermediate  = false;
    206212    $img_url_basename = wp_basename( $img_url );
     
    220226
    221227    // try for a new style intermediate size
    222     if ( $intermediate = image_get_intermediate_size( $id, $size ) ) {
     228    $intermediate = image_get_intermediate_size( $id, $size );
     229
     230    if ( $intermediate ) {
    223231        $img_url         = str_replace( $img_url_basename, $intermediate['file'], $img_url );
    224232        $width           = $intermediate['width'];
    225233        $height          = $intermediate['height'];
    226234        $is_intermediate = true;
    227     } elseif ( $size == 'thumbnail' ) {
     235    } elseif ( $size === 'thumbnail' ) {
    228236        // fall back to the old thumbnail
    229         if ( ( $thumb_file = wp_get_attachment_thumb_file( $id ) ) && $info = getimagesize( $thumb_file ) ) {
     237        $thumb_file = wp_get_attachment_thumb_file( $id );
     238        $info       = getimagesize( $thumb_file );
     239
     240        if ( $thumb_file && $info ) {
    230241            $img_url         = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url );
    231242            $width           = $info[0];
     
    234245        }
    235246    }
     247
    236248    if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) {
    237249        // any other type: use the real image
     
    246258        return array( $img_url, $width, $height, $is_intermediate );
    247259    }
     260
    248261    return false;
    249 
    250262}
    251263
     
    413425    }
    414426
    415     $width_ratio = $height_ratio = 1.0;
    416     $did_width   = $did_height = false;
     427    $width_ratio  = 1.0;
     428    $height_ratio = 1.0;
     429    $did_width    = false;
     430    $did_height   = false;
    417431
    418432    if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
     
    447461
    448462    // Note: $did_width means it is possible $smaller_ratio == $width_ratio.
    449     if ( $did_width && $w == $max_width - 1 ) {
     463    if ( $did_width && $w === $max_width - 1 ) {
    450464        $w = $max_width; // Round it up
    451465    }
    452466
    453467    // Note: $did_height means it is possible $smaller_ratio == $height_ratio.
    454     if ( $did_height && $h == $max_height - 1 ) {
     468    if ( $did_height && $h === $max_height - 1 ) {
    455469        $h = $max_height; // Round it up
    456470    }
     
    521535     */
    522536    $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
     537
    523538    if ( null !== $output ) {
    524539        return $output;
     
    577592
    578593    // if the resulting image would be the same size or larger we don't want to resize it
    579     if ( $new_w >= $orig_w && $new_h >= $orig_h && $dest_w != $orig_w && $dest_h != $orig_h ) {
     594    if ( $new_w >= $orig_w && $new_h >= $orig_h && intval( $dest_w ) !== intval( $orig_w ) && intval( $dest_h ) !== intval( $orig_h ) ) {
    580595        return false;
    581596    }
     
    688703 */
    689704function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
    690     if ( ! $size || ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) || empty( $imagedata['sizes'] ) ) {
     705    $imagedata = wp_get_attachment_metadata( $post_id );
     706
     707    if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
    691708        return false;
    692709    }
     
    705722        foreach ( $imagedata['sizes'] as $_size => $data ) {
    706723            // If there's an exact match to an existing image size, short circuit.
    707             if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) {
     724            if ( intval( $data['width'] ) === intval( $size[0] ) && intval( $data['height'] ) === intval( $size[1] ) ) {
    708725                $candidates[ $data['width'] * $data['height'] ] = $data;
    709726                break;
     
    786803 */
    787804function get_intermediate_image_sizes() {
    788     $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    789     $image_sizes                = array( 'thumbnail', 'medium', 'medium_large', 'large' ); // Standard sizes
    790     if ( ! empty( $_wp_additional_image_sizes ) ) {
    791         $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
     805    $default_sizes    = array( 'thumbnail', 'medium', 'medium_large', 'large' );
     806    $additional_sizes = wp_get_additional_image_sizes();
     807
     808    if ( ! empty( $additional_sizes ) ) {
     809        $default_sizes = array_merge( $default_sizes, array_keys( $additional_sizes ) );
    792810    }
    793811
     
    797815     * @since 2.5.0
    798816     *
    799      * @param array $image_sizes An array of intermediate image sizes. Defaults
    800      *                           are 'thumbnail', 'medium', 'medium_large', 'large'.
    801      */
    802     return apply_filters( 'intermediate_image_sizes', $image_sizes );
     817     * @param array $default_sizes An array of intermediate image sizes. Defaults
     818     *                             are 'thumbnail', 'medium', 'medium_large', 'large'.
     819     */
     820    return apply_filters( 'intermediate_image_sizes', $default_sizes );
     821}
     822
     823/**
     824 * Returns a normalized list of all currently registered image sub-sizes.
     825 *
     826 * @since 5.3.0
     827 * @uses wp_get_additional_image_sizes()
     828 * @uses get_intermediate_image_sizes()
     829 *
     830 * @return array Associative array of the registered image sub-sizes.
     831 */
     832function wp_get_registered_image_subsizes() {
     833    $additional_sizes = wp_get_additional_image_sizes();
     834    $all_sizes        = array();
     835
     836    foreach ( get_intermediate_image_sizes() as $size_name ) {
     837        $size_data = array(
     838            'width'  => 0,
     839            'height' => 0,
     840            'crop'   => false,
     841        );
     842
     843        if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
     844            // For sizes added by plugins and themes.
     845            $size_data['width'] = intval( $additional_sizes[ $size_name ]['width'] );
     846        } else {
     847            // For default sizes set in options.
     848            $size_data['width'] = intval( get_option( "{$size_name}_size_w" ) );
     849        }
     850
     851        if ( isset( $additional_sizes[ $size_name ]['height'] ) ) {
     852            $size_data['height'] = intval( $additional_sizes[ $size_name ]['height'] );
     853        } else {
     854            $size_data['height'] = intval( get_option( "{$size_name}_size_h" ) );
     855        }
     856
     857        if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) {
     858            // This size isn't set.
     859            continue;
     860        }
     861
     862        if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) {
     863            $size_data['crop'] = (bool) $additional_sizes[ $size_name ]['crop'];
     864        } else {
     865            $size_data['crop'] = (bool) get_option( "{$size_name}_crop" );
     866        }
     867
     868        $all_sizes[ $size_name ] = $size_data;
     869    }
     870
     871    return $all_sizes;
    803872}
    804873
     
    827896        $src = false;
    828897
    829         if ( $icon && $src = wp_mime_type_icon( $attachment_id ) ) {
    830             /** This filter is documented in wp-includes/post.php */
    831             $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
    832 
    833             $src_file                = $icon_dir . '/' . wp_basename( $src );
    834             @list( $width, $height ) = getimagesize( $src_file );
     898        if ( $icon ) {
     899            $src = wp_mime_type_icon( $attachment_id );
     900
     901            if ( $src ) {
     902                /** This filter is documented in wp-includes/post.php */
     903                $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
     904
     905                $src_file                = $icon_dir . '/' . wp_basename( $src );
     906                @list( $width, $height ) = getimagesize( $src_file );
     907            }
    835908        }
    836909
     
    10151088 */
    10161089function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
    1017     if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) {
     1090    $image = wp_get_attachment_image_src( $attachment_id, $size );
     1091
     1092    if ( ! $image ) {
    10181093        return false;
    10191094    }
     
    11471222        // If the file name is part of the `src`, we've confirmed a match.
    11481223        if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
    1149             $src_matched = $is_src = true;
     1224            $src_matched = true;
     1225            $is_src      = true;
    11501226        }
    11511227
     
    12331309 */
    12341310function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
    1235     if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) {
     1311    $image = wp_get_attachment_image_src( $attachment_id, $size );
     1312
     1313    if ( ! $image ) {
    12361314        return false;
    12371315    }
     
    13191397    }
    13201398
    1321     $selected_images = $attachment_ids = array();
     1399    $selected_images = array();
     1400    $attachment_ids  = array();
    13221401
    13231402    foreach ( $matches[0] as $image ) {
    1324         if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
    1325             ( $attachment_id = absint( $class_id[1] ) ) ) {
    1326 
    1327             /*
    1328              * If exactly the same image tag is used more than once, overwrite it.
    1329              * All identical tags will be replaced later with 'str_replace()'.
    1330              */
    1331             $selected_images[ $image ] = $attachment_id;
    1332             // Overwrite the ID when the same image is included more than once.
    1333             $attachment_ids[ $attachment_id ] = true;
     1403        if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) {
     1404            $attachment_id = absint( $class_id[1] );
     1405
     1406            if ( $attachment_id ) {
     1407                /*
     1408                 * If exactly the same image tag is used more than once, overwrite it.
     1409                 * All identical tags will be replaced later with 'str_replace()'.
     1410                 */
     1411                $selected_images[ $image ] = $attachment_id;
     1412                // Overwrite the ID when the same image is included more than once.
     1413                $attachment_ids[ $attachment_id ] = true;
     1414            }
    13341415        }
    13351416    }
     
    15401621     */
    15411622    $output = apply_filters( 'img_caption_shortcode', '', $attr, $content );
    1542     if ( $output != '' ) {
     1623
     1624    if ( ! empty( $output ) ) {
    15431625        return $output;
    15441626    }
     
    15581640
    15591641    $atts['width'] = (int) $atts['width'];
     1642
    15601643    if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) {
    15611644        return $content;
    15621645    }
    15631646
    1564     $id = $caption_id = $describedby = '';
     1647    $id          = '';
     1648    $caption_id  = '';
     1649    $describedby = '';
    15651650
    15661651    if ( $atts['id'] ) {
     
    16041689
    16051690    $style = '';
     1691
    16061692    if ( $caption_width ) {
    16071693        $style = 'style="width: ' . (int) $caption_width . 'px" ';
     
    17061792     */
    17071793    $output = apply_filters( 'post_gallery', '', $attr, $instance );
    1708     if ( $output != '' ) {
     1794
     1795    if ( ! empty( $output ) ) {
    17091796        return $output;
    17101797    }
     
    18511938
    18521939    $i = 0;
     1940
    18531941    foreach ( $attachments as $id => $attachment ) {
    18541942
    18551943        $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : '';
     1944
    18561945        if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
    18571946            $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr );
     
    18611950            $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr );
    18621951        }
     1952
    18631953        $image_meta = wp_get_attachment_metadata( $id );
    18641954
    18651955        $orientation = '';
     1956
    18661957        if ( isset( $image_meta['height'], $image_meta['width'] ) ) {
    18671958            $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
    18681959        }
     1960
    18691961        $output .= "<{$itemtag} class='gallery-item'>";
    18701962        $output .= "
     
    18721964                $image_output
    18731965            </{$icontag}>";
     1966
    18741967        if ( $captiontag && trim( $attachment->post_excerpt ) ) {
    18751968            $output .= "
     
    18781971                </{$captiontag}>";
    18791972        }
     1973
    18801974        $output .= "</{$itemtag}>";
    1881         if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) {
     1975
     1976        if ( ! $html5 && $columns > 0 && ++$i % $columns === 0 ) {
    18821977            $output .= '<br style="clear: both" />';
    18831978        }
     
    20262121     */
    20272122    $output = apply_filters( 'post_playlist', '', $attr, $instance );
    2028     if ( $output != '' ) {
     2123
     2124    if ( ! empty( $output ) ) {
    20292125        return $output;
    20302126    }
     
    23362432     */
    23372433    $override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance );
     2434
    23382435    if ( '' !== $override ) {
    23392436        return $override;
     
    23602457    if ( ! empty( $atts['src'] ) ) {
    23612458        $type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
    2362         if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) {
     2459
     2460        if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
    23632461            return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
    23642462        }
     2463
    23652464        $primary = true;
    23662465        array_unshift( $default_types, 'src' );
     
    23692468            if ( ! empty( $atts[ $ext ] ) ) {
    23702469                $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() );
     2470
    23712471                if ( strtolower( $type['ext'] ) === $ext ) {
    23722472                    $primary = true;
     
    23782478    if ( ! $primary ) {
    23792479        $audios = get_attached_media( 'audio', $post_id );
     2480
    23802481        if ( empty( $audios ) ) {
    23812482            return;
     
    23842485        $audio       = reset( $audios );
    23852486        $atts['src'] = wp_get_attachment_url( $audio->ID );
     2487
    23862488        if ( empty( $atts['src'] ) ) {
    23872489            return;
     
    23992501     */
    24002502    $library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' );
     2503
    24012504    if ( 'mediaelement' === $library && did_action( 'init' ) ) {
    24022505        wp_enqueue_style( 'wp-mediaelement' );
     
    24322535
    24332536    $attr_strings = array();
     2537
    24342538    foreach ( $html_atts as $k => $v ) {
    24352539        $attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
     
    24372541
    24382542    $html = '';
     2543
    24392544    if ( 'mediaelement' === $library && 1 === $instance ) {
    24402545        $html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
    24412546    }
     2547
    24422548    $html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) );
    24432549
    24442550    $fileurl = '';
    24452551    $source  = '<source type="%s" src="%s" />';
     2552
    24462553    foreach ( $default_types as $fallback ) {
    24472554        if ( ! empty( $atts[ $fallback ] ) ) {
     
    24492556                $fileurl = $atts[ $fallback ];
    24502557            }
     2558
    24512559            $type  = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
    24522560            $url   = add_query_arg( '_', $instance, $atts[ $fallback ] );
     
    24582566        $html .= wp_mediaelement_fallback( $fileurl );
    24592567    }
     2568
    24602569    $html .= '</audio>';
    24612570
     
    25452654     */
    25462655    $override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance );
     2656
    25472657    if ( '' !== $override ) {
    25482658        return $override;
     
    25832693    }
    25842694
    2585     $is_vimeo      = $is_youtube = false;
     2695    $is_vimeo      = false;
     2696    $is_youtube    = false;
    25862697    $yt_pattern    = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
    25872698    $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#';
     
    25912702        $is_vimeo   = ( preg_match( $vimeo_pattern, $atts['src'] ) );
    25922703        $is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) );
     2704
    25932705        if ( ! $is_youtube && ! $is_vimeo ) {
    25942706            $type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
    2595             if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) {
     2707
     2708            if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
    25962709                return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
    25972710            }
     
    28142927
    28152928    foreach ( $attachments as $k => $attachment ) {
    2816         if ( $attachment->ID == $post->ID ) {
     2929        if ( intval( $attachment->ID ) === intval( $post->ID ) ) {
    28172930            break;
    28182931        }
     
    28672980        $attachment = (object) $attachment;
    28682981    }
     2982
    28692983    if ( ! is_object( $attachment ) ) {
    28702984        return array();
     
    28792993        $objects[] = 'attachment:' . substr( $filename, strrpos( $filename, '.' ) + 1 );
    28802994    }
     2995
    28812996    if ( ! empty( $attachment->post_mime_type ) ) {
    28822997        $objects[] = 'attachment:' . $attachment->post_mime_type;
     2998
    28832999        if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
    28843000            foreach ( explode( '/', $attachment->post_mime_type ) as $token ) {
     
    28913007
    28923008    $taxonomies = array();
     3009
    28933010    foreach ( $objects as $object ) {
    2894         if ( $taxes = get_object_taxonomies( $object, $output ) ) {
     3011        $taxes = get_object_taxonomies( $object, $output );
     3012
     3013        if ( $taxes ) {
    28953014            $taxonomies = array_merge( $taxonomies, $taxes );
    28963015        }
     
    29183037function get_taxonomies_for_attachments( $output = 'names' ) {
    29193038    $taxonomies = array();
     3039
    29203040    foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
    29213041        foreach ( $taxonomy->object_type as $object_type ) {
    2922             if ( 'attachment' == $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {
    2923                 if ( 'names' == $output ) {
     3042            if ( 'attachment' === $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {
     3043                if ( 'names' === $output ) {
    29243044                    $taxonomies[] = $taxonomy->name;
    29253045                } else {
     
    31943314 */
    31953315function wp_prepare_attachment_for_js( $attachment ) {
    3196     if ( ! $attachment = get_post( $attachment ) ) {
     3316    $attachment = get_post( $attachment );
     3317
     3318    if ( ! $attachment ) {
    31973319        return;
    31983320    }
    31993321
    3200     if ( 'attachment' != $attachment->post_type ) {
     3322    if ( 'attachment' !== $attachment->post_type ) {
    32013323        return;
    32023324    }
     
    33173439
    33183440            /** This filter is documented in wp-includes/media.php */
    3319             if ( $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) ) {
     3441            $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size );
     3442
     3443            if ( $downsize ) {
    33203444                if ( empty( $downsize[3] ) ) {
    33213445                    continue;
     
    38173941 */
    38183942function get_attached_media( $type, $post = 0 ) {
    3819     if ( ! $post = get_post( $post ) ) {
     3943    $post = get_post( $post );
     3944
     3945    if ( ! $post ) {
    38203946        return array();
    38213947    }
     
    39074033 */
    39084034function get_post_galleries( $post, $html = true ) {
    3909     if ( ! $post = get_post( $post ) ) {
     4035    $post = get_post( $post );
     4036
     4037    if ( ! $post ) {
    39104038        return array();
    39114039    }
     
    40284156 */
    40294157function wp_maybe_generate_attachment_metadata( $attachment ) {
    4030     if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id = (int) $attachment->ID ) ) {
     4158    if ( empty( $attachment ) || empty( $attachment->ID ) ) {
    40314159        return;
    40324160    }
    40334161
    4034     $file = get_attached_file( $attachment_id );
    4035     $meta = wp_get_attachment_metadata( $attachment_id );
     4162    $attachment_id = (int) $attachment->ID;
     4163    $file          = get_attached_file( $attachment_id );
     4164    $meta          = wp_get_attachment_metadata( $attachment_id );
     4165
    40364166    if ( empty( $meta ) && file_exists( $file ) ) {
    4037         $_meta             = get_post_meta( $attachment_id );
    4038         $regeneration_lock = 'wp_generating_att_' . $attachment_id;
    4039         if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $regeneration_lock ) ) {
    4040             set_transient( $regeneration_lock, $file );
     4167        $_meta = get_post_meta( $attachment_id );
     4168        $_lock = 'wp_generating_att_' . $attachment_id;
     4169
     4170        if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $_lock ) ) {
     4171            set_transient( $_lock, $file );
    40414172            wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
    4042             delete_transient( $regeneration_lock );
     4173            delete_transient( $_lock );
    40434174        }
    40444175    }
     
    40734204    }
    40744205
    4075     $sql     = $wpdb->prepare(
     4206    $sql = $wpdb->prepare(
    40764207        "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
    40774208        $path
    40784209    );
     4210
    40794211    $post_id = $wpdb->get_var( $sql );
    40804212
Note: See TracChangeset for help on using the changeset viewer.