// RPECK 23/03/2024 - Get the Diocese values from the Redux backend
// We want to loop through the various Diocese elements and output a list with a link
global $kadence_child_theme;
// RPECK 23/03/2024 - Check if the Kadence Child Theme variable is an array and contains the Diocese key
// This will save resources if it is not true
if(is_array($kadence_child_theme) && array_key_exists('diocese', $kadence_child_theme)) {
// RPECK 23/03/2024 - Initialize an instance of DOMDocument
// We will populate the various HTML elements with it
$dom = new DOMDocument();
// RPECK 23/03/2024 - Sanitize the repeater data
// Gives us the ability to loop through it using the foreach block below
$diocese_repeater_data = \KadenceChild\Helpers::repeaterData($kadence_child_theme['diocese']);
// RPECK 23/03/2024 - Loop through Diocese elements and output the different values
// Provides structure which can be replicated
foreach($diocese_repeater_data as $diocese) {
// RPECK 23/03/2024 - Create container element
// This will be used to populate the block
$container = $dom->createElement(‘div’);
$container->setAttribute(‘class’, ‘diocese_container’);
// RPECK 23/03/2024 – Check to see if a link is present
// If the link exists, we need to add it to the DOMDocument so the image is clickable
if(array_key_exists(‘diocese_url’,$diocese) && !empty($diocese[‘diocese_url’])) {
// RPECK 23/03/2024 – Add the anchor element to DOMDocument
// Gives us the ability to allocate the correct attributes etc
$link = $dom->createElement(‘a’);
$link->setAttribute(‘href’, $diocese[‘diocese_url’]);
$link->setAttribute(‘title’, $diocese[‘diocese_name’]);
$link->setAttribute(‘target’, ‘_blank’);
// RPECK 23/03/2024 – Append to container
// This ensures we have the correct values to output
$container->appendChild($link);
}
// RPECK 23/03/2024 – Create an image element
// This is the primary focus of the system. It should only be created if the Diocese ‘image’ value exists
if(array_key_exists(‘diocese_logo’,$diocese) && !empty($diocese[‘diocese_logo’])) {
// RPECK 23/03/2024 – Add the image element to DOMDocument
// Gives us the ability to allocate the correct attributes etc
$image = $dom->createElement(‘img’);
$image->setAttribute(‘src’, $diocese[‘diocese_logo’][‘url’]);
// RPECK 23/03/2024 – Append to container
// This ensures we have the correct values to output
if(isset($link)) {
// RPECK 23/03/2024 – If the link exists, append the image to it, rather than the container
// This ensures we are able to output the various elements inside the link
$link->appendChild($image);
} else {
// RPECK 23/03/2024 – If the link does not exist, append the image to the container instead
$container->appendChild($image);
}
}
// RPECK 23/03/2024 – Append the container to the main $dom document
// After doing this, we can output the HTML as required
$dom->appendChild($container);
// RPECK 23/03/2024 – Clear the $link variable
// Ws conflicting with the way the system was showing
unset($link, $image, $container);
}
// RPECK 23/03/2024 – If we are at this point, output the DOMDocument HTML
echo $dom->saveHTML();
}