@@ -62,7 +62,7 @@ static function add_image_placeholders( $content ) {
6262
6363 static function process_image ( $ matches ) {
6464 // In case you want to change the placeholder image
65- $ placeholder_image = apply_filters ( 'lazyload_images_placeholder_image ' , self ::get_url ( 'images/1x1.trans.gif ' ) );
65+ $ placeholder_image = apply_filters ( 'lazyload_images_placeholder_image ' , self ::get_url ( 'images/1x1.trans.gif ' ), $ matches );
6666
6767 $ old_attributes_str = $ matches [2 ];
6868 $ old_attributes = wp_kses_hair ( $ old_attributes_str , wp_allowed_protocols () );
@@ -73,13 +73,16 @@ static function process_image( $matches ) {
7373
7474 $ image_src = $ old_attributes ['src ' ]['value ' ];
7575
76- // Remove src and lazy-src since we manually add them
77- $ new_attributes = $ old_attributes ;
78- unset( $ new_attributes ['src ' ], $ new_attributes ['data-lazy-src ' ] );
76+ // Also retain the srcset, and sizes if present. The latter is to keep w3c validator happy (sizes requires srcset)
77+ $ lazy_srcset_attribute = ( ! empty ( $ old_attributes ['srcset ' ] ) ) ? 'data-lazy-srcset=" ' . $ old_attributes ['srcset ' ]['value ' ] . '" ' : '' ;
78+ $ lazy_sizes_attribute = ( ! empty ( $ old_attributes ['sizes ' ] ) ) ? 'data-lazy-sizes=" ' . $ old_attributes ['sizes ' ]['value ' ] . '" ' : '' ;
79+ // Remove src, lazy-src, srcset, lazy-srcset, sizes and data-lazy-sizes since we manually add them
80+ $ new_attributes = $ old_attributes ;
81+ unset( $ new_attributes ['src ' ], $ new_attributes ['data-lazy-src ' ], $ new_attributes ['srcset ' ], $ new_attributes ['data-lazy-srcset ' ], $ new_attributes ['sizes ' ], $ new_attributes ['data-lazy-sizes ' ] );
7982
8083 $ new_attributes_str = self ::build_attributes_string ( $ new_attributes );
81-
82- return sprintf ( '<img src="%1$s" data-lazy-src="%2$s" %3$s><noscript>%4 $s</noscript> ' , esc_url ( $ placeholder_image ) , esc_url ( $ image_src ), $ new_attributes_str , $ matches [0 ] );
84+ $ placeholder_url = strpos ( $ placeholder_image , ' data: ' ) == 0 ? $ placeholder_image : esc_url ( $ placeholder_image );
85+ return sprintf ( '<img src="%1$s" data-lazy-src="%2$s" %3$s %4$s %5$s ><noscript>%6 $s</noscript> ' , $ placeholder_url , esc_url ( $ image_src ), $ lazy_srcset_attribute , $ lazy_sizes_attribute , $ new_attributes_str , $ matches [0 ] );
8386 }
8487
8588 private static function build_attributes_string ( $ attributes ) {
0 commit comments