From c249adea5953758556b76865523989695082f67c Mon Sep 17 00:00:00 2001 From: Ricardo Moro Date: Thu, 5 May 2022 17:14:59 -0300 Subject: [PATCH] =?UTF-8?q?Adi=C3=A7=C3=A3o=20de=20verifica=C3=A7=C3=A3o?= =?UTF-8?q?=20para=20evitar=20erros=20no=20PHP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/lazyload.php | 60 ++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/inc/lazyload.php b/inc/lazyload.php index fcc8d1c..54e885e 100644 --- a/inc/lazyload.php +++ b/inc/lazyload.php @@ -1,47 +1,51 @@ loadHTML($content); + if (!empty($content)) { + $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"); + $dom = new DOMDocument(); + @$dom->loadHTML($content); - $images = []; + $images = []; - foreach ($dom->getElementsByTagName('img') as $node) { - $images[] = $node; - } + foreach ($dom->getElementsByTagName('img') as $node) { + $images[] = $node; + } - foreach ($images as $node) { - $fallback = $node->cloneNode(true); + foreach ($images as $node) { + $fallback = $node->cloneNode(true); - $oldClass = $node->getAttribute('class'); - $newClass = 'lazyload ' . $oldClass; - $node->setAttribute('class', $newClass); + $oldClass = $node->getAttribute('class'); + $newClass = 'lazyload ' . $oldClass; + $node->setAttribute('class', $newClass); - $node->setAttribute('data-sizes', 'auto'); + $node->setAttribute('data-sizes', 'auto'); - $oldsrc = $node->getAttribute('src'); + $oldsrc = $node->getAttribute('src'); - if ($oldsrc) { - $node->setAttribute('data-src', $oldsrc); - $node->removeAttribute('src'); - } + if ($oldsrc) { + $node->setAttribute('data-src', $oldsrc); + $node->removeAttribute('src'); + } - $oldsrcset = $node->getAttribute('srcset'); + $oldsrcset = $node->getAttribute('srcset'); - if ($oldsrcset) { - $node->setAttribute('data-srcset', $oldsrcset); - $newsrcset = ''; - $node->setAttribute('srcset', $newsrcset); + if ($oldsrcset) { + $node->setAttribute('data-srcset', $oldsrcset); + $newsrcset = ''; + $node->setAttribute('srcset', $newsrcset); + } + + $node->setAttribute('loading', 'lazy'); + $node->setAttribute('decoding', 'async'); } - $node->setAttribute('loading', 'lazy'); - $node->setAttribute('decoding', 'async'); - } + $newHtml = preg_replace('/^/', '', str_replace( array('', '', '', ''), array('', '', '', ''), $dom->saveHTML())); - $newHtml = preg_replace('/^/', '', str_replace( array('', '', '', ''), array('', '', '', ''), $dom->saveHTML())); + return $newHtml; + } - return $newHtml; + return $content; } add_filter('the_content', 'ifrs_add_lazyload', 99);