Skip to content

Commit

Permalink
Disable editor uploading WebP, AVIF or HEIC when the server doesn’t s…
Browse files Browse the repository at this point in the history
…upport these types
  • Loading branch information
adamsilverstein committed Jan 10, 2025
1 parent be9a01a commit 18f9911
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,19 @@ function get_default_block_editor_settings() {

$allowed_mime_types = get_allowed_mime_types();

// Iterate through mime types and remove those unsupported by the image editor.
foreach ( $allowed_mime_types as $mime_type_extensions => $mime_type ) {
if ( ! wp_image_editor_supports( array( 'mime_type' => $mime_type ) ) ) {
unset( $allowed_mime_types[ $mime_type_extensions ] );
}
// Check if WebP images can be edited.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
unset( $allowed_mime_types['image/webp'] );
}

// Check if AVIF images can be edited.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
unset( $allowed_mime_types['image/avif'] );
}

// Check if HEIC images can be edited.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/heic' ) ) ) {
unset( $allowed_mime_types['image/heic'] );
}

$editor_settings = array(
Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/tests/blocks/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public function test_get_allowed_block_types_deprecated_filter_post_editor() {

/**
* @ticket 52920
* @ticket 61167
*/
public function test_get_default_block_editor_settings() {
$settings = get_default_block_editor_settings();
Expand Down Expand Up @@ -300,6 +301,26 @@ public function test_get_default_block_editor_settings() {
);
$this->assertIsInt( $settings['maxUploadFileSize'] );
$this->assertTrue( $settings['__unstableGalleryWithImageBlocks'] );

// Confirm that `allowedMimeTypes` matches the supported mime types.
$allowed_mime_types = get_allowed_mime_types();

// Check if WebP images can be edited.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
unset( $allowed_mime_types['image/webp'] );
}

// Check if AVIF images can be edited.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
unset( $allowed_mime_types['image/avif'] );
}

// Check if HEIC images can be edited.
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/heic' ) ) ) {
unset( $allowed_mime_types['image/heic'] );
}

$this->assertSameSets( $allowed_mime_types, $settings['allowedMimeTypes'] );
}

/**
Expand Down

0 comments on commit 18f9911

Please sign in to comment.