From 9ef78246d8bddcc7d70a520d692ba10a85a34bd6 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Tue, 5 Nov 2019 18:44:02 +0000 Subject: [PATCH] Upload: - Run the `wp_generate_attachment_metadata` filter at the end in `wp_update_image_subsizes()` when new metadata was generated and additional image sub-sizes were created. - Add another arg in the `wp_generate_attachment_metadata` filter for additional context. - Fix inline docs and ensure the new image meta is always saved before starting image post-processing. Props SergeyBiryukov, azaozz. Merges [46621], [46622], and [46651] to the 5.3 branch. Fixes #48472. Built from https://develop.svn.wordpress.org/branches/5.3@46655 git-svn-id: http://core.svn.wordpress.org/branches/5.3@46455 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/image.php | 40 ++++++++++++++++++++++++------------- wp-includes/version.php | 2 +- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index d58bdc88f6..475f2867b2 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -142,20 +142,28 @@ function wp_update_image_subsizes( $attachment_id ) { // Previously failed upload? // If there is an uploaded file, make all sub-sizes and generate all of the attachment meta. if ( ! empty( $image_file ) ) { - return wp_create_image_subsizes( $image_file, $attachment_id ); + $image_meta = wp_create_image_subsizes( $image_file, $attachment_id ); } else { return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) ); } + } else { + $missing_sizes = wp_get_missing_image_subsizes( $attachment_id ); + + if ( empty( $missing_sizes ) ) { + return $image_meta; + } + + // This also updates the image meta. + $image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id ); } - $missing_sizes = wp_get_missing_image_subsizes( $attachment_id ); + /** This filter is documented in wp-admin/includes/image.php */ + $image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' ); - if ( empty( $missing_sizes ) ) { - return $image_meta; - } + // Save the updated metadata. + wp_update_attachment_metadata( $attachment_id, $image_meta ); - // This also updates the image meta. - return _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id ); + return $image_meta; } /** @@ -274,8 +282,6 @@ function wp_create_image_subsizes( $file, $attachment_id ) { if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) { $image_meta['image_meta']['orientation'] = 1; } - - wp_update_attachment_metadata( $attachment_id, $image_meta ); } else { // TODO: log errors. } @@ -306,14 +312,17 @@ function wp_create_image_subsizes( $file, $attachment_id ) { if ( ! empty( $image_meta['image_meta']['orientation'] ) ) { $image_meta['image_meta']['orientation'] = 1; } - - wp_update_attachment_metadata( $attachment_id, $image_meta ); } else { // TODO: log errors. } } } + // Initial save of the new metadata. + // At this point the file was uploaded and moved to the uploads directory + // but the image sub-sizes haven't been created yet and the `sizes` array is empty. + wp_update_attachment_metadata( $attachment_id, $image_meta ); + $new_sizes = wp_get_registered_image_subsizes(); /** @@ -577,11 +586,14 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { * Filters the generated attachment meta data. * * @since 2.1.0 + * @since 5.3.0 The `$context` parameter was added. * - * @param array $metadata An array of attachment meta data. - * @param int $attachment_id Current attachment ID. + * @param array $metadata An array of attachment meta data. + * @param int $attachment_id Current attachment ID. + * @param string $context Additional context. Can be 'create' when metadata was initially created for new attachment + * or 'update' when the metadata was updated. */ - return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); + return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 7c58b1e8f1..47eef5b515 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-RC3-46653'; +$wp_version = '5.3-RC3-46655'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.