Link to image file should point to original upload regardless of fallback setting#2570
Link to image file should point to original upload regardless of fallback setting#2570faisalahammad wants to merge 2 commits into
Conversation
… setting - REST attachment source_url now points to the original upload when this plugin swapped the main file for a modern format (fallback setting off) - core's own -scaled resizing is left untouched, only our own format swap is detected and corrected - img tag rendering still shows the modern format as before Link to image file should point to the original upload no matter the fallback setting. Right now it only does that when fallback is on, since with fallback off the plugin swaps the attachment file itself to the modern format and wp_get_attachment_url() follows that swap. Fixes WordPress#2470
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #2570 +/- ##
==========================================
+ Coverage 70.35% 70.40% +0.04%
==========================================
Files 91 91
Lines 7867 7890 +23
==========================================
+ Hits 5535 5555 +20
- Misses 2332 2335 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@ms-studio can you check to see if this resolves your issue? |
For oversized images that WordPress scales to '-scaled', the REST source_url filter now points at the swapped scaled output-format file (leaves-scaled.webp) instead of the unscaled original upload (leaves.jpg), matching the reviewer's expectation for the Link to image file href. Non-scaled swapped images still resolve to metadata.original_image as before. Addresses PR feedback in WordPress#2570.
Summary
Fixes #2470
Relevant technical choices
When "Also generate fallback images in the original upload format" is disabled, Modern Image Formats swaps the attachment's main file for the generated modern-format version and backs up the true original in the
original_imagemetadata. Because of that,wp_get_attachment_url()legitimately returns the modern-format file, and the "Link to image file" href on Image/Gallery blocks (built from the RESTsource_urlfield) ends up pointing to it instead of the original upload. With fallback enabled this doesn't happen, so the link behavior was inconsistent depending on that one setting - as discussed and agreed on in the issue thread.This PR filters the REST attachment response so
source_url(andmedia_details.sizes.full.source_url) point back to the original file when this plugin has swapped formats. Detection compares the mime type of the currently attached file against the backed-up original's mime type, so it only triggers on this plugin's own format swap and leaves core's own unrelated "-scaled" resizing (same mime type) untouched. The<img>tag rendering pipeline is untouched too - it still shows the modern format as before, only the link/href changes.This affects new image insertions going forward, since Gutenberg bakes the href into the block's stored HTML at insert time. Already-published posts keep their existing link until the image is re-inserted or the block is re-saved.
Added
plugins/webp-uploads/tests/test-attachment-url.phpcovering: format swap detected, fallback enabled (no-op), core's own scaled images (no-op), and img tag still showing the modern format.Use of AI Tools
Used Claude Code as a coding assistant for this PR: investigating the issue thread and the plugin's upload/rewrite pipeline, drafting the fix and its tests. I reviewed the logic, ran the full webp-uploads PHPUnit suite, phpcs, and phpstan locally, and verified the fix and edge cases manually before opening this PR.