feat(ext-blog): Add timezone support for blog publication dates and search

Configure `general.timezone` in root config to control blog publication input and display. Update PostsRepository to validate dates against the site timezone and enforce a minimum search length for public queries. Modify admin views to display publication dates in the site's local timezone and update search labels. Add tests for the new timezone handling and search validation logic.
This commit is contained in:
2026-12-02 12:08:45 +01:00
parent 8ebed89e73
commit 58e05e88ae
8 changed files with 56 additions and 37 deletions
@@ -1,12 +1,14 @@
<?php
$postsRepository = new \Plugi\Extensions\Blog\PostsRepository();
$timezone = new \DateTimeZone((string) (phpb_config('general.timezone') ?: 'UTC'));
$search = is_string($_GET['q'] ?? null) ? trim($_GET['q']) : '';
$pageValue = $_GET['page'] ?? null;
$page = is_string($pageValue) ? filter_var($pageValue, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) : false;
$page = $page === false ? 1 : $page;
$listingPath = isset($_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI']) ? phpb_full_url(phpb_current_relative_url(true)) : '/';
$pageResult = $postsRepository->getPageForListing($search, $page);
$searchTooShort = $search !== '' && mb_strlen($search) < \Plugi\Extensions\Blog\PostsRepository::MIN_PUBLIC_SEARCH_LENGTH;
$totalPages = max(1, (int) ceil($pageResult['total'] / \Plugi\Extensions\Blog\PostsRepository::POSTS_PER_PAGE));
if ($page > $totalPages) {
$page = $totalPages;
@@ -17,13 +19,15 @@ $searchParameters = $search === '' ? [] : ['q' => $search];
<section class="space-y-6" aria-label="Blog posts">
<form class="flex flex-col gap-3 sm:flex-row sm:items-end" method="get" action="<?= phpb_e($listingPath) ?>">
<label class="flex w-full max-w-md flex-col gap-2">
<span class="text-sm font-medium">Search posts</span>
<span class="text-sm font-medium">Search titles, slugs, and excerpts (3+ characters)</span>
<input class="input w-full" type="search" name="q" value="<?= phpb_e($search) ?>" />
</label>
<button class="btn" type="submit">Search</button>
</form>
<?php if ($pageResult['total'] === 0): ?>
<?php if ($searchTooShort): ?>
<div class="alert alert-info" role="status">Enter at least <?= \Plugi\Extensions\Blog\PostsRepository::MIN_PUBLIC_SEARCH_LENGTH ?> characters to search.</div>
<?php elseif ($pageResult['total'] === 0): ?>
<div class="alert alert-info" role="status">
<?= $search === '' ? 'No published posts yet.' : 'No published posts match &quot;' . phpb_e($search) . '&quot;.' ?>
</div>
@@ -43,9 +47,9 @@ $searchParameters = $search === '' ? [] : ['q' => $search];
<span class="badge badge-outline"><?= phpb_e($post['category_name']) ?></span>
<?php endif; ?>
<?php if (!empty($post['published_at'])): ?>
<?php $publishedTimestamp = strtotime($post['published_at']); ?>
<?php if ($publishedTimestamp !== false): ?>
<time datetime="<?= phpb_e(date('c', $publishedTimestamp)) ?>"><?= phpb_e(date('F j, Y', $publishedTimestamp)) ?></time>
<?php $publishedDate = date_create($post['published_at'], $timezone); ?>
<?php if ($publishedDate !== false): ?>
<time datetime="<?= phpb_e($publishedDate->format('c')) ?>"><?= phpb_e($publishedDate->format('F j, Y')) ?></time>
<?php endif; ?>
<?php endif; ?>
</div>