@@ -587,18 +587,6 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
587587 return $ image ;
588588 }
589589
590- // TODO: Add a filterable option to determine image extensions, see https://github.com/WordPress/performance/issues/187 for more details.
591- $ target_image_extensions = array (
592- 'jpg ' ,
593- 'jpeg ' ,
594- );
595-
596- // Creates a regular extension to find all the URLS with the provided extension for img tag.
597- preg_match_all ( '/[^\s"]+\.(?: ' . implode ( '| ' , $ target_image_extensions ) . ')/i ' , $ image , $ matches );
598- if ( empty ( $ matches ) ) {
599- return $ image ;
600- }
601-
602590 /**
603591 * Filters mime types that should be used to update all images in the content. The order of
604592 * mime types matters. The first mime type in the list will be used if it is supported by an image.
@@ -623,46 +611,37 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
623611 return $ image ;
624612 }
625613
626- $ basename = wp_basename ( $ metadata ['file ' ] );
627- $ urls = $ matches [0 ];
628- foreach ( $ urls as $ url ) {
629- $ src_filename = wp_basename ( $ url );
614+ // Replace the full size image if present.
615+ if ( isset ( $ metadata ['sources ' ][ $ target_mime ]['file ' ] ) ) {
616+ $ basename = wp_basename ( $ metadata ['file ' ] );
617+ if ( $ basename !== $ metadata ['sources ' ][ $ target_mime ]['file ' ] ) {
618+ $ image = str_replace (
619+ $ basename ,
620+ $ metadata ['sources ' ][ $ target_mime ]['file ' ],
621+ $ image
622+ );
623+ }
624+ }
630625
631- // Replace the full size image if present.
632- if ( isset ( $ metadata ['sources ' ][ $ target_mime ][ ' file ' ] ) && strpos ( $ url , $ basename ) !== false ) {
633- $ image = str_replace ( $ src_filename , $ metadata [ ' sources ' ][ $ target_mime ][ ' file ' ], $ image );
626+ // Replace sub sizes for the image if present.
627+ foreach ( $ metadata ['sizes ' ] as $ name => $ size_data ) {
628+ if ( empty ( $ size_data [ ' file ' ] ) ) {
634629 continue ;
635630 }
636631
637- if ( empty ( $ metadata [ ' sizes ' ] ) ) {
632+ if ( empty ( $ size_data [ ' sources ' ][ $ target_mime ][ ' file ' ] ) ) {
638633 continue ;
639634 }
640635
641- $ extension = wp_check_filetype ( $ src_filename );
642- // Extension was not set properly no action possible or extension is already in the expected mime.
643- if ( empty ( $ extension ['type ' ] ) || $ extension ['type ' ] === $ target_mime ) {
636+ if ( $ size_data ['file ' ] === $ size_data ['sources ' ][ $ target_mime ]['file ' ] ) {
644637 continue ;
645638 }
646639
647- // Find the appropriate size for the provided URL.
648- foreach ( $ metadata ['sizes ' ] as $ name => $ size_data ) {
649- // Not the size we are looking for.
650- if ( empty ( $ size_data ['file ' ] ) || $ src_filename !== $ size_data ['file ' ] ) {
651- continue ;
652- }
653-
654- if ( empty ( $ size_data ['sources ' ][ $ target_mime ]['file ' ] ) ) {
655- continue ;
656- }
657-
658- // This is the same as the file we want to replace nothing to do here.
659- if ( $ size_data ['sources ' ][ $ target_mime ]['file ' ] === $ src_filename ) {
660- continue ;
661- }
662-
663- $ image = str_replace ( $ src_filename , $ size_data ['sources ' ][ $ target_mime ]['file ' ], $ image );
664- break ;
665- }
640+ $ image = str_replace (
641+ $ size_data ['file ' ],
642+ $ size_data ['sources ' ][ $ target_mime ]['file ' ],
643+ $ image
644+ );
666645 }
667646
668647 return $ image ;
0 commit comments