WebOrbiton
v1.0.0.8

Publisium

141 lines · 6.3 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/user-auth.php';
  8. require_once __DIR__ . '/includes/language.php';
  9. require_once __DIR__ . '/includes/site-front.php';
  10. require_once __DIR__ . '/includes/block-editor.php';
  11. require_once __DIR__ . '/includes/pagination.php';
  12. require_once __DIR__ . '/includes/article-card.php';
  13. require_once __DIR__ . '/includes/featured-banner.php';
  14. ​
  15. UserAuth::boot();
  16. ​
  17. $db = Database::site();
  18. $settings = SiteFront::settings();
  19. ​
  20. // With the short home address on, /index.php moves to / (and /pl/index.php to /pl/).
  21. $requestPath = (string) parse_url((string) ($_SERVER['REQUEST_URI'] ?? ''), PHP_URL_PATH);
  22. if (SiteFront::cleanHomeUrl() && ($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'GET' && str_ends_with($requestPath, '/index.php')) {
  23. $requestQuery = (string) parse_url((string) ($_SERVER['REQUEST_URI'] ?? ''), PHP_URL_QUERY);
  24. header('Location: ' . SiteFront::homeUrl() . ($requestQuery !== '' ? '?' . $requestQuery : ''), true, 301);
  25. exit;
  26. }
  27. ​
  28. $latestForBanner = $db->query(
  29. "SELECT id, title, slug, cover_image_path FROM articles a WHERE status = 'published'" . Languages::articleFilterSql('a') . " ORDER BY published_at DESC LIMIT 6"
  30. )->fetchAll();
  31. $latestForBanner = Languages::articles($latestForBanner);
  32. ​
  33. $featuredBannerArticles = Languages::articles(Languages::onlyTranslated(featuredBannerArticles($db, $settings)));
  34. $excludedIds = featuredExcludedIds($settings, $featuredBannerArticles);
  35. $excludeSql = (empty($excludedIds) ? '' : ' AND a.id NOT IN (' . implode(',', $excludedIds) . ')') . Languages::articleFilterSql('a');
  36. ​
  37. $currentPage = Pagination::currentPage();
  38. $totalArticles = (int) $db->query("SELECT COUNT(*) AS c FROM articles a WHERE a.status = 'published'" . $excludeSql)->fetch()['c'];
  39. $isStaticArticleLoading = ($settings['article_loading_mode'] ?? 'scroll') === 'static';
  40. $perPage = $isStaticArticleLoading
  41. ? max(1, $totalArticles)
  42. : max(1, (int) $settings['articles_per_page_home']);
  43. $currentPage = $isStaticArticleLoading ? 1 : $currentPage;
  44. $offset = Pagination::offset($currentPage, $perPage);
  45. $totalPages = Pagination::totalPages($totalArticles, $perPage);
  46. ​
  47. $featuredStatement = $db->prepare(
  48. "SELECT a.*, c.name AS category_name, c.slug AS category_slug FROM articles a LEFT JOIN categories c ON c.id = a.category_id WHERE a.status = 'published'" . $excludeSql . " ORDER BY a.published_at DESC LIMIT :limit OFFSET :offset"
  49. );
  50. $featuredStatement->bindValue('limit', $perPage, PDO::PARAM_INT);
  51. $featuredStatement->bindValue('offset', $offset, PDO::PARAM_INT);
  52. $featuredStatement->execute();
  53. $featuredArticles = Languages::articles($featuredStatement->fetchAll());
  54. ​
  55. SiteFront::setAdContext('home');
  56. $homepageSource = (string) ($settings['homepage_blocks'] ?? '');
  57. if (Languages::current() !== '') {
  58. $homeTranslation = Languages::translation('home', 1, Languages::current());
  59. if ($homeTranslation !== null && trim((string) $homeTranslation['title']) !== '' && trim((string) $homeTranslation['content_blocks']) !== '') {
  60. $homepageSource = (string) $homeTranslation['content_blocks'];
  61. }
  62. }
  63. $homepageBlocks = BlockEditor::homepageBlocks(['homepage_blocks' => $homepageSource]);
  64. $homeSections = [];
  65. foreach ($homepageBlocks as $homeBlock) {
  66. if (in_array($homeBlock['type'], BlockEditor::HOME_SYSTEM_TYPES, true)) {
  67. $homeSections[] = $homeBlock['type'];
  68. continue;
  69. }
  70. $lastSection = count($homeSections) - 1;
  71. if ($lastSection >= 0 && is_array($homeSections[$lastSection])) {
  72. $homeSections[$lastSection][] = $homeBlock;
  73. } else {
  74. $homeSections[] = [$homeBlock];
  75. }
  76. }
  77. ​
  78. if (in_array('contact_form', array_column($homepageBlocks, 'type'), true)) {
  79. header('Cache-Control: private, no-store');
  80. }
  81. ​
  82. $pageTitle = $settings['seo_title'] !== '' ? $settings['seo_title'] : $settings['site_name'];
  83. $pageDescription = $settings['seo_description'] !== '' ? $settings['seo_description'] : $settings['site_description'];
  84. $showBanner = true;
  85. $pageType = 'index';
  86. ​
  87. require __DIR__ . '/includes/front-header.php';
  88. ​
  89. ?>
  90. <?php if (!empty($settings['site_description'])): ?>
  91. <section class="hero-section">
  92. <div class="hero-eyebrow"><?= htmlspecialchars(Language::get('nav_home', 'Home')) ?></div>
  93. <h1 class="hero-title"><?= htmlspecialchars($settings['site_name']) ?></h1>
  94. <p class="hero-subtitle"><?= htmlspecialchars($settings['site_description']) ?></p>
  95. </section>
  96. <?php endif; ?>
  97. ​
  98. <?php foreach (SiteFront::activeAds('homepage-top') as $homepageTopAd): ?>
  99. <?= SiteFront::renderAd($homepageTopAd) ?>
  100. <?php endforeach; ?>
  101. ​
  102. <?php foreach ($homeSections as $homeSection): ?>
  103. <?php if (is_array($homeSection)): ?>
  104. <div class="article-content home-blocks">
  105. <?= BlockEditor::render(json_encode(['blocks' => $homeSection], JSON_UNESCAPED_UNICODE)) ?>
  106. </div>
  107. <?php elseif ($homeSection === 'home_featured'): ?>
  108. <?php if (($settings['featured_banner_enabled'] ?? '0') === '1'): ?>
  109. <div class="section-heading-row">
  110. <h2 class="section-heading"><?= htmlspecialchars(Language::get('home_featured', 'Featured')) ?></h2>
  111. </div>
  112. ​
  113. <?php if (!empty($featuredBannerArticles)): ?>
  114. <?= renderFeaturedBanner($featuredBannerArticles) ?>
  115. <?php endif; ?>
  116. <?php endif; ?>
  117. <?php elseif ($homeSection === 'home_articles'): ?>
  118. <div class="article-grid" id="article-list" data-pagination-source="home">
  119. <?php $inFeedAds = SiteFront::activeAds('in-feed'); ?>
  120. <?php foreach ($featuredArticles as $inFeedIndex => $article): ?>
  121. <?= renderArticleCard($article) ?>
  122. <?php if ($inFeedIndex === 2 && !empty($inFeedAds)): ?>
  123. <?php foreach ($inFeedAds as $inFeedAd): ?>
  124. <?= SiteFront::renderAd($inFeedAd) ?>
  125. <?php endforeach; ?>
  126. <?php endif; ?>
  127. <?php endforeach; ?>
  128. <?php if (empty($featuredArticles)): ?>
  129. <p><?= htmlspecialchars(Language::get('home_no_articles', 'No articles yet. Check back soon.')) ?></p>
  130. <?php endif; ?>
  131. </div>
  132. ​
  133. <div id="pagination-container">
  134. <?= $isStaticArticleLoading ? '' : Pagination::render($settings['pagination_style'], SiteFront::homeUrl(), $currentPage, $totalPages, [], 'article-list') ?>
  135. </div>
  136. <?php endif; ?>
  137. <?php endforeach; ?>
  138. ​
  139. <?= SiteFront::renderAds('homepage-bottom') ?>
  140. ​
  141. <?php require __DIR__ . '/includes/front-footer.php'; ?>