Repository navigation
fix(sync): auto-regenerate thumbnail sizes left stale after an image edit #1314
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -277,6 +277,31 @@ public function edit_upload( $attachment_id ) { | |
| return $this->upload_asset( $attachment_id, 'edit' ); | ||
| } | ||
|
|
||
| /** | ||
| * Regenerate an attachment's thumbnail sizes when they no longer match the current attached file. | ||
| * | ||
| * @param int $attachment_id The attachment ID. | ||
| * | ||
| * @return array|\WP_Error | ||
| */ | ||
| public function regenerate_sizes( $attachment_id ) { | ||
| if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { | ||
| require_once ABSPATH . 'wp-admin/includes/image.php'; | ||
| } | ||
|
|
||
| $file = get_attached_file( $attachment_id ); | ||
| $metadata = wp_generate_attachment_metadata( $attachment_id, $file ); | ||
| if ( empty( $metadata ) ) { | ||
| return new \WP_Error( 'cloudinary_regenerate_sizes_failed', __( 'Could not regenerate attachment sizes.', 'cloudinary' ) ); | ||
| } | ||
|
|
||
| wp_update_attachment_metadata( $attachment_id, $metadata ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blocker: I checked this locally with a 3000x2000 upload (big-image If Suggest: keep the current metadata and replace only $current = wp_get_attachment_metadata( $attachment_id );
$current['sizes'] = $metadata['sizes'];
wp_update_attachment_metadata( $attachment_id, $current ); |
||
| // Persist the new signature, otherwise this would re-trigger on every subsequent render. | ||
| $this->sync->set_signature_item( $attachment_id, 'sizes' ); | ||
|
|
||
| return $metadata; | ||
| } | ||
|
|
||
| /** | ||
| * Upload an asset to Cloudinary. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blocker: The prefix check marks a valid WP state as stale. When
image_edit_thumbnails_separatelyis on and the user edits with target "All sizes except thumbnail" (nothumb), core keeps the old thumbnail on purpose. The main file and other sizes get the-e{timestamp}base.I checked this locally (wp-env, WP 7.1):
On the next render,
regenerate_sizes()overwrites the user's unedited thumbnail with a crop of the edited image. Themedium_largeentry also keeps the old name in this output. The rotated 600px-wide image is too small for that size, so core does not regenerate it, and the old file stays on disk. A name-only check flags this case too.Instead, check that each size file exists on disk (
path_join( dirname( get_attached_file( $id ) ), $size['file'] )). This targets the real bug (no local file and no relationship row) and does not touch valid partial edits.