WordPress.org

Make WordPress Core

Changeset 46621


Ignore:
Timestamp:
10/30/2019 09:08:32 PM (2 years ago)
Author:
azaozz
Message:

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 post-processing.

Fixes #48472 for trunk.

File:
1 edited

Legend:

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

    r46565 r46621  
    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     }
    150 
    151     $missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
    152 
    153     if ( empty( $missing_sizes ) ) {
    154         return $image_meta;
    155     }
    156 
    157     // This also updates the image meta.
    158     return _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
     149    } else {
     150        $missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
     151
     152        if ( empty( $missing_sizes ) ) {
     153            return $image_meta;
     154        }
     155
     156        // This also updates the image meta.
     157        $image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
     158    }
     159
     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
     
    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 {
     
    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 {
     
    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
     
    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
Note: See TracChangeset for help on using the changeset viewer.