diff --git a/includes/Images/ImageLazyLoader.php b/includes/Images/ImageLazyLoader.php index ecf559b8..7870add1 100644 --- a/includes/Images/ImageLazyLoader.php +++ b/includes/Images/ImageLazyLoader.php @@ -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. @@ -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( + '//s', + '', + '', + $content + ); + } + // Attempt to parse the HTML content using htmlentities for encoding. $content = '' . $content . ''; if ( ! $doc->loadHTML( $content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ) ) {