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.
87 lines
5.2 KiB
PHP
87 lines
5.2 KiB
PHP
<?php
|
|
|
|
$postsRepository = new \Plugi\Extensions\Blog\PostsRepository();
|
|
$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);
|
|
$totalPages = max(1, (int) ceil($pageResult['total'] / \Plugi\Extensions\Blog\PostsRepository::POSTS_PER_PAGE));
|
|
if ($page > $totalPages) {
|
|
$page = $totalPages;
|
|
}
|
|
$posts = $pageResult['posts'];
|
|
$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>
|
|
<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): ?>
|
|
<div class="alert alert-info" role="status">
|
|
<?= $search === '' ? 'No published posts yet.' : 'No published posts match "' . phpb_e($search) . '".' ?>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="grid gap-6 md:grid-cols-2">
|
|
<?php foreach ($posts as $post): ?>
|
|
<article class="card h-full border border-base-300 bg-base-100 shadow-sm">
|
|
<?php if (!empty($post['featured_image_url'])): ?>
|
|
<?php $featuredImageAlt = trim((string) ($post['featured_image_alt_text'] ?? '')) ?: $post['title']; ?>
|
|
<figure class="px-6 pt-6">
|
|
<img src="<?= phpb_e($post['featured_image_url']) ?>" alt="<?= phpb_e($featuredImageAlt) ?>" class="aspect-video w-full rounded-box object-cover">
|
|
</figure>
|
|
<?php endif; ?>
|
|
<div class="card-body gap-4">
|
|
<div class="flex flex-wrap items-center gap-3 text-sm text-base-content/60">
|
|
<?php if (!empty($post['category_name'])): ?>
|
|
<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 endif; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
<h2 class="card-title text-xl">
|
|
<a class="link link-hover" href="<?= phpb_e(phpb_full_url('/blog/' . rawurlencode($post['slug']))) ?>"><?= phpb_e($post['title']) ?></a>
|
|
</h2>
|
|
<?php if ($post['excerpt'] !== ''): ?>
|
|
<p class="whitespace-pre-line text-base-content/80"><?= phpb_e($post['excerpt']) ?></p>
|
|
<?php endif; ?>
|
|
<div class="card-actions mt-auto justify-end">
|
|
<a class="btn btn-ghost btn-sm" href="<?= phpb_e(phpb_full_url('/blog/' . rawurlencode($post['slug']))) ?>">
|
|
Read article<span class="sr-only">: <?= phpb_e($post['title']) ?></span>
|
|
<i class="ph ph-arrow-right" aria-hidden="true"></i>
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</article>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($totalPages > 1): ?>
|
|
<nav class="flex justify-center" aria-label="Blog post pages">
|
|
<div class="join">
|
|
<?php if ($page > 1): ?>
|
|
<a class="join-item btn btn-sm" rel="prev" href="<?= phpb_e($listingPath . '?' . http_build_query($searchParameters + ['page' => $page - 1])) ?>">Previous</a>
|
|
<?php else: ?>
|
|
<span class="join-item btn btn-sm btn-disabled" aria-disabled="true">Previous</span>
|
|
<?php endif; ?>
|
|
<span class="join-item btn btn-sm btn-disabled" aria-current="page">Page <?= phpb_e($page) ?> of <?= phpb_e($totalPages) ?></span>
|
|
<?php if ($page < $totalPages): ?>
|
|
<a class="join-item btn btn-sm" rel="next" href="<?= phpb_e($listingPath . '?' . http_build_query($searchParameters + ['page' => $page + 1])) ?>">Next</a>
|
|
<?php else: ?>
|
|
<span class="join-item btn btn-sm btn-disabled" aria-disabled="true">Next</span>
|
|
<?php endif; ?>
|
|
</div>
|
|
</nav>
|
|
<?php endif; ?>
|
|
</section>
|