Skip to content
Merged
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
36 changes: 36 additions & 0 deletions includes/Images/ImageLazyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ private function get_inline_script() {
window.nfdPerformance.imageOptimization.lazyLoading = ' . wp_json_encode( self::$exclusions ) . ';';
}

/**
* Cleans up content by replacing specific patterns with replacements.
* This method is used to sanitize or modify content before lazy loading is applied.
*
* @param string $pattern Regular expression pattern to match.
* @param string $search String to search for in the content.
* @param string $replace String to replace the search string with.
* @param string $content The content to be cleaned.
* @return string The cleaned content.
*/
public function clean_content( $pattern, $search, $replace, $content ) {

if ( empty( $content ) || empty( $pattern ) || empty( $search ) ) {
return $content;
}

$content = preg_replace_callback(
$pattern,
function ( $matches ) use ( $search, $replace ) {
$cleanedIframe = str_replace( $search, $replace, $matches[0] );
return $cleanedIframe;
},
$content
);
return $content;
}

/**
* Applies lazy loading to images in HTML content.
* Skips images with specified exclusion classes or attributes.
Expand All @@ -134,6 +161,15 @@ public function apply_lazy_loading( $content ) {
libxml_use_internal_errors( true );

try {
if ( function_exists( 'et_setup_theme' ) ) {
$content = $this->clean_content(
'/<iframe(.*?)<\/iframe>/s',
'<!-- [et_pb_line_break_holder] -->',
'',
$content
);
}

// Attempt to parse the HTML content using htmlentities for encoding.
$content = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>' . $content . '</body></html>';
if ( ! $doc->loadHTML( $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ) ) {
Expand Down
Loading