Ticket #55443: reintroduce-webp-default-output.2.diff
File reintroduce-webp-default-output.2.diff, 12.9 KB (added by , 2 years ago) |
---|
-
src/wp-admin/includes/admin-filters.php
diff --git a/src/wp-admin/includes/admin-filters.php b/src/wp-admin/includes/admin-filters.php index e80c111dde..f5f9600332 100644
a b add_filter( 'media_upload_library', 'media_upload_library' ); 37 37 38 38 add_filter( 'media_upload_tabs', 'update_gallery_tab' ); 39 39 40 add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' ); 41 40 42 // Admin color schemes. 41 43 add_action( 'admin_init', 'register_admin_color_schemes', 1 ); 42 44 add_action( 'admin_head', 'wp_color_scheme_settings' ); -
src/wp-admin/includes/image-edit.php
diff --git a/src/wp-admin/includes/image-edit.php b/src/wp-admin/includes/image-edit.php index e814fc47e7..9c71e68794 100644
a b function wp_save_image( $post_id ) { 917 917 } 918 918 919 919 // Save the full-size file, also needed to create sub-sizes. 920 if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) { 920 $saved = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ); 921 if ( ! $saved ) { 921 922 $return->error = esc_js( __( 'Unable to save the image.' ) ); 922 923 return $return; 923 924 } 925 $new_path = $saved['path']; 924 926 925 927 if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) { 926 928 $tag = false; -
src/wp-admin/includes/media.php
diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php index 510fff563d..e9cea8b6fb 100644
a b function wp_media_attach_action( $parent_id, $action = 'attach' ) { 3843 3843 exit; 3844 3844 } 3845 3845 } 3846 3847 /** 3848 * Filters the default image output mapping. 3849 * 3850 * With this filter callback, WebP image files will be generated for certain JPEG source files. 3851 * 3852 * @since 6.1.0 3853 * 3854 * @param array $output_mapping Map of mime type to output format. 3855 * @retun array The adjusted default output mapping. 3856 */ 3857 function wp_default_image_output_mapping( $output_mapping ) { 3858 $output_mapping['image/jpeg'] = 'image/webp'; 3859 return $output_mapping; 3860 } -
src/wp-includes/class-wp-image-editor.php
diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index caa3092d36..48472bb11f 100644
a b abstract class WP_Image_Editor { 446 446 $name = wp_basename( $this->file, ".$ext" ); 447 447 $new_ext = strtolower( $extension ? $extension : $ext ); 448 448 449 // When the file extension being generated doesn't match the original image file extension, 450 // add the original extension to the suffix to ensure a unique file name. Prevents 451 // generated file name conflicts when a source image type can have multiple extensions, 452 // eg. .jpg, .jpeg and .jpe are all valid JPEG extensions. 453 if ( ! empty( $extension ) && $extension !== $ext ) { 454 $suffix .= "-{$ext}"; 455 } 456 449 457 if ( ! is_null( $dest_path ) ) { 450 458 if ( ! wp_is_stream( $dest_path ) ) { 451 459 $_dest_path = realpath( $dest_path ); -
tests/phpunit/tests/image/editor.php
diff --git a/tests/phpunit/tests/image/editor.php b/tests/phpunit/tests/image/editor.php index 487dad0664..d478dd6bf1 100644
a b class Tests_Image_Editor extends WP_Image_UnitTestCase { 18 18 require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; 19 19 20 20 require_once DIR_TESTDATA . '/../includes/mock-image-editor.php'; 21 add_filter( 'image_editor_output_format', '__return_empty_array' ); 21 22 22 23 // This needs to come after the mock image editor class is loaded. 23 24 parent::set_up(); 24 25 } 25 26 27 /** 28 * Tear down the class. 29 */ 30 public function tear_down() { 31 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 32 parent::tear_down(); 33 } 34 26 35 /** 27 36 * Test wp_get_image_editor() where load returns true 28 37 * … … class Tests_Image_Editor extends WP_Image_UnitTestCase { 226 235 $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor->generate_filename( null, get_temp_dir() ) ) ) ) ); 227 236 228 237 // Test with a suffix only. 229 $this->assertSame( 'canola-100x50 .png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );238 $this->assertSame( 'canola-100x50-jpg.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) ); 230 239 231 240 // Combo! 232 $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new .png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );241 $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new-jpg.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) ); 233 242 234 243 // Test with a stream destination. 235 244 $this->assertSame( 'file://testing/path/canola-100x50.jpg', $editor->generate_filename( null, 'file://testing/path' ) ); -
tests/phpunit/tests/image/editorGd.php
diff --git a/tests/phpunit/tests/image/editorGd.php b/tests/phpunit/tests/image/editorGd.php index 5d967bdcdb..88dfb80299 100644
a b class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 17 17 require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; 18 18 require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; 19 19 20 add_filter( 'image_editor_output_format', '__return_empty_array' ); 21 20 22 // This needs to come after the mock image editor class is loaded. 21 23 parent::set_up(); 22 24 } … … class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 30 32 31 33 $this->remove_added_uploads(); 32 34 35 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 36 33 37 parent::tear_down(); 34 38 } 35 39 -
tests/phpunit/tests/image/editorImagick.php
diff --git a/tests/phpunit/tests/image/editorImagick.php b/tests/phpunit/tests/image/editorImagick.php index 02e0590a2e..a6698ec22c 100644
a b class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 18 18 require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php'; 19 19 require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php'; 20 20 21 add_filter( 'image_editor_output_format', '__return_empty_array' ); 22 21 23 // This needs to come after the mock image editor class is loaded. 22 24 parent::set_up(); 23 25 } … … class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 31 33 32 34 $this->remove_added_uploads(); 33 35 36 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 37 34 38 parent::tear_down(); 35 39 } 36 40 -
tests/phpunit/tests/image/functions.php
diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 86d559145e..75e5514c8b 100644
a b class Tests_Image_Functions extends WP_UnitTestCase { 25 25 foreach ( glob( $folder ) as $file ) { 26 26 unlink( $file ); 27 27 } 28 29 add_filter( 'image_editor_output_format', '__return_empty_array' ); 30 } 31 32 /** 33 * Tear down the class. 34 */ 35 public function tear_down() { 36 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 37 parent::tear_down(); 28 38 } 29 39 30 40 /** -
tests/phpunit/tests/image/intermediateSize.php
diff --git a/tests/phpunit/tests/image/intermediateSize.php b/tests/phpunit/tests/image/intermediateSize.php index 830359427a..deb72c11e0 100644
a b 5 5 * @group upload 6 6 */ 7 7 class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 8 /** 9 * Set up the test fixture. 10 */ 11 public function set_up() { 12 add_filter( 'image_editor_output_format', '__return_empty_array' ); 13 14 parent::set_up(); 15 } 16 8 17 public function tear_down() { 9 18 $this->remove_added_uploads(); 10 19 … … class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 12 21 remove_image_size( 'false-height' ); 13 22 remove_image_size( 'false-width' ); 14 23 remove_image_size( 'off-by-one' ); 24 25 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 26 15 27 parent::tear_down(); 16 28 } 17 29 -
tests/phpunit/tests/image/resize.php
diff --git a/tests/phpunit/tests/image/resize.php b/tests/phpunit/tests/image/resize.php index 5b302ce295..8c448c842c 100644
a b abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase 14 14 parent::set_up(); 15 15 16 16 add_filter( 'wp_image_editors', array( $this, 'wp_image_editors' ) ); 17 add_filter( 'image_editor_output_format', '__return_empty_array' ); 18 } 19 20 /** 21 * Tear down the class. 22 */ 23 public function tear_down() { 24 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 25 parent::tear_down(); 17 26 } 18 27 19 28 public function wp_image_editors() { -
tests/phpunit/tests/media.php
diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index ba4bedf51e..52a71e23a3 100644
a b CAP; 33 33 self::$_sizes = wp_get_additional_image_sizes(); 34 34 $GLOBALS['_wp_additional_image_sizes'] = array(); 35 35 36 $filename = DIR_TESTDATA . '/images/' . self::$large_filename; 36 $filename = DIR_TESTDATA . '/images/' . self::$large_filename; 37 add_filter( 'image_editor_output_format', '__return_empty_array' ); 37 38 self::$large_id = $factory->attachment->create_upload_object( $filename ); 38 39 39 40 $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' ); … … CAP; 68 69 69 70 public static function wpTearDownAfterClass() { 70 71 $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes; 72 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 71 73 } 72 74 73 75 public static function tear_down_after_class() { … … EOF; 3617 3619 // Clean up the above filter. 3618 3620 remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 ); 3619 3621 } 3622 3623 /** 3624 * Test the wp_default_image_output_mapping function. 3625 * 3626 * @ticket 55443 3627 */ 3628 public function test_wp_default_image_output_mapping() { 3629 $mapping = wp_default_image_output_mapping( array() ); 3630 $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping ); 3631 } 3632 3633 /** 3634 * Test that wp_default_image_output_mapping doesn't overwrite existing mappings. 3635 * 3636 * @ticket 55443 3637 */ 3638 public function test_wp_default_image_output_mapping_existing() { 3639 $mapping = array( 'mime/png' => 'mime/webp' ); 3640 $mapping = wp_default_image_output_mapping( $mapping ); 3641 $this->assertSame( 3642 array( 3643 'mime/png' => 'mime/webp', 3644 'image/jpeg' => 'image/webp', 3645 ), 3646 $mapping 3647 ); 3648 } 3649 3650 /** 3651 * Test that the image editor default output for JPEGs is WebP 3652 * 3653 * @ticket 55443 3654 */ 3655 public function test_wp_image_editor_default_output_maps_to_webp() { 3656 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 3657 3658 $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' ); 3659 if ( is_wp_error( $editor ) ) { 3660 $this->markTestSkipped( $editor->get_error_message() ); 3661 } 3662 $resized = $editor->resize( 100, 100, false ); 3663 3664 if ( is_wp_error( $resized ) ) { 3665 $this->markTestSkipped( $resized->get_error_message() ); 3666 } 3667 3668 $dest_file = $editor->generate_filename(); 3669 $saved = $editor->save( $dest_file ); 3670 3671 if ( is_wp_error( $saved ) ) { 3672 $this->markTestSkipped( $saved->get_error_message() ); 3673 } 3674 add_filter( 'image_editor_output_format', '__return_empty_array' ); 3675 3676 $this->assertSame( 'image/webp', $saved['mime-type'] ); 3677 $this->assertSame( 'canola-100x75.webp', $saved['file'] ); 3678 } 3620 3679 } 3621 3680 3622 3681 /** -
tests/phpunit/tests/post/attachments.php
diff --git a/tests/phpunit/tests/post/attachments.php b/tests/phpunit/tests/post/attachments.php index 2922c185d2..1bbc0c6f79 100644
a b 6 6 * @group upload 7 7 */ 8 8 class Tests_Post_Attachments extends WP_UnitTestCase { 9 /** 10 * Set up the test fixture. 11 */ 12 public function set_up() { 13 add_filter( 'image_editor_output_format', '__return_empty_array' ); 14 15 parent::set_up(); 16 } 9 17 10 18 public function tear_down() { 11 19 // Remove all uploads. 12 20 $this->remove_added_uploads(); 21 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 13 22 parent::tear_down(); 14 23 } 15 24 -
tests/phpunit/tests/rest-api/rest-attachments-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index 106906ee2b..2c97f81c05 100644
a b class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 93 93 94 94 add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 ); 95 95 add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 ); 96 add_filter( 'image_editor_output_format', '__return_empty_array' ); 96 97 } 97 98 98 99 public function wpSetUpBeforeRequest( $result ) { … … class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 121 122 WP_Image_Editor_Mock::$size_return = null; 122 123 } 123 124 125 remove_filter( 'image_editor_output_format', '__return_empty_array' ); 126 124 127 parent::tear_down(); 125 128 } 126 129