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/tests/phpunit/tests/image/header.php

    r41017 r41732  
    138138        }
    139139
     140        /**
     141         * @ticket 21819
     142         */
     143        function test_check_get_previous_crop() {
     144                $id = wp_insert_attachment( array(
     145                        'post_status' => 'publish',
     146                        'post_title' => 'foo.png',
     147                        'post_type' => 'post',
     148                        'guid' => 'http://localhost/foo.png'
     149                ) );
     150
     151                // Create inital crop object.
     152                $cropped_1 = 'foo-cropped-1.png';
     153                $object = $this->custom_image_header->create_attachment_object( $cropped_1, $id );
     154
     155                // Ensure no previous crop exists.
     156                $previous = $this->custom_image_header->get_previous_crop( $object );
     157                $this->assertFalse( $previous );
     158
     159                // Create the inital crop attachment and set it as the header.
     160                $cropped_1_id = $this->custom_image_header->insert_attachment( $object, $cropped_1 );
     161                $key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
     162                update_post_meta( $cropped_1_id, $key, time() );
     163                update_post_meta( $cropped_1_id, '_wp_attachment_is_custom_header', get_stylesheet() );
     164
     165                // Create second crop.
     166                $cropped_2 = 'foo-cropped-2.png';
     167                $object = $this->custom_image_header->create_attachment_object( $cropped_2, $id );
     168
     169                // Test that a previous crop is found.
     170                $previous = $this->custom_image_header->get_previous_crop( $object );
     171                $this->assertSame( $previous, $cropped_1_id );
     172        }
    140173}
Note: See TracChangeset for help on using the changeset viewer.