Skip to content

Commit

Permalink
Lazy Images: Add filter to allow skipping an image with certain attri…
Browse files Browse the repository at this point in the history
…butes
  • Loading branch information
ebinnion committed Feb 8, 2018
1 parent ed23c90 commit 3a7eaa3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions modules/lazy-images/lazy-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ static function process_image( $matches ) {
return $matches[0];
}

/**
* Allow plugins and themes to conditionally skip processing an image via its attributes.
*
* @module-lazy-images
*
* @since 5.9.0
*
* @param bool Default to not skip processing the current image.
* @param array An array of attributes via wp_kses_hair() for the current image.
*/
if ( apply_filters( 'jetpack_lazy_images_skip_image_with_atttributes', false, $old_attributes_kses_hair ) ) {
return $matches[0];
}

$old_attributes = self::flatten_kses_hair_data( $old_attributes_kses_hair );
$new_attributes = self::process_image_attributes( $old_attributes );
$new_attributes_str = self::build_attributes_string( $new_attributes );
Expand Down
20 changes: 20 additions & 0 deletions tests/php/modules/lazy-images/test_class.lazy-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,22 @@ function get_should_skip_image_with_filtered_empty_blacklist_data() {
);
}

function test_jetpack_lazy_images_skip_image_with_atttributes_filter() {
$instance = Jetpack_Lazy_Images::instance();
$src = '<img src="image.jpg" srcset="medium.jpg 1000w, large.jpg 2000w" class="wp-post-image"/>';

$this->assertContains( 'src="placeholder.jpg"', $instance->add_image_placeholders( $src ) );

add_filter( 'jetpack_lazy_images_skip_image_with_atttributes', '__return_true' );
$this->assertNotContains( 'src="placeholder.jpg"', $instance->add_image_placeholders( $src ) );
remove_filter( 'jetpack_lazy_images_skip_image_with_atttributes', '__return_true' );

add_filter( 'jetpack_lazy_images_skip_image_with_atttributes', array( $this, '__skip_if_srcset' ), 10, 2 );
$this->assertNotContains( 'src="placeholder.jpg"', $instance->add_image_placeholders( $src ) );
$this->assertContains( 'src="placeholder.jpg"', $instance->add_image_placeholders( '<img src="image.jpg" />' ) );
remove_filter( 'jetpack_lazy_images_skip_image_with_atttributes', array( $this, '__skip_if_srcset' ), 10, 2 );
}

/*
* Helpers
*/
Expand Down Expand Up @@ -314,4 +330,8 @@ public function __get_output_content() {

return trim( $contents );
}

public function __skip_if_srcset( $should_skip, $attributes ) {
return isset( $attributes['srcset'] );
}
}

0 comments on commit 3a7eaa3

Please sign in to comment.