Skip to content

Restore and backup image sizes alongside the sources properties #242

New issue

Have a question about this project? Sign up for a free account to open an issue and contact its maintainers and the community.

By clicking “Sign up for ”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on ? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a6f5e07
Add class to mimic image operations on the editor.
mitoghMar 19, 2022
4820501
Restore and backup image sizes including `sources`.
mitoghMar 19, 2022
ab7bf05
Merge branch 'trunk' into feature/228-restore-and-backup-image-sources
mitoghMar 22, 2022
7e82041
Remove handling of edited images
mitoghMar 23, 2022
a2b82e3
Update the `success` logic to handle additional cases.
mitoghMar 23, 2022
ba0cbdf
Update logic to store and restore backup.
mitoghMar 23, 2022
2dd29d8
Remove empty space
mitoghMar 23, 2022
b16cee1
Update prefix on functions
mitoghMar 23, 2022
8a8e967
Update prefix on functions
mitoghMar 23, 2022
322dd3d
Update prefix on functions
mitoghMar 23, 2022
226a5aa
Update hooks reference
mitoghMar 23, 2022
8a809ed
Backup the data from previous metadata
mitoghMar 23, 2022
6d134e5
Merge branch 'trunk' into feature/228-restore-and-backup-image-sources
mitoghMar 24, 2022
76307e6
Merge branch 'trunk' into feature/228-restore-and-backup-image-sources
mitoghMar 24, 2022
951c4ca
Remove the need of auxilary variable to hold the sources
mitoghMar 26, 2022
61e25e8
Add `since` doc block
mitoghMar 28, 2022
4c26f4f
Align parameters on the doc block
mitoghMar 28, 2022
973a7b8
Removal of non required parameter
mitoghMar 28, 2022
cb35f42
Align parameters to follow WordPress guidelines
mitoghMar 28, 2022
7880f6b
Include the `@since` tags on missing functions
mitoghMar 28, 2022
ca8d28e
Move logic of the hook into a variable
mitoghMar 28, 2022
c381398
Add white spaces on tests
mitoghMar 30, 2022
93e0d01
Add white spaces on tests
mitoghMar 30, 2022
c165a11
Removal of `sanitize_text_field` due this value is not required.
mitoghMar 30, 2022
7d96583
Run logic only if was executed when doing ajax.
mitoghMar 30, 2022
7a113d3
Update code flow for more performant checks
mitoghMar 30, 2022
e202548
Split conditionals into separate statements
mitoghMar 30, 2022
188c7df
Removal of non requried conditional
mitoghMar 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
Next Next commit
Remove handling of edited images
This would be handled once #158 is merged so
the current approach can be adjusted accordingly.
  • Loading branch information
@mitogh
mitogh committedMar 23, 2022
commit 7e82041726a206909fa9d7aa59520619c9af6de4
26 changes: 2 additions & 24 deletions modules/images/webp-uploads/load.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -784,30 +784,8 @@ function webp_uploads_restore_image( $attachment_id, $data ) {
return $data;
}

