@@ -116,6 +116,33 @@ private function get_inline_script() {
116
116
window.nfdPerformance.imageOptimization.lazyLoading = ' . wp_json_encode ( self ::$ exclusions ) . '; ' ;
117
117
}
118
118
119
+ /**
120
+ * Cleans up content by replacing specific patterns with replacements.
121
+ * This method is used to sanitize or modify content before lazy loading is applied.
122
+ *
123
+ * @param string $pattern Regular expression pattern to match.
124
+ * @param string $search String to search for in the content.
125
+ * @param string $replace String to replace the search string with.
126
+ * @param string $content The content to be cleaned.
127
+ * @return string The cleaned content.
128
+ */
129
+ public function clean_content ( $ pattern , $ search , $ replace , $ content ) {
130
+
131
+ if ( empty ( $ content ) || empty ( $ pattern ) || empty ( $ search ) ) {
132
+ return $ content ;
133
+ }
134
+
135
+ $ content = preg_replace_callback (
136
+ $ pattern ,
137
+ function ( $ matches ) use ( $ search , $ replace ) {
138
+ $ cleanedIframe = str_replace ( $ search , $ replace , $ matches [0 ] );
139
+ return $ cleanedIframe ;
140
+ },
141
+ $ content
142
+ );
143
+ return $ content ;
144
+ }
145
+
119
146
/**
120
147
* Applies lazy loading to images in HTML content.
121
148
* Skips images with specified exclusion classes or attributes.
@@ -134,6 +161,15 @@ public function apply_lazy_loading( $content ) {
134
161
libxml_use_internal_errors ( true );
135
162
136
163
try {
164
+ if ( function_exists ( 'et_setup_theme ' ) ) {
165
+ $ content = $ this ->clean_content (
166
+ '/<iframe(.*?)<\/iframe>/s ' ,
167
+ '<!-- [et_pb_line_break_holder] --> ' ,
168
+ '' ,
169
+ $ content
170
+ );
171
+ }
172
+
137
173
// Attempt to parse the HTML content using htmlentities for encoding.
138
174
$ content = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body> ' . $ content . '</body></html> ' ;
139
175
if ( ! $ doc ->loadHTML ( $ content , LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ) ) {
0 commit comments