Media: Allow uploading images from URLs without extensions.

Enable `download_url()` to fetch and verify file types if the URL does not contain a file extension. This allows URL downloads to handle media endpoints like istockphoto.com that use file IDs and formatting arguments to deliver images.

Props masteradhoc, mitogh, joedolson, hellofromTonya, antpb, audrasjb, navi161, dmsnell.
Fixes #54738.
Built from https://develop.svn.wordpress.org/trunk@59902


git-svn-id: http://core.svn.wordpress.org/trunk@59244 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
joedolson
2025-03-03 00:19:23 +00:00
parent a10c294d3e
commit 371495c91d
2 changed files with 19 additions and 1 deletions

View File

@@ -1241,6 +1241,24 @@ function download_url( $url, $timeout = 300, $signature_verification = false ) {
}
}
$mime_type = wp_remote_retrieve_header( $response, 'content-type' );
if ( $mime_type && 'tmp' === pathinfo( $tmpfname, PATHINFO_EXTENSION ) ) {
$valid_mime_types = array_flip( get_allowed_mime_types() );
if ( ! empty( $valid_mime_types[ $mime_type ] ) ) {
$extensions = explode( '|', $valid_mime_types[ $mime_type ] );
$new_image_name = substr( $tmpfname, 0, -4 ) . ".{$extensions[0]}";
if ( 0 === validate_file( $new_image_name ) ) {
if ( rename( $tmpfname, $new_image_name ) ) {
$tmpfname = $new_image_name;
}
if ( ( $tmpfname !== $new_image_name ) && file_exists( $new_image_name ) ) {
unlink( $new_image_name );
}
}
}
}
$content_md5 = wp_remote_retrieve_header( $response, 'Content-MD5' );
if ( $content_md5 ) {

View File

@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.8-alpha-59901';
$wp_version = '6.8-alpha-59902';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.