Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Restore and backup image sizes alongside the sources properties #242

Merged

Conversation

mitogh
Copy link
Member

@mitogh mitogh commented Mar 19, 2022

Summary

Fixes #228

Relevant technical choices

Usage of _sources metadata key

A _sources property is stored to hold the top-level sources for the full image size when an image is edited, after the backup is updated the _sources property would be moved into the appropriate full size inside of the backup metadata and the temporarily _sources property would be removed from the attachment image.

This key is used so when the metadata is updated we still have a reference to the previous sources values to the full size image when the image is edited. This is not the case for the rest of real sizes, this is only the case for full sizes.

When the image is restored the sources property is populated again from the backup metadata if the full-orig has the sources property.

Removal of edited images

When an image is edited and restored any edited version of the images are removed if IMAGE_EDIT_OVERWRITE is defined as a truthy value.

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

The class is in charge of performing operations to an image
as if a user is performing those actions from the image editor
but from the BE. This class is a utility class to be used
from within the test context to mimic the user behavior of
performing edits into an image.
When an image is edited the `sources` properties is going to be backup with
the rest of the sizes properties, however that's not the case for the `full`
size which is a virtual type and the top level `sources` would be moved
into the backup metadata.

When the image is restored any edited image available in the sources array would
be removed before the restore takes place. The `sources` property for the `full-orig`
would be placed at the top level `sources` with the rest of the metadata.
@mitogh mitogh self-assigned this Mar 19, 2022
@mitogh mitogh added [Focus] Images [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Mar 19, 2022
@mitogh mitogh added this to the 1.0.0-beta.3 milestone Mar 19, 2022
@mitogh mitogh added the [Type] Enhancement A suggestion for improvement of an existing feature label Mar 19, 2022
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitogh Left a few comments. High-level summary:

  • We can simplify this implementation by introducing our own _wp_attachment_backup_sources meta key.
  • This meta key will be used to store the sources only for the original image (since WP core otherwise wouldn't store it).
  • The other sources are automatically backed up by WP core, since it just copies everything over for the sub-sizes. That actually already works today, which is nice to see.
  • When restoring, we then only need to include logic to restore the original image sources.
  • We don't need to delete any image files because edited images will not have any sources added (that'll be part of Perform updates on secondary image mime types when original image is edited #158). In other words, there will not be anything to delete other than what WP core already handles.

modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
This would be handled once #158 is merged so
the current approach can be adjusted accordingly.
When an image is edited using a target other than the
all and the thumbnail make sure the `success` method
returns the correct value.
Due to the only value that is not correctly stored is
the top level sources attributes this now is stored
in a separate meta value. Stored in `_wp_attachment_backup_sources`

Logic was added to handle scenarios where only the thumbnail
or all images except the thumbnail are modified.
@mitogh
Copy link
Member Author

mitogh commented Mar 23, 2022

Thanks for this review @felixarntz just updated this PR based on your feedback, let me know in case you have any additional comments or feedback.

@mitogh mitogh requested a review from felixarntz March 23, 2022 20:38
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitogh Left a few follow-up comments, I still have some open questions, and a few small nit-picks.

modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
mitogh and others added 8 commits March 23, 2022 15:11
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
@mitogh mitogh changed the base branch from trunk to release/1.0.0-beta.4 March 24, 2022 23:54
Move the hooks for the update right into the place where they are
actually used.
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitogh This looks great now from a functionality perspective! I left a few comments on cleaning up a few things, we can simplify some code and reuse the function instead of rewriting it twice.

The main point of feedback here is that we also should do an extra security check to ensure we're updating the correct attachment.

modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Show resolved Hide resolved
modules/images/webp-uploads/load.php Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
mitogh and others added 5 commits March 28, 2022 16:35
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
The `backup_sizes` parameter was not used so it can be safely removed
from the code execution.
Reuse the hook as now is stored in a variable, so it can be reused in
both hooks.
@mitogh mitogh requested a review from felixarntz March 28, 2022 22:51
@felixarntz felixarntz changed the base branch from release/1.0.0-beta.4 to release/1.0.0-rc.1 March 29, 2022 17:59
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitogh Awesome work!

cc @adamsilverstein for incorporating this into the core patch (in there we can do it without that hook weirdness present here)

Copy link
Contributor

@eugene-manuilov eugene-manuilov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, @mitogh! Added some comments with minor improvements. Please, let me know whether it makes sense to you.

modules/images/webp-uploads/load.php Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
tests/modules/images/webp-uploads/webp-uploads-test.php Outdated Show resolved Hide resolved
tests/modules/images/webp-uploads/webp-uploads-test.php Outdated Show resolved Hide resolved
mitogh and others added 7 commits March 30, 2022 10:49
Co-authored-by: Eugene Manuilov <manuilov@google.com>
Co-authored-by: Eugene Manuilov <manuilov@google.com>
Co-authored-by: Eugene Manuilov <manuilov@google.com>
Co-authored-by: Eugene Manuilov <manuilov@google.com>
Make sure the code performns in a more structured way
Copy link
Contributor

@eugene-manuilov eugene-manuilov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks, @mitogh.

@felixarntz felixarntz merged commit ad0a330 into release/1.0.0-rc.1 Mar 31, 2022
@felixarntz felixarntz deleted the feature/228-restore-and-backup-image-sources branch March 31, 2022 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restore and backup Image sizes with sources when an image is edited
3 participants