Opened 9 years ago
Last modified 5 weeks ago
#43119 new defect (bug)
Allow passing empty post ID to media_handle_sideload()
| Reported by: | swissspidy | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Media | Version: | 2.6 |
| Severity: | normal | Keywords: | has-unit-tests has-patch |
| Cc: | Focuses: |
Description
I stumbled upon this via https://wordpress.stackexchange.com/questions/291344/why-does-media-handle-sideload-upload-to-last-months-folder.
Basically, the person was trying to run media_handle_sideload( $file_array, $post_id = 0 ) to upload files. This works fine in so far as it calls wp_insert_attachment() which accepts a post ID of 0 (it's the default) so the attachment won't be associated with a specific post.
However, media_handle_sideload() doesn't fully support $post_id = 0 (or $post_id = null, for that matter) because of the if ( $post = get_post( $post_id ) ) { … } condition since get_post( 0 ) fetches the global post and overrides $time.
This means you cannot upload a media file and have $time set to today.
To reproduce:
- Create a post with the date set to last month
- Set this post as
$GLOBALS['post'] - Run
media_handle_sideload( $file_array, 0 ) - Notice that the upload folder will be the one from last month, not this month.
Proposed fix:
Change the condition from if ( $post = get_post( $post_id ) ) { … } to if ( $post_id && $post = get_post( $post_id ) ) { … }
Consequences:
The upload folder will point to the current month, not the date from the global post if using $post_id = 0 or $post_id = null. Not a big deal IMHO.
Attachments (3)
Change History (8)
#2
@
9 years ago
- Keywords has-patch added; needs-patch removed
Thanks for patch @AugusGils and the tests @andizer
In 43119.2.diff there are some adjustments:
- Combines patch + tests into a single patch.
- To speed things up it avoids the
download_url()and uses an existing file instead. - Changes
$default = date( '/Y/m', current_time( 'timestamp' ) );to$default = current_time( '/Y/m/' );for simplification. - Some changes according to the Coding Standard.
- Adds cleanups.
Hope it helps.
This ticket was mentioned in PR #14194 on WordPress/wordpress-develop by @nhrrob.
5 weeks ago
#4
- Keywords needs-refresh removed
When media_handle_sideload() is called without a post ID, get_post( 0 ) falls back to the global post, so the file is stored in that post's year/month upload folder instead of the current one. This only looks up the post when a post ID is passed. It refreshes 43119.2.diff with new unit tests that set a global post.
Testing instructions
- Create a post dated in the past (for example January 2018).
- With that post set as the global
$post, sideload a file without a post ID, for example withwp eval:require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $GLOBALS['post'] = get_post( <old post ID> ); $tmp = wp_tempnam( 'logo.png' ); copy( ABSPATH . 'wp-includes/images/w-logo-blue.png', $tmp ); echo get_attached_file( media_handle_sideload( array( 'name' => 'logo.png', 'tmp_name' => $tmp ), 0 ) );
- Before: the file is saved in
uploads/2018/01/. After: it is saved in the current month's folder. - Run
npm run test:php -- --filter Tests_Media_mediaHandleSideload.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Refreshing the existing patch, writing the unit tests and verifying with WP-CLI; reviewed and tested by me.
#5
@
5 weeks ago
Patch Testing Report
Patch tested: https://github.com/WordPress/wordpress-develop/pull/14194 (head 1facb42)
Environment
- WordPress: 7.2-alpha-63166-src
- PHP: 8.3.35
- Server: wordpress-develop local Docker environment (nginx), PHPUnit 9.6.38
- Database: MySQL 9.7.2
- Browser: N/A (unit tests and WP-CLI)
- OS: Windows 11
- Theme: Twenty Twenty-Five 1.5
- MU Plugins: None activated
- Plugins: None activated
Steps taken
- Created a post dated 2018-01-15 and set it as the global
$post. - With WP-CLI, ran
media_handle_sideload()with post ID0, withnull, and with the old post's ID.- trunk: all three files were saved in
uploads/2018/01/. - PR:
0andnullwere saved inuploads/2026/10/; the old post's ID still useduploads/2018/01/.
- trunk: all three files were saved in
- Ran
tests/phpunit/tests/media/mediaHandleSideload.php: 4 tests pass on single site, multisite and with--order-by=reverse. With trunk'smedia.php, the 3 empty post ID cases fail. - Ran the
mediagroup with the patch: 871 tests, no failures (7 skipped). - ✅ Patch is solving the problem: without a post ID, the file goes into the current month's folder instead of the global post's.
Expected result
- Without a post ID,
media_handle_sideload()uses the current month's upload folder, while a passed post ID still uses that post's date.
Additional Notes
- AI assistance: Claude Code (Claude Opus 5.5) was used to run the tests and the WP-CLI checks; I reviewed the results.
Made a check on $post_id to make sure it's a valid one, this to make sure the correct upload folder is used.