<!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>[54417] trunk: Media: ensure the `wp_editor_set_quality` filter consistently passes the correct output mime type.</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/54417">54417</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/54417","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-10-07 19:15:59 +0000 (Fri, 07 Oct 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: ensure the `wp_editor_set_quality` filter consistently passes the correct output mime type.

Ensure that the mime type passed to the `wp_editor_set_quality` filter is correct when the output format is altered with the `image_editor_output_format` filter and the image is saved multiple times, for example when generating sub sizes. Previously, the original image mime type was passed instead of the output type after the initial save.

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

<h3>Modified Paths</h3>
<ul>
<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="#trunktestsphpunittestsmediaphp">trunk/tests/phpunit/tests/media.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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-10-07 19:01:32 UTC (rev 54416)
+++ trunk/src/wp-includes/class-wp-image-editor.php     2022-10-07 19:15:59 UTC (rev 54417)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -414,8 +414,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        // The image will be converted when saving. Set the quality for the new mime-type if not already set.
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( $mime_type !== $this->output_mime_type ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->output_mime_type = $mime_type;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $this->set_quality();
</del><span class="cx" style="display: block; padding: 0 10px">                         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $this->set_quality();
</ins><span class="cx" style="display: block; padding: 0 10px">                 } elseif ( ! empty( $this->output_mime_type ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        // Reset output_mime_type and quality.
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->output_mime_type = null;
</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-10-07 19:01:32 UTC (rev 54416)
+++ trunk/tests/phpunit/tests/image/editor.php  2022-10-07 19:15:59 UTC (rev 54417)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -105,9 +105,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Ensure wp_editor_set_quality filter applies if it exists before editor instantiation.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 100, $editor->get_quality() );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               // Clean up.
-               remove_filter( 'wp_editor_set_quality', $func_100_percent );
</del><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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -119,6 +116,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $editor = wp_get_image_editor( DIR_TESTDATA . '/images/test-image.png' );
</span><span class="cx" style="display: block; padding: 0 10px">                $editor->set_mime_type( 'image/png' ); // Ensure mime-specific filters act properly.
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Quality setting for the source image. For PNG the fallback default of 82 is used.
+               $this->assertSame( 82, $editor->get_quality(), 'Default quality setting is 82.' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Set conversions for uploaded images.
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -125,12 +125,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Quality setting for the source image. For PNG the fallback default of 82 is used.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 82, $editor->get_quality(), 'Default quality setting is 82.' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Quality should change to the output format's value.
-               // A PNG image will be converted to WEBP whose quialty should be 86.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // When saving, quality should change to the output format's value.
+               // A PNG image will be converted to WebP whose quality should be 86.
</ins><span class="cx" style="display: block; padding: 0 10px">                 $editor->save();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertSame( 86, $editor->get_quality(), 'Output image format is WEBP. Quality setting for it should be 86.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 86, $editor->get_quality(), 'Output image format is WebP. Quality setting for it should be 86.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Removing PNG to WEBP conversion on save. Quality setting should reset to the default.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Saving again should not change the quality.
+               $editor->save();
+               $this->assertSame( 86, $editor->get_quality(), 'Output image format is WebP. Quality setting for it should be 86.' );
+
+               // Removing PNG to WebP conversion on save. Quality setting should reset to the default.
</ins><span class="cx" style="display: block; padding: 0 10px">                 remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $editor->save();
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 82, $editor->get_quality(), 'After removing image conversion quality setting should reset to the default of 82.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -149,9 +153,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 56, $editor->get_quality(), 'Filtered default quality for JPEG is 56.' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Quality should change to the output format's value as filtered above.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // A JPEG image will be converted to WEBP whose quialty should be 42.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // A JPEG image will be converted to WebP whose quialty should be 42.
</ins><span class="cx" style="display: block; padding: 0 10px">                 $editor->save();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertSame( 42, $editor->get_quality(), 'Image conversion from JPEG to WEBP. Filtered WEBP quality shoild be 42.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 42, $editor->get_quality(), 'Image conversion from JPEG to WEBP. Filtered WEBP quality should be 42.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // After removing the conversion the quality setting should reset to the filtered value for the original image type, JPEG.
</span><span class="cx" style="display: block; padding: 0 10px">                remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -161,8 +165,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $editor->get_quality(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'After removing image conversion the quality setting should reset to the filtered value for JPEG, 56.'
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               remove_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality' ) );
</del><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="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-10-07 19:01:32 UTC (rev 54416)
+++ trunk/tests/phpunit/tests/media.php 2022-10-07 19:15:59 UTC (rev 54417)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3621,6 +3621,97 @@
</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 that generated files with the `image_editor_output_format` applied use the correct
+        * quality level based on their mime type.
+        *
+        * @ticket 56442
+        */
+       public function test_quality_with_image_conversion_file_sizes() {
+               add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) );
+               $temp_dir = get_temp_dir();
+               $file     = $temp_dir . '/33772.jpg';
+               copy( DIR_TESTDATA . '/images/33772.jpg', $file );
+
+               // Set JPEG output quality very low and WebP quality very high, this should force all generated WebP images to
+               // be larger than the the matching generated JPEGs.
+               add_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low_jpeg' ), 10, 2 );
+
+               $editor = wp_get_image_editor( $file );
+
+               // Verify that the selected editor supports WebP output.
+               if ( ! $editor->supports_mime_type( 'image/webp' ) ) {
+                       $this->markTestSkipped( 'WebP is not supported by the selected image editor.' );
+               }
+
+               $attachment_id = self::factory()->attachment->create_object(
+                       array(
+                               'post_mime_type' => 'image/jpeg',
+                               'file'           => $file,
+                       )
+               );
+
+               add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
+
+               // Generate all sizes as JPEGs.
+               $jpeg_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
+               remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) );
+
+               // Generate all sizes as WebP.
+               add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
+               $webp_sizes = wp_generate_attachment_metadata( $attachment_id, $file );
+               remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
+
+               // The main (scaled) image: the JPEG should be smaller than the WebP.
+               $this->assertLessThan( $webp_sizes['filesize'], $jpeg_sizes['filesize'], 'The JPEG should be smaller than the WebP.' );
+
+               // Sub-sizes: for each size, the JPEGs should be smaller than the WebP.
+               $sizes_to_compare = array_intersect_key( $jpeg_sizes['sizes'], $webp_sizes['sizes'] );
+               foreach ( $sizes_to_compare as $size => $size_data ) {
+                       $this->assertLessThan( $webp_sizes['sizes'][ $size ]['filesize'], $jpeg_sizes['sizes'][ $size ]['filesize'] );
+               }
+       }
+
+       /**
+        * Add threshold to create a `-scaled` output image for testing.
+        */
+       public function add_big_image_size_threshold() {
+               return 1000;
+       }
+
+       /**
+        * Output JPEG files.
+        */
+       public function image_editor_output_jpeg() {
+               return array( 'image/jpeg' => 'image/jpeg' );
+       }
+
+       /**
+        * Output WebP files.
+        */
+       public function image_editor_output_webp() {
+               return array( 'image/jpeg' => 'image/webp' );
+       }
+
+       /**
+        * Changes the quality using very low quality for JPEGs and very high quality
+        * for WebPs, used to verify the filter is applying correctly.
+        *
+        * @param int    $quality   Default quality.
+        * @param string $mime_type Image mime-type.
+        * @return int The changed quality.
+        */
+       public function image_editor_change_quality_low_jpeg( $quality, $mime_type ) {
+               if ( 'image/jpeg' === $mime_type ) {
+                       return 1;
+               } elseif ( 'image/webp' === $mime_type ) {
+                       return 100;
+               } else {
+                       return 30;
+               }
+       }
+
</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>
</div>

</body>
</html>