Opened 4 years ago
Closed 4 years ago
#50972 closed defect (bug) (fixed)
media_handle_sideload() does not allow $post_data to override the post_date that gets used by wp_upload_dir
Reported by: | jamesgol | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 5.7 | Priority: | normal |
Severity: | minor | Version: | 5.3 |
Component: | Media | Keywords: | has-patch commit |
Focuses: | Cc: |
Description
Now that $post_id is an optional attribute to media_handle_sideload() it should be possible to override the $time passed to wp_handle_sideload() via the optional $post_data array.
My use case is calling media_handle_sideload() via a custom plugin, only after the existing library is checked for duplicates is the post actually created so there is no $post_id to reference (yet).
Attachments (6)
Change History (23)
#2
@
4 years ago
- Keywords has-patch added
- Milestone changed from Awaiting Review to 5.6
- Owner set to SergeyBiryukov
- Status changed from new to reviewing
#3
@
4 years ago
Hi there!
isset( $post_data )
value is always true when it was empty array or non empty array.
Update the patch without checking it.
#4
@
4 years ago
I always assume that someone's going to do something wrong like try to pass in null or false as an argument. So in my opinion it's safer to at least check. One step further would be a check for is_array().
This ticket was mentioned in Slack in #core-media by antpb. View the logs.
4 years ago
This ticket was mentioned in Slack in #core-media by antpb. View the logs.
4 years ago
#8
@
4 years ago
- Milestone changed from 5.6 to 5.7
It was mentioned in the recent Media meeting that the sideloading functions could benefit from a refactor to allow testing as it is currently hard to test the same way that media_sideload_image
was and did not receive tests.
Being so late in beta cycle it's probably best to move this out of the 5.6 milestone. If anyone feels differently please feel free to move it back in.
This ticket was mentioned in Slack in #core-media by antpb. View the logs.
4 years ago
#11
@
4 years ago
- Keywords needs-unit-tests removed
So unfortunately, we can not add some unit tests easily for this function.
The patch seems quite minimal, so it shouldn't affect that much.
That said, we need some steps in order to do some manual testing, would be nice to get it tested by 2 different people at least.
In the meantime, we should open another ticket to handle the refactoring part in order to make unit test creation possible for these functions.
#12
@
4 years ago
Patch 50972.2.diff includes the following changes:
- Fixes a typo in the
isset
keyname - Uses the same year check strategy as with post, i.e. for consistency
- Optimizes setting the time by doing the
get_post
if nopost_date
is passed in the post data
#13
@
4 years ago
Testing strategy:
Used this code in a must-use script:
<?php add_action( 'admin_init', function() { if ( ! isset( $_GET['test50972'] ) ) { return; } $url = 'https://s.w.org/about/images/logos/wordpress-logo-stacked-rgb.png'; $tmp = download_url( $url ); if ( is_wp_error( $tmp ) ) { return; } $file_array = array( 'name' => basename( $url ), 'tmp_name' => $tmp ); $post_id = 0; $post_data = array(); //array( 'post_date' => '2019-01-01 00:00:00' ); media_handle_sideload( $file_array, $post_id, null, $post_data ); } );
Test an unattached file without passing post date:
- Go to the Media Library in the backend
- Append
?test50972=1
to the end of the URL in your browser - Press enter/return to load that URL
- The logo image should now appear in the library
- The image's path should be
wp-content/uploads/currentYear/currentMonth/ /Users/hellofromtonya/Dev/wordpress-develop/src/wp-content/uploads/2021/02/wordpress-logo-stacked-rgb.png
and the other versions of it
This is the previous expected behavior.
Test when attached to the default "Hello World" post 1 without passing post date:
- Change the date of the "Hello World" post to a different year such as "2020"
- In the test code above, change the
$post_id = 1;
- Repeat the above test
- The new image's path should be in `wp-content/uploads/currentYear/2020/
This is the previous expected behavior.
Test when passing the post's date:
- In the test code above, change the
$post_data
to use the commented out array:$post_data = array( 'post_date' => '2019-01-01 00:00:00' );
- Repeat the above test
- The new image's path should be in `wp-content/uploads/currentYear/2019/
This is the new change.
#14
@
4 years ago
FWIW: When I submitted this I tested my original patch and it worked as expected. I ended up just working around the issue because I needed the functionality immediately at the time.
#15
@
4 years ago
Just tested the patch as well, it works as expected, great enhancement on the patch @hellofromTonya well done.
#50971 was marked as a duplicate.