diff --git a/config.dist.php b/config.dist.php index e29c7a3..b4be3ae 100644 --- a/config.dist.php +++ b/config.dist.php @@ -11,6 +11,7 @@ return [ */ "general" => [ "language" => "en", + "timezone" => "UTC", "assets_url" => "/plugi-core/dist", "uploads_url" => "/uploads", ], diff --git a/extensions/plugi-ext-blog/README.md b/extensions/plugi-ext-blog/README.md index 47a9438..1b4d4b2 100644 --- a/extensions/plugi-ext-blog/README.md +++ b/extensions/plugi-ext-blog/README.md @@ -8,9 +8,11 @@ The blog extension provides Website Manager post and category management plus tw 2. Open Website Manager and apply the pending database definition update. Existing installations need this update to create or update the extension-owned post and category tables, including the optional featured-image field. 3. Add categories if useful, then create posts as drafts or publish them from the Blog section. +Set `general.timezone` in the root `config.php` to the site's IANA timezone (for example, `Europe/Berlin`); it defaults to `UTC` and controls blog publication input and display. Publication dates are stored as site-local `DATETIME` values, so existing installations should configure the timezone that matches their existing post dates. + ## Create the public pages -1. Create a regular Plugi page with the route `/blog` and add the **Published Posts** block. It lists published posts newest first; drafts are never shown. +1. Create a regular Plugi page with the route `/blog` and add the **Published Posts** block. It lists published posts newest first; drafts are never shown. Public search matches titles, slugs, and excerpts (at least three characters), not full post bodies. 2. Create another regular page with the route `/blog/{slug}` and add the **Blog Post** block. The core router supplies the named `slug` parameter to the block. 3. Apply the site’s normal page layout and theme to both pages. Their presentation remains controlled by the page builder and theme. diff --git a/extensions/plugi-ext-blog/admin/Classes/PostsRepository.php b/extensions/plugi-ext-blog/admin/Classes/PostsRepository.php index a600ae5..bc64e15 100644 --- a/extensions/plugi-ext-blog/admin/Classes/PostsRepository.php +++ b/extensions/plugi-ext-blog/admin/Classes/PostsRepository.php @@ -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.'); } diff --git a/extensions/plugi-ext-blog/admin/Views/EditPost.php b/extensions/plugi-ext-blog/admin/Views/EditPost.php index 65d033b..5d5ac41 100644 --- a/extensions/plugi-ext-blog/admin/Views/EditPost.php +++ b/extensions/plugi-ext-blog/admin/Views/EditPost.php @@ -5,6 +5,7 @@ use Plugi\Extensions\Blog\PostsRepository; use Plugi\Repositories\UploadRepository; $postsRepository = new PostsRepository(); +$timezone = new \DateTimeZone((string) (phpb_config('general.timezone') ?: 'UTC')); $post = [ 'title' => '', 'slug' => '', @@ -19,7 +20,7 @@ if (isset($_GET['id']) && ctype_digit((string) $_GET['id'])) { $post = $postsRepository->findWithId($_GET['id']) ?? $post; } $isEditing = isset($post['id']); -$publishedAt = $post['published_at'] ? date('Y-m-d\TH:i', strtotime($post['published_at'])) : ''; +$publishedAt = $post['published_at'] ? (new \DateTimeImmutable($post['published_at'], $timezone))->format('Y-m-d\TH:i') : ''; $categories = (new CategoriesRepository())->getAllForAdmin(); $imageUploads = array_values(array_filter((new UploadRepository())->getAll(), static fn($file): bool => is_string($file->mime_type ?? null) && str_starts_with(strtolower($file->mime_type), 'image/'))); ?> @@ -147,7 +148,7 @@ $imageUploads = array_values(array_filter((new UploadRepository())->getAll(), st
Leave blank to use the current time when publishing. Future dates are not supported.
+Times use the site timezone (= phpb_e($timezone->getName()) ?>). Leave blank to use the current time when publishing. Future dates are not supported.