<?php
header("Content-Type: application/xml; charset=utf-8");

$posts = [];

foreach(glob(__DIR__.'/data/posts/*.json') as $file){
  $data = json_decode(file_get_contents($file), true);
  if(is_array($data)) $posts = array_merge($posts,$data);
}

/* SORT */
usort($posts, function($a,$b){
  return strtotime($b['date']) - strtotime($a['date']);
});

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>

<rss version="2.0">
<channel>

<title>Your News Site</title>
<link>https://smartstudyweb.com</link>
<description>Latest news updates</description>

<?php foreach(array_slice($posts,0,50) as $p): ?>
<item>
<title><?= htmlspecialchars($p['title']) ?></title>
<link>https://smartstudyweb.com/article.php?slug=<?= $p['slug'] ?></link>
<pubDate><?= date(DATE_RSS, strtotime($p['date'])) ?></pubDate>
<description><![CDATA[<?= substr(strip_tags($p['content']),0,200) ?>]]></description>
</item>
<?php endforeach; ?>

</channel>
</rss>