<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[54086] trunk: Media: Output WebP by default when uploading JPEGs.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/54086">54086</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/54086","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>adamsilverstein</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-09-06 21:13:17 +0000 (Tue, 06 Sep 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Media: Output WebP by default when uploading JPEGs.

Uploaded JPEGs will automatically be converted to WebP sub-sizes instead of JPEG, saving space and making sites faster.

The original JPEG upload is always retained and can be accessed by calling `wp_get_original_image_url`.

Props azaozz, flixos90.
Fixes <a href="https://core.trac.wordpress.org/ticket/55443">#55443</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesadminfiltersphp">trunk/src/wp-admin/includes/admin-filters.php</a></li>
<li><a href="#trunksrcwpadminincludesimageeditphp">trunk/src/wp-admin/includes/image-edit.php</a></li>
<li><a href="#trunksrcwpadminincludesmediaphp">trunk/src/wp-admin/includes/media.php</a></li>
<li><a href="#trunksrcwpincludesclasswpimageeditorphp">trunk/src/wp-includes/class-wp-image-editor.php</a></li>
<li><a href="#trunktestsphpunittestsimageeditorphp">trunk/tests/phpunit/tests/image/editor.php</a></li>
<li><a href="#trunktestsphpunittestsimageeditorGdphp">trunk/tests/phpunit/tests/image/editorGd.php</a></li>
<li><a href="#trunktestsphpunittestsimageeditorImagickphp">trunk/tests/phpunit/tests/image/editorImagick.php</a></li>
<li><a href="#trunktestsphpunittestsimagefunctionsphp">trunk/tests/phpunit/tests/image/functions.php</a></li>
<li><a href="#trunktestsphpunittestsimageintermediateSizephp">trunk/tests/phpunit/tests/image/intermediateSize.php</a></li>
<li><a href="#trunktestsphpunittestsimageresizephp">trunk/tests/phpunit/tests/image/resize.php</a></li>
<li><a href="#trunktestsphpunittestsmediaphp">trunk/tests/phpunit/tests/media.php</a></li>
<li><a href="#trunktestsphpunittestspostattachmentsphp">trunk/tests/phpunit/tests/post/attachments.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestattachmentscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesadminfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/admin-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/admin-filters.php     2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/src/wp-admin/includes/admin-filters.php       2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -37,6 +37,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'media_upload_tabs', 'update_gallery_tab' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'image_editor_output_format', 'wp_default_image_output_mapping' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Admin color schemes.
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'admin_init', 'register_admin_color_schemes', 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'admin_head', 'wp_color_scheme_settings' );
</span></span></pre></div>
<a id="trunksrcwpadminincludesimageeditphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/image-edit.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/image-edit.php        2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/src/wp-admin/includes/image-edit.php  2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -917,10 +917,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // Save the full-size file, also needed to create sub-sizes.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $saved = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
+       if ( ! $saved ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $return->error = esc_js( __( 'Unable to save the image.' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                return $return;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $new_path = $saved['path'];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $tag = false;
</span></span></pre></div>
<a id="trunksrcwpadminincludesmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/media.php     2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/src/wp-admin/includes/media.php       2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3843,3 +3843,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                exit;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * 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;
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesclasswpimageeditorphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-image-editor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-image-editor.php   2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/src/wp-includes/class-wp-image-editor.php     2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -424,19 +424,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return array( $filename, $new_ext, $mime_type );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * Builds an output filename based on current file, and adding proper suffix
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+        * Builds an output filename based on current file, and adding proper suffix.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @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
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $suffix
-        * @param string $dest_path
-        * @param string $extension
-        * @return string filename
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // $suffix will be appended to the destination filename, just before the extension.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $suffix ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( null === $suffix ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $suffix = $this->get_suffix();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -457,7 +464,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         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}";
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestsimageeditorphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/editor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/editor.php        2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/tests/phpunit/tests/image/editor.php  2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -18,6 +18,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                require_once DIR_TESTDATA . '/../includes/mock-image-editor.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'image_editor_output_format', '__return_empty_array' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // This needs to come after the mock image editor class is loaded.
</span><span class="cx" style="display: block; padding: 0 10px">                parent::set_up();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,6 +25,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Tear down the class.
+        */
+       public function tear_down() {
+               remove_filter( 'image_editor_output_format', '__return_empty_array' );
+               parent::tear_down();
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Test wp_get_image_editor() where load returns true
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 6821
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -226,10 +235,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ), trailingslashit( realpath( dirname( $editor->generate_filename( null, get_temp_dir() ) ) ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Test with a suffix only.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertSame( 'canola-100x50.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 'canola-100x50-jpg.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Combo!
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new-jpg.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Test with a stream destination.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 'file://testing/path/canola-100x50.jpg', $editor->generate_filename( null, 'file://testing/path' ) );
</span></span></pre></div>
<a id="trunktestsphpunittestsimageeditorGdphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/editorGd.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/editorGd.php      2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/tests/phpunit/tests/image/editorGd.php        2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,6 +17,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
</span><span class="cx" style="display: block; padding: 0 10px">                require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'image_editor_output_format', '__return_empty_array' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // This needs to come after the mock image editor class is loaded.
</span><span class="cx" style="display: block; padding: 0 10px">                parent::set_up();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -30,6 +32,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->remove_added_uploads();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'image_editor_output_format', '__return_empty_array' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 parent::tear_down();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsimageeditorImagickphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/editorImagick.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/editorImagick.php 2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/tests/phpunit/tests/image/editorImagick.php   2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -18,6 +18,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
</span><span class="cx" style="display: block; padding: 0 10px">                require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'image_editor_output_format', '__return_empty_array' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // This needs to come after the mock image editor class is loaded.
</span><span class="cx" style="display: block; padding: 0 10px">                parent::set_up();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -31,6 +33,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->remove_added_uploads();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'image_editor_output_format', '__return_empty_array' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 parent::tear_down();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsimagefunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/functions.php     2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/tests/phpunit/tests/image/functions.php       2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -25,9 +25,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( glob( $folder ) as $file ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        unlink( $file );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               add_filter( 'image_editor_output_format', '__return_empty_array' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Tear down the class.
+        */
+       public function tear_down() {
+               remove_filter( 'image_editor_output_format', '__return_empty_array' );
+               parent::tear_down();
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Gets the available image editor engine classes.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return string[] Available image editor classes; empty array when none are available.
</span></span></pre></div>
<a id="trunktestsphpunittestsimageintermediateSizephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/intermediateSize.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/intermediateSize.php      2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/tests/phpunit/tests/image/intermediateSize.php        2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5,6 +5,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @group upload
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Set up the test fixture.
+        */
+       public function set_up() {
+               add_filter( 'image_editor_output_format', '__return_empty_array' );
+
+               parent::set_up();
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function tear_down() {
</span><span class="cx" style="display: block; padding: 0 10px">                $this->remove_added_uploads();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,6 +21,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                remove_image_size( 'false-height' );
</span><span class="cx" style="display: block; padding: 0 10px">                remove_image_size( 'false-width' );
</span><span class="cx" style="display: block; padding: 0 10px">                remove_image_size( 'off-by-one' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               remove_filter( 'image_editor_output_format', '__return_empty_array' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 parent::tear_down();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsimageresizephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/resize.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/resize.php        2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/tests/phpunit/tests/image/resize.php  2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,8 +14,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                parent::set_up();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'wp_image_editors', array( $this, 'wp_image_editors' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'image_editor_output_format', '__return_empty_array' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Tear down the class.
+        */
+       public function tear_down() {
+               remove_filter( 'image_editor_output_format', '__return_empty_array' );
+               parent::tear_down();
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function wp_image_editors() {
</span><span class="cx" style="display: block; padding: 0 10px">                return array( $this->editor_engine );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunktestsphpunittestsmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media.php       2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/tests/phpunit/tests/media.php 2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -33,7 +33,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                self::$_sizes                          = wp_get_additional_image_sizes();
</span><span class="cx" style="display: block; padding: 0 10px">                $GLOBALS['_wp_additional_image_sizes'] = array();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $filename       = DIR_TESTDATA . '/images/' . self::$large_filename;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $filename = DIR_TESTDATA . '/images/' . self::$large_filename;
+               add_filter( 'image_editor_output_format', '__return_empty_array' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 self::$large_id = $factory->attachment->create_upload_object( $filename );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -68,6 +69,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public static function wpTearDownAfterClass() {
</span><span class="cx" style="display: block; padding: 0 10px">                $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'image_editor_output_format', '__return_empty_array' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public static function tear_down_after_class() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3617,6 +3619,59 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Clean up the above filter.
</span><span class="cx" style="display: block; padding: 0 10px">                remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * 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() );
+               $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 );
+               $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'] );
+               }
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestspostattachmentsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/post/attachments.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/attachments.php    2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/tests/phpunit/tests/post/attachments.php      2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,10 +6,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @group upload
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Post_Attachments extends WP_UnitTestCase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Set up the test fixture.
+        */
+       public function set_up() {
+               add_filter( 'image_editor_output_format', '__return_empty_array' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                parent::set_up();
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function tear_down() {
</span><span class="cx" style="display: block; padding: 0 10px">                // Remove all uploads.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->remove_added_uploads();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'image_editor_output_format', '__return_empty_array' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 parent::tear_down();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestattachmentscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php        2022-09-06 21:11:41 UTC (rev 54085)
+++ trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php  2022-09-06 21:13:17 UTC (rev 54086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -93,6 +93,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'image_editor_output_format', '__return_empty_array' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function wpSetUpBeforeRequest( $result ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -121,6 +122,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        WP_Image_Editor_Mock::$size_return = null;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'image_editor_output_format', '__return_empty_array' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 parent::tear_down();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre>
</div>
</div>

</body>
</html>