<?php
ob_clean(); // IMPORTANT: clear unwanted output
header("Content-Type: application/xl; charset=utf-8");

$date = gmdate("Y-m-d\TH:i:s\Z");
$host = "https://" . $_SERVER['HTTP_HOST'];

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

<?php
$categories = $this->db->query("
    SELECT 
        wlcat.category_id,
        wlcat.updated_at,
        wlmt.page_url as meta_url
    FROM wl_categories wlcat
    LEFT JOIN wl_meta_tags wlmt 
        ON wlmt.entity_id = wlcat.category_id
        AND wlmt.is_fixed = 'C'
    WHERE wlcat.status = 'A'
")->result_array();

foreach ($categories as $cat) {

    if (empty($cat['meta_url'])) continue;

    $lastmod = !empty($cat['updated_at']) 
        ? gmdate("Y-m-d\TH:i:s\Z", strtotime($cat['updated_at'])) 
        : $date;

    $url = $host . '/' . $cat['meta_url'];
?>

    <url>
        <loc><?= htmlspecialchars($url, ENT_XML1, 'UTF-8'); ?></loc>
        <lastmod><?= $lastmod; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>

<?php } ?>

</urlset>