WordPress.org

Make WordPress Core

Changeset 38113


Ignore:
Timestamp:
07/20/16 07:34:54 (13 months ago)
Author:
mikeschroder
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image-edit.php

    r38015 r38113  
    826826    } 
    827827 
     828    /* 
     829     * We need to remove any existing resized image files because 
     830     * a new crop or rotate could generate different sizes (and hence, filenames), 
     831     * keeping the new resized images from overwriting the existing image files. 
     832     * https://core.trac.wordpress.org/ticket/32171 
     833    */ 
     834    if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE && ! empty( $meta['sizes'] ) ) { 
     835        foreach ( $meta['sizes'] as $size ) { 
     836            if ( ! empty( $size['file'] ) && preg_match( '/-e[0-9]{13}-/', $size['file'] ) ) { 
     837                $delete_file = path_join( $path_parts['dirname'], $size['file'] ); 
     838                wp_delete_file( $delete_file ); 
     839            } 
     840        } 
     841    } 
     842 
    828843    if ( isset( $sizes ) ) { 
    829844        $_sizes = array(); 
  • trunk/tests/phpunit/tests/ajax/MediaEdit.php

    r35309 r38113  
    5252        $this->assertArrayHasKey('medium', $media_meta['sizes'], 'cropped attachment should have data for medium size'); 
    5353    } 
     54 
     55    /** 
     56     * @ticket 32171 
     57     */ 
     58    public function testImageEditOverwriteConstant() { 
     59        define( 'IMAGE_EDIT_OVERWRITE', true ); 
     60 
     61        include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); 
     62 
     63        $filename = DIR_TESTDATA . '/images/canola.jpg'; 
     64        $contents = file_get_contents( $filename ); 
     65 
     66        $upload = wp_upload_bits( basename( $filename ), null, $contents ); 
     67        $id = $this->_make_attachment( $upload ); 
     68 
     69        $_REQUEST['action'] = 'image-editor'; 
     70        $_REQUEST['context'] = 'edit-attachment'; 
     71        $_REQUEST['postid'] = $id; 
     72        $_REQUEST['target'] = 'all'; 
     73        $_REQUEST['do'] = 'save'; 
     74        $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]'; 
     75 
     76        $ret = wp_save_image( $id ); 
     77 
     78        $media_meta = wp_get_attachment_metadata( $id ); 
     79        $sizes1 = $media_meta['sizes']; 
     80 
     81        $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":189,"h":322}}]'; 
     82 
     83        $ret = wp_save_image( $id ); 
     84 
     85        $media_meta = wp_get_attachment_metadata( $id ); 
     86        $sizes2 = $media_meta['sizes']; 
     87 
     88        $file_path = dirname( get_attached_file( $id ) ); 
     89 
     90        foreach ( $sizes1 as $key => $size ) { 
     91            if ( $sizes2[ $key ]['file'] !== $size['file'] ) { 
     92                $files_that_shouldnt_exist[] = $file_path . '/' . $size['file']; 
     93            } 
     94        } 
     95 
     96        foreach ( $files_that_shouldnt_exist as $file ) { 
     97            $this->assertFalse( file_exists( $file ), 'IMAGE_EDIT_OVERWRITE is leaving garbage image files behind.' ); 
     98        } 
     99    } 
    54100} 
Note: See TracChangeset for help on using the changeset viewer.