diff --git a/wp-admin/includes/admin-filters.php b/wp-admin/includes/admin-filters.php index e80c111dde..f5f9600332 100644 --- a/wp-admin/includes/admin-filters.php +++ b/wp-admin/includes/admin-filters.php @@ -37,6 +37,8 @@ add_filter( 'media_upload_library', 'media_upload_library' ); add_filter( 'media_upload_tabs', 'update_gallery_tab' ); +add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' ); + // Admin color schemes. add_action( 'admin_init', 'register_admin_color_schemes', 1 ); add_action( 'admin_head', 'wp_color_scheme_settings' ); diff --git a/wp-admin/includes/image-edit.php b/wp-admin/includes/image-edit.php index e814fc47e7..9c71e68794 100644 --- a/wp-admin/includes/image-edit.php +++ b/wp-admin/includes/image-edit.php @@ -917,10 +917,12 @@ function wp_save_image( $post_id ) { } // Save the full-size file, also needed to create sub-sizes. - if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) { + $saved = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ); + if ( ! $saved ) { $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; diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 51f21ddb7d..3e2093ac2b 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -3843,3 +3843,18 @@ function wp_media_attach_action( $parent_id, $action = 'attach' ) { exit; } } + +/** + * 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. + * @retun array The adjusted default output mapping. + */ +function wp_default_image_output_mapping( $output_mapping ) { + $output_mapping['image/jpeg'] = 'image/webp'; + return $output_mapping; +} diff --git a/wp-includes/class-wp-image-editor.php b/wp-includes/class-wp-image-editor.php index caa3092d36..15e4aa8d6e 100644 --- a/wp-includes/class-wp-image-editor.php +++ b/wp-includes/class-wp-image-editor.php @@ -424,19 +424,26 @@ 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 - * @param string $dest_path - * @param string $extension - * @return string filename + * @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. */ public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) { // $suffix will be appended to the destination filename, just before the extension. - if ( ! $suffix ) { + if ( null === $suffix ) { $suffix = $this->get_suffix(); } @@ -457,7 +464,21 @@ abstract class WP_Image_Editor { } } - return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}"; + 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}"; } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index a839b7c54b..00fe2ce1c9 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-54085'; +$wp_version = '6.1-alpha-54086'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.