From 6d59caf4331d747d4e5e05eb0b1ec7f929346c61 Mon Sep 17 00:00:00 2001 From: Rafiq <36886362+vairafiq@users.noreply.github.com> Date: Sun, 18 Aug 2024 15:32:04 +0600 Subject: [PATCH] fix image upload (#1807) * fix image upload * fix * improved * improved --- includes/rest-api/functions.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/includes/rest-api/functions.php b/includes/rest-api/functions.php index 3d1f9f1846..bfa01d1bf2 100644 --- a/includes/rest-api/functions.php +++ b/includes/rest-api/functions.php @@ -127,12 +127,23 @@ function directorist_rest_upload_image_from_url( $image_url ) { ); } + $allowed_mime_types = get_allowed_mime_types(); + + // Add extension to the name when downloaded from extension less url + if ( strrpos( $file_array['name'], '.' ) === false ) { + $mime_type = mime_content_type( $file_array['tmp_name'] ); + $_mime_types = array_flip( $allowed_mime_types ); + $extensions = $_mime_types[ $mime_type ] ?? ''; + $extensions = explode( '|', $extensions, 2 ); + $file_array['name'] .= '.' . $extensions[0]; + } + // Do the validation and storage stuff. $file = wp_handle_sideload( $file_array, array( 'test_form' => false, - 'mimes' => directorist_rest_allowed_image_mime_types(), + 'mimes' => $allowed_mime_types, ), current_time( 'Y/m' ) );