From 45cb25d4f26af4ebc8f9ca914d5566976580e642 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 10 Feb 2020 23:45:50 +0000 Subject: [PATCH] Media: In `media_sideload_image()`, store the original attachment URL in the `_source_url` post meta value. Props dshanske, joemcgill, antpb. Fixes #48164. git-svn-id: https://develop.svn.wordpress.org/trunk@47251 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/media.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php index 6d1b085c2a..eba2c8b8db 100644 --- a/src/wp-admin/includes/media.php +++ b/src/wp-admin/includes/media.php @@ -967,11 +967,14 @@ function wp_media_upload_handler() { * @since 4.2.0 Introduced the `$return` parameter. * @since 4.8.0 Introduced the 'id' option within the `$return` parameter. * @since 5.3.0 The `$post_id` parameter was made optional. + * @since 5.4.0 The original URL of the attachment is stored in the `_source_url` + * post meta value. * * @param string $file The URL of the image to download. * @param int $post_id Optional. The post ID the media is to be associated with. * @param string $desc Optional. Description of the image. - * @param string $return Optional. Accepts 'html' (image tag html) or 'src' (URL), or 'id' (attachment ID). Default 'html'. + * @param string $return Optional. Accepts 'html' (image tag html) or 'src' (URL), + * or 'id' (attachment ID). Default 'html'. * @return string|WP_Error Populated HTML img tag on success, WP_Error object otherwise. */ function media_sideload_image( $file, $post_id = 0, $desc = null, $return = 'html' ) { @@ -1002,8 +1005,13 @@ function media_sideload_image( $file, $post_id = 0, $desc = null, $return = 'htm if ( is_wp_error( $id ) ) { @unlink( $file_array['tmp_name'] ); return $id; - // If attachment id was requested, return it early. - } elseif ( 'id' === $return ) { + } + + // Store the original attachment source in meta. + add_post_meta( $id, '_source_url', $file ); + + // If attachment id was requested, return it. + if ( 'id' === $return ) { return $id; }