Make WordPress Core

Ticket #48472: 48472.diff

File 48472.diff, 3.1 KB (added by azaozz, 5 years ago)
  • src/wp-admin/includes/image.php

     
    142142                // Previously failed upload?
    143143                // If there is an uploaded file, make all sub-sizes and generate all of the attachment meta.
    144144                if ( ! empty( $image_file ) ) {
    145                         return wp_create_image_subsizes( $image_file, $attachment_id );
     145                        $image_meta = wp_create_image_subsizes( $image_file, $attachment_id );
    146146                } else {
    147147                        return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
    148148                }
    149         }
     149        } else {
     150                $missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
    150151
    151         $missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
     152                if ( empty( $missing_sizes ) ) {
     153                        return $image_meta;
     154                }
    152155
    153         if ( empty( $missing_sizes ) ) {
    154                 return $image_meta;
     156                // This also updates the image meta.
     157                $image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
    155158        }
    156159
    157         // This also updates the image meta.
    158         return _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
     160        /** This filter is documented in wp-admin/includes/image.php */
     161        $image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' );
     162
     163        // Save the updated metadata.
     164        wp_update_attachment_metadata( $attachment_id, $image_meta );
     165
     166        return $image_meta;
    159167}
    160168
    161169/**
     
    275283                                        $image_meta['image_meta']['orientation'] = 1;
    276284                                }
    277285
     286                                // Initial save of the new metadata when the original image was scaled.
     287                                // At this point the file was uploaded and moved to the uploads directory
     288                                // but the image sub-sizes haven't been created yet and the `sizes` array is empty.
    278289                                wp_update_attachment_metadata( $attachment_id, $image_meta );
    279290                        } else {
    280291                                // TODO: log errors.
     
    307318                                        $image_meta['image_meta']['orientation'] = 1;
    308319                                }
    309320
     321                                // Initial save of the new metadata when the original image was rotated.
    310322                                wp_update_attachment_metadata( $attachment_id, $image_meta );
    311323                        } else {
    312324                                // TODO: log errors.
    313325                        }
    314326                }
     327        } else {
     328                // Initial save of the new metadata when the image was not scaled or rotated.
     329                wp_update_attachment_metadata( $attachment_id, $image_meta );
    315330        }
    316331
    317332        $new_sizes = wp_get_registered_image_subsizes();
     
    578593         *
    579594         * @since 2.1.0
    580595         *
    581          * @param array $metadata      An array of attachment meta data.
    582          * @param int   $attachment_id Current attachment ID.
     596         * @param array  $metadata      An array of attachment meta data.
     597         * @param int    $attachment_id Current attachment ID.
     598         * @param string $context       Additional context. Can be 'create' when metadata was initially created for new attachment
     599         *                              or 'update' when the metadata was updated.
    583600         */
    584         return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
     601        return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' );
    585602}
    586603
    587604/**