Make WordPress Core

Changeset 46655


Ignore:
Timestamp:
11/05/2019 06:42:43 PM (5 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 image post-processing.

Props SergeyBiryukov, azaozz.
Merges [46621], [46622], and [46651] to the 5.3 branch.
Fixes #48472.

File:
1 edited

Legend:

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

    r46565 r46655  
    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
     
    275283                    $image_meta['image_meta']['orientation'] = 1;
    276284                }
    277 
    278                 wp_update_attachment_metadata( $attachment_id, $image_meta );
    279285            } else {
    280286                // TODO: log errors.
     
    307313                    $image_meta['image_meta']['orientation'] = 1;
    308314                }
    309 
    310                 wp_update_attachment_metadata( $attachment_id, $image_meta );
    311315            } else {
    312316                // TODO: log errors.
     
    314318        }
    315319    }
     320
     321    // Initial save of the new metadata.
     322    // At this point the file was uploaded and moved to the uploads directory
     323    // but the image sub-sizes haven't been created yet and the `sizes` array is empty.
     324    wp_update_attachment_metadata( $attachment_id, $image_meta );
    316325
    317326    $new_sizes = wp_get_registered_image_subsizes();
     
    578587     *
    579588     * @since 2.1.0
    580      *
    581      * @param array $metadata      An array of attachment meta data.
    582      * @param int   $attachment_id Current attachment ID.
     589     * @since 5.3.0 The `$context` parameter was added.
     590     *
     591     * @param array  $metadata      An array of attachment meta data.
     592     * @param int    $attachment_id Current attachment ID.
     593     * @param string $context       Additional context. Can be 'create' when metadata was initially created for new attachment
     594     *                              or 'update' when the metadata was updated.
    583595     */
    584     return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
     596    return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' );
    585597}
    586598
Note: See TracChangeset for help on using the changeset viewer.