diff --git a/classes/class-eio-lazy-load.php b/classes/class-eio-lazy-load.php index 198ca0de..473ec7f6 100644 --- a/classes/class-eio-lazy-load.php +++ b/classes/class-eio-lazy-load.php @@ -360,8 +360,6 @@ function filter_page_output( $buffer ) { $lazy_source = $source; $this->set_attribute( $lazy_source, 'data-srcset', $srcset ); $this->remove_attribute( $lazy_source, 'srcset' ); - // TODO: remove this after testing. - /* $this->set_attribute( $lazy_source, 'srcset', $this->placeholder_src, true ); */ $picture = str_replace( $source, $lazy_source, $picture ); } } @@ -372,21 +370,23 @@ function filter_page_output( $buffer ) { } } } - // Video elements, looking for poster attributes that are images. - /* $videos = $this->get_elements_from_html( $buffer, 'video' ); */ - $videos = ''; - if ( $this->is_iterable( $videos ) ) { - foreach ( $videos as $index => $video ) { - $this->debug_message( 'parsing a video element' ); - $file = $this->get_attribute( $video, 'poster' ); - if ( $file ) { - $this->debug_message( "checking webp for video poster: $file" ); - if ( $this->validate_image_tag( $file ) ) { - $this->set_attribute( $video, 'data-poster-webp', $this->placeholder_src ); - $this->set_attribute( $video, 'data-poster-image', $file ); - $this->remove_attribute( $video, 'poster' ); - $this->debug_message( "found webp for video poster: $file" ); - $buffer = str_replace( $videos[ $index ], $video, $buffer ); + // Iframe elements, looking for stuff like YouTube embeds. + if ( in_array( 'iframe', $this->user_element_exclusions, true ) ) { + $frames = ''; + } else { + $frames = $this->get_elements_from_html( $buffer, 'iframe' ); + } + if ( $this->is_iterable( $frames ) ) { + foreach ( $frames as $index => $frame ) { + $this->debug_message( 'parsing an iframe element' ); + $url = $this->get_attribute( $frame, 'src' ); + if ( $url && $this->validate_iframe_tag( $frame ) ) { + $this->debug_message( "lazifying iframe for: $url" ); + $this->set_attribute( $frame, 'data-src', $url ); + $this->remove_attribute( $frame, 'src' ); + $this->set_attribute( $frame, 'class', trim( $this->get_attribute( $frame, 'class' ) . ' lazyload' ), true ); + if ( $frame !== $frames[ $index ] ) { + $buffer = str_replace( $frames[ $index ], $frame, $buffer ); } } } @@ -448,11 +448,8 @@ function parse_img_tag( $image, $file = '' ) { $insert_dimensions = true; } } - // Check for native lazy loading images. - $loading_attr = $this->get_attribute( $image, 'loading' ); - if ( defined( 'EIO_ENABLE_NATIVE_LAZY' ) && EIO_ENABLE_NATIVE_LAZY && ! $loading_attr && is_numeric( $width_attr ) && is_numeric( $height_attr ) ) { - $this->set_attribute( $image, 'loading', 'lazy' ); - } + + $use_native_lazy = false; $placeholder_types = array(); if ( $this->parsing_exactdn && $this->allow_lqip && apply_filters( 'eio_use_lqip', $this->get_option( $this->prefix . 'use_lqip' ), $file ) ) { @@ -485,6 +482,7 @@ function parse_img_tag( $image, $file = '' ) { $this->debug_message( 'using lqip, maybe' ); if ( false === strpos( $file, 'nggid' ) && ! preg_match( '#\.svg(\?|$)#', $file ) && strpos( $file, $this->exactdn_domain ) ) { $placeholder_src = add_query_arg( array( 'lazy' => 1 ), $file ); + $use_native_lazy = true; break 2; } break; @@ -513,9 +511,11 @@ function parse_img_tag( $image, $file = '' ) { if ( $filename_width && $filename_height ) { $placeholder_src = $exactdn->generate_url( $this->content_url . 'lazy/placeholder-' . $filename_width . 'x' . $filename_height . '.png' ); + $use_native_lazy = true; break 2; } else { $placeholder_src = add_query_arg( array( 'lazy' => 2 ), $file ); + $use_native_lazy = true; break 2; } } @@ -536,6 +536,7 @@ function parse_img_tag( $image, $file = '' ) { $png_placeholder_src = $this->create_piip( $filename_width, $filename_height ); if ( $png_placeholder_src ) { $placeholder_src = $png_placeholder_src; + $use_native_lazy = true; break 2; } } @@ -547,6 +548,13 @@ function parse_img_tag( $image, $file = '' ) { $this->debug_message( "current placeholder is $placeholder_src" ); $placeholder_src = apply_filters( 'eio_lazy_placeholder', $placeholder_src, $image ); + + // Check for native lazy loading images. + $loading_attr = $this->get_attribute( $image, 'loading' ); + if ( ( ! defined( 'EIO_DISABLE_NATIVE_LAZY' ) || ! EIO_DISABLE_NATIVE_LAZY ) && ! $loading_attr && $use_native_lazy ) { + $this->set_attribute( $image, 'loading', 'lazy' ); + } + if ( $srcset ) { if ( strpos( $placeholder_src, '64,R0lGOD' ) ) { $this->set_attribute( $image, 'srcset', $placeholder_src, true ); @@ -827,6 +835,34 @@ function validate_bgimage_tag( $tag ) { return true; } + /** + * Checks if an iframe tag is allowed to be lazy loaded. + * + * @param string $tag The tag. + * @return bool True if the tag is allowed, false otherwise. + */ + function validate_iframe_tag( $tag ) { + $this->debug_message( '' . __METHOD__ . '()' ); + $exclusions = apply_filters( + 'eio_lazy_iframe_exclusions', + array_merge( + array( + 'data-no-lazy=', + 'lazyload', + 'skip-lazy', + ), + $this->user_exclusions + ), + $tag + ); + foreach ( $exclusions as $exclusion ) { + if ( false !== strpos( $tag, $exclusion ) ) { + return false; + } + } + return true; + } + /** * Build a PNG inline image placeholder. * diff --git a/classes/class-exactdn.php b/classes/class-exactdn.php index 0b1a4f9b..2967b5a2 100644 --- a/classes/class-exactdn.php +++ b/classes/class-exactdn.php @@ -2662,6 +2662,15 @@ function exactdn_remove_args( $args, $image_url, $scheme ) { * @return boolean True to skip the page, unchanged otherwise. */ function skip_page( $skip = false, $uri = '' ) { + if ( false !== strpos( $uri, 'cornerstone=' ) || false !== strpos( $uri, 'cornerstone-endpoint' ) ) { + return true; + } + if ( false !== strpos( $uri, 'et_fb=' ) ) { + return true; + } + if ( false !== strpos( $uri, 'tatsu=' ) ) { + return true; + } if ( false !== strpos( $uri, 'ct_builder=' ) ) { return true; } diff --git a/common.php b/common.php index 5b0418cb..430ac55d 100644 --- a/common.php +++ b/common.php @@ -3162,7 +3162,13 @@ function ewww_image_optimizer_imagick_create_webp( $file, $type, $webpfile ) { $color = $image->getImageColorspace(); ewwwio_debug_message( "color space is $color" ); if ( Imagick::COLORSPACE_CMYK === $color ) { + if ( ewwwio_is_file( EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'vendor/icc/sRGB2014.icc' ) ) { + $icc_profile = file_get_contents( EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'vendor/icc/sRGB2014.icc' ); + $image->profileImage( 'icc', $icc_profile ); + } $image->transformImageColorspace( Imagick::COLORSPACE_SRGB ); + $image->setImageProfile( 'icc', null ); + $profiles = array(); } $image->setImageFormat( 'WEBP' ); if ( $sharp_yuv ) { diff --git a/readme.txt b/readme.txt index e9edc72f..48178bcd 100644 --- a/readme.txt +++ b/readme.txt @@ -135,7 +135,10 @@ That's not a question, but since I made it up, I'll answer it. See this resource = 6.2.0 = * added: PHP-based WebP Conversion via GD/Imagick in free mode when exec() is disabled * added: enable -sharp_yuv option for WebP conversion with the EIO_WEBP_SHARP_YUV override +* added: WebP Conversion for CMYK images +* added: Lazy Load for iframes, add 'iframe' in exclusions to disable * added: compatibility with S3 Uploads 3.x +* changed: native lazy loading is now enabled for right-sized PNG placeholders, override with EIO_DISABLE_NATIVE_LAZY constant * fixed: empty .webp images sometimes produced when cwebp encounters an error = 6.1.9 = diff --git a/tests/test-agr.php b/tests/test-agr.php index a3730874..535b1fdd 100644 --- a/tests/test-agr.php +++ b/tests/test-agr.php @@ -43,7 +43,7 @@ function setUp() { * Test that GD is active and Imagick is not -- otherwise our tests are bogus. */ function test_gd_active() { - $this->assertTrue( ewww_image_optimizer_gd_support() ); + $this->assertNotEmpty( ewww_image_optimizer_gd_support() ); $this->assertFalse( ewww_image_optimizer_imagick_support() ); } diff --git a/vendor/icc/sRGB2014.icc b/vendor/icc/sRGB2014.icc new file mode 100644 index 00000000..49afbfef Binary files /dev/null and b/vendor/icc/sRGB2014.icc differ