[BUG] Object versioning: advmo_path saves wrong version after upload
-
When object versioning is enabled,
updateAttachmentMetadata()recalculatesget_attachment_subdir()after the upload completes. Sinceget_object_version()generates the version fromdate("dHis"), even a 1-second delay between the upload and the metadata save results in a different versioning string. Files are uploaded to S3 at the correct path butadvmo_pathstores a different path, causing all images to appear broken. Environment:- Advanced Media Offloader: 4.4.1
- WordPress: 6.9
- PHP: 8.x
- Cloud Provider: Amazon S3
- Object Versioning: Enabled
- Media organized by year/month: Yes
- Other relevant installed plugins: EWWW Image Optimizer (WebP generation and PNG to JPG enabled), Regenerate Thumbnails
Steps to Reproduce
- Enable object versioning in ADVMO settings
- Upload a new image via Media Library (or click “Offload Now” on an existing one)
- Wait for offload to complete
- Check
advmo_pathinwp_postmetafor the attachment - Check S3 bucket for the actual file location
Expected Behavior
advmo_pathshould match the S3 key where files were uploaded. Actual BehaviorFiles are uploaded to S3 at
wp-content/uploads/2026/04/14081741/image.jpgbutadvmo_pathis saved aswp-content/uploads/2026/04/14081743/(2 seconds later). The frontend then requests images from the wrong path, resulting in broken images (404). Debug EvidenceAdded
error_logcalls toS3_Provider::uploadFile()to trace the actualputObjectcalls:ADVMO_DEBUG putObject BEFORE: Key=wp-content/uploads/2023/07/14081741/image.jpg | file_exists=YES ADVMO_DEBUG putObject SUCCESS: Key=wp-content/uploads/2023/07/14081741/image.jpg | StatusCode=200All uploads go to versioning
14081741. But the savedadvmo_pathpost meta contains14081743.On S3:
- Folder
wp-content/uploads/2023/07/14081741/EXISTS with all files - Folder
wp-content/uploads/2023/07/14081743/DOES NOT EXIST
Root Cause
In
CloudAttachmentUploader.php, the upload flow is:- Line 324 (
uploadToCloud):$subdir = $this->get_attachment_subdir($attachment_id)– generates version14081741viadate("dHis")and saves it toadvmo_object_versionmeta - All files are uploaded to S3 using this
$subdir uploadToCloudreturnstrue- Line 40 (
uploadAttachment): callsupdateAttachmentMetadata($attachment_id) - Line 534 (
updateAttachmentMetadata): calls$this->get_attachment_subdir($attachment_id)AGAIN get_attachment_subdircallsget_object_version, which somehow generates a NEW version14081743instead of returning the saved one
The result: files on S3 at path with version X,
advmo_pathsaved with version Y. Proposed FixuploadToCloud()should return the$subdirit used for the upload, andupdateAttachmentMetadata()should receive it as a parameter instead of recalculating it.Before (current code):
// uploadAttachment() if ($this->uploadToCloud($attachment_id)) { $this->updateAttachmentMetadata($attachment_id); return true; } // uploadToCloud() returns bool private function uploadToCloud(int $attachment_id): bool { // ... return true; } // updateAttachmentMetadata() recalculates subdir private function updateAttachmentMetadata(int $attachment_id): void { update_post_meta($attachment_id, 'advmo_path', $this->get_attachment_subdir($attachment_id)); // ... }After (proposed fix):
// uploadAttachment() $subdir = $this->uploadToCloud($attachment_id); if ($subdir !== false) { $this->updateAttachmentMetadata($attachment_id, $subdir); return true; } // uploadToCloud() returns the subdir used, or false on failure private function uploadToCloud(int $attachment_id): string|false { // ... return $subdir; // instead of return true } // updateAttachmentMetadata() uses the subdir from the upload private function updateAttachmentMetadata(int $attachment_id, string $subdir): void { update_post_meta($attachment_id, 'advmo_path', $subdir); // ... }This ensures
advmo_pathalways matches the actual S3 upload path. The fix has been tested and confirmed working.
You must be logged in to reply to this topic.