Skip to content

Commit c780c76

Browse files
authored
Merge pull request #297 from newfold-labs/FIX/PRESS7-524-lazy-load-with-divi
Clean iframe tags if DIVI
2 parents 7d2d9b4 + 78bd2ff commit c780c76

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

includes/Images/ImageLazyLoader.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ private function get_inline_script() {
116116
window.nfdPerformance.imageOptimization.lazyLoading = ' . wp_json_encode( self::$exclusions ) . ';';
117117
}
118118

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+
119146
/**
120147
* Applies lazy loading to images in HTML content.
121148
* Skips images with specified exclusion classes or attributes.
@@ -134,6 +161,15 @@ public function apply_lazy_loading( $content ) {
134161
libxml_use_internal_errors( true );
135162

136163
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+
137173
// Attempt to parse the HTML content using htmlentities for encoding.
138174
$content = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>' . $content . '</body></html>';
139175
if ( ! $doc->loadHTML( $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ) ) {

0 commit comments

Comments
 (0)