From 18f99116a95ae400ad7f04a7974355513da63c93 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 10 Jan 2025 10:20:06 -0700 Subject: [PATCH] =?UTF-8?q?Disable=20editor=20uploading=20WebP,=20AVIF=20o?= =?UTF-8?q?r=20HEIC=20when=20the=20server=20doesn=E2=80=99t=20support=20th?= =?UTF-8?q?ese=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/wp-includes/block-editor.php | 18 +++++++++++++----- tests/phpunit/tests/blocks/editor.php | 21 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index d35d0cda58ce7..4b9aa76b94000 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -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( diff --git a/tests/phpunit/tests/blocks/editor.php b/tests/phpunit/tests/blocks/editor.php index 0682839605da9..a04d3638d745a 100644 --- a/tests/phpunit/tests/blocks/editor.php +++ b/tests/phpunit/tests/blocks/editor.php @@ -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(); @@ -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'] ); } /**