feat(ext-blog): Add Markdown support and featured images to Plugi Blog Extension

Updated README.md to document Markdown rendering and image authoring capabilities. Added PostContentRenderer class for safe Markdown parsing with XSS protection. Updated PostsRepository to fetch and resolve featured image data from the uploads table. Added UI controls in EditPost.php for selecting images and inserting inline Markdown. Updated views to display featured images on post cards and detail pages.
This commit is contained in:
2026-12-02 10:20:36 +00:00
parent 32582e9279
commit e97d96c1f5
9 changed files with 336 additions and 17 deletions
@@ -35,10 +35,12 @@ class PostsRepository extends BaseRepository
public function getPublishedForListing(): array
{
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
return $this->db->select(
"SELECT posts.*, categories.name AS category_name FROM {$this->table} posts LEFT JOIN {$categoriesTable} categories ON categories.id = posts.category_id WHERE posts.status = ? ORDER BY posts.published_at DESC, posts.id DESC",
$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
@@ -54,8 +56,9 @@ class PostsRepository extends BaseRepository
private function getPostPage(string $search, int $page, bool $publishedOnly): array
{
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
$uploadsTable = $_ENV['DATABASE_PREFIX'] . 'uploads';
$from = $publishedOnly
? "FROM {$this->table} posts LEFT JOIN {$categoriesTable} categories ON categories.id = posts.category_id"
? "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/%'"
: "FROM {$this->table} posts";
$filters = [];
$parameters = [];
@@ -75,13 +78,16 @@ class PostsRepository extends BaseRepository
$page = min(max(1, $page), max(1, (int) ceil((int) $total / self::POSTS_PER_PAGE)));
$offset = ($page - 1) * self::POSTS_PER_PAGE;
$select = $publishedOnly
? 'SELECT posts.*, categories.name AS category_name'
? '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'
: 'SELECT posts.*';
$order = $publishedOnly ? 'posts.published_at DESC, posts.id DESC' : 'posts.updated_at DESC, posts.id DESC';
$posts = $this->db->select(
"{$select} {$from}{$where} ORDER BY {$order} LIMIT " . self::POSTS_PER_PAGE . " OFFSET {$offset}",
$parameters
);
if ($publishedOnly) {
$posts = array_map(fn(array $post): array => $this->resolveFeaturedImage($post), $posts);
}
return ['posts' => $posts, 'total' => (int) $total];
}
@@ -89,12 +95,13 @@ class PostsRepository extends BaseRepository
public function findPublishedBySlug(string $slug): ?array
{
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
$uploadsTable = $_ENV['DATABASE_PREFIX'] . 'uploads';
$posts = $this->db->select(
"SELECT posts.*, categories.name AS category_name FROM {$this->table} posts LEFT JOIN {$categoriesTable} categories ON categories.id = posts.category_id WHERE posts.slug = ? AND posts.status = ? LIMIT 1",
"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.slug = ? AND posts.status = ? LIMIT 1",
[$slug, 'published']
);
return $posts[0] ?? null;
return isset($posts[0]) ? $this->resolveFeaturedImage($posts[0]) : null;
}
public function clearCategory(int $categoryId): bool
@@ -137,6 +144,23 @@ class PostsRepository extends BaseRepository
$categoryId = (int) $categoryId;
}
$featuredImageId = $data['featured_image_id'] ?? '';
if ($featuredImageId === '' || $featuredImageId === null) {
$featuredImageId = null;
} elseif (!is_string($featuredImageId) || strlen($featuredImageId) > 50) {
throw new InvalidArgumentException('Choose an existing image upload.');
} else {
$uploadsTable = $_ENV['DATABASE_PREFIX'] . 'uploads';
$images = $this->db->select(
"SELECT public_id FROM {$uploadsTable} WHERE public_id = ? AND LOWER(mime_type) LIKE 'image/%' LIMIT 1",
[$featuredImageId]
);
if ($images === []) {
throw new InvalidArgumentException('Choose an existing image upload.');
}
$featuredImageId = $images[0]['public_id'];
}
$publishedAt = $this->validatedPublicationDate($data['published_at'] ?? '', $status);
return [
@@ -144,12 +168,30 @@ class PostsRepository extends BaseRepository
'slug' => $slug,
'excerpt' => $excerpt,
'body' => $body,
'featured_image_id' => $featuredImageId,
'category_id' => $categoryId,
'status' => $status,
'published_at' => $publishedAt,
];
}
private function resolveFeaturedImage(array $post): array
{
$publicId = $post['featured_image_public_id'] ?? null;
$originalFile = $post['featured_image_original_file'] ?? null;
if (is_string($publicId) && $publicId !== '' && is_string($originalFile) && $originalFile !== '') {
$image = new \Plugi\UploadedFile();
$image->public_id = $publicId;
$image->original_file = $originalFile;
$post['featured_image_url'] = $image->getUrl();
} else {
$post['featured_image_url'] = null;
$post['featured_image_alt_text'] = null;
}
unset($post['featured_image_public_id'], $post['featured_image_original_file']);
return $post;
}
private function validatedPublicationDate(mixed $value, string $status): ?string
{
if ($value === null || $value === '') {