Skip to content

Commit 775240c

Browse files
tijmenbruggemanrkoopmans
authored andcommitted
bug: set order of mimetypes to avif 1st, webp2. reuse protected class variable for checking if source an optimized image
1 parent 73d509a commit 775240c

2 files changed

Lines changed: 3 additions & 6 deletions

File tree

src/class-tiny-picture.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function __construct( $html, $base_dir, $domains ) {
198198
$this->raw_html = $html;
199199
$this->base_dir = $base_dir;
200200
$this->allowed_domains = $domains;
201-
$this->valid_mimetypes = array( 'image/webp', 'image/avif' );
201+
$this->valid_mimetypes = array( 'image/avif', 'image/webp' );
202202
}
203203

204204
protected static function get_attribute_value( $element, $name ) {
@@ -405,15 +405,12 @@ class Tiny_Picture_Source extends Tiny_Source_Base {
405405
public function augment_picture_element() {
406406
$modified_sources = array();
407407

408-
// handle existing sources
409-
$optimized_types = [ 'image/webp', 'image/avif' ];
410-
411408
foreach ( $this->get_element_by_tag( $this->raw_html, 'source' ) as $source_tag_html ) {
412409
$type_attr = self::get_attribute_value( $source_tag_html, 'type' );
413410
$type_attr = null !== $type_attr ? strtolower( trim( $type_attr ) ) : '';
414411

415412
// Skip if already optimized.
416-
if ( '' !== $type_attr && in_array( $type_attr, $optimized_types, true ) ) {
413+
if ( '' !== $type_attr && in_array( $type_attr, $this->valid_mimetypes, true ) ) {
417414
continue;
418415
}
419416

test/unit/TinyPictureTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function test_adds_both_avif_and_webp()
199199
$this->wp->createImage(1000, '2025/01', 'test.avif');
200200

201201
$input = '<img src="/wp-content/uploads/2025/01/test.png">';
202-
$expected = '<picture><source srcset="/wp-content/uploads/2025/01/test.webp" type="image/webp" /><source srcset="/wp-content/uploads/2025/01/test.avif" type="image/avif" /><img src="/wp-content/uploads/2025/01/test.png"></picture>';
202+
$expected = '<picture><source srcset="/wp-content/uploads/2025/01/test.avif" type="image/avif" /><source srcset="/wp-content/uploads/2025/01/test.webp" type="image/webp" /><img src="/wp-content/uploads/2025/01/test.png"></picture>';
203203
$output = $this->tiny_picture->replace_sources($input);
204204

205205
$this->assertEquals($expected, $output);

0 commit comments

Comments
 (0)