Skip to content
This repository was archived by the owner on Dec 23, 2020. It is now read-only.

Add support for skip-lazy #4

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ function test_wp_lazy_load_content_media() {
// The following should not be modified because there already is a 'loading' attribute.
$img_eager = str_replace( ' />', ' loading="eager" />', $img );

// The following should not be modified either, because 'skip-lazy' is present.
$img_skiplazy = str_replace( 'class="', 'class="skip-lazy ', $img );

$content = '
<p>Image, standard.</p>
%1$s
Expand All @@ -46,11 +49,14 @@ function test_wp_lazy_load_content_media() {
<p>Image, with pre-existing "loading" attribute.</p>
%5$s

<p>Image, with "skip-lazy" set, not to be modified.</p>
%6$s

<p>Iframe, standard. Should not be modified by default.</p>
%4$s';

$content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $iframe, $img_eager );
$content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $iframe, $img_eager );
$content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $iframe, $img_eager, $img_skiplazy );
$content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $iframe, $img_eager, $img_skiplazy );

$this->assertSame( $content_filtered, wp_add_lazy_load_attributes( $content_unfiltered ) );
}
Expand Down
6 changes: 3 additions & 3 deletions wp-lazy-loading.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function _wp_lazy_loading_initialize_filters() {
* @return string Modified tag.
*/
function _wp_lazy_loading_add_attribute_to_avatar( $avatar ) {
if ( in_array( 'img', wp_get_lazy_load_tags(), true ) && false === strpos( $avatar, ' loading=' ) ) {
if ( in_array( 'img', wp_get_lazy_load_tags(), true ) && false === strpos( $avatar, ' loading=' ) && false === strpos( $avatar, 'skip-lazy' ) ) {
$avatar = str_replace( '<img ', '<img loading="lazy" ', $avatar );
}

Expand All @@ -83,7 +83,7 @@ function _wp_lazy_loading_add_attribute_to_avatar( $avatar ) {
* @return array Modified attributes.
*/
function _wp_lazy_loading_add_attribute_to_attachment_image( $attr ) {
if ( in_array( 'img', wp_get_lazy_load_tags(), true ) ) {
if ( in_array( 'img', wp_get_lazy_load_tags(), true ) && ( ! array_key_exists( 'class', $attr ) || false === strpos( $attr['class'], 'skip-lazy' ) ) && ! array_key_exists( 'data-skip-lazy', $attr ) ) {
$attr['loading'] = 'lazy';
}

Expand Down Expand Up @@ -148,7 +148,7 @@ function wp_add_lazy_load_attributes( $content ) {
return preg_replace_callback(
'/<(' . implode( '|', $tags ) . ')(\s)[^>]+>/',
function( array $matches ) {
if ( ! preg_match( '/\sloading\s*=/', $matches[0] ) ) {
if ( ! preg_match( '/\sloading\s*=/', $matches[0] ) && false === strpos( $matches[0], 'skip-lazy' ) ) {
$tag = $matches[1];
$space = $matches[2];

Expand Down