diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index fed0fc16e8ef1..f48eab8fbbe87 100644 --- a/src/wp-includes/class-wp-image-editor.php +++ b/src/wp-includes/class-wp-image-editor.php @@ -356,26 +356,7 @@ protected function get_output_format( $filename = null, $mime_type = null ) { $new_ext = $file_ext; } - /** - * Filters the image editor output format mapping. - * - * Enables filtering the mime type used to save images. By default, - * the mapping array is empty, so the mime type matches the source image. - * - * @see WP_Image_Editor::get_output_format() - * - * @since 5.8.0 - * - * @param string[] $output_format { - * An array of mime type mappings. Maps a source mime type to a new - * destination mime type. Default empty array. - * - * @type string ...$0 The new mime type. - * } - * @param string $filename Path to the image. - * @param string $mime_type The source image mime type. - */ - $output_format = apply_filters( 'image_editor_output_format', array(), $filename, $mime_type ); + get_default_image_editor_output_format( $filename, $mime_type ); if ( isset( $output_format[ $mime_type ] ) && $this->supports_mime_type( $output_format[ $mime_type ] ) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 10ad8e8b5e589..f4b15286a08e8 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -2692,7 +2692,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 = get_default_image_editor_output_format( $_dir . $filename, $mime_type ); $alt_types = array(); if ( ! empty( $output_formats[ $mime_type ] ) ) { @@ -2760,6 +2760,47 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) } } + /** + * Get the default output format. Return is filtered by `image_editor_output_format`. + * + * @since 6.4.0 + * + * @param string $filename Path to the image. + * @param string $mime_type The source image mime type. + * + * @return string[] $output_format { + * An array of mime type mappings. Maps a source mime type to a new + * destination mime type. Default maps jpeg to webp. + * + * @type string ...$0 The new mime type. + * } + */ + function get_default_image_editor_output_format( $filename, $mime_type ) { + $default = array ( + 'image/jpeg' => 'image/webp', + ); + /** + * Filters the image editor output format mapping. + * + * Enables filtering the mime type used to save images. By default, + * the mapping array is empty, so the mime type matches the source image. + * + * @see WP_Image_Editor::get_output_format() + * + * @since 5.8.0 + * + * @param string[] $output_format { + * An array of mime type mappings. Maps a source mime type to a new + * destination mime type. Default maps jpeg to webp. + * + * @type string ...$0 The new mime type. + * } + * @param string $filename Path to the image. + * @param string $mime_type The source image mime type. + */ + return apply_filters( 'image_editor_output_format', $default(), $filename, $mime_type ); + } + /** * Filters the result when generating a unique file name. * diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index b74933881f909..ee995b8e65781 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3919,8 +3919,7 @@ function wp_get_image_editor( $path, $args = array() ) { // Check and set the output mime type mapped to the input type. if ( isset( $args['mime_type'] ) ) { - /** This filter is documented in wp-includes/class-wp-image-editor.php */ - $output_format = apply_filters( 'image_editor_output_format', array(), $path, $args['mime_type'] ); + $output_format = get_default_image_editor_output_format( $path, $args['mime_type'] ); if ( isset( $output_format[ $args['mime_type'] ] ) ) { $args['output_mime_type'] = $output_format[ $args['mime_type'] ]; }