From 61bd1364dd17e946216d7ba461d12dd79b65c594 Mon Sep 17 00:00:00 2001 From: vairafiq Date: Sun, 18 Aug 2024 15:13:33 +0600 Subject: [PATCH 1/4] fix image upload --- includes/rest-api/functions.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/includes/rest-api/functions.php b/includes/rest-api/functions.php index 3d1f9f1846..a8c606b933 100644 --- a/includes/rest-api/functions.php +++ b/includes/rest-api/functions.php @@ -127,12 +127,20 @@ function directorist_rest_upload_image_from_url( $image_url ) { ); } + + // 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'] ); + list( , $extension ) = explode( '/', $mime_type, 2 ); + $file_array['name'] .= '.' . $extension; + } + // Do the validation and storage stuff. $file = wp_handle_sideload( $file_array, array( 'test_form' => false, - 'mimes' => directorist_rest_allowed_image_mime_types(), + 'mimes' => get_allowed_mime_types(), ), current_time( 'Y/m' ) ); From 3badc6deb7a14a85eb899c86fbc1dd4b926a8fb7 Mon Sep 17 00:00:00 2001 From: vairafiq Date: Sun, 18 Aug 2024 15:15:09 +0600 Subject: [PATCH 2/4] fix --- includes/rest-api/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/rest-api/functions.php b/includes/rest-api/functions.php index a8c606b933..7a44b0b967 100644 --- a/includes/rest-api/functions.php +++ b/includes/rest-api/functions.php @@ -140,7 +140,7 @@ function directorist_rest_upload_image_from_url( $image_url ) { $file_array, array( 'test_form' => false, - 'mimes' => get_allowed_mime_types(), + 'mimes' => directorist_rest_allowed_image_mime_types(), ), current_time( 'Y/m' ) ); From 95e32a65c95dd5c64eb6fbeef4cd3b1f839d25d0 Mon Sep 17 00:00:00 2001 From: vairafiq Date: Sun, 18 Aug 2024 15:28:37 +0600 Subject: [PATCH 3/4] 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 7a44b0b967..ad31a5ae47 100644 --- a/includes/rest-api/functions.php +++ b/includes/rest-api/functions.php @@ -135,12 +135,23 @@ function directorist_rest_upload_image_from_url( $image_url ) { $file_array['name'] .= '.' . $extension; } + $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' ) ); From ed06f5553a5f46ae358bdf501cf0b3fb479874c0 Mon Sep 17 00:00:00 2001 From: vairafiq Date: Sun, 18 Aug 2024 15:30:56 +0600 Subject: [PATCH 4/4] improved --- includes/rest-api/functions.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/includes/rest-api/functions.php b/includes/rest-api/functions.php index ad31a5ae47..bfa01d1bf2 100644 --- a/includes/rest-api/functions.php +++ b/includes/rest-api/functions.php @@ -127,14 +127,6 @@ function directorist_rest_upload_image_from_url( $image_url ) { ); } - - // 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'] ); - list( , $extension ) = explode( '/', $mime_type, 2 ); - $file_array['name'] .= '.' . $extension; - } - $allowed_mime_types = get_allowed_mime_types(); // Add extension to the name when downloaded from extension less url