Media: Revert WebP generation.

Given [https://make.wordpress.org/core/2022/09/11/webp-in-core-for-6-1/ Matt's recent post about removing WebP from core] and possibly implementing the feature in a future [https://make.wordpress.org/core/2022/09/11/canonical-plugins-revisited/ "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.

git-svn-id: https://develop.svn.wordpress.org/trunk@54226 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
David Baumwald 2022-09-19 22:51:53 +00:00
parent cc3cb33ef5
commit 3b3d7ebb71
17 changed files with 33 additions and 340 deletions

View File

@ -917,12 +917,10 @@ function wp_save_image( $post_id ) {
}
// Save the full-size file, also needed to create sub-sizes.
$saved = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
if ( ! $saved ) {
if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
$return->error = esc_js( __( 'Unable to save the image.' ) );
return $return;
}
$new_path = $saved['path'];
if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
$tag = false;

View File

@ -451,9 +451,6 @@ function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
if ( method_exists( $editor, 'make_subsize' ) ) {
foreach ( $new_sizes as $new_size_name => $new_size_data ) {
// Include size name in the data.
$new_size_data['name'] = $new_size_name;
$new_size_meta = $editor->make_subsize( $new_size_data );
if ( is_wp_error( $new_size_meta ) ) {

View File

@ -227,7 +227,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
* @since 3.5.0
*
* @param array $sizes {
* Associative array of image size names and their data.
* An array of image size data arrays.
*
* Either a height or width must be provided.
* If one of the two is set to null, the resize will
@ -247,9 +247,6 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
$metadata = array();
foreach ( $sizes as $size => $size_data ) {
// Include size name in the data.
$size_data['name'] = $size;
$meta = $this->make_subsize( $size_data );
if ( ! is_wp_error( $meta ) ) {
@ -264,15 +261,13 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
* Create an image sub-size and return the image meta data value for it.
*
* @since 5.3.0
* @since 6.1.0 The $sizes parameter may now include a $name key for each entry.
*
* @param array $size_data {
* Array of size data.
*
* @type int $width The maximum width in pixels.
* @type int $height The maximum height in pixels.
* @type bool $crop Whether to crop the image to exact dimensions.
* @type string $name Image size name.
* @type int $width The maximum width in pixels.
* @type int $height The maximum height in pixels.
* @type bool $crop Whether to crop the image to exact dimensions.
* }
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
* WP_Error object on error.
@ -282,8 +277,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
}
$orig_size = $this->size;
$orig_size_name = $this->size_name;
$orig_size = $this->size;
if ( ! isset( $size_data['width'] ) ) {
$size_data['width'] = null;
@ -297,10 +291,6 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
$size_data['crop'] = false;
}
if ( isset( $size_data['name'] ) ) {
$this->update_size_name( $size_data['name'] );
}
$resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
if ( is_wp_error( $resized ) ) {
@ -310,8 +300,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
imagedestroy( $resized );
}
$this->size = $orig_size;
$this->size_name = $orig_size_name;
$this->size = $orig_size;
if ( ! is_wp_error( $saved ) ) {
unset( $saved['path'] );

View File

@ -438,7 +438,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
* @since 3.5.0
*
* @param array $sizes {
* Associative array of image size names and their data.
* An array of image size data arrays.
*
* Either a height or width must be provided.
* If one of the two is set to null, the resize will
@ -458,9 +458,6 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
$metadata = array();
foreach ( $sizes as $size => $size_data ) {
// Include size name in the data.
$size_data['name'] = $size;
$meta = $this->make_subsize( $size_data );
if ( ! is_wp_error( $meta ) ) {
@ -475,15 +472,13 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
* Create an image sub-size and return the image meta data value for it.
*
* @since 5.3.0
* @since 6.1.0 The $sizes parameter may now include a $name key for each entry.
*
* @param array $size_data {
* Array of size data.
*
* @type int $width The maximum width in pixels.
* @type int $height The maximum height in pixels.
* @type bool $crop Whether to crop the image to exact dimensions.
* @type string $name Image size name.
* @type int $width The maximum width in pixels.
* @type int $height The maximum height in pixels.
* @type bool $crop Whether to crop the image to exact dimensions.
* }
* @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
* WP_Error object on error.
@ -493,9 +488,8 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
}
$orig_size = $this->size;
$orig_size_name = $this->size_name;
$orig_image = $this->image->getImage();
$orig_size = $this->size;
$orig_image = $this->image->getImage();
if ( ! isset( $size_data['width'] ) ) {
$size_data['width'] = null;
@ -509,10 +503,6 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
$size_data['crop'] = false;
}
if ( isset( $size_data['name'] ) ) {
$this->update_size_name( $size_data['name'] );
}
$resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
if ( is_wp_error( $resized ) ) {
@ -525,9 +515,8 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
$this->image = null;
}
$this->size = $orig_size;
$this->size_name = $orig_size_name;
$this->image = $orig_image;
$this->size = $orig_size;
$this->image = $orig_image;
if ( ! is_wp_error( $saved ) ) {
unset( $saved['path'] );

View File

@ -15,7 +15,6 @@
abstract class WP_Image_Editor {
protected $file = null;
protected $size = null;
protected $size_name = '';
protected $mime_type = null;
protected $output_mime_type = null;
protected $default_mime_type = 'image/jpeg';
@ -119,7 +118,7 @@ abstract class WP_Image_Editor {
* @abstract
*
* @param array $sizes {
* Associative array of image size names and their data. Default sizes are 'small', 'medium', 'large'.
* An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
*
* @type array ...$0 {
* @type int $width Image width.
@ -187,7 +186,7 @@ abstract class WP_Image_Editor {
*
* @since 3.5.0
*
* @return array {
* @return int[] {
* Dimensions of the image.
*
* @type int $width The image width.
@ -203,9 +202,9 @@ abstract class WP_Image_Editor {
*
* @since 3.5.0
*
* @param int $width The image width.
* @param int $height The image height.
* @return true True on success, false on failure.
* @param int $width
* @param int $height
* @return true
*/
protected function update_size( $width = null, $height = null ) {
$this->size = array(
@ -215,28 +214,6 @@ abstract class WP_Image_Editor {
return true;
}
/**
* Gets the current image size name.
*
* @since 6.1.0
*
* @return string Image size name, or empty string if none set.
*/
public function get_size_name() {
return $this->size_name;
}
/**
* Sets the current image size name.
*
* @since 6.1.0
*
* @param string $size_name The image size name.
*/
protected function update_size_name( $size_name ) {
$this->size_name = (string) $size_name;
}
/**
* Gets the Image Compression quality on a 1-100% scale.
*
@ -388,7 +365,6 @@ abstract class WP_Image_Editor {
* @see WP_Image_Editor::get_output_format()
*
* @since 5.8.0
* @since 6.1.0 The $size_name parameter was added.
*
* @param string[] $output_format {
* An array of mime type mappings. Maps a source mime type to a new
@ -398,9 +374,8 @@ abstract class WP_Image_Editor {
* }
* @param string $filename Path to the image.
* @param string $mime_type The source image mime type.
* @param string $size_name The image size name to create, or empty string if not set.
*/
$output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type, $this->size_name );
$output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type );
if ( isset( $output_format[ $mime_type ] )
&& $this->supports_mime_type( $output_format[ $mime_type ] )
@ -450,26 +425,19 @@ abstract class WP_Image_Editor {
return array( $filename, $new_ext, $mime_type );
}
/**
* Builds an output filename based on current file, and adding proper suffix.
/**
* Builds an output filename based on current file, and adding proper suffix
*
* @since 3.5.0
* @since 6.1.0 Skips adding a suffix when set to an empty string. When the
* file extension being generated doesn't match the image file extension,
* add the extension to the suffix
*
* @param string $suffix Optional. Suffix to add to the filename. The default null
* will result in a 'widthxheight' suffix. Passing
* an empty string will result in no suffix.
* @param string $dest_path Optional. The path to save the file to. The default null
* will use the image file path.
* @param string $extension Optional. The file extension to use. The default null
* will use the image file extension.
* @return string filename The generated file name.
* @param string $suffix
* @param string $dest_path
* @param string $extension
* @return string filename
*/
public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
// $suffix will be appended to the destination filename, just before the extension.
if ( null === $suffix ) {
if ( ! $suffix ) {
$suffix = $this->get_suffix();
}
@ -490,21 +458,7 @@ abstract class WP_Image_Editor {
}
}
if ( empty( $suffix ) ) {
$suffix = '';
} else {
$suffix = "-{$suffix}";
}
// When the file extension being generated doesn't match the image file extension,
// add the extension to the suffix to ensure a unique file name. Prevents
// name conflicts when a single image type can have multiple extensions,
// eg. .jpg, .jpeg and .jpe are all valid JPEG extensions.
if ( ! empty( $extension ) && $extension !== $ext ) {
$suffix .= "-{$ext}";
}
return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
}
/**

View File

@ -636,8 +636,6 @@ add_action( 'media_buttons', 'media_buttons' );
add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
add_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping', 10, 4 );
// Embeds.
add_action( 'rest_api_init', 'wp_oembed_register_route' );
add_filter( 'rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4 );

View File

@ -2690,7 +2690,7 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = null )
*/
if ( $is_image ) {
/** This filter is documented in wp-includes/class-wp-image-editor.php */
$output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type, '' );
$output_formats = apply_filters( 'image_editor_output_format', array(), $_dir . $filename, $mime_type );
$alt_types = array();
if ( ! empty( $output_formats[ $mime_type ] ) ) {

View File

@ -3926,51 +3926,6 @@ function _wp_image_editor_choose( $args = array() ) {
return false;
}
/**
* Filters the default image output mapping.
*
* With this filter callback, WebP image files will be generated for certain JPEG source files.
*
* @since 6.1.0
*
* @param array $output_mapping Map of mime type to output format.
* @param string $filename Path to the image.
* @param string $mime_type The source image mime type.
* @param string $size_name Optional. The image size name to create, or empty string if not set. Default empty string.
* @return array The adjusted default output mapping.
*/
function wp_default_image_output_mapping( $output_mapping, $filename, $mime_type, $size_name = '' ) {
// If size name is specified, check whether the size supports additional MIME types like WebP.
if ( $size_name ) {
// Include only the core sizes that do not rely on add_image_size(). Additional image sizes are opt-in.
$enabled_sizes = array(
'thumbnail' => true,
'medium' => true,
'medium_large' => true,
'large' => true,
'post-thumbnail' => true,
);
/**
* Filters the sizes that support secondary mime type output. Developers can use this
* to control the generation of additional mime type sub-sized images.
*
* @since 6.1.0
*
* @param array $enabled_sizes Map of size names and whether they support secondary mime type output.
*/
$enabled_sizes = apply_filters( 'wp_image_sizes_with_additional_mime_type_support', $enabled_sizes );
// Bail early if the size does not support additional MIME types.
if ( empty( $enabled_sizes[ $size_name ] ) ) {
return $output_mapping;
}
}
$output_mapping['image/jpeg'] = 'image/webp';
return $output_mapping;
}
/**
* Prints default Plupload arguments.
*

View File

@ -18,20 +18,11 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase {
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once DIR_TESTDATA . '/../includes/mock-image-editor.php';
add_filter( 'image_editor_output_format', '__return_empty_array' );
// This needs to come after the mock image editor class is loaded.
parent::set_up();
}
/**
* Tear down the class.
*/
public function tear_down() {
remove_filter( 'image_editor_output_format', '__return_empty_array' );
parent::tear_down();
}
/**
* Test wp_get_image_editor() where load returns true
*
@ -235,10 +226,10 @@ class Tests_Image_Editor extends WP_Image_UnitTestCase {
$this->assertSame( trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor->generate_filename( null, get_temp_dir() ) ) ) ) );
// Test with a suffix only.
$this->assertSame( 'canola-100x50-jpg.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
$this->assertSame( 'canola-100x50.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
// Combo!
$this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new-jpg.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
$this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
// Test with a stream destination.
$this->assertSame( 'file://testing/path/canola-100x50.jpg', $editor->generate_filename( null, 'file://testing/path' ) );

View File

@ -17,8 +17,6 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
add_filter( 'image_editor_output_format', '__return_empty_array' );
// This needs to come after the mock image editor class is loaded.
parent::set_up();
}
@ -32,8 +30,6 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
$this->remove_added_uploads();
remove_filter( 'image_editor_output_format', '__return_empty_array' );
parent::tear_down();
}

View File

@ -18,8 +18,6 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';
add_filter( 'image_editor_output_format', '__return_empty_array' );
// This needs to come after the mock image editor class is loaded.
parent::set_up();
}
@ -33,8 +31,6 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
$this->remove_added_uploads();
remove_filter( 'image_editor_output_format', '__return_empty_array' );
parent::tear_down();
}

View File

@ -25,16 +25,6 @@ class Tests_Image_Functions extends WP_UnitTestCase {
foreach ( glob( $folder ) as $file ) {
unlink( $file );
}
add_filter( 'image_editor_output_format', '__return_empty_array' );
}
/**
* Tear down the class.
*/
public function tear_down() {
remove_filter( 'image_editor_output_format', '__return_empty_array' );
parent::tear_down();
}
/**

View File

@ -5,15 +5,6 @@
* @group upload
*/
class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
/**
* Set up the test fixture.
*/
public function set_up() {
add_filter( 'image_editor_output_format', '__return_empty_array' );
parent::set_up();
}
public function tear_down() {
$this->remove_added_uploads();
@ -21,9 +12,6 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
remove_image_size( 'false-height' );
remove_image_size( 'false-width' );
remove_image_size( 'off-by-one' );
remove_filter( 'image_editor_output_format', '__return_empty_array' );
parent::tear_down();
}

View File

@ -14,15 +14,6 @@ abstract class WP_Tests_Image_Resize_UnitTestCase extends WP_Image_UnitTestCase
parent::set_up();
add_filter( 'wp_image_editors', array( $this, 'wp_image_editors' ) );
add_filter( 'image_editor_output_format', '__return_empty_array' );
}
/**
* Tear down the class.
*/
public function tear_down() {
remove_filter( 'image_editor_output_format', '__return_empty_array' );
parent::tear_down();
}
public function wp_image_editors() {

View File

@ -33,8 +33,7 @@ CAP;
self::$_sizes = wp_get_additional_image_sizes();
$GLOBALS['_wp_additional_image_sizes'] = array();
$filename = DIR_TESTDATA . '/images/' . self::$large_filename;
add_filter( 'image_editor_output_format', '__return_empty_array' );
$filename = DIR_TESTDATA . '/images/' . self::$large_filename;
self::$large_id = $factory->attachment->create_upload_object( $filename );
$post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' );
@ -69,7 +68,6 @@ CAP;
public static function wpTearDownAfterClass() {
$GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
remove_filter( 'image_editor_output_format', '__return_empty_array' );
}
public static function tear_down_after_class() {
@ -3623,131 +3621,6 @@ EOF;
// Clean up the above filter.
remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 );
}
/**
* Test the wp_default_image_output_mapping function.
*
* @ticket 55443
*/
public function test_wp_default_image_output_mapping() {
$mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', '' );
$this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
}
/**
* Test that wp_default_image_output_mapping doesn't overwrite existing mappings.
*
* @ticket 55443
*/
public function test_wp_default_image_output_mapping_existing() {
$mapping = array( 'mime/png' => 'mime/webp' );
$mapping = wp_default_image_output_mapping( $mapping, 'test.jpg', 'image/jpeg', '' );
$this->assertSame(
array(
'mime/png' => 'mime/webp',
'image/jpeg' => 'image/webp',
),
$mapping
);
}
/**
* Test that the image editor default output for JPEGs is WebP.
*
* @ticket 55443
*/
public function test_wp_image_editor_default_output_maps_to_webp() {
remove_filter( 'image_editor_output_format', '__return_empty_array' );
$editor = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
$this->assertNotWPError( $editor );
$resized = $editor->resize( 100, 100, false );
$this->assertNotWPError( $resized );
$saved = $editor->save();
$this->assertNotWPError( $saved );
if ( $editor->supports_mime_type( 'image/webp' ) ) {
$this->assertSame( 'image/webp', $saved['mime-type'] );
$this->assertSame( 'canola-100x75-jpg.webp', $saved['file'] );
} else {
$this->assertSame( 'image/jpeg', $saved['mime-type'] );
$this->assertSame( 'canola-100x75.jpg', $saved['file'] );
}
}
/**
* @ticket 56526
* @dataProvider data_wp_default_image_output_mapping_size_filter
*/
public function test_wp_default_image_output_mapping_size_filter( $size_name, $filter_callback, $expects_webp ) {
remove_all_filters( 'wp_image_sizes_with_additional_mime_type_support' );
if ( $filter_callback ) {
add_filter( 'wp_image_sizes_with_additional_mime_type_support', $filter_callback );
}
$mapping = wp_default_image_output_mapping( array(), 'test.jpg', 'image/jpeg', $size_name );
if ( $expects_webp ) {
$this->assertSame( array( 'image/jpeg' => 'image/webp' ), $mapping );
} else {
$this->assertSame( array(), $mapping );
}
}
public function data_wp_default_image_output_mapping_size_filter() {
return array(
'default size thumbnail' => array(
'thumbnail',
null,
true,
),
'default size medium' => array(
'medium',
null,
true,
),
'default size medium_large' => array(
'medium_large',
null,
true,
),
'default size large' => array(
'large',
null,
true,
),
'default size unset' => array(
'medium',
function( $enabled_sizes ) {
unset( $enabled_sizes['medium'] );
return $enabled_sizes;
},
false,
),
'default size set to false' => array(
'medium',
function( $enabled_sizes ) {
$enabled_sizes['medium'] = false;
return $enabled_sizes;
},
false,
),
'custom size' => array(
'custom',
null,
false,
),
'custom size opted in' => array(
'custom',
function( $enabled_sizes ) {
$enabled_sizes['custom'] = true;
return $enabled_sizes;
},
true,
),
);
}
}
/**

View File

@ -6,19 +6,10 @@
* @group upload
*/
class Tests_Post_Attachments extends WP_UnitTestCase {
/**
* Set up the test fixture.
*/
public function set_up() {
add_filter( 'image_editor_output_format', '__return_empty_array' );
parent::set_up();
}
public function tear_down() {
// Remove all uploads.
$this->remove_added_uploads();
remove_filter( 'image_editor_output_format', '__return_empty_array' );
parent::tear_down();
}

View File

@ -93,7 +93,6 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
add_filter( 'image_editor_output_format', '__return_empty_array' );
}
public function wpSetUpBeforeRequest( $result ) {
@ -122,8 +121,6 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
WP_Image_Editor_Mock::$size_return = null;
}
remove_filter( 'image_editor_output_format', '__return_empty_array' );
parent::tear_down();
}