Fix the upload date in image metadata on back date posts #131
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the Change
We noticed when uploading an image directly to an older post (via Gutenberg or through Media buttons on TinyMCE), thumbnails weren't loading properly and appeared broken. It became apparent that WordPress will backdate images to a post whose
post_date
is in the last month or older. When the Azure plugin swooped in, this logic wasn't factored in when the metadata was being built and would incorrectly set it for the current month. This resulted in images appearing broken as they would link to the wrong folder date.E.g. uploading an image to November 2020, WordPress would place the image in
/uploads/2020/11/
however the Azure plugin would store metadata to/uploads/2021/01/
if that was the current month and year.This PR corrects that by ensuring we leverage
wp_upload_dir()
ability to change the directory in the uploads based on if we are uploading to a post and matching the date of the post. This mirrors the logic performed inwp-admin/includes/media.php:294-299
Alternate Designs
Benefits
Possible Drawbacks
Verification Process
Ran tests with various posts published, drafts, old posts and brand new posts, large and small images, tested that also all image sizes are being generated as expected. Tested image uploading directly in the Media Library and pages which will always default to the current date. Tested image uploading to custom post types. Lastly, tested in single and multisite installs.
All resulted in successfully uploaded images with the correct upload date that matches the post date or current date if Media Library or page post type.
Checklist:
Applicable Issues
These appear to be relevant issues to this fix
#122
#126
Changelog Entry