Add blog extension

This commit is contained in:
2026-12-01 14:04:08 +01:00
parent 6f02b0bc8d
commit a85d497087
19 changed files with 1005 additions and 2 deletions
@@ -0,0 +1,92 @@
<?php
use Plugi\Extensions\Blog\CategoriesRepository;
use Plugi\Extensions\Blog\PostsRepository;
$postsRepository = new PostsRepository();
$post = [
'title' => '',
'slug' => '',
'excerpt' => '',
'body' => '',
'category_id' => null,
'status' => 'draft',
'published_at' => null,
];
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'])) : '';
$categories = (new CategoriesRepository())->getAllForAdmin();
?>
<header class="mb-6 flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div class="flex items-center gap-3">
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'posts']) ?>" class="btn btn-ghost btn-square btn-sm" aria-label="Back to posts" title="Back to posts">
<i class="ph-duotone ph-arrow-left text-xl" aria-hidden="true"></i>
</a>
<div class="space-y-1">
<p class="text-xs font-semibold uppercase tracking-[0.18em] text-primary">Blog editor</p>
<h2 class="text-xl font-bold tracking-tight"><?= $isEditing ? 'Edit post' : 'New post' ?></h2>
<p class="text-sm text-base-content/60">Save as a draft or publish the article.</p>
</div>
</div>
</header>
<form action="/ext--blog/save-post" method="post" class="space-y-6">
<?php phpb_csrf_field(); ?>
<?php if ($isEditing): ?>
<input type="hidden" name="id" value="<?= phpb_e((string) $post['id']) ?>">
<?php endif; ?>
<section class="card border border-base-300 bg-base-100 shadow-sm">
<div class="card-body gap-5 p-4 sm:p-6 lg:p-8">
<div>
<label for="post-title" class="label"><span class="label-text font-medium">Title</span></label>
<input id="post-title" type="text" name="title" maxlength="255" required value="<?= phpb_e($post['title']) ?>" class="input w-full">
</div>
<div>
<label for="post-slug" class="label"><span class="label-text font-medium">Slug</span></label>
<input id="post-slug" type="text" name="slug" maxlength="191" pattern="[a-z0-9]+(-[a-z0-9]+)*" required value="<?= phpb_e($post['slug']) ?>" class="input w-full" aria-describedby="post-slug-help">
<p id="post-slug-help" class="mt-1 text-sm text-base-content/60">Use lowercase letters, numbers, and hyphens.</p>
</div>
<div>
<label for="post-excerpt" class="label"><span class="label-text font-medium">Excerpt</span></label>
<textarea id="post-excerpt" name="excerpt" rows="3" class="textarea w-full"><?= phpb_e($post['excerpt']) ?></textarea>
</div>
<div>
<label for="post-body" class="label"><span class="label-text font-medium">Body</span></label>
<textarea id="post-body" name="body" rows="12" class="textarea w-full" aria-describedby="post-body-help"><?= phpb_e($post['body']) ?></textarea>
<p id="post-body-help" class="mt-1 text-sm text-base-content/60">Body content is rendered as escaped plain text on the public page.</p>
</div>
<div class="grid gap-5 sm:grid-cols-2">
<div>
<label for="post-category" class="label"><span class="label-text font-medium">Category</span></label>
<select id="post-category" name="category_id" class="select w-full">
<option value="">No category</option>
<?php foreach ($categories as $category): ?>
<option value="<?= phpb_e((string) $category['id']) ?>" <?= (string) $category['id'] === (string) $post['category_id'] ? 'selected' : '' ?>><?= phpb_e($category['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="post-status" class="label"><span class="label-text font-medium">Status</span></label>
<select id="post-status" name="status" class="select w-full">
<option value="draft" <?= $post['status'] === 'draft' ? 'selected' : '' ?>>Draft</option>
<option value="published" <?= $post['status'] === 'published' ? 'selected' : '' ?>>Published</option>
</select>
</div>
</div>
<div>
<label for="post-published-at" class="label"><span class="label-text font-medium">Publication date</span></label>
<input id="post-published-at" type="datetime-local" name="published_at" value="<?= phpb_e($publishedAt) ?>" class="input w-full" aria-describedby="post-published-at-help">
<p id="post-published-at-help" class="mt-1 text-sm text-base-content/60">Leave blank to use the current time when publishing. Future dates are not supported.</p>
</div>
</div>
</section>
<div class="flex flex-wrap justify-end gap-3">
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'posts']) ?>" class="btn btn-ghost">Cancel</a>
<button type="submit" class="btn btn-primary">Save post</button>
</div>
</form>
@@ -0,0 +1,82 @@
<?php
use Plugi\Extensions\Blog\CategoriesRepository;
$categoriesRepository = new CategoriesRepository();
$categories = $categoriesRepository->getAllForAdmin();
$category = ['name' => '', 'slug' => ''];
if (isset($_GET['id']) && ctype_digit((string) $_GET['id'])) {
$category = $categoriesRepository->findWithId($_GET['id']) ?? $category;
}
$isEditing = isset($category['id']);
?>
<section class="space-y-5">
<header class="space-y-1">
<p class="text-xs font-semibold uppercase tracking-[0.18em] text-primary">Blog extension</p>
<h2 class="text-xl font-bold tracking-tight text-base-content">Categories</h2>
<p class="text-sm text-base-content/60">Organize posts with one optional category each.</p>
</header>
<form action="/ext--blog/save-category" method="post" class="card border border-base-300 bg-base-100 shadow-sm">
<?php phpb_csrf_field(); ?>
<?php if ($isEditing): ?>
<input type="hidden" name="id" value="<?= phpb_e((string) $category['id']) ?>">
<?php endif; ?>
<div class="card-body gap-4 p-4 sm:p-6">
<h3 class="card-title text-lg"><?= $isEditing ? 'Edit category' : 'New category' ?></h3>
<div class="grid gap-4 sm:grid-cols-2">
<div>
<label for="category-name" class="label"><span class="label-text font-medium">Name</span></label>
<input id="category-name" type="text" name="name" maxlength="255" required value="<?= phpb_e($category['name']) ?>" class="input w-full">
</div>
<div>
<label for="category-slug" class="label"><span class="label-text font-medium">Slug</span></label>
<input id="category-slug" type="text" name="slug" maxlength="191" pattern="[a-z0-9]+(-[a-z0-9]+)*" required value="<?= phpb_e($category['slug']) ?>" class="input w-full">
</div>
</div>
<div class="flex flex-wrap justify-end gap-3">
<?php if ($isEditing): ?>
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'categories']) ?>" class="btn btn-ghost">Cancel</a>
<?php endif; ?>
<button type="submit" class="btn btn-primary"><?= $isEditing ? 'Save category' : 'Add category' ?></button>
</div>
</div>
</form>
<?php if (empty($categories)): ?>
<div class="card border border-dashed border-base-300 bg-base-100 shadow-sm" role="status">
<div class="card-body items-center px-5 py-10 text-center">
<h3 class="card-title text-lg">No categories yet</h3>
<p class="text-sm text-base-content/60">Create a category above to make it available to posts.</p>
</div>
</div>
<?php else: ?>
<div class="overflow-x-auto rounded-box border border-base-300 bg-base-100">
<table class="table table-zebra w-full min-w-[32rem]">
<thead><tr><th>Name</th><th>Slug</th><th class="w-40 text-right">Actions</th></tr></thead>
<tbody>
<?php foreach ($categories as $item): ?>
<tr>
<td><?= phpb_e($item['name']) ?></td>
<td><span class="font-mono text-sm"><?= phpb_e($item['slug']) ?></span></td>
<td>
<div class="flex items-center justify-end gap-1">
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'categories', 'id' => $item['id']]) ?>" class="btn btn-ghost btn-square btn-sm" title="Edit category" aria-label="Edit <?= phpb_e($item['name']) ?>">
<i class="ph-duotone ph-pencil-line text-lg" aria-hidden="true"></i>
</a>
<form action="/ext--blog/delete-category" method="post" onsubmit="return confirm('Delete this category? Posts will remain uncategorized.')">
<?php phpb_csrf_field(); ?>
<input type="hidden" name="id" value="<?= phpb_e((string) $item['id']) ?>">
<button type="submit" class="btn btn-ghost btn-square btn-sm text-error" title="Delete category" aria-label="Delete <?= phpb_e($item['name']) ?>">
<i class="ph-duotone ph-trash text-lg" aria-hidden="true"></i>
</button>
</form>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</section>
@@ -0,0 +1,82 @@
<?php
use Plugi\Extensions\Blog\CategoriesRepository;
use Plugi\Extensions\Blog\PostsRepository;
$posts = (new PostsRepository())->getAllForAdmin();
$categories = (new CategoriesRepository())->getAllForAdmin();
$categoryNames = [];
foreach ($categories as $category) {
$categoryNames[$category['id']] = $category['name'];
}
?>
<section class="space-y-5">
<header class="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between">
<div class="space-y-1">
<p class="text-xs font-semibold uppercase tracking-[0.18em] text-primary">Blog extension</p>
<h2 class="text-xl font-bold tracking-tight text-base-content">Posts</h2>
<p class="text-sm text-base-content/60"><?= count($posts) ?> post<?= count($posts) !== 1 ? 's' : '' ?></p>
</div>
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'editPost']) ?>" class="btn btn-primary">
<i class="ph-duotone ph-plus" aria-hidden="true"></i>
New post
</a>
</header>
<?php if (empty($posts)): ?>
<div class="card border border-dashed border-base-300 bg-base-100 shadow-sm" role="status">
<div class="card-body items-center gap-3 px-5 py-12 text-center sm:py-16">
<span class="grid size-14 place-items-center rounded-2xl bg-primary/10 text-primary">
<i class="text-3xl ph-duotone ph-article" aria-hidden="true"></i>
</span>
<h3 class="card-title mt-1 text-lg">No posts yet</h3>
<p class="max-w-md text-sm leading-relaxed text-base-content/60">Create a draft or publish your first article.</p>
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'editPost']) ?>" class="btn btn-primary mt-2">
<i class="ph-duotone ph-plus" aria-hidden="true"></i>
Create your first post
</a>
</div>
</div>
<?php else: ?>
<div class="overflow-x-auto rounded-box border border-base-300 bg-base-100">
<table class="table table-zebra w-full min-w-[48rem]">
<thead>
<tr>
<th>Title</th>
<th>Category</th>
<th>Status</th>
<th>Published</th>
<th class="w-40 text-right">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post): ?>
<tr>
<td>
<div class="font-medium"><?= phpb_e($post['title']) ?></div>
<div class="text-xs text-base-content/60">/<?= phpb_e($post['slug']) ?></div>
</td>
<td><?= phpb_e($categoryNames[$post['category_id']] ?? '—') ?></td>
<td><span class="badge <?= $post['status'] === 'published' ? 'badge-success' : 'badge-warning' ?>"><?= phpb_e(ucfirst($post['status'])) ?></span></td>
<td><?= phpb_e($post['published_at'] ?? '—') ?></td>
<td>
<div class="flex items-center justify-end gap-1">
<a href="<?= phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'editPost', 'id' => $post['id']]) ?>" class="btn btn-ghost btn-square btn-sm" title="Edit post" aria-label="Edit <?= phpb_e($post['title']) ?>">
<i class="ph-duotone ph-pencil-line text-lg" aria-hidden="true"></i>
</a>
<form action="/ext--blog/delete-post" method="post" onsubmit="return confirm('Delete this post?')">
<?php phpb_csrf_field(); ?>
<input type="hidden" name="id" value="<?= phpb_e((string) $post['id']) ?>">
<button type="submit" class="btn btn-ghost btn-square btn-sm text-error" title="Delete post" aria-label="Delete <?= phpb_e($post['title']) ?>">
<i class="ph-duotone ph-trash text-lg" aria-hidden="true"></i>
</button>
</form>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</section>