Skip to content

Commit

Permalink
Adição de verificação para evitar erros no PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardomoro committed May 5, 2022
1 parent 6c5d75d commit c249ade
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions inc/lazyload.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
<?php
// Lazyload Images
function ifrs_add_lazyload($content) {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$dom = new DOMDocument();
@$dom->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('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));

$newHtml = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
return $newHtml;
}

return $newHtml;
return $content;
}

add_filter('the_content', 'ifrs_add_lazyload', 99);
Expand Down

0 comments on commit c249ade

Please sign in to comment.