WordPress.org

Make WordPress Core

Changeset 45538


Ignore:
Timestamp:
06/15/2019 01:01:48 AM (5 weeks 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.

Location:
trunk/src
Files:
4 edited

Legend:

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

    r44785 r45538  
    6868
    6969/**
    70  * Generate post thumbnail attachment meta data.
     70 * Compare the existing image sub-sizes (as saved in the attachment meta)
     71 * to the currently registered image sub-sizes, and return the difference.
     72 *
     73 * Registered sub-sizes that are larger than the image are skipped.
     74 *
     75 * @since 5.3.0
     76 *
     77 * @param int $attachment_id The image attachment post ID.
     78 * @return array An array of the image sub-sizes that are currently defined but don't exist for this image.
     79 */
     80function wp_get_missing_image_subsizes( $attachment_id ) {
     81    if ( ! wp_attachment_is_image( $attachment_id ) ) {
     82        return array();
     83    }
     84
     85    $registered_sizes = wp_get_registered_image_subsizes();
     86    $image_meta       = wp_get_attachment_metadata( $attachment_id );
     87
     88    // Meta error?
     89    if ( empty( $image_meta ) ) {
     90        return $defined_sizes;
     91    }
     92
     93    $full_width     = (int) $image_meta['width'];
     94    $full_height    = (int) $image_meta['height'];
     95    $possible_sizes = array();
     96
     97    // Skip registered sizes that are too large for the uploaded image.
     98    foreach ( $registered_sizes as $size_name => $size_data ) {
     99        if ( image_resize_dimensions( $full_width, $full_height, $size_data['width'], $size_data['height'], $size_data['crop'] ) ) {
     100            $possible_sizes[ $size_name ] = $size_data;
     101        }
     102    }
     103
     104    if ( empty( $image_meta['sizes'] ) ) {
     105        $image_meta['sizes'] = array();
     106    }
     107
     108    // Remove sizes that already exist. Only checks for matching "size names".
     109    // It is possible that the dimensions for a particular size name have changed.
     110    // For example the user has changed the values on the Settings -> Media screen.
     111    // However we keep the old sub-sizes with the previous dimensions
     112    // as the image may have been used in an older post.
     113    $missing_sizes = array_diff_key( $possible_sizes, $image_meta['sizes'] );
     114
     115    /**
     116     * Filters the array of missing image sub-sizes for an uploaded image.
     117     *
     118     * @since 5.3.0
     119     *
     120     * @param array $missing_sizes Array with the missing image sub-sizes.
     121     * @param array $image_meta    The image meta data.
     122     * @param int   $attachment_id The image attachment post ID.
     123     */
     124    return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id );
     125}
     126
     127/**
     128 * If any of the currently registered image sub-sizes are missing,
     129 * create them and update the image meta data.
     130 *
     131 * @since 5.3.0
     132 *
     133 * @param int $attachment_id The image attachment post ID.
     134 * @return array The updated image meta data array.
     135 */
     136function wp_update_image_subsizes( $attachment_id ) {
     137    $missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
     138    $image_meta    = wp_get_attachment_metadata( $attachment_id );
     139
     140    if ( empty( $missing_sizes ) ) {
     141        return $image_meta;
     142    }
     143
     144    $image_file = get_attached_file( $attachment_id );
     145
     146    // This also updates the image meta.
     147    return _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
     148}
     149
     150/**
     151 * Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
     152 *
     153 * Intended for use after an image is uploaded. Saves/updates the image metadata after each
     154 * sub-size is created. If there was an error, it is added to the returned image metadata array.
     155 *
     156 * @since 5.3.0
     157 *
     158 * @param string $file          Full path to the image file.
     159 * @param array  $image_meta    The attachment meta data array.
     160 * @param int    $attachment_id Attachment Id to process.
     161 * @return array The attachment metadata with updated `sizes` array. Includes an array of errors encountered while resizing.
     162 */
     163function wp_create_image_subsizes( $file, $image_meta, $attachment_id ) {
     164    if ( empty( $image_meta ) || ! isset( $image_meta['width'], $image_meta['height'] ) ) {
     165        // New uploaded image.
     166        $imagesize            = getimagesize( $file );
     167        $image_meta['width']  = $imagesize[0];
     168        $image_meta['height'] = $imagesize[1];
     169
     170        // Make the file path relative to the upload dir.
     171        $image_meta['file'] = _wp_relative_upload_path( $file );
     172
     173        // Fetch additional metadata from EXIF/IPTC.
     174        $exif_meta = wp_read_image_metadata( $file );
     175
     176        if ( $exif_meta ) {
     177            $image_meta['image_meta'] = $exif_meta;
     178        }
     179    }
     180
     181    $new_sizes = wp_get_registered_image_subsizes();
     182
     183    /**
     184     * Filters the image sizes automatically generated when uploading an image.
     185     *
     186     * @since 2.9.0
     187     * @since 4.4.0 Added the `$image_meta` argument.
     188     * @since 5.3.0 Added the `$attachment_id` argument.
     189     *
     190     * @param array $new_sizes     Associative array of image sizes to be created.
     191     * @param array $image_meta    The image meta data: width, height, file, sizes, etc.
     192     * @param int   $attachment_id The attachment post ID for the image.
     193     */
     194    $new_sizes = apply_filters( 'intermediate_image_sizes_advanced', $new_sizes, $image_meta, $attachment_id );
     195
     196    return _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id );
     197}
     198
     199/**
     200 * Low-level function to create image sub-sizes.
     201 *
     202 * Updates the image meta after each sub-size is created.
     203 * Errors are stored in the returned image metadata array.
     204 *
     205 * @since 5.3.0
     206 * @access private
     207 *
     208 * $padam array  $new_sizes     Array defining what sizes to create.
     209 * @param string $file          Full path to the image file.
     210 * @param array  $image_meta    The attachment meta data array.
     211 * @param int    $attachment_id Attachment Id to process.
     212 * @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
     213 */
     214function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
     215    // Check if any of the new sizes already exist.
     216    if ( isset( $image_meta['sizes'] ) && is_array( $image_meta['sizes'] ) ) {
     217        foreach ( $image_meta['sizes'] as $size_name => $size_meta ) {
     218            // Only checks "size name" so we don't override existing images even if the dimensions
     219            // don't match the currently defined size with the same name.
     220            // To change the behavior, unset changed/mismatched sizes in the `sizes` array in image meta.
     221            if ( array_key_exists( $size_name, $new_sizes ) ) {
     222                unset( $new_sizes[ $size_name ] );
     223            }
     224        }
     225    } else {
     226        $image_meta['sizes'] = array();
     227    }
     228
     229    if ( ! empty( $new_sizes ) ) {
     230        $editor = wp_get_image_editor( $file );
     231
     232        if ( ! is_wp_error( $editor ) ) {
     233            if ( method_exists( $editor, 'make_subsize' ) ) {
     234                foreach ( $new_sizes as $new_size_name => $new_size_data ) {
     235                    $new_size_meta = $editor->make_subsize( $new_size_data );
     236
     237                    if ( is_wp_error( $new_size_meta ) ) {
     238                        if ( empty( $image_meta['subsize_errors'] ) ) {
     239                            $image_meta['subsize_errors'] = array();
     240                        }
     241
     242                        $error = array(
     243                            'error_code'    => $new_size_meta->get_error_code(),
     244                            'error_message' => $new_size_meta->get_error_message(),
     245                        );
     246
     247                        // Store the error code and error message for displaying in the UI.
     248                        $image_meta['subsize_errors'][ $new_size_name ] = $error;
     249                    } else {
     250                        // The sub-size was created successfully.
     251                        // Clear out previous errors in creating this subsize.
     252                        if ( ! empty( $image_meta['subsize_errors'][ $new_size_name ] ) ) {
     253                            unset( $image_meta['subsize_errors'][ $new_size_name ] );
     254                        }
     255
     256                        if ( empty( $image_meta['subsize_errors'] ) ) {
     257                            unset( $image_meta['subsize_errors'] );
     258                        }
     259
     260                        // Save the size meta value.
     261                        $image_meta['sizes'][ $new_size_name ] = $new_size_meta;
     262                    }
     263
     264                    wp_update_attachment_metadata( $attachment_id, $image_meta );
     265                }
     266            } else {
     267                // Fall back to `$editor->multi_resize()`.
     268                $created_sizes = $editor->multi_resize( $new_sizes );
     269
     270                if ( ! empty( $created_sizes ) ) {
     271                    $image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes );
     272                    unset( $image_meta['subsize_errors'] );
     273                    wp_update_attachment_metadata( $attachment_id, $image_meta );
     274                }
     275            }
     276        }
     277    }
     278
     279    return $image_meta;
     280}
     281
     282/**
     283 * Generate attachment meta data and create image sub-sizes for images.
    71284 *
    72285 * @since 2.1.0
     
    84297
    85298    if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) {
    86         $imagesize          = getimagesize( $file );
    87         $metadata['width']  = $imagesize[0];
    88         $metadata['height'] = $imagesize[1];
    89 
    90         // Make the file path relative to the upload dir.
    91         $metadata['file'] = _wp_relative_upload_path( $file );
    92 
    93299        // Make thumbnails and other intermediate sizes.
    94         $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    95 
    96         $sizes = array();
    97         foreach ( get_intermediate_image_sizes() as $s ) {
    98             $sizes[ $s ] = array(
    99                 'width'  => '',
    100                 'height' => '',
    101                 'crop'   => false,
    102             );
    103             if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) {
    104                 // For theme-added sizes
    105                 $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] );
    106             } else {
    107                 // For default sizes set in options
    108                 $sizes[ $s ]['width'] = get_option( "{$s}_size_w" );
    109             }
    110 
    111             if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) {
    112                 // For theme-added sizes
    113                 $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] );
    114             } else {
    115                 // For default sizes set in options
    116                 $sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
    117             }
    118 
    119             if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) {
    120                 // For theme-added sizes
    121                 $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop'];
    122             } else {
    123                 // For default sizes set in options
    124                 $sizes[ $s ]['crop'] = get_option( "{$s}_crop" );
    125             }
    126         }
    127 
    128         /**
    129          * Filters the image sizes automatically generated when uploading an image.
    130          *
    131          * @since 2.9.0
    132          * @since 4.4.0 Added the `$metadata` argument.
    133          * @since 5.1.0 Added the `$attachment_id` argument.
    134          *
    135          * @param array $sizes         An associative array of image sizes.
    136          * @param array $metadata      An associative array of image metadata: width, height, file.
    137          * @param int   $attachment_id Current attachment ID.
    138          */
    139         $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata, $attachment_id );
    140 
    141         if ( $sizes ) {
    142             $editor = wp_get_image_editor( $file );
    143 
    144             if ( ! is_wp_error( $editor ) ) {
    145                 $metadata['sizes'] = $editor->multi_resize( $sizes );
    146             }
    147         } else {
    148             $metadata['sizes'] = array();
    149         }
    150 
    151         // Fetch additional metadata from EXIF/IPTC.
    152         $image_meta = wp_read_image_metadata( $file );
    153         if ( $image_meta ) {
    154             $metadata['image_meta'] = $image_meta;
    155         }
     300        $metadata = wp_create_image_subsizes( $file, $metadata, $attachment_id );
    156301    } elseif ( wp_attachment_is( 'video', $attachment ) ) {
    157302        $metadata = wp_read_video_metadata( $file );
     
    235380        $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata );
    236381
    237         $sizes                      = array();
    238         $_wp_additional_image_sizes = wp_get_additional_image_sizes();
    239 
    240         foreach ( $fallback_sizes as $s ) {
    241             if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) {
    242                 $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] );
    243             } else {
    244                 $sizes[ $s ]['width'] = get_option( "{$s}_size_w" );
    245             }
    246 
    247             if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) {
    248                 $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] );
    249             } else {
    250                 $sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
    251             }
    252 
    253             if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) {
    254                 $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop'];
    255             } else {
    256                 // Force thumbnails to be soft crops.
    257                 if ( 'thumbnail' !== $s ) {
    258                     $sizes[ $s ]['crop'] = get_option( "{$s}_crop" );
    259                 }
    260             }
     382        $defined_sizes = wp_get_registered_image_subsizes();
     383        $merged_sizes  = array_intersect_key( $defined_sizes, array_flip( $fallback_sizes ) );
     384
     385        // Force thumbnails to be soft crops.
     386        if ( isset( $merged_sizes['thumbnail'] ) && is_array( $merged_sizes['thumbnail'] ) ) {
     387            $merged_sizes['thumbnail']['crop'] = false;
    261388        }
    262389
    263390        // Only load PDFs in an image editor if we're processing sizes.
    264         if ( ! empty( $sizes ) ) {
     391        if ( ! empty( $merged_sizes ) ) {
    265392            $editor = wp_get_image_editor( $file );
    266393
     
    283410
    284411                    if ( ! is_wp_error( $editor ) ) {
    285                         $metadata['sizes']         = $editor->multi_resize( $sizes );
     412                        $metadata['sizes']         = $editor->multi_resize( $merged_sizes );
    286413                        $metadata['sizes']['full'] = $uploaded;
    287414                    }
     
    450577    $exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) );
    451578
    452     if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types ) ) {
     579    if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) {
    453580        $exif = @exif_read_data( $file );
    454581
     
    572699    if ( empty( $info ) ) {
    573700        $result = false;
    574     } elseif ( ! in_array( $info[2], $displayable_image_types ) ) {
     701    } elseif ( ! in_array( $info[2], $displayable_image_types, true ) ) {
    575702        $result = false;
    576703    } else {
     
    655782
    656783    if ( $filepath && file_exists( $filepath ) ) {
    657         if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) {
     784        $data = image_get_intermediate_size( $attachment_id, $size );
     785
     786        if ( 'full' != $size && $data ) {
     787            $filepath = path_join( dirname( $filepath ), $data['file'] );
     788
    658789            /**
    659790             * Filters the path to the current image.
     
    667798             * @param string $size          Size of the image.
    668799             */
    669             $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );
    670         }
    671     } elseif ( function_exists( 'fopen' ) && true == ini_get( 'allow_url_fopen' ) ) {
     800            $filepath = apply_filters( 'load_image_to_edit_filesystempath', $filepath, $attachment_id, $size );
     801        }
     802    } elseif ( function_exists( 'fopen' ) && true === ini_get( 'allow_url_fopen' ) ) {
    672803        /**
    673804         * Filters the image URL if not in the local filesystem.
     
    706837 */
    707838function _copy_image_file( $attachment_id ) {
    708     $dst_file = $src_file = get_attached_file( $attachment_id );
     839    $dst_file = get_attached_file( $attachment_id );
     840    $src_file = $dst_file;
     841
    709842    if ( ! file_exists( $src_file ) ) {
    710843        $src_file = _load_image_to_edit_path( $attachment_id );
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r44550 r45538  
    4444        // On some setups GD library does not provide imagerotate() - Ticket #11536
    4545        if ( isset( $args['methods'] ) &&
    46             in_array( 'rotate', $args['methods'] ) &&
     46            in_array( 'rotate', $args['methods'], true ) &&
    4747            ! function_exists( 'imagerotate' ) ) {
    4848
     
    179179    protected function _resize( $max_w, $max_h, $crop = false ) {
    180180        $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
     181
    181182        if ( ! $dims ) {
    182183            return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file );
    183184        }
     185
    184186        list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
    185187
     
    218220     */
    219221    public function multi_resize( $sizes ) {
    220         $metadata  = array();
     222        $metadata = array();
     223
     224        foreach ( $sizes as $size => $size_data ) {
     225            $meta = $this->make_subsize( $size_data );
     226
     227            if ( ! is_wp_error( $meta ) ) {
     228                $metadata[ $size ] = $meta;
     229            }
     230        }
     231
     232        return $metadata;
     233    }
     234
     235    /**
     236     * Create an image sub-size and return the image meta data value for it.
     237     *
     238     * @since 5.3.0
     239     *
     240     * @param array $size_data Array of width, height, and whether to crop.
     241     * @return WP_Error|array WP_Error on error, or the image data array for inclusion in the `sizes` array in the image meta.
     242     */
     243    public function make_subsize( $size_data ) {
     244        if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
     245            return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
     246        }
     247
    221248        $orig_size = $this->size;
    222249
    223         foreach ( $sizes as $size => $size_data ) {
    224             if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
    225                 continue;
    226             }
    227 
    228             if ( ! isset( $size_data['width'] ) ) {
    229                 $size_data['width'] = null;
    230             }
    231             if ( ! isset( $size_data['height'] ) ) {
    232                 $size_data['height'] = null;
    233             }
    234 
    235             if ( ! isset( $size_data['crop'] ) ) {
    236                 $size_data['crop'] = false;
    237             }
    238 
    239             $image     = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
    240             $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
    241 
    242             if ( ! is_wp_error( $image ) && ! $duplicate ) {
    243                 $resized = $this->_save( $image );
    244 
    245                 imagedestroy( $image );
    246 
    247                 if ( ! is_wp_error( $resized ) && $resized ) {
    248                     unset( $resized['path'] );
    249                     $metadata[ $size ] = $resized;
    250                 }
    251             }
    252 
    253             $this->size = $orig_size;
    254         }
    255 
    256         return $metadata;
     250        if ( ! isset( $size_data['width'] ) ) {
     251            $size_data['width'] = null;
     252        }
     253
     254        if ( ! isset( $size_data['height'] ) ) {
     255            $size_data['height'] = null;
     256        }
     257
     258        if ( ! isset( $size_data['crop'] ) ) {
     259            $size_data['crop'] = false;
     260        }
     261
     262        $resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
     263
     264        if ( is_wp_error( $resized ) ) {
     265            $saved = $resized;
     266        } else {
     267            $saved = $this->_save( $resized );
     268            imagedestroy( $resized );
     269        }
     270
     271        $this->size = $orig_size;
     272
     273        if ( ! is_wp_error( $saved ) ) {
     274            unset( $saved['path'] );
     275        }
     276
     277        return $saved;
    257278    }
    258279
     
    392413        }
    393414
    394         if ( 'image/gif' == $mime_type ) {
     415        if ( 'image/gif' === $mime_type ) {
    395416            if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
    396417                return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    397418            }
    398         } elseif ( 'image/png' == $mime_type ) {
     419        } elseif ( 'image/png' === $mime_type ) {
    399420            // convert from full colors to index colors, like original PNG.
    400421            if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) {
     
    405426                return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
    406427            }
    407         } elseif ( 'image/jpeg' == $mime_type ) {
     428        } elseif ( 'image/jpeg' === $mime_type ) {
    408429            if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) {
    409430                return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r42746 r45538  
    109109        // setIteratorIndex is optional unless mime is an animated format.
    110110        // Here, we just say no if you are missing it and aren't loading a jpeg.
    111         if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && $mime_type != 'image/jpeg' ) {
     111        if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && $mime_type !== 'image/jpeg' ) {
    112112                return false;
    113113        }
     
    147147            $filename       = $this->file;
    148148
    149             if ( 'pdf' == $file_extension ) {
     149            if ( 'pdf' === $file_extension ) {
    150150                $filename = $this->pdf_setup();
    151151            }
     
    194194
    195195        try {
    196             if ( 'image/jpeg' == $this->mime_type ) {
     196            if ( 'image/jpeg' === $this->mime_type ) {
    197197                $this->image->setImageCompressionQuality( $quality );
    198198                $this->image->setImageCompression( imagick::COMPRESSION_JPEG );
     
    261261            return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) );
    262262        }
     263
    263264        list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
    264265
     
    313314         * Imagick constant is defined or fall back to our default filter.
    314315         */
    315         if ( in_array( $filter_name, $allowed_filters ) && defined( 'Imagick::' . $filter_name ) ) {
     316        if ( in_array( $filter_name, $allowed_filters, true ) && defined( 'Imagick::' . $filter_name ) ) {
    316317            $filter = constant( 'Imagick::' . $filter_name );
    317318        } else {
     
    362363
    363364            // Set appropriate quality settings after resizing.
    364             if ( 'image/jpeg' == $this->mime_type ) {
     365            if ( 'image/jpeg' === $this->mime_type ) {
    365366                if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) {
    366367                    $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 );
     
    414415     *
    415416     * @param array $sizes {
    416      *     An array of image size arrays. Default sizes are 'small', 'medium', 'medium_large', 'large'.
     417     *     An array of image size arrays. Default sizes are 'thumbnail', 'medium', 'medium_large', 'large'.
    417418     *
    418419     *     Either a height or width must be provided.
     
    431432     */
    432433    public function multi_resize( $sizes ) {
    433         $metadata   = array();
     434        $metadata = array();
     435
     436        foreach ( $sizes as $size => $size_data ) {
     437            $meta = $this->make_subsize( $size_data );
     438
     439            if ( ! is_wp_error( $meta ) ) {
     440                $metadata[ $size ] = $meta;
     441            }
     442        }
     443
     444        return $metadata;
     445    }
     446
     447    /**
     448     * Create an image sub-size and return the image meta data value for it.
     449     *
     450     * @since 5.3.0
     451     *
     452     * @param array $size_data Array of width, height, and whether to crop.
     453     * @return WP_Error|array WP_Error on error, or the image data array for inclusion in the `sizes` array in the image meta.
     454     */
     455    public function make_subsize( $size_data ) {
     456        if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
     457            return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
     458        }
     459
    434460        $orig_size  = $this->size;
    435461        $orig_image = $this->image->getImage();
    436462
    437         foreach ( $sizes as $size => $size_data ) {
    438             if ( ! $this->image ) {
    439                 $this->image = $orig_image->getImage();
    440             }
    441 
    442             if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
    443                 continue;
    444             }
    445 
    446             if ( ! isset( $size_data['width'] ) ) {
    447                 $size_data['width'] = null;
    448             }
    449             if ( ! isset( $size_data['height'] ) ) {
    450                 $size_data['height'] = null;
    451             }
    452 
    453             if ( ! isset( $size_data['crop'] ) ) {
    454                 $size_data['crop'] = false;
    455             }
    456 
    457             $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
    458             $duplicate     = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
    459 
    460             if ( ! is_wp_error( $resize_result ) && ! $duplicate ) {
    461                 $resized = $this->_save( $this->image );
    462 
    463                 $this->image->clear();
    464                 $this->image->destroy();
    465                 $this->image = null;
    466 
    467                 if ( ! is_wp_error( $resized ) && $resized ) {
    468                     unset( $resized['path'] );
    469                     $metadata[ $size ] = $resized;
    470                 }
    471             }
    472 
    473             $this->size = $orig_size;
    474         }
    475 
     463        if ( ! isset( $size_data['width'] ) ) {
     464            $size_data['width'] = null;
     465        }
     466
     467        if ( ! isset( $size_data['height'] ) ) {
     468            $size_data['height'] = null;
     469        }
     470
     471        if ( ! isset( $size_data['crop'] ) ) {
     472            $size_data['crop'] = false;
     473        }
     474
     475        $resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
     476
     477        if ( is_wp_error( $resized ) ) {
     478            $saved = $resized;
     479        } else {
     480            $saved = $this->_save( $this->image );
     481
     482            $this->image->clear();
     483            $this->image->destroy();
     484            $this->image = null;
     485        }
     486
     487        $this->size  = $orig_size;
    476488        $this->image = $orig_image;
    477489
    478         return $metadata;
     490        if ( ! is_wp_error( $saved ) ) {
     491            unset( $saved['path'] );
     492        }
     493
     494        return $saved;
    479495    }
    480496
     
    718734            // Strip profiles.
    719735            foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) {
    720                 if ( ! in_array( $key, $protected_profiles ) ) {
     736                if ( ! in_array( $key, $protected_profiles, true ) ) {
    721737                    $this->image->removeImageProfile( $key );
    722738                }
  • 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.