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

Commit aa51c8e

Browse files
committed
Update wp_add_lazy_load_attributes()
Filter eachg tag after adding the `loading` attribute. Change priority to 25, after other "display" filters.
1 parent b64c890 commit aa51c8e

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

Diff for: wp-lazy-loading.php

+32-15
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@
3232
function _wp_lazy_loading_initialize_filters() {
3333
// The following filters would be merged into core.
3434
foreach ( array( 'the_content', 'the_excerpt', 'comment_text', 'widget_text_content' ) as $filter ) {
35-
// Before parsing blocks and shortcodes.
36-
// TODO: Comments do not support images. Revisit.
37-
// TODO: This should not exclude images from dynamic blocks and shortcodes. Look at fixing the filter priority.
38-
add_filter( $filter, 'wp_add_lazy_load_attributes', 8 );
35+
// After parsing blocks and shortcodes.
36+
add_filter( $filter, 'wp_add_lazy_load_attributes', 25 );
3937
}
4038

4139
// The following filters are only needed while this is a feature plugin.
@@ -119,8 +117,6 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
119117
return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context );
120118
}
121119

122-
123-
// TODO: update docs.
124120
/**
125121
* Add `loading="lazy"` to `img` and/or `iframe` HTML tags.
126122
*
@@ -129,7 +125,7 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
129125
*
130126
* @since (TBD)
131127
*
132-
* @param string $content The raw post content to be filtered.
128+
* @param string $content The HTML content to be filtered.
133129
* @param string $context Optional. Additional context to pass to the filters. Defaults to `current_filter()` when not set.
134130
* @return string Converted content with 'loading' attributes added to images.
135131
*/
@@ -149,23 +145,44 @@ function wp_add_lazy_load_attributes( $content, $context = null ) {
149145
$tags[] = 'iframe';
150146
}
151147

148+
// Check if the tag exists in $content.
149+
foreach ( $tags as $index => $tag_name ) {
150+
if ( false === strpos( $content, "<$tag_name" ) ) {
151+
unset( $tags[ $index ] );
152+
}
153+
}
154+
152155
if ( empty( $tags ) ) {
153156
return $content;
154157
}
155158

156159
return preg_replace_callback(
157-
'/<(' . implode( '|', $tags ) . ')(\s)[^>]+>/',
158-
function( array $matches ) {
159-
if ( ! preg_match( '/\sloading\s*=/', $matches[0] ) ) {
160-
$tag = $matches[1];
161-
$space = $matches[2];
162-
163-
return str_replace( '<' . $tag . $space, '<' . $tag . $space . 'loading="lazy" ', $matches[0] );
160+
'/<(' . implode( '|', $tags ) . ')\s[^>]+>/',
161+
function( array $matches ) use( $content, $context ) {
162+
if ( ! preg_match( '/\bloading\s*=/', $matches[0] ) ) {
163+
$tag_name = $matches[1];
164+
$old_html = $matches[0];
165+
166+
$new_html = str_replace( "<$tag_name", "<$tag_name" . ' loading="lazy"', $old_html );
167+
168+
/**
169+
* Filters the tag HTML after adding the `loading` attribute.
170+
*
171+
* The variable part of the filter name is the filtered tag's name. For example for `img`
172+
* the filter would become `wp_add_lazy_loading_to_img`.
173+
*
174+
* @since (TBD)
175+
*
176+
* @param string $new_html The tag's HTML after adding the attribute.
177+
* @param string $old_html The tag's HTML before adding the attribute.
178+
* @param string $content The HTML content that is being filtered.
179+
* @param string $context Optional. Additional context. Defaults to `current_filter()`.
180+
*/
181+
return apply_filters( "wp_add_lazy_loading_to_{$tag_name}", $new_html, $old_html, $content, $context );
164182
}
165183

166184
return $matches[0];
167185
},
168186
$content
169187
);
170188
}
171-

0 commit comments

Comments
 (0)