From 27a113951b4991b80cc9b8ca8f664eafcf9611b7 Mon Sep 17 00:00:00 2001 From: Anthony Burchell Date: Mon, 8 Feb 2021 23:20:56 +0000 Subject: [PATCH] Media: Allow `post_date` to be respected in `media_handle_sideload()`. Previously, date information was unable to be changed when using `media_handle_sideload()`. Now you can override the date for a media item using `$post_data['post_date']` before using the function. Props jamesgol, mukesh27, SergeyBiryukov, hellofromTonya, Mista-Flo. Fixes #50972. git-svn-id: https://develop.svn.wordpress.org/trunk@50258 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/media.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php index 8e929f38a7..6626937568 100644 --- a/src/wp-admin/includes/media.php +++ b/src/wp-admin/includes/media.php @@ -430,21 +430,23 @@ function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrid * @since 2.6.0 * @since 5.3.0 The `$post_id` parameter was made optional. * - * @param array $file_array Array that represents a `$_FILES` upload array. - * @param int $post_id Optional. The post ID the media is associated with. - * @param string $desc Optional. Description of the side-loaded file. Default null. - * @param array $post_data Optional. Post data to override. Default empty array. + * @param string[] $file_array Array that represents a `$_FILES` upload array. + * @param int $post_id Optional. The post ID the media is associated with. + * @param string $desc Optional. Description of the side-loaded file. Default null. + * @param array $post_data Optional. Post data to override. Default empty array. * @return int|WP_Error The ID of the attachment or a WP_Error on failure. */ function media_handle_sideload( $file_array, $post_id = 0, $desc = null, $post_data = array() ) { $overrides = array( 'test_form' => false ); - $time = current_time( 'mysql' ); - $post = get_post( $post_id ); - - if ( $post ) { - if ( substr( $post->post_date, 0, 4 ) > 0 ) { + if ( isset( $post_data['post_date'] ) && substr( $post_data['post_date'], 0, 4 ) > 0 ) { + $time = $post_data['post_date']; + } else { + $post = get_post( $post_id ); + if ( $post && substr( $post->post_date, 0, 4 ) > 0 ) { $time = $post->post_date; + } else { + $time = current_time( 'mysql' ); } }