Media: Clean up prior image edits if IMAGE_EDIT_OVERWRITE is true.

When `IMAGE_EDIT_OVERWRITE` is set to true, edited image files are
supposed to be deleted when an image is restored to the original.

However, when an image was edited more than once, and then restored,
files created during previous edits were left behind.

Fixes this behavior by updating `wp_save_image()` to clean up
leftover images after each edit when `IMAGE_EDIT_OVERWRITE` is true.

Props bradt, chriscct7, joemcgill.
Fixes #32171.
Built from https://develop.svn.wordpress.org/trunk@38113


git-svn-id: http://core.svn.wordpress.org/trunk@38054 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Mike Schroder 2016-07-20 07:35:28 +00:00
parent c64e599ae2
commit 1736b7b29c
2 changed files with 16 additions and 1 deletions

View File

@ -825,6 +825,21 @@ function wp_save_image( $post_id ) {
$success = $delete = $nocrop = true;
}
/*
* We need to remove any existing resized image files because
* a new crop or rotate could generate different sizes (and hence, filenames),
* keeping the new resized images from overwriting the existing image files.
* https://core.trac.wordpress.org/ticket/32171
*/
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE && ! empty( $meta['sizes'] ) ) {
foreach ( $meta['sizes'] as $size ) {
if ( ! empty( $size['file'] ) && preg_match( '/-e[0-9]{13}-/', $size['file'] ) ) {
$delete_file = path_join( $path_parts['dirname'], $size['file'] );
wp_delete_file( $delete_file );
}
}
}
if ( isset( $sizes ) ) {
$_sizes = array();

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-beta3-38112';
$wp_version = '4.6-beta3-38113';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.