[wp-trac] [WordPress Trac] #43119: Allow passing empty post ID to media_handle_sideload()
WordPress Trac
noreply at wordpress.org
Wed Jan 17 20:56:39 UTC 2018
#43119: Allow passing empty post ID to media_handle_sideload()
--------------------------+------------------------------------------
Reporter: swissspidy | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 2.6
Severity: normal | Keywords: needs-patch needs-unit-tests
Focuses: |
--------------------------+------------------------------------------
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:
1. Create a post with the date set to last month
1. Set this post as `$GLOBALS['post']`
1. Run `media_handle_sideload( $file_array, 0 )`
1. 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.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/43119>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list