Make WordPress Core

Ticket #55443: reintroduce-webp-default-output.3.diff

File reintroduce-webp-default-output.3.diff, 14.3 KB (added by adamsilverstein, 18 months 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' ); 
    3737
    3838add_filter( 'media_upload_tabs', 'update_gallery_tab' );
    3939
     40add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' );
     41
    4042// Admin color schemes.
    4143add_action( 'admin_init', 'register_admin_color_schemes', 1 );
    4244add_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 ) { 
    917917        }
    918918
    919919        // 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 ) {
    921922                $return->error = esc_js( __( 'Unable to save the image.' ) );
    922923                return $return;
    923924        }
     925        $new_path = $saved['path'];
    924926
    925927        if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
    926928                $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' ) { 
    38433843                exit;
    38443844        }
    38453845}
     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 */
     3857function 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..15e4aa8d6e 100644
    a b abstract class WP_Image_Editor { 
    424424                return array( $filename, $new_ext, $mime_type );
    425425        }
    426426
    427         /**
    428          * Builds an output filename based on current file, and adding proper suffix
     427                /**
     428         * Builds an output filename based on current file, and adding proper suffix.
    429429         *
    430430         * @since 3.5.0
    431          *
    432          * @param string $suffix
    433          * @param string $dest_path
    434          * @param string $extension
    435          * @return string filename
     431         * @since 6.1.0 Skips adding a suffix when set to an empty string. When the
     432         *              file extension being generated doesn't match the image file extension,
     433         *              add the extension to the suffix
     434         *
     435         * @param string $suffix    Optional. Suffix to add to the filename. The default null
     436         *                          will result in a 'widthxheight' suffix. Passing
     437         *                          an empty string will result in no suffix.
     438         * @param string $dest_path Optional. The path to save the file to. The default null
     439         *                          will use the image file path.
     440         * @param string $extension Optional. The file extension to use. The default null
     441         *                          will use the image file extension.
     442         * @return string filename The generated file name.
    436443         */
    437444        public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
    438445                // $suffix will be appended to the destination filename, just before the extension.
    439                 if ( ! $suffix ) {
     446                if ( null === $suffix ) {
    440447                        $suffix = $this->get_suffix();
    441448                }
    442449
    abstract class WP_Image_Editor { 
    457464                        }
    458465                }
    459466
    460                 return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
     467                if ( empty( $suffix ) ) {
     468                        $suffix = '';
     469                } else {
     470                        $suffix = "-{$suffix}";
     471                }
     472
     473                // When the file extension being generated doesn't match the image file extension,
     474                // add the extension to the suffix to ensure a unique file name. Prevents
     475                // name conflicts when a single image type can have multiple extensions,
     476                // eg. .jpg, .jpeg and .jpe are all valid JPEG extensions.
     477                if ( ! empty( $extension ) && $extension !== $ext ) {
     478                        $suffix .= "-{$ext}";
     479                }
     480
     481                return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
    461482        }
    462483
    463484        /**
  • 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 { 
    1818                require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
    1919
    2020                require_once DIR_TESTDATA . '/../includes/mock-image-editor.php';
     21                add_filter( 'image_editor_output_format', '__return_empty_array' );
    2122
    2223                // This needs to come after the mock image editor class is loaded.
    2324                parent::set_up();
    2425        }
    2526
     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
    2635        /**
    2736         * Test wp_get_image_editor() where load returns true
    2837         *
    class Tests_Image_Editor extends WP_Image_UnitTestCase { 
    226235                $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor->generate_filename( null, get_temp_dir() ) ) ) ) );
    227236
    228237                // 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' ) ) );
    230239
    231240                // 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' ) );
    233242
    234243                // Test with a stream destination.
    235244                $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 { 
    1717                require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
    1818                require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
    1919
     20                add_filter( 'image_editor_output_format', '__return_empty_array' );
     21
    2022                // This needs to come after the mock image editor class is loaded.
    2123                parent::set_up();
    2224        }
    class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { 
    3032
    3133                $this->remove_added_uploads();
    3234
     35                remove_filter( 'image_editor_output_format', '__return_empty_array' );
     36
    3337                parent::tear_down();
    3438        }
    3539
  • 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 { 
    1818                require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
    1919                require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';
    2020
     21                add_filter( 'image_editor_output_format', '__return_empty_array' );
     22
    2123                // This needs to come after the mock image editor class is loaded.
    2224                parent::set_up();
    2325        }
    class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase { 
    3133
    3234                $this->remove_added_uploads();
    3335
     36                remove_filter( 'image_editor_output_format', '__return_empty_array' );
     37
    3438                parent::tear_down();
    3539        }
    3640
  • 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 { 
    2525                foreach ( glob( $folder ) as $file ) {
    2626                        unlink( $file );
    2727                }
     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();
    2838        }
    2939
    3040        /**
  • 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  
    55 * @group upload
    66 */
    77class 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
    817        public function tear_down() {
    918                $this->remove_added_uploads();
    1019
    class Tests_Image_Intermediate_Size extends WP_UnitTestCase { 
    1221                remove_image_size( 'false-height' );
    1322                remove_image_size( 'false-width' );
    1423                remove_image_size( 'off-by-one' );
     24
     25                remove_filter( 'image_editor_output_format', '__return_empty_array' );
     26
    1527                parent::tear_down();
    1628        }
    1729
  • 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 
    1414                parent::set_up();
    1515
    1616                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();
    1726        }
    1827
    1928        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..1388e6c210 100644
    a b CAP; 
    3333                self::$_sizes                          = wp_get_additional_image_sizes();
    3434                $GLOBALS['_wp_additional_image_sizes'] = array();
    3535
    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' );
    3738                self::$large_id = $factory->attachment->create_upload_object( $filename );
    3839
    3940                $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' );
    CAP; 
    6869
    6970        public static function wpTearDownAfterClass() {
    7071                $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
     72                remove_filter( 'image_editor_output_format', '__return_empty_array' );
    7173        }
    7274
    7375        public static function tear_down_after_class() {
    EOF; 
    36173619                // Clean up the above filter.
    36183620                remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 );
    36193621        }
     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                $this->assertNotWPError( $editor );
     3660
     3661                $resized = $editor->resize( 100, 100, false );
     3662                $this->assertNotWPError( $resized );
     3663
     3664                $saved = $editor->save();
     3665                $this->assertNotWPError( $saved );
     3666
     3667                if ( $editor->supports_mime_type( 'image/webp' ) ) {
     3668                        $this->assertSame( 'image/webp', $saved['mime-type'] );
     3669                        $this->assertSame( 'canola-100x75-jpg.webp', $saved['file'] );
     3670                } else {
     3671                        $this->assertSame( 'image/jpeg', $saved['mime-type'] );
     3672                        $this->assertSame( 'canola-100x75.jpg', $saved['file'] );
     3673                }
     3674        }
    36203675}
    36213676
    36223677/**
  • 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  
    66 * @group upload
    77 */
    88class 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        }
    917
    1018        public function tear_down() {
    1119                // Remove all uploads.
    1220                $this->remove_added_uploads();
     21                remove_filter( 'image_editor_output_format', '__return_empty_array' );
    1322                parent::tear_down();
    1423        }
    1524
  • 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 
    9393
    9494                add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
    9595                add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
     96                add_filter( 'image_editor_output_format', '__return_empty_array' );
    9697        }
    9798
    9899        public function wpSetUpBeforeRequest( $result ) {
    class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 
    121122                        WP_Image_Editor_Mock::$size_return = null;
    122123                }
    123124
     125                remove_filter( 'image_editor_output_format', '__return_empty_array' );
     126
    124127                parent::tear_down();
    125128        }
    126129