Skip to content

Commit

Permalink
re-enable native lazyload for png placeholders, lazy for iframes, and…
Browse files Browse the repository at this point in the history
… cmyk webp
  • Loading branch information
nosilver4u committed Jul 13, 2021
1 parent 918ffed commit ca55bee
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 23 deletions.
80 changes: 58 additions & 22 deletions classes/class-eio-lazy-load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Expand All @@ -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 );
}
}
}
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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 );
Expand Down Expand Up @@ -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( '<b>' . __METHOD__ . '()</b>' );
$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.
*
Expand Down
9 changes: 9 additions & 0 deletions classes/class-exactdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion tests/test-agr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}

Expand Down
Binary file added vendor/icc/sRGB2014.icc
Binary file not shown.

0 comments on commit ca55bee

Please sign in to comment.