Make WordPress Core

Changeset 54226


Ignore:
Timestamp:
09/19/2022 10:51:53 PM (19 months ago)
Author:
davidbaumwald
Message:

Media: Revert WebP generation.

Given Matt's recent post about removing WebP from core and possibly implementing the feature in a future Canonical Plugin, this change reverts changesets [54086], [54094], and [54097]. Additionally, [54210] contained a coding standards follow-up in one of the affected files that is no longer needed.

Reverts [54086], [54094], and [54097].

Props SergeyBiryukov.
See #55443.

Location:
trunk
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image-edit.php

    r54086 r54226  
    918918
    919919    // Save the full-size file, also needed to create sub-sizes.
    920     $saved = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
    921     if ( ! $saved ) {
     920    if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
    922921        $return->error = esc_js( __( 'Unable to save the image.' ) );
    923922        return $return;
    924923    }
    925     $new_path = $saved['path'];
    926924
    927925    if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
  • trunk/src/wp-admin/includes/image.php

    r54097 r54226  
    452452    if ( method_exists( $editor, 'make_subsize' ) ) {
    453453        foreach ( $new_sizes as $new_size_name => $new_size_data ) {
    454             // Include size name in the data.
    455             $new_size_data['name'] = $new_size_name;
    456 
    457454            $new_size_meta = $editor->make_subsize( $new_size_data );
    458455
  • trunk/src/wp-includes/class-wp-image-editor-gd.php

    r54210 r54226  
    228228     *
    229229     * @param array $sizes {
    230      *     Associative array of image size names and their data.
     230     *     An array of image size data arrays.
    231231     *
    232232     *     Either a height or width must be provided.
     
    248248
    249249        foreach ( $sizes as $size => $size_data ) {
    250             // Include size name in the data.
    251             $size_data['name'] = $size;
    252 
    253250            $meta = $this->make_subsize( $size_data );
    254251
     
    265262     *
    266263     * @since 5.3.0
    267      * @since 6.1.0 The $sizes parameter may now include a $name key for each entry.
    268264     *
    269265     * @param array $size_data {
    270266     *     Array of size data.
    271267     *
    272      *     @type int    $width  The maximum width in pixels.
    273      *     @type int    $height The maximum height in pixels.
    274      *     @type bool   $crop   Whether to crop the image to exact dimensions.
    275      *     @type string $name   Image size name.
     268     *     @type int  $width  The maximum width in pixels.
     269     *     @type int  $height The maximum height in pixels.
     270     *     @type bool $crop   Whether to crop the image to exact dimensions.
    276271     * }
    277272     * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
     
    283278        }
    284279
    285         $orig_size      = $this->size;
    286         $orig_size_name = $this->size_name;
     280        $orig_size = $this->size;
    287281
    288282        if ( ! isset( $size_data['width'] ) ) {
     
    296290        if ( ! isset( $size_data['crop'] ) ) {
    297291            $size_data['crop'] = false;
    298         }
    299 
    300         if ( isset( $size_data['name'] ) ) {
    301             $this->update_size_name( $size_data['name'] );
    302292        }
    303293
     
    311301        }
    312302
    313         $this->size      = $orig_size;
    314         $this->size_name = $orig_size_name;
     303        $this->size = $orig_size;
    315304
    316305        if ( ! is_wp_error( $saved ) ) {
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r54097 r54226  
    439439     *
    440440     * @param array $sizes {
    441      *     Associative array of image size names and their data.
     441     *     An array of image size data arrays.
    442442     *
    443443     *     Either a height or width must be provided.
     
    459459
    460460        foreach ( $sizes as $size => $size_data ) {
    461             // Include size name in the data.
    462             $size_data['name'] = $size;
    463 
    464461            $meta = $this->make_subsize( $size_data );
    465462
     
    476473     *
    477474     * @since 5.3.0
    478      * @since 6.1.0 The $sizes parameter may now include a $name key for each entry.
    479475     *
    480476     * @param array $size_data {
    481477     *     Array of size data.
    482478     *
    483      *     @type int    $width  The maximum width in pixels.
    484      *     @type int    $height The maximum height in pixels.
    485      *     @type bool   $crop   Whether to crop the image to exact dimensions.
    486      *     @type string $name   Image size name.
     479     *     @type int  $width  The maximum width in pixels.
     480     *     @type int  $height The maximum height in pixels.
     481     *     @type bool $crop   Whether to crop the image to exact dimensions.
    487482     * }
    488483     * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
     
    494489        }
    495490
    496         $orig_size      = $this->size;
    497         $orig_size_name = $this->size_name;
    498         $orig_image     = $this->image->getImage();
     491        $orig_size  = $this->size;
     492        $orig_image = $this->image->getImage();
    499493
    500494        if ( ! isset( $size_data['width'] ) ) {
     
    508502        if ( ! isset( $size_data['crop'] ) ) {
    509503            $size_data['crop'] = false;
    510         }
    511 
    512         if ( isset( $size_data['name'] ) ) {
    513             $this->update_size_name( $size_data['name'] );
    514504        }
    515505
     
    526516        }
    527517
    528         $this->size      = $orig_size;
    529         $this->size_name = $orig_size_name;
    530         $this->image     = $orig_image;
     518        $this->size  = $orig_size;
     519        $this->image = $orig_image;
    531520
    532521        if ( ! is_wp_error( $saved ) ) {
  • trunk/src/wp-includes/class-wp-image-editor.php

    r54133 r54226  
    1616    protected $file              = null;
    1717    protected $size              = null;
    18     protected $size_name         = '';
    1918    protected $mime_type         = null;
    2019    protected $output_mime_type  = null;
     
    120119     *
    121120     * @param array $sizes {
    122      *     Associative array of image size names and their data. Default sizes are 'small', 'medium', 'large'.
     121     *     An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
    123122     *
    124123     *     @type array ...$0 {
     
    188187     * @since 3.5.0
    189188     *
    190      * @return array {
     189     * @return int[] {
    191190     *     Dimensions of the image.
    192191     *
     
    204203     * @since 3.5.0
    205204     *
    206      * @param int $width  The image width.
    207      * @param int $height The image height.
    208      * @return true True on success, false on failure.
     205     * @param int $width
     206     * @param int $height
     207     * @return true
    209208     */
    210209    protected function update_size( $width = null, $height = null ) {
     
    214213        );
    215214        return true;
    216     }
    217 
    218     /**
    219      * Gets the current image size name.
    220      *
    221      * @since 6.1.0
    222      *
    223      * @return string Image size name, or empty string if none set.
    224      */
    225     public function get_size_name() {
    226         return $this->size_name;
    227     }
    228 
    229     /**
    230      * Sets the current image size name.
    231      *
    232      * @since 6.1.0
    233      *
    234      * @param string $size_name The image size name.
    235      */
    236     protected function update_size_name( $size_name ) {
    237         $this->size_name = (string) $size_name;
    238215    }
    239216
     
    389366         *
    390367         * @since 5.8.0
    391          * @since 6.1.0 The $size_name parameter was added.
    392368         *
    393369         * @param string[] $output_format {
     
    399375         * @param string $filename  Path to the image.
    400376         * @param string $mime_type The source image mime type.
    401          * @param string $size_name The image size name to create, or empty string if not set.
    402377         */
    403         $output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type, $this->size_name );
     378        $output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type );
    404379
    405380        if ( isset( $output_format[ $mime_type ] )
     
    451426    }
    452427
    453         /**
    454      * Builds an output filename based on current file, and adding proper suffix.
    455      *
    456      * @since 3.5.0
    457      * @since 6.1.0 Skips adding a suffix when set to an empty string. When the
    458      *              file extension being generated doesn't match the image file extension,
    459      *              add the extension to the suffix
    460      *
    461      * @param string $suffix    Optional. Suffix to add to the filename. The default null
    462      *                          will result in a 'widthxheight' suffix. Passing
    463      *                          an empty string will result in no suffix.
    464      * @param string $dest_path Optional. The path to save the file to. The default null
    465      *                          will use the image file path.
    466      * @param string $extension Optional. The file extension to use. The default null
    467      *                          will use the image file extension.
    468      * @return string filename The generated file name.
     428    /**
     429     * Builds an output filename based on current file, and adding proper suffix
     430     *
     431     * @since 3.5.0
     432     *
     433     * @param string $suffix
     434     * @param string $dest_path
     435     * @param string $extension
     436     * @return string filename
    469437     */
    470438    public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
    471439        // $suffix will be appended to the destination filename, just before the extension.
    472         if ( null === $suffix ) {
     440        if ( ! $suffix ) {
    473441            $suffix = $this->get_suffix();
    474442        }
     
    491459        }
    492460
    493         if ( empty( $suffix ) ) {
    494             $suffix = '';
    495         } else {
    496             $suffix = "-{$suffix}";
    497         }
    498 
    499         // When the file extension being generated doesn't match the image file extension,
    500         // add the extension to the suffix to ensure a unique file name. Prevents
    501         // name conflicts when a single image type can have multiple extensions,
    502         // eg. .jpg, .jpeg and .jpe are all valid JPEG extensions.
    503         if ( ! empty( $extension ) && $extension !== $ext ) {
    504             $suffix .= "-{$ext}";
    505         }
    506 
    507         return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
     461        return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
    508462    }
    509463
  • trunk/src/wp-includes/default-filters.php

    r54214 r54226  
    637637add_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
    638638
    639 add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping', 10, 4 );
    640 
    641639// Embeds.
    642640add_action( 'rest_api_init', 'wp_oembed_register_route' );
  • trunk/src/wp-includes/functions.php

    r54176 r54226  
    26912691        if ( $is_image ) {
    26922692            /** This filter is documented in wp-includes/class-wp-image-editor.php */
    2693             $output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type, '' );
     2693            $output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type );
    26942694            $alt_types      = array();
    26952695
  • trunk/src/wp-includes/media.php

    r54173 r54226  
    39283928
    39293929/**
    3930  * Filters the default image output mapping.
    3931  *
    3932  * With this filter callback, WebP image files will be generated for certain JPEG source files.
    3933  *
    3934  * @since 6.1.0
    3935  *
    3936  * @param array $output_mapping Map of mime type to output format.
    3937  * @param string $filename  Path to the image.
    3938  * @param string $mime_type The source image mime type.
    3939  * @param string $size_name Optional. The image size name to create, or empty string if not set. Default empty string.
    3940  * @return array The adjusted default output mapping.
    3941  */
    3942 function wp_default_image_output_mapping( $output_mapping, $filename, $mime_type, $size_name = '' ) {
    3943     // If size name is specified, check whether the size supports additional MIME types like WebP.
    3944     if ( $size_name ) {
    3945         // Include only the core sizes that do not rely on add_image_size(). Additional image sizes are opt-in.
    3946         $enabled_sizes = array(
    3947             'thumbnail'      => true,
    3948             'medium'         => true,
    3949             'medium_large'   => true,
    3950             'large'          => true,
    3951             'post-thumbnail' => true,
    3952         );
    3953 
    3954         /**
    3955          * Filters the sizes that support secondary mime type output. Developers can use this
    3956          * to control the generation of additional mime type sub-sized images.
    3957          *
    3958          * @since 6.1.0
    3959          *
    3960          * @param array $enabled_sizes Map of size names and whether they support secondary mime type output.
    3961          */
    3962         $enabled_sizes = apply_filters( 'wp_image_sizes_with_additional_mime_type_support', $enabled_sizes );
    3963 
    3964         // Bail early if the size does not support additional MIME types.
    3965         if ( empty( $enabled_sizes[ $size_name ] ) ) {
    3966             return $output_mapping;
    3967         }
    3968     }
    3969 
    3970     $output_mapping['image/jpeg'] = 'image/webp';
    3971     return $output_mapping;
    3972 }
    3973 
    3974 /**
    39753930 * Prints default Plupload arguments.
    39763931 *
  • trunk/tests/phpunit/tests/image/editor.php

    r54086 r54226  
    1919
    2020        require_once DIR_TESTDATA . '/../includes/mock-image-editor.php';
    21         add_filter( 'image_editor_output_format', '__return_empty_array' );
    2221
    2322        // This needs to come after the mock image editor class is loaded.
    2423        parent::set_up();
    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();
    3324    }
    3425
     
    236227
    237228        // Test with a suffix only.
    238         $this->assertSame( 'canola-100x50-jpg.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
     229        $this->assertSame( 'canola-100x50.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
    239230
    240231        // Combo!
    241         $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new-jpg.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
     232        $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
    242233
    243234        // Test with a stream destination.
  • trunk/tests/phpunit/tests/image/editorGd.php

    r54086 r54226  
    1818        require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
    1919
    20         add_filter( 'image_editor_output_format', '__return_empty_array' );
    21 
    2220        // This needs to come after the mock image editor class is loaded.
    2321        parent::set_up();
     
    3230
    3331        $this->remove_added_uploads();
    34 
    35         remove_filter( 'image_editor_output_format', '__return_empty_array' );
    3632
    3733        parent::tear_down();
  • trunk/tests/phpunit/tests/image/editorImagick.php

    r54086 r54226  
    1919        require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';
    2020
    21         add_filter( 'image_editor_output_format', '__return_empty_array' );
    22 
    2321        // This needs to come after the mock image editor class is loaded.
    2422        parent::set_up();
     
    3331
    3432        $this->remove_added_uploads();
    35 
    36         remove_filter( 'image_editor_output_format', '__return_empty_array' );
    3733
    3834        parent::tear_down();
  • trunk/tests/phpunit/tests/image/functions.php

    r54090 r54226  
    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();
    3828    }
    3929
  • trunk/tests/phpunit/tests/image/intermediateSize.php

    r54086 r54226  
    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 
    178    public function tear_down() {
    189        $this->remove_added_uploads();
     
    2213        remove_image_size( 'false-width' );
    2314        remove_image_size( 'off-by-one' );
    24 
    25         remove_filter( 'image_editor_output_format', '__return_empty_array' );
    26 
    2715        parent::tear_down();
    2816    }
  • trunk/tests/phpunit/tests/image/resize.php

    r54086 r54226  
    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();
    2617    }
    2718
  • trunk/tests/phpunit/tests/media.php

    r54128 r54226  
    3434        $GLOBALS['_wp_additional_image_sizes'] = array();
    3535
    36         $filename = DIR_TESTDATA . '/images/' . self::$large_filename;
    37         add_filter( 'image_editor_output_format', '__return_empty_array' );
     36        $filename       = DIR_TESTDATA . '/images/' . self::$large_filename;
    3837        self::$large_id = $factory->attachment->create_upload_object( $filename );
    3938
     
    7069    public static function wpTearDownAfterClass() {
    7170        $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
    72         remove_filter( 'image_editor_output_format', '__return_empty_array' );
    7371    }
    7472
     
    36243622        remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 );
    36253623    }
    3626 
    3627     /**
    3628      * Test the wp_default_image_output_mapping function.
    3629      *
    3630      * @ticket 55443
    3631      */
    3632     public function test_wp_default_image_output_mapping() {
    3633         $mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', '' );
    3634         $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
    3635     }
    3636 
    3637     /**
    3638      * Test that wp_default_image_output_mapping doesn't overwrite existing mappings.
    3639      *
    3640      * @ticket 55443
    3641      */
    3642     public function test_wp_default_image_output_mapping_existing() {
    3643         $mapping = array( 'mime/png' => 'mime/webp' );
    3644         $mapping = wp_default_image_output_mapping( $mapping, 'test.jpg', 'image/jpeg', '' );
    3645         $this->assertSame(
    3646             array(
    3647                 'mime/png'   => 'mime/webp',
    3648                 'image/jpeg' => 'image/webp',
    3649             ),
    3650             $mapping
    3651         );
    3652     }
    3653 
    3654     /**
    3655      * Test that the image editor default output for JPEGs is WebP.
    3656      *
    3657      * @ticket 55443
    3658      */
    3659     public function test_wp_image_editor_default_output_maps_to_webp() {
    3660         remove_filter( 'image_editor_output_format', '__return_empty_array' );
    3661 
    3662         $editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
    3663         $this->assertNotWPError( $editor );
    3664 
    3665         $resized = $editor->resize( 100, 100, false );
    3666         $this->assertNotWPError( $resized );
    3667 
    3668         $saved = $editor->save();
    3669         $this->assertNotWPError( $saved );
    3670 
    3671         if ( $editor->supports_mime_type( 'image/webp' ) ) {
    3672             $this->assertSame( 'image/webp', $saved['mime-type'] );
    3673             $this->assertSame( 'canola-100x75-jpg.webp', $saved['file'] );
    3674         } else {
    3675             $this->assertSame( 'image/jpeg', $saved['mime-type'] );
    3676             $this->assertSame( 'canola-100x75.jpg', $saved['file'] );
    3677         }
    3678     }
    3679 
    3680     /**
    3681      * @ticket 56526
    3682      * @dataProvider data_wp_default_image_output_mapping_size_filter
    3683      */
    3684     public function test_wp_default_image_output_mapping_size_filter( $size_name, $filter_callback, $expects_webp ) {
    3685         remove_all_filters( 'wp_image_sizes_with_additional_mime_type_support' );
    3686         if ( $filter_callback ) {
    3687             add_filter( 'wp_image_sizes_with_additional_mime_type_support', $filter_callback );
    3688         }
    3689 
    3690         $mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', $size_name );
    3691         if ( $expects_webp ) {
    3692             $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
    3693         } else {
    3694             $this->assertSame( array(), $mapping );
    3695         }
    3696     }
    3697 
    3698     public function data_wp_default_image_output_mapping_size_filter() {
    3699         return array(
    3700             'default size thumbnail'    => array(
    3701                 'thumbnail',
    3702                 null,
    3703                 true,
    3704             ),
    3705             'default size medium'       => array(
    3706                 'medium',
    3707                 null,
    3708                 true,
    3709             ),
    3710             'default size medium_large' => array(
    3711                 'medium_large',
    3712                 null,
    3713                 true,
    3714             ),
    3715             'default size large'        => array(
    3716                 'large',
    3717                 null,
    3718                 true,
    3719             ),
    3720             'default size unset'        => array(
    3721                 'medium',
    3722                 function( $enabled_sizes ) {
    3723                     unset( $enabled_sizes['medium'] );
    3724                     return $enabled_sizes;
    3725                 },
    3726                 false,
    3727             ),
    3728             'default size set to false' => array(
    3729                 'medium',
    3730                 function( $enabled_sizes ) {
    3731                     $enabled_sizes['medium'] = false;
    3732                     return $enabled_sizes;
    3733                 },
    3734                 false,
    3735             ),
    3736             'custom size'               => array(
    3737                 'custom',
    3738                 null,
    3739                 false,
    3740             ),
    3741             'custom size opted in'      => array(
    3742                 'custom',
    3743                 function( $enabled_sizes ) {
    3744                     $enabled_sizes['custom'] = true;
    3745                     return $enabled_sizes;
    3746                 },
    3747                 true,
    3748             ),
    3749         );
    3750     }
    37513624}
    37523625
  • trunk/tests/phpunit/tests/post/attachments.php

    r54086 r54226  
    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     }
    179
    1810    public function tear_down() {
    1911        // Remove all uploads.
    2012        $this->remove_added_uploads();
    21         remove_filter( 'image_editor_output_format', '__return_empty_array' );
    2213        parent::tear_down();
    2314    }
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r54090 r54226  
    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' );
    9796    }
    9897
     
    122121            WP_Image_Editor_Mock::$size_return = null;
    123122        }
    124 
    125         remove_filter( 'image_editor_output_format', '__return_empty_array' );
    126123
    127124        parent::tear_down();
Note: See TracChangeset for help on using the changeset viewer.