Make WordPress Core


Ignore:
Timestamp:
09/07/2022 09:43:28 PM (18 months ago)
Author:
flixos90
Message:

Media: Generate WebP only for certain registered image sizes.

The existing filter image_editor_output_format receives an additional parameter $size_name which is populated whenever it controls the output format for a specific registered image size to create. Otherwise, it remains empty. In order to achieve this, a low level change has been added in bringing a new $size_name class property to the WP_Image_Editor base class, which is introduced in a backward compatible way that will not cause conflicts with custom implementations.

This parameter is then used in new logic inside the wp_default_image_output_mapping() callback function for the filter, controlling whether image/jpeg should map to image/webp output or not. By default, this is enabled for all WordPress core image sizes by default, and this list can be modified using a new wp_image_sizes_with_additional_mime_type_support filter, e.g. to remove core sizes or add custom sizes.

The customization per image size may be further enhanced by providing a more declarative API via a new parameter on the add_image_size() function.

Props eugenemanuilov, flixos90, adamsilverstein, joegrainger.

Fixes #56526.
See #55443, #56288.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r54086 r54097  
    36273627     */
    36283628    public function test_wp_default_image_output_mapping() {
    3629         $mapping = wp_default_image_output_mapping( array() );
     3629        $mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', '' );
    36303630        $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
    36313631    }
     
    36383638    public function test_wp_default_image_output_mapping_existing() {
    36393639        $mapping = array( 'mime/png' => 'mime/webp' );
    3640         $mapping = wp_default_image_output_mapping( $mapping );
     3640        $mapping = wp_default_image_output_mapping( $mapping, 'test.jpg', 'image/jpeg', '' );
    36413641        $this->assertSame(
    36423642            array(
     
    36733673        }
    36743674    }
     3675
     3676    /**
     3677     * @ticket 56526
     3678     * @dataProvider data_wp_default_image_output_mapping_size_filter
     3679     */
     3680    public function test_wp_default_image_output_mapping_size_filter( $size_name, $filter_callback, $expects_webp ) {
     3681        remove_all_filters( 'wp_image_sizes_with_additional_mime_type_support' );
     3682        if ( $filter_callback ) {
     3683            add_filter( 'wp_image_sizes_with_additional_mime_type_support', $filter_callback );
     3684        }
     3685
     3686        $mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', $size_name );
     3687        if ( $expects_webp ) {
     3688            $this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
     3689        } else {
     3690            $this->assertSame( array(), $mapping );
     3691        }
     3692    }
     3693
     3694    public function data_wp_default_image_output_mapping_size_filter() {
     3695        return array(
     3696            'default size thumbnail'    => array(
     3697                'thumbnail',
     3698                null,
     3699                true,
     3700            ),
     3701            'default size medium'       => array(
     3702                'medium',
     3703                null,
     3704                true,
     3705            ),
     3706            'default size medium_large' => array(
     3707                'medium_large',
     3708                null,
     3709                true,
     3710            ),
     3711            'default size large'        => array(
     3712                'large',
     3713                null,
     3714                true,
     3715            ),
     3716            'default size unset'        => array(
     3717                'medium',
     3718                function( $enabled_sizes ) {
     3719                    unset( $enabled_sizes['medium'] );
     3720                    return $enabled_sizes;
     3721                },
     3722                false,
     3723            ),
     3724            'default size set to false' => array(
     3725                'medium',
     3726                function( $enabled_sizes ) {
     3727                    $enabled_sizes['medium'] = false;
     3728                    return $enabled_sizes;
     3729                },
     3730                false,
     3731            ),
     3732            'custom size'               => array(
     3733                'custom',
     3734                null,
     3735                false,
     3736            ),
     3737            'custom size opted in'      => array(
     3738                'custom',
     3739                function( $enabled_sizes ) {
     3740                    $enabled_sizes['custom'] = true;
     3741                    return $enabled_sizes;
     3742                },
     3743                true,
     3744            ),
     3745        );
     3746    }
    36753747}
    36763748
Note: See TracChangeset for help on using the changeset viewer.