// If `IMAGE_EDIT_OVERWRITE` is defined and is truthy remove any edited images if present before replacing the metadata.
if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) {
$file = get_attached_file( $attachment_id );
$dirname = pathinfo( $file, PATHINFO_DIRNAME );
$metadata = wp_get_attachment_metadata( $attachment_id );

$sources = isset( $metadata['sources'] ) && is_array( $metadata['sources'] ) ? $metadata['sources'] : array();

foreach ( $sources as $mime => $properties ) {
if ( empty( $properties['file'] ) ) {
continue;
}

// Delete only if it's an edited image.
if ( preg_match( '/-e\d{13}/', $properties['file'] ) ) {
$delete_file = path_join( $dirname, $properties['file'] );
wp_delete_file( $delete_file );
}
}

foreach ( $metadata['sizes'] as $size_name => $properties ) {
if ( ! isset( $properties['sources'] ) || ! is_array( $properties['sources'] ) ) {
continue;
}
// TODO: Handle the case If `IMAGE_EDIT_OVERWRITE` is defined and is truthy remove any edited images if present before replacing the metadata.
// See: https://.com/WordPress/performance/issues/158.

foreach ( $properties['sources'] as $mime => $source_properties ) {
if ( empty( $source_properties['file'] ) ) {
Expand Down
150 changes: 0 additions & 150 deletions tests/modules/images/webp-uploads/webp-uploads-test.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -850,80 +850,6 @@ public function it_should_backup_the_sources_structure_alongside_the_full_size()
$this->assertArrayNotHasKey( '_file', $metadata );
}

/**
* Backup sources from edited images
*
* @test
*/
public function it_should_backup_sources_from_edited_images() {
$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' );
$original_metadata = wp_get_attachment_metadata( $attachment_id );

$editor = new WP_Image_Edit( $attachment_id );
$editor->rotate_right()->save();
$this->assertTrue( $editor->success() );

$metadata = wp_get_attachment_metadata( $attachment_id );
$updated_metadata = $metadata;
$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
$filename = pathinfo( $updated_metadata['file'], PATHINFO_FILENAME );

$this->assertArrayHasKey( 'sources', $backup_sizes['full-orig'] );
$this->assertMatchesRegularExpression( '/-e\d{13}/', $filename );
// Fake the creation of sources array to the existing metadata.
$updated_metadata['sources'] = array(
get_post_mime_type( $attachment_id ) => $filename,
);

foreach ( $updated_metadata['sizes'] as $size_name => $props ) {
$updated_metadata['sizes'][ $size_name ]['sources'] = array(
$props['mime-type'] => $props['file'],
);
}

wp_update_attachment_metadata( $attachment_id, $updated_metadata );

$editor->rotate_left()->save();
$this->assertTrue( $editor->success() );

$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );

// Make sure the original images were stored in the backup.
foreach ( $backup_sizes as $size_name => $properties ) {
if ( preg_match( '/-\d{13}/', $size_name ) ) {
continue;
}

$real_name = str_replace( '-orig', '', $size_name );
if ( 'full' === $real_name ) {
$sources = $original_metadata['sources'];
} else {
$sources = $original_metadata['sizes'][ $real_name ]['sources'];
}

$this->assertArrayHasKey( 'sources', $properties, "Sources not present in '{$size_name}'" );
$this->assertSame( $sources, $properties['sources'], "The '{$size_name} is not identical.'" );
}

// Make sure that the edited images were stored correctly in the backup.
foreach ( $backup_sizes as $size_name => $properties ) {
// Test only the edited names.
if ( ! preg_match( '/-\d{13}/', $size_name ) ) {
continue;
}

$real_name = preg_replace( '/-\d{13}/', '', $size_name );
if ( 'full' === $real_name ) {
$sources = $updated_metadata['sources'];
} else {
$sources = $updated_metadata['sizes'][ $real_name ]['sources'];
}

$this->assertArrayHasKey( 'sources', $properties, "Sources not present in '{$size_name}'" );
$this->assertSame( $sources, $properties['sources'], "The '{$size_name} is not identical.'" );
}
}

/**
* Store all the information on the original backup key when image edit overwrite is defined
*
Expand DownExpand Up@@ -1004,80 +930,4 @@ public function it_should_restore_the_sources_array_from_the_backup_when_an_imag
$this->assertSame( $backup_sizes[ $size_name . '-orig' ]['sources'], $properties['sources'] );
}
}

/**
* Delete edited images when image edit overwrite is defined
*
* @test
*/
public function it_should_delete_edited_images_when_image_edit_overwrite_is_defined() {
define( 'IMAGE_EDIT_OVERWRITE', true );

$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->flip_vertical()->save();
$this->assertTrue( $editor->success() );

$metadata = wp_get_attachment_metadata( $attachment_id );
$file = get_attached_file( $attachment_id );

// Populate the sources array due this one requires: https://.com/WordPress/performance/issues/158.
$metadata['sources'] = array(
'image/jpeg' => array(
'file' => pathinfo( $file, PATHINFO_FILENAME ),
'filesize' => filesize( $file ),
),
'image/webp' => webp_uploads_generate_additional_image_source(
$attachment_id,
array(
'width' => $metadata['width'],
'height' => $metadata['height'],
'crop' => false,
),
'image/webp',
str_replace( '.jpeg', '.webp', $file )
),
);

$directory = pathinfo( $file, PATHINFO_DIRNAME );
foreach ( wp_get_registered_image_subsizes() as $size_name => $props ) {
if ( ! isset( $metadata['sizes'][ $size_name ] ) ) {
continue;
}

$metadata['sizes'][ $size_name ]['sources'] = array(
$metadata['sizes'][ $size_name ]['mime-type'] => array(
'file' => $metadata['sizes'][ $size_name ]['file'],
'filesize' => filesize( $directory . DIRECTORY_SEPARATOR . $metadata['sizes'][ $size_name ]['file'] ),
),
'image/webp' => webp_uploads_generate_additional_image_source(
$attachment_id,
array(
'width' => $props['width'],
'height' => $props['height'],
'crop' => $props['crop'],
),
'image/webp'
),
);
}

wp_update_attachment_metadata( $attachment_id, $metadata );

$metadata_before_restore = wp_get_attachment_metadata( $attachment_id );

wp_restore_image( $attachment_id );

$this->assertFileDoesNotExist( $metadata_before_restore['file'] );
$this->assertFileDoesNotExist( $directory . DIRECTORY_SEPARATOR . $metadata_before_restore['sources']['image/jpeg']['file'] );
$this->assertFileDoesNotExist( $directory . DIRECTORY_SEPARATOR . $metadata_before_restore['sources']['image/webp']['file'] );

$this->assertArrayHasKey( 'sizes', $metadata_before_restore );
foreach ( $metadata_before_restore['sizes'] as $size_name => $properties ) {
$this->assertArrayHasKey( 'sources', $properties );
foreach ( $properties['sources'] as $mime => $values ) {
$this->assertFileDoesNotExist( $directory . DIRECTORY_SEPARATOR . $values['file'] );
}
}
}
}