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.


git-svn-id: https://develop.svn.wordpress.org/trunk@41732 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe McGill
2017-10-04 14:58:07 +00:00
parent d77da9cd2a
commit 59c0329461
4 changed files with 100 additions and 2 deletions
+33
View File
@@ -137,4 +137,37 @@ class Tests_Image_Header extends WP_UnitTestCase {
$this->assertGreaterThan( 0, $cropped_id );
}
/**
* @ticket 21819
*/
function test_check_get_previous_crop() {
$id = wp_insert_attachment( array(
'post_status' => 'publish',
'post_title' => 'foo.png',
'post_type' => 'post',
'guid' => 'http://localhost/foo.png'
) );
// Create inital crop object.
$cropped_1 = 'foo-cropped-1.png';
$object = $this->custom_image_header->create_attachment_object( $cropped_1, $id );
// Ensure no previous crop exists.
$previous = $this->custom_image_header->get_previous_crop( $object );
$this->assertFalse( $previous );
// Create the inital crop attachment and set it as the header.
$cropped_1_id = $this->custom_image_header->insert_attachment( $object, $cropped_1 );
$key = '_wp_attachment_custom_header_last_used_' . get_stylesheet();
update_post_meta( $cropped_1_id, $key, time() );
update_post_meta( $cropped_1_id, '_wp_attachment_is_custom_header', get_stylesheet() );
// Create second crop.
$cropped_2 = 'foo-cropped-2.png';
$object = $this->custom_image_header->create_attachment_object( $cropped_2, $id );
// Test that a previous crop is found.
$previous = $this->custom_image_header->get_previous_crop( $object );
$this->assertSame( $previous, $cropped_1_id );
}
}