Repository navigation
fix(sync): auto-regenerate thumbnail sizes left stale after an image edit - #1314
gabrielcld2 wants to merge 2 commits into
Conversation
…edit Editing an image via the WP image editor can rename the main file without regenerating its registered thumbnail sizes, leaving `sizes` metadata pointing at filenames that no longer exist. This causes Delivery::find_attachment_size_urls() to run an uncached wp_postmeta lookup on every render of that attachment, since nothing resolves the stale URLs. Adds a `sizes` sync type (mirroring the existing `edit` type) that detects this drift on render via the existing realtime sync pipeline and regenerates the attachment metadata, self-healing both new edits and already-drifted attachments without any new cron job or bulk scan.
wp_get_attachment_metadata()'s PHPDoc already types 'sizes' as array, making the is_array() check dead code; empty() already covers the missing-key and empty-array cases.
utkarshcloudinary
left a comment
There was a problem hiding this comment.
Left 2 comments.
| $base = ! empty( $ext ) ? substr( $base, 0, - ( strlen( $ext ) + 1 ) ) : $base; | ||
|
|
||
| foreach ( $metadata['sizes'] as $size ) { | ||
| if ( empty( $size['file'] ) || 0 !== strpos( $size['file'], $base . '-' ) ) { |
There was a problem hiding this comment.
blocker: The prefix check marks a valid WP state as stale. When image_edit_thumbnails_separately is 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):
file: 2026/10/pr1314-nothumb-2-e1791880319565.jpg
medium => pr1314-nothumb-2-e1791880319565-225x300.jpg
thumbnail => pr1314-nothumb-2-150x150.jpg
medium_large => pr1314-nothumb-2-768x576.jpg
has_outdated_sizes: true
thumbnail after regenerate: pr1314-nothumb-2-e1791880319565-150x150.jpg
On the next render, regenerate_sizes() overwrites the user's unedited thumbnail with a crop of the edited image. The medium_large entry 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.
| return new \WP_Error( 'cloudinary_regenerate_sizes_failed', __( 'Could not regenerate attachment sizes.', 'cloudinary' ) ); | ||
| } | ||
|
|
||
| wp_update_attachment_metadata( $attachment_id, $metadata ); |
There was a problem hiding this comment.
blocker: wp_generate_attachment_metadata() builds a fresh array, so this call replaces the full metadata record. Keys that are not regenerated are lost.
I checked this locally with a 3000x2000 upload (big-image -scaled path) and a third-party key:
before keys: width,height,file,filesize,sizes,image_meta,original_image,third_party_key
after keys: width,height,file,filesize,sizes,image_meta
If original_image is lost, wp_get_original_image_path() stops working. This plugin uses that path for uploads (filter_backup_original, get_original_image). Other plugins' metadata (for example WPML, or custom keys) is also lost.
Suggest: keep the current metadata and replace only sizes (and maybe width/height/filesize):
$current = wp_get_attachment_metadata( $attachment_id );
$current['sizes'] = $metadata['sizes'];
wp_update_attachment_metadata( $attachment_id, $current );
The issue
Editing an image via the WordPress image editor (crop/scale) can rename the main attached file (adding an
-e{timestamp}suffix) without regenerating its registered thumbnailsizes.The
sizesmetadata is left pointing at the pre-edit filenames, which no longer exist on disk and have no matching entry inwp_cloudinary_relationships.Every render of that attachment then falls through to
Delivery::find_attachment_size_urls(), which runs an uncachedwp_postmetaquery trying (and failing) to resolve the stale URL.On high-traffic pages (e.g. a site icon used on every page) this becomes a query on every single request.
The fix
This adds a new
sizessync type, modeled directly on the existingeditsync type:Media::has_outdated_sizes()(php/class-media.php) detects when thesizesmetadata's filenames no longer match the current attached file's basename.Upload_Sync::regenerate_sizes()(php/sync/class-upload-sync.php) callswp_generate_attachment_metadata()to rebuild the sizes, then persists the new signature viaset_signature_item()so it doesn't re-run on every subsequent render.Because this rides the existing realtime sync pipeline, it self-heals both future edits and attachments that are already drifted today.
i.e. no new cron job, scheduled scan, or admin action required. No code changes needed outside these three files.
Limitations
QA notes
Prerequisites:
developbranch to reproduce the issueqa-check-sizes.phpbelow added at the root of the repo:Manual steps:
npx wp-env run cli -- wp eval-file /var/www/html/wp-content/plugins/cloudinary_wordpress/qa-check-sizes.php) → should printOutdated sizes: YES - bug present.<head>on every page) to trigger a render.develop: stillYES - bug present— never fixes itself.no - in sync, and the sizes list shows the new, edited filenames.