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
@@ -3,12 +3,14 @@
namespace Plugi\Extensions\Blog;
use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;
use Plugi\Repositories\BaseRepository;
class PostsRepository extends BaseRepository
{
public const POSTS_PER_PAGE = 10;
public const MIN_PUBLIC_SEARCH_LENGTH = 3;
protected $table = 'ext__blog_posts';
@@ -32,17 +34,6 @@ class PostsRepository extends BaseRepository
return $this->db->select("SELECT * FROM {$this->table} ORDER BY updated_at DESC, id DESC", []);
}
public function getPublishedForListing(): array
{
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
$uploadsTable = $_ENV['DATABASE_PREFIX'] . 'uploads';
$posts = $this->db->select(
"SELECT posts.*, categories.name AS category_name, featured_image.public_id AS featured_image_public_id, featured_image.original_file AS featured_image_original_file, featured_image.alt_text AS featured_image_alt_text FROM {$this->table} posts LEFT JOIN {$categoriesTable} categories ON categories.id = posts.category_id LEFT JOIN {$uploadsTable} featured_image ON featured_image.public_id = posts.featured_image_id AND LOWER(featured_image.mime_type) LIKE 'image/%' WHERE posts.status = ? ORDER BY posts.published_at DESC, posts.id DESC",
['published']
);
return array_map(fn(array $post): array => $this->resolveFeaturedImage($post), $posts);
}
public function getPageForAdmin(string $search, int $page): array
{
return $this->getPostPage($search, $page, false);
@@ -55,6 +46,11 @@ class PostsRepository extends BaseRepository
private function getPostPage(string $search, int $page, bool $publishedOnly): array
{
$search = trim($search);
if ($publishedOnly && $search !== '' && mb_strlen($search) < self::MIN_PUBLIC_SEARCH_LENGTH) {
return ['posts' => [], 'total' => 0];
}
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
$uploadsTable = $_ENV['DATABASE_PREFIX'] . 'uploads';
$from = $publishedOnly
@@ -68,9 +64,14 @@ class PostsRepository extends BaseRepository
$parameters[] = 'published';
}
if (trim($search) !== '') {
$filters[] = '(posts.title LIKE ? ESCAPE \'!\' OR posts.slug LIKE ? ESCAPE \'!\' OR posts.excerpt LIKE ? ESCAPE \'!\' OR posts.body LIKE ? ESCAPE \'!\')';
$filters[] = $publishedOnly
? '(posts.title LIKE ? ESCAPE \'!\' OR posts.slug LIKE ? ESCAPE \'!\' OR posts.excerpt LIKE ? ESCAPE \'!\')'
: '(posts.title LIKE ? ESCAPE \'!\' OR posts.slug LIKE ? ESCAPE \'!\' OR posts.excerpt LIKE ? ESCAPE \'!\' OR posts.body LIKE ? ESCAPE \'!\')';
$pattern = '%' . str_replace(['!', '%', '_'], ['!!', '!%', '!_'], trim($search)) . '%';
array_push($parameters, $pattern, $pattern, $pattern, $pattern);
array_push($parameters, $pattern, $pattern, $pattern);
if (!$publishedOnly) {
$parameters[] = $pattern;
}
}
$where = $filters === [] ? '' : ' WHERE ' . implode(' AND ', $filters);
@@ -194,8 +195,9 @@ class PostsRepository extends BaseRepository
private function validatedPublicationDate(mixed $value, string $status): ?string
{
$timezone = new DateTimeZone((string) (phpb_config('general.timezone') ?: 'UTC'));
if ($value === null || $value === '') {
return $status === 'published' ? date('Y-m-d H:i:s') : null;
return $status === 'published' ? (new DateTimeImmutable('now', $timezone))->format('Y-m-d H:i:s') : null;
}
if (!is_string($value)) {
throw new InvalidArgumentException('Enter a valid publication date.');
@@ -203,12 +205,12 @@ class PostsRepository extends BaseRepository
$value = trim($value);
$format = str_contains($value, 'T') ? 'Y-m-d\\TH:i' : 'Y-m-d H:i:s';
$date = DateTimeImmutable::createFromFormat('!' . $format, $value);
$date = DateTimeImmutable::createFromFormat('!' . $format, $value, $timezone);
$errors = DateTimeImmutable::getLastErrors();
if ($date === false || ($errors !== false && ($errors['warning_count'] > 0 || $errors['error_count'] > 0)) || $date->format($format) !== $value) {
throw new InvalidArgumentException('Enter a valid publication date.');
}
if ($date > new DateTimeImmutable()) {
if ($date > new DateTimeImmutable('now', $timezone)) {
throw new InvalidArgumentException('Publication dates cannot be in the future.');
}