diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index c3b8e9621d..bb9ff58830 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -244,6 +244,7 @@ function webp_uploads_update_attachment_metadata( $data, $attachment_id ) { return webp_uploads_backup_sources( $attachment_id, $data ); case 'wp_restore_image': // When an image has been restored. + $data = webp_uploads_backup_sources( $attachment_id, $data ); return webp_uploads_restore_image( $attachment_id, $data ); } } @@ -387,9 +388,8 @@ function webp_uploads_get_next_full_size_key_from_backup( $attachment_id ) { */ function webp_uploads_restore_image( $attachment_id, $data ) { $backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ); - if ( ! is_array( $backup_sources ) ) { - return $data; + $backup_sources = array(); } if ( ! isset( $backup_sources['full-orig'] ) || ! is_array( $backup_sources['full-orig'] ) ) { diff --git a/tests/modules/images/webp-uploads/image-edit-tests.php b/tests/modules/images/webp-uploads/image-edit-tests.php index 9e247ea250..4b356bce40 100644 --- a/tests/modules/images/webp-uploads/image-edit-tests.php +++ b/tests/modules/images/webp-uploads/image-edit-tests.php @@ -75,10 +75,13 @@ public function it_should_restore_the_sources_array_from_the_backup_when_an_imag $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); $this->assertImageHasSource( $attachment_id, 'image/webp' ); - $metadata = wp_get_attachment_metadata( $attachment_id ); + $metadata = wp_get_attachment_metadata( $attachment_id ); + $updated_backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ); $this->assertSame( $backup_sources['full-orig'], $metadata['sources'] ); - $this->assertSame( $backup_sources, get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ) ); + $this->assertNotSame( $backup_sources, $updated_backup_sources ); + $this->assertCount( 1, $backup_sources ); + $this->assertCount( 2, $updated_backup_sources ); $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); foreach ( $backup_sizes as $size_name => $properties ) { @@ -428,4 +431,67 @@ public function it_should_prevent_to_store_an_empty_set_of_sources() { $this->assertTrue( $editor->success() ); $this->assertEmpty( get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ) ); } + + /** + * Store the next image hash on the backup sources + * + * @test + */ + public function it_should_store_the_next_image_hash_on_the_backup_sources() { + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); + $editor = new WP_Image_Edit( $attachment_id ); + // Edit the image. + $editor->rotate_right()->save(); + // Restore the image. + wp_restore_image( $attachment_id ); + + $backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ); + + $this->assertIsArray( $backup_sources ); + $this->assertCount( 2, $backup_sources ); + foreach ( array_keys( $backup_sources ) as $name ) { + if ( 'full-orig' === $name ) { + $this->assertSame( 'full-orig', $name ); + } else { + $this->assertSizeNameIsHashed( '', $name ); + } + } + } + + /** + * Create backup of full size images with the same hash keys as the edited images + * + * @test + */ + public function it_should_create_backup_of_full_size_images_with_the_same_hash_keys_as_the_edited_images() { + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); + + $editor = new WP_Image_Edit( $attachment_id ); + $editor->rotate_right()->save(); + $this->assertTrue( $editor->success() ); + $editor->rotate_right()->save(); + $this->assertTrue( $editor->success() ); + $editor->flip_right()->save(); + $this->assertTrue( $editor->success() ); + + $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); + $backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ); + + $this->assertIsArray( $backup_sources ); + $this->assertIsArray( $backup_sizes ); + $this->assertCount( 3, $backup_sources ); + + foreach ( $backup_sources as $size_name => $sources ) { + if ( 'full-orig' === $size_name ) { + // Skip this size name due this name does not have a hash name. + continue; + } + + // Ensure the size name is an actual hashed value. + $this->assertSizeNameIsHashed( 'full', $size_name ); + // Ensure the full-{hash} name is the same created on the backup sizes or that both hash names are the same. + $this->assertArrayHasKey( $size_name, $backup_sizes ); + $this->assertIsArray( $backup_sizes[ $size_name ] ); + } + } }