<?php
// /blog/feed.xml.php  → point a rewrite rule at this, or rename to feed.xml if your server allows PHP execution on .xml
require_once $_SERVER['DOCUMENT_ROOT'] . '/blog-cms/cms-config.php';
header('Content-Type: application/rss+xml; charset=UTF-8');

$db = cms_db();
$rows = $db->query(
    "SELECT slug, title, lead_paragraph, published_date, featured_image_path, updated_at
     FROM blogs WHERE status='published' ORDER BY published_date DESC LIMIT 30"
)->fetchAll();

function xesc($s) { return htmlspecialchars((string)$s, ENT_QUOTES | ENT_XML1, 'UTF-8'); }

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>The Propshop Blog</title>
  <link><?= SITE_URL . BLOG_BASE ?>/</link>
  <atom:link href="<?= SITE_URL . BLOG_BASE ?>/feed.xml" rel="self" type="application/rss+xml"/>
  <description>Trade show booth design, exhibit rentals, and event marketing insights.</description>
  <language>en-us</language>
  <lastBuildDate><?= date(DATE_RSS) ?></lastBuildDate>
<?php foreach ($rows as $r): ?>
  <item>
    <title><?= xesc($r['title']) ?></title>
    <link><?= SITE_URL . BLOG_BASE . '/' . $r['slug'] ?></link>
    <guid isPermaLink="true"><?= SITE_URL . BLOG_BASE . '/' . $r['slug'] ?></guid>
    <pubDate><?= date(DATE_RSS, strtotime($r['published_date'])) ?></pubDate>
    <description><?= xesc(mb_strimwidth(strip_tags($r['lead_paragraph'] ?? ''), 0, 300, '…')) ?></description>
    <?php if (!empty($r['featured_image_path'])): ?>
    <enclosure url="<?= xesc($r['featured_image_path']) ?>" type="image/webp"/>
    <?php endif; ?>
  </item>
<?php endforeach; ?>
</channel>
</rss>