Make WordPress Core


Ignore:
Timestamp:
10/04/2017 02:58:07 PM (9 years ago)
Author:
joemcgill
Message:

Customizer: Minimize duplicate header crops in the media library.

This adds Custom_Image_Header::get_previous_crop(), which finds any
previously cropped headers created from the same base image and replaces
that attachment rather than creating a new attachment.

After updating a crop, the replaced images is also removed from the list
of previous header images in the Customizer.

See #21819.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/custom-header.php

    r41161 r41732  
    11651165                        'post_mime_type' => $image_type,
    11661166                        'guid' => $url,
    1167                         'context' => 'custom-header'
     1167                        'context' => 'custom-header',
     1168                        'post_parent' => $parent_attachment_id,
    11681169                );
    11691170
     
    11811182         */
    11821183        final public function insert_attachment( $object, $cropped ) {
     1184                $parent_id = isset( $object['post_parent'] ) ? $object['post_parent'] : null;
     1185                unset( $object['post_parent'] );
     1186
    11831187                $attachment_id = wp_insert_attachment( $object, $cropped );
    11841188                $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped );
     1189
    11851190                /**
    11861191                 * Filters the header image attachment metadata.
     
    11941199                $metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata );
    11951200                wp_update_attachment_metadata( $attachment_id, $metadata );
     1201
     1202                if ( $parent_id ) {
     1203                        $meta = add_post_meta( $attachment_id, '_wp_attachment_parent', $parent_id, true );
     1204                }
     1205
    11961206                return $attachment_id;
    11971207        }
     
    12421252                $object = $this->create_attachment_object( $cropped, $attachment_id );
    12431253
    1244                 unset( $object['ID'] );
     1254                $previous = $this->get_previous_crop( $object );
     1255
     1256                if ( $previous ) {
     1257                        $object['ID'] = $previous;
     1258                } else {
     1259                        unset( $object['ID'] );
     1260                }
    12451261
    12461262                $new_attachment_id = $this->insert_attachment( $object, $cropped );
     
    13971413                return $header_images;
    13981414        }
     1415
     1416        /**
     1417         * Get the ID of a previous crop from the same base image.
     1418         *
     1419         * @since 4.9.0
     1420         *
     1421         * @param  array $object A crop attachment object.
     1422         * @return int|false An attachment ID if one exists. False if none.
     1423         */
     1424        public function get_previous_crop( $object ) {
     1425                $header_images = $this->get_uploaded_header_images();
     1426
     1427                // Bail early if there are no header images.
     1428                if ( empty( $header_images ) ) {
     1429                        return false;
     1430                }
     1431
     1432                $previous = false;
     1433
     1434                foreach ( $header_images as $image ) {
     1435                        if ( $image['attachment_parent'] === $object['post_parent'] ) {
     1436                                $previous = $image['attachment_id'];
     1437                                break;
     1438                        }
     1439                }
     1440
     1441                return $previous;
     1442        }
    13991443}
Note: See TracChangeset for help on using the changeset viewer.