<?php
declare(strict_types=1);

header('Content-Type: application/xml; charset=UTF-8');

$base = 'https://shop.zaycwreedom.ru';
$urls = [
    ['loc' => $base . '/', 'priority' => '1.0'],
    ['loc' => $base . '/register.php', 'priority' => '0.9'],
    ['loc' => $base . '/blog/', 'priority' => '0.8'],
];

foreach (glob(__DIR__ . '/blog/articles/*.json') ?: [] as $file) {
    $a = json_decode((string)file_get_contents($file), true);
    if (!is_array($a) || empty($a['slug'])) {
        continue;
    }

    $urls[] = [
        'loc' => $base . '/blog/' . rawurlencode((string)$a['slug']) . '.php',
        'priority' => '0.7',
        'lastmod' => substr((string)($a['published_at'] ?? date('c')), 0, 10),
    ];
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($urls as $u): ?>
  <url>
    <loc><?= htmlspecialchars($u['loc'], ENT_XML1 | ENT_COMPAT, 'UTF-8') ?></loc>
    <?php if (!empty($u['lastmod'])): ?><lastmod><?= htmlspecialchars($u['lastmod'], ENT_XML1 | ENT_COMPAT, 'UTF-8') ?></lastmod><?php endif; ?>
    <priority><?= htmlspecialchars($u['priority'], ENT_XML1 | ENT_COMPAT, 'UTF-8') ?></priority>
  </url>
<?php endforeach; ?>
</urlset>
