32
32
function _wp_lazy_loading_initialize_filters () {
33
33
// The following filters would be merged into core.
34
34
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 );
39
37
}
40
38
41
39
// The following filters are only needed while this is a feature plugin.
@@ -119,8 +117,6 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
119
117
return (bool ) apply_filters ( 'wp_lazy_loading_enabled ' , $ default , $ tag_name , $ context );
120
118
}
121
119
122
-
123
- // TODO: update docs.
124
120
/**
125
121
* Add `loading="lazy"` to `img` and/or `iframe` HTML tags.
126
122
*
@@ -129,7 +125,7 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
129
125
*
130
126
* @since (TBD)
131
127
*
132
- * @param string $content The raw post content to be filtered.
128
+ * @param string $content The HTML content to be filtered.
133
129
* @param string $context Optional. Additional context to pass to the filters. Defaults to `current_filter()` when not set.
134
130
* @return string Converted content with 'loading' attributes added to images.
135
131
*/
@@ -149,23 +145,44 @@ function wp_add_lazy_load_attributes( $content, $context = null ) {
149
145
$ tags [] = 'iframe ' ;
150
146
}
151
147
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
+
152
155
if ( empty ( $ tags ) ) {
153
156
return $ content ;
154
157
}
155
158
156
159
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 );
164
182
}
165
183
166
184
return $ matches [0 ];
167
185
},
168
186
$ content
169
187
);
170
188
}
171
-
0 commit comments