Make WordPress Core


Ignore:
Timestamp:
03/25/2025 02:04:47 PM (18 months ago)
Author:
adamsilverstein
Message:

Media: prevent uploading image types the server doesn’t support.

Normalize behavior between uploading in the media library and uploading directly to the block editor. Now, when uploading an image with a mime type the server does not support (either in the media library or the block editor), the user will see an error message “This image cannot be processed by the web server. Convert it to JPEG or PNG before uploading”.

Alos, add a new filter wp_prevent_unsupported_mime_type_uploads which determines whether the server should prevent uploads for image types it doesn't support. The default value is true and the filter also receives the uploaded image mime type.

Props: joomskys, adamsilverstein, azaozz, swissspidy, joemcgill, flixos90, audrasjb. 

Fixes #61167

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r58865 r60084  
    134134                                array( 'status' => rest_authorization_required_code() )
    135135                        );
     136                }
     137                $files = $request->get_file_params();
     138
     139                /**
     140                 * Filter whether the server should prevent uploads for image types it doesn't support. Default true.
     141                 *
     142                 * Developers can use this filter to enable uploads of certain image types. By default image types that are not
     143                 * supported by the server are prevented from being uploaded.
     144                 *
     145                 * @since 6.8.0
     146                 *
     147                 * @param bool        $check_mime Whether to prevent uploads of unsupported image types.
     148                 * @param string|null $mime_type  The mime type of the file being uploaded (if available).
     149                 */
     150                $prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, isset( $files['file']['type'] ) ? $files['file']['type'] : null );
     151
     152                // If the upload is an image, check if the server can handle the mime type.
     153                if (
     154                        $prevent_unsupported_uploads &&
     155                        isset( $files['file']['type'] ) &&
     156                        str_starts_with( $files['file']['type'], 'image/' )
     157                ) {
     158                        // Check if the image editor supports the type.
     159                        if ( ! wp_image_editor_supports( array( 'mime_type' => $files['file']['type'] ) ) ) {
     160                                return new WP_Error(
     161                                        'rest_upload_image_type_not_supported',
     162                                        __( 'The web server cannot generate responsive image sizes for this image. Convert it to JPEG or PNG before uploading.' ),
     163                                        array( 'status' => 400 )
     164                                );
     165                        }
    136166                }
    137167
Note: See TracChangeset for help on using the changeset viewer.