<?php
// /feed.xml — RSS 2.0 feed
header('Content-Type: application/rss+xml; charset=utf-8');
require_once __DIR__ . '/includes/config.php';

if (page_cache_start('feed', 600)) exit;

$posts = db()->query("SELECT * FROM posts WHERE status='published' ORDER BY created_at DESC LIMIT 30")->fetchAll();

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
    <title><?= e(settings('site_name', SITE_NAME)) ?> — Blog</title>
    <link><?= SITE_URL ?>/blog</link>
    <atom:link href="<?= SITE_URL ?>/feed.xml" rel="self" type="application/rss+xml" />
    <description><?= e(settings('site_description', SITE_DESC)) ?></description>
    <language>tr-TR</language>
    <copyright>&#169; <?= date('Y') ?> <?= e(settings('site_name', SITE_NAME)) ?></copyright>
    <lastBuildDate><?= date('r') ?></lastBuildDate>
    <generator>RinLayer CMS</generator>
    <image>
        <url><?= SITE_URL ?>/assets/img/logo.png</url>
        <title><?= e(settings('site_name', SITE_NAME)) ?></title>
        <link><?= SITE_URL ?></link>
    </image>
<?php foreach ($posts as $p): ?>
    <item>
        <title><?= e($p['title']) ?></title>
        <link><?= SITE_URL ?>/blog/<?= e($p['slug']) ?></link>
        <guid isPermaLink="true"><?= SITE_URL ?>/blog/<?= e($p['slug']) ?></guid>
        <pubDate><?= date('r', strtotime($p['created_at'])) ?></pubDate>
        <?php if ($p['tags']): foreach (explode(',', $p['tags']) as $tag): ?>
        <category><?= e(trim($tag)) ?></category>
        <?php endforeach; endif; ?>
        <description><![CDATA[<?= $p['excerpt'] ?? truncate(strip_tags($p['content']), 300) ?>]]></description>
        <content:encoded><![CDATA[<?= $p['content'] ?>]]></content:encoded>
    </item>
<?php endforeach; ?>
</channel>
</rss>
<?php page_cache_end('feed', 600); ?>
