Skip to content

Commit 2f4a2f9

Browse files
committed
Adds option to disable lazyload on PNG.
1 parent bf7e5c0 commit 2f4a2f9

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

inc/replacer.php

+30-4
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,6 @@ protected function strip_image_size_maybe( $src ) {
662662
$stripped_src = str_replace( $src_parts[1], '', $src );
663663
$upload_dir = wp_get_upload_dir();
664664
// Extracts the file path to the image minus the base url
665-
666665
$file_path = substr( $stripped_src, strlen( $upload_dir['baseurl'] ) );
667666

668667
if ( file_exists( $upload_dir['basedir'] . $file_path ) ) {
@@ -874,7 +873,7 @@ public function filter_the_content( $content ) {
874873
$new_tag = str_replace( 'width="' . $width . '"', 'width="' . $new_sizes['width'] . '"', $new_tag );
875874
$new_tag = str_replace( 'height="' . $height . '"', 'height="' . $new_sizes['height'] . '"', $new_tag );
876875

877-
if ( $this->lazyload ) {
876+
if ( $this->lazyload && $this->can_lazyload( $tmp ) ) {
878877
$new_sizes['quality'] = 'eco';
879878
$low_url = $this->get_imgcdn_url( $tmp, $new_sizes );
880879

@@ -948,13 +947,40 @@ public static function parse_images_from_html( $content ) {
948947
* @return string The HTML without the <header/> tag
949948
*/
950949
public static function strip_header_from_content( $content ) {
951-
if ( preg_match('/<header.*<\/header>/ismU', $content, $matches) !== 1 ) {
950+
if ( preg_match( '/<header.*<\/header>/ismU', $content, $matches ) !== 1 ) {
952951
return $content;
953952
}
954-
953+
955954
return str_replace( $matches[0], '', $content );
956955
}
957956

957+
/**
958+
* Check if the lazyload is allowed for this url.
959+
*
960+
* @param string $url Url.
961+
*
962+
* @return bool We can lazyload?
963+
*/
964+
public function can_lazyload( $url ) {
965+
if ( ! defined( 'OPTML_DISABLE_PNG_LAZYLOAD' ) ) {
966+
return true;
967+
}
968+
if ( ! OPTML_DISABLE_PNG_LAZYLOAD ) {
969+
return true;
970+
}
971+
$type = wp_check_filetype(
972+
basename( $url ),
973+
array(
974+
'png' => 'image/png',
975+
)
976+
);
977+
if ( ! isset( $type['ext'] ) || empty( $type['ext'] ) ) {
978+
return true;
979+
}
980+
981+
return false;
982+
}
983+
958984
/**
959985
* Extract slashed urls from content.
960986
*

tests/test-lazyload.php

+11
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,15 @@ public function test_lazy_load() {
4040
$this->assertNotContains( 'http://example.org', $replaced_content );
4141
}
4242

43+
44+
public function test_lazy_load_off() {
45+
46+
define( 'OPTML_DISABLE_PNG_LAZYLOAD', true );
47+
48+
$replaced_content = Optml_Replacer::instance()->filter_the_content( Test_Replacer::IMG_TAGS_PNG );
49+
50+
$this->assertContains( 'i.optimole.com', $replaced_content );
51+
$this->assertNotContains( 'data-opt-src', $replaced_content );
52+
53+
}
4354
}

tests/test-replacer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
* Class Test_Generic.
1313
*/
1414
class Test_Replacer extends WP_UnitTestCase {
15-
const IMG_TAGS = '<div id="wp-custom-header" class="wp-custom-header"><img src="http://example.org/wp-content/themes/twentyseventeen/assets/images/header.jpg" width="2000" height="1200" alt="Test" /></div></div>';
15+
const IMG_TAGS = '<div id="wp-custom-header" class="wp-custom-header"><img src="http://example.org/wp-content/themes/twentyseventeen/assets/images/header.jpg" width="2000" height="1200" alt="Test" /></div></div> ';
16+
const IMG_TAGS_PNG = '<div id="wp-custom-header" class="wp-custom-header"><img src="http://example.org/wp-content/themes/twentyseventeen/assets/images/header.png" width="2000" height="1200" alt="Test" /></div></div>';
1617
const IMG_URLS = '
1718
http://example.org/wp-content/themes/test/assets/images/header.png
1819
http://example.org/wp-content/themes/test/assets/images/header.jpeg

0 commit comments

Comments
 (0)