Media: In `media_sideload_image()`, store the original attachment URL in the `_source_url` post meta value.

Props dshanske, joemcgill, antpb.
Fixes #48164.
Built from https://develop.svn.wordpress.org/trunk@47251


git-svn-id: http://core.svn.wordpress.org/trunk@47051 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-02-10 23:47:04 +00:00
parent 56906bcf59
commit 507d056b7e
2 changed files with 12 additions and 4 deletions

View File

@ -967,11 +967,14 @@ function wp_media_upload_handler() {
* @since 4.2.0 Introduced the `$return` parameter. * @since 4.2.0 Introduced the `$return` parameter.
* @since 4.8.0 Introduced the 'id' option within 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.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 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 int $post_id Optional. The post ID the media is to be associated with.
* @param string $desc Optional. Description of the image. * @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. * @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' ) { 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 ) ) { if ( is_wp_error( $id ) ) {
@unlink( $file_array['tmp_name'] ); @unlink( $file_array['tmp_name'] );
return $id; 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; return $id;
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.4-alpha-47250'; $wp_version = '5.4-alpha-47251';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.