Make WordPress Core


Ignore:
Timestamp:
10/09/2024 11:30:05 PM (3 months ago)
Author:
peterwilsoncc
Message:

Media: Update file size meta data when editing images.

Fixes an error in which the file size meta data retained the original upload's values follow a user editing the images in the media screen.

The original images' file sizes are now stored in the backup image data to allow for them to be restored when a user restores the original image.

Props ankit-k-gupta, antpb, audrasjb, chaion07, gauravsingh7, joedolson, oglekler, pls78, rajinsharwar, sayedulsayem, vertisoft.
Fixes #59684.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/ajax/wpAjaxImageEditor.php

    r57244 r59202  
    115115        }
    116116    }
     117
     118    /**
     119     * Ensure the filesize is updated after editing an image.
     120     *
     121     * Tests that the image meta data file size is updated after editing an image,
     122     * this includes both the full size image and all the generated sizes.
     123     *
     124     * @ticket 59684
     125     */
     126    public function test_filesize_updated_after_editing_an_image() {
     127        require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     128
     129        $filename = DIR_TESTDATA . '/images/canola.jpg';
     130        $contents = file_get_contents( $filename );
     131
     132        $upload              = wp_upload_bits( wp_basename( $filename ), null, $contents );
     133        $id                  = $this->_make_attachment( $upload );
     134        $original_image_meta = wp_get_attachment_metadata( $id );
     135
     136        $_REQUEST['action']  = 'image-editor';
     137        $_REQUEST['context'] = 'edit-attachment';
     138        $_REQUEST['postid']  = $id;
     139        $_REQUEST['target']  = 'all';
     140        $_REQUEST['do']      = 'save';
     141        $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
     142
     143        wp_save_image( $id );
     144
     145        $post_edit_meta = wp_get_attachment_metadata( $id );
     146
     147        $pre_file_sizes         = array_combine( array_keys( $original_image_meta['sizes'] ), array_column( $original_image_meta['sizes'], 'filesize' ) );
     148        $pre_file_sizes['full'] = $original_image_meta['filesize'];
     149
     150        $post_file_sizes         = array_combine( array_keys( $post_edit_meta['sizes'] ), array_column( $post_edit_meta['sizes'], 'filesize' ) );
     151        $post_file_sizes['full'] = $post_edit_meta['filesize'];
     152
     153        foreach ( $pre_file_sizes as $size => $size_filesize ) {
     154            // These are asserted individually as each image size needs to be checked separately.
     155            $this->assertNotSame( $size_filesize, $post_file_sizes[ $size ], "Filesize for $size should have changed after editing an image." );
     156        }
     157    }
     158
     159    /**
     160     * Ensure the filesize is restored after restoring the original image.
     161     *
     162     * Tests that the image meta data file size is restored after restoring the original image,
     163     * this includes both the full size image and all the generated sizes.
     164     *
     165     * @ticket 59684
     166     */
     167    public function test_filesize_restored_after_restoring_original_image() {
     168        require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     169
     170        $filename = DIR_TESTDATA . '/images/canola.jpg';
     171        $contents = file_get_contents( $filename );
     172
     173        $upload              = wp_upload_bits( wp_basename( $filename ), null, $contents );
     174        $id                  = $this->_make_attachment( $upload );
     175        $original_image_meta = wp_get_attachment_metadata( $id );
     176
     177        $_REQUEST['action']  = 'image-editor';
     178        $_REQUEST['context'] = 'edit-attachment';
     179        $_REQUEST['postid']  = $id;
     180        $_REQUEST['target']  = 'all';
     181        $_REQUEST['do']      = 'save';
     182        $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
     183
     184        wp_save_image( $id );
     185        wp_restore_image( $id );
     186
     187        $post_restore_meta = wp_get_attachment_metadata( $id );
     188
     189        $pre_file_sizes         = array_combine( array_keys( $original_image_meta['sizes'] ), array_column( $original_image_meta['sizes'], 'filesize' ) );
     190        $pre_file_sizes['full'] = $original_image_meta['filesize'];
     191
     192        $post_restore_file_sizes         = array_combine( array_keys( $post_restore_meta['sizes'] ), array_column( $post_restore_meta['sizes'], 'filesize' ) );
     193        $post_restore_file_sizes['full'] = $post_restore_meta['filesize'];
     194
     195        $this->assertSameSetsWithIndex( $pre_file_sizes, $post_restore_file_sizes, 'Filesize should have restored after restoring the original image.' );
     196    }
    117197}
Note: See TracChangeset for help on using the changeset viewer.