WebOrbiton
v1.0.0.1

StocketBase

48 lines · 1.9 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/includes/config.php';
  6. require_once __DIR__ . '/includes/database.php';
  7. require_once __DIR__ . '/includes/site-front.php';
  8. ​
  9. $settings = SiteFront::settings();
  10. ​
  11. if ($settings['sitemap_rss_enabled'] !== '1') {
  12. http_response_code(404);
  13. exit;
  14. }
  15. ​
  16. $db = Database::site();
  17. $baseUrl = rtrim(Config::get('APP_URL', ''), '/') . Config::get('APP_BASE_PATH', '');
  18. ​
  19. $products = $db->query(
  20. "SELECT p.id, p.title, p.slug, p.excerpt, p.published_at FROM products p WHERE p.status = 'published' ORDER BY p.published_at DESC LIMIT 30"
  21. )->fetchAll();
  22. ​
  23. header('Content-Type: application/rss+xml; charset=utf-8');
  24. ​
  25. echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  26. echo '<rss version="2.0"><channel>' . "\n";
  27. echo '<title>' . htmlspecialchars($settings['site_name']) . '</title>' . "\n";
  28. $channelLanguage = Languages::current();
  29. $channelLink = $channelLanguage === '' ? $baseUrl . '/index.php' : rtrim((string) Config::get('APP_URL', ''), '/') . Languages::homeUrl($channelLanguage);
  30. echo '<link>' . htmlspecialchars($channelLink) . '</link>' . "\n";
  31. if ($channelLanguage !== '') {
  32. echo '<language>' . htmlspecialchars($channelLanguage) . '</language>' . "\n";
  33. }
  34. echo '<description>' . htmlspecialchars($settings['site_description']) . '</description>' . "\n";
  35. ​
  36. foreach ($products as $product) {
  37. $url = rtrim(Config::get('APP_URL', ''), '/') . SiteFront::productUrl($product['slug']);
  38. echo '<item>';
  39. echo '<title>' . htmlspecialchars($product['title']) . '</title>';
  40. echo '<link>' . htmlspecialchars($url) . '</link>';
  41. echo '<guid>' . htmlspecialchars($url) . '</guid>';
  42. echo '<description>' . htmlspecialchars($product['excerpt']) . '</description>';
  43. echo '<pubDate>' . htmlspecialchars(date(DATE_RSS, strtotime($product['published_at']))) . '</pubDate>';
  44. echo '</item>' . "\n";
  45. }
  46. ​
  47. echo '</channel></rss>';
  48. ​