• When object versioning is enabled, updateAttachmentMetadata() recalculates get_attachment_subdir() after the upload completes. Since get_object_version() generates the version from date("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 but advmo_path stores 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

    1. Enable object versioning in ADVMO settings
    2. Upload a new image via Media Library (or click “Offload Now” on an existing one)
    3. Wait for offload to complete
    4. Check advmo_path in wp_postmeta for the attachment
    5. Check S3 bucket for the actual file location

    Expected Behavior

    advmo_path should match the S3 key where files were uploaded. Actual Behavior

    Files are uploaded to S3 at wp-content/uploads/2026/04/14081741/image.jpg but advmo_path is saved as wp-content/uploads/2026/04/14081743/ (2 seconds later). The frontend then requests images from the wrong path, resulting in broken images (404). Debug Evidence

    Added error_log calls to S3_Provider::uploadFile() to trace the actual putObject calls:

    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=200

    All uploads go to versioning 14081741. But the saved advmo_path post meta contains 14081743.

    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:

    1. Line 324 (uploadToCloud): $subdir = $this->get_attachment_subdir($attachment_id) – generates version 14081741 via date("dHis") and saves it to advmo_object_version meta
    2. All files are uploaded to S3 using this $subdir
    3. uploadToCloud returns true
    4. Line 40 (uploadAttachment): calls updateAttachmentMetadata($attachment_id)
    5. Line 534 (updateAttachmentMetadata): calls $this->get_attachment_subdir($attachment_id) AGAIN
    6. get_attachment_subdir calls get_object_version, which somehow generates a NEW version 14081743 instead of returning the saved one

    The result: files on S3 at path with version X, advmo_path saved with version Y. Proposed Fix

    uploadToCloud() should return the $subdir it used for the upload, and updateAttachmentMetadata() 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_path always matches the actual S3 upload path. The fix has been tested and confirmed working.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.