Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturmamedov/765aa4370441c3cce629459453f86924 to your computer and use it in GitHub Desktop.
Save arturmamedov/765aa4370441c3cce629459453f86924 to your computer and use it in GitHub Desktop.
<?php
$gallery_loop = false;
$the_content = get_the_content(); // all content
$galleries = []; //Create empty figures array that will hold all of our parsed HTML data
while ($gallery_loop) {
$gallery = $the_content; // <figure .. </figure>
//<!-- wp:gallery {"ids":[742,739,737],"linkTo":"none"} -->
$cut_from = '<!-- wp:gallery';
$cut_to = '<!-- /wp:gallery -->';
//$cut_from = '<ul class="blocks-gallery-grid';
//$cut_to = '</ul>';
$pos1 = strpos($gallery, $cut_from);
$pos2 = strpos($gallery, $cut_to, $pos1);
if ($pos2) {
$gallery = substr($gallery, $pos1, ($pos2 - $pos1 + strlen($cut_to)));
$the_content = str_replace($gallery, '[gallery]'.$pos1.'-'.$pos2.'[/gallery]', $the_content);
//exit(htmlentities($gallery));
//Create a new DOM document
$dom = new DOMDocument;
//Parse the HTML.
@$dom->loadHTML($gallery);
//dd($gallery);
//Create new XP
$xp = new DOMXpath($dom);
//$_xp = $dom->getElementsByTagName('ul')->item(0);
//dd($_xp);
//dd($xp);
//Get all <figure> elements
$figureElements = $xp->query('/html/body/figure/ul//figure');
//dd($figureElements);
//Create number variable to keep track of our $galleries array index
$figureCount = 0;
//Loop through each <figure> element
foreach ($figureElements as $figureElement) {
if (! $figureElement) {
continue;
}
//$galleries[$pos1.'-'.$pos2][$figureCount]["class"] = trim($figureElement->getAttribute('class'));
//$galleries[$pos1.'-'.$pos2][$figureCount]["src"] = trim($figureElement->getAttribute('src'));
//$galleries[$pos1.'-'.$pos2][$figureCount]["alt"] = trim($figureElement->getAttribute('alt'));
$galleries[$pos1.'-'.$pos2][$figureCount]["src"] = $xp->query('//img', $figureElement)->item($figureCount)->getAttribute('src');
$galleries[$pos1.'-'.$pos2][$figureCount]["alt"] = $xp->query('//img', $figureElement)->item($figureCount)->getAttribute('alt');
//Check that an img class exists, otherwise set the value to null. If we don't do this PHP will throw a NOTICE.
/*if (boolval($xp->evaluate('//img', $figureElement)->item($figureCount))) {
$galleries[$pos1.'-'.$pos2][$figureCount]["img"]["class"] = $xp->query('//img', $figureElement)->item($figureCount)->getAttribute('class');
} else {
$galleries[$pos1.'-'.$pos2][$figureCount]["img"]["class"] = null;
}*/
//Check that a <figcaption> element exists, otherwise set the value to null
if (boolval($xp->evaluate('//figcaption', $figureElement)->item($figureCount))) {
$galleries[$pos1.'-'.$pos2][$figureCount]["class"] = $xp->query('//figcaption', $figureElement)->item($figureCount)->nodeValue;
} else {
$galleries[$pos1.'-'.$pos2][$figureCount]["class"] = null;
}
//Increment our $figureCount so that we know we can create a new array index.
$figureCount++;
}
} else {
$gallery_loop = false;
break;
}
}
//dd($the_content);
if ($galleries) {
foreach ($galleries as $gallery_key => $figures) {
$gallery_html = '';
$gallery_html .= '<div class="owl-carousel mad-owl-center-2 mad-grid mad-owl-moving nav-size-2 no-dots">';
foreach ($figures as $figure) {
$gallery_html .= '<div class="mad-owl-item">';
$gallery_html .= '<a href="'.$figure['src'].'" data-gallery="page-'.$gallery_key.'" title="'.$figure['alt'].'">';
ob_start();
\App\thumber($figure['src'], 645, 373, $figure['class'], $figure['alt'], $figure['alt']);
$htmlStr = ob_get_contents();
$gallery_html .= $htmlStr;
ob_end_clean();
$gallery_html .= '</a>';
$gallery_html .= '</div>';
}
$gallery_html .= '</div>';
$the_content = str_replace('[gallery]'.$gallery_key.'[/gallery]', $gallery_html, $the_content);
}
//dump(($the_content));
// dd();
}
?>
<?php
<div class="slick-carousel">
<?php foreach ($galleries as $_figures): ?>
<?php foreach ($_figures as $figure): ?>
<div class="mad-owl-item">
<a href="{{ $figure['src'] }}" data-gallery="page" title="{{ $figure['alt'] }}">
<?php echo \App\thumber($figure['src'], 645, 373, $figure['class'], $figure['alt'], $figure['alt'], ); ?>
</a>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
</div>
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment