+
+
\ No newline at end of file
diff --git a/extensions/plugi-ext-blog/config.php b/extensions/plugi-ext-blog/config.php
new file mode 100644
index 0000000..132045b
--- /dev/null
+++ b/extensions/plugi-ext-blog/config.php
@@ -0,0 +1,38 @@
+ 'Blog',
+ 'author' => 'Paul Spenke',
+ 'identifier' => 'blog',
+ 'licence' => 'MIT',
+ 'version' => '1.0.0',
+ 'tables' => [
+ 'posts' => [
+ 'columns' => [
+ 'title' => 'varchar(255) NOT NULL',
+ 'slug' => 'varchar(191) NOT NULL',
+ 'excerpt' => 'text NOT NULL',
+ 'body' => 'longtext NOT NULL',
+ 'category_id' => 'int NULL',
+ 'status' => "varchar(20) NOT NULL DEFAULT 'draft'",
+ 'published_at' => 'datetime NULL',
+ 'created_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP',
+ 'updated_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
+ ],
+ 'uniqueKeys' => [
+ 'slug' => ['slug'],
+ ],
+ ],
+ 'categories' => [
+ 'columns' => [
+ 'name' => 'varchar(255) NOT NULL',
+ 'slug' => 'varchar(191) NOT NULL',
+ 'created_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP',
+ 'updated_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
+ ],
+ 'uniqueKeys' => [
+ 'slug' => ['slug'],
+ ],
+ ],
+ ],
+];
\ No newline at end of file
diff --git a/extensions/plugi-ext-blog/extension.php b/extensions/plugi-ext-blog/extension.php
new file mode 100644
index 0000000..cd4a0a4
--- /dev/null
+++ b/extensions/plugi-ext-blog/extension.php
@@ -0,0 +1,21 @@
+ __DIR__ . '/routes/savePost.php',
+ '/ext--blog/delete-post' => __DIR__ . '/routes/deletePost.php',
+ '/ext--blog/save-category' => __DIR__ . '/routes/saveCategory.php',
+ '/ext--blog/delete-category' => __DIR__ . '/routes/deleteCategory.php',
+]);
+
+Extensions::addBlocks([
+ 'post-list' => __DIR__ . '/blocks/post-list',
+ 'post-detail' => __DIR__ . '/blocks/post-detail',
+]);
+
+Extensions::registerBackend('blog', [
+ 'title' => 'Blog',
+ 'include' => __DIR__ . '/admin/blog.php',
+ 'icon' => '',
+]);
\ No newline at end of file
diff --git a/extensions/plugi-ext-blog/routes/deleteCategory.php b/extensions/plugi-ext-blog/routes/deleteCategory.php
new file mode 100644
index 0000000..e9cff9e
--- /dev/null
+++ b/extensions/plugi-ext-blog/routes/deleteCategory.php
@@ -0,0 +1,24 @@
+ 0) {
+ $deleted = (new \Plugi\Extensions\Blog\CategoriesRepository())->destroy((int) $_POST['id']);
+ }
+} catch (\Throwable) {
+ $deleted = false;
+}
+phpb_redirect(phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'categories']), [
+ 'message-type' => $deleted ? 'success' : 'error',
+ 'message' => $deleted ? 'Category deleted; associated posts are uncategorized.' : 'The category could not be deleted.',
+]);
\ No newline at end of file
diff --git a/extensions/plugi-ext-blog/routes/deletePost.php b/extensions/plugi-ext-blog/routes/deletePost.php
new file mode 100644
index 0000000..e3cef7f
--- /dev/null
+++ b/extensions/plugi-ext-blog/routes/deletePost.php
@@ -0,0 +1,24 @@
+ 0) {
+ $deleted = (new \Plugi\Extensions\Blog\PostsRepository())->destroy((int) $_POST['id']);
+ }
+} catch (\Throwable) {
+ $deleted = false;
+}
+phpb_redirect(phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'posts']), [
+ 'message-type' => $deleted ? 'success' : 'error',
+ 'message' => $deleted ? 'Post deleted.' : 'The post could not be deleted.',
+]);
\ No newline at end of file
diff --git a/extensions/plugi-ext-blog/routes/saveCategory.php b/extensions/plugi-ext-blog/routes/saveCategory.php
new file mode 100644
index 0000000..20d27a3
--- /dev/null
+++ b/extensions/plugi-ext-blog/routes/saveCategory.php
@@ -0,0 +1,30 @@
+ 'ext--blog', 'action' => 'categories']);
+$data = array_intersect_key($_POST, array_flip(['name', 'slug']));
+try {
+ if (isset($_POST['id']) && is_string($_POST['id']) && ctype_digit($_POST['id']) && (int) $_POST['id'] > 0) {
+ $saved = $categoriesRepository->update(['id' => (int) $_POST['id']], $data);
+ } elseif (!isset($_POST['id'])) {
+ $saved = $categoriesRepository->create($data);
+ } else {
+ $saved = false;
+ }
+ $message = $saved ? ['message-type' => 'success', 'message' => 'Category saved.'] : ['message-type' => 'error', 'message' => 'The category could not be saved.'];
+} catch (\InvalidArgumentException $exception) {
+ $message = ['message-type' => 'error', 'message' => $exception->getMessage()];
+} catch (\Throwable) {
+ $message = ['message-type' => 'error', 'message' => 'The category could not be saved. Check that the slug is unique.'];
+}
+phpb_redirect($redirectUrl, $message);
\ No newline at end of file
diff --git a/extensions/plugi-ext-blog/routes/savePost.php b/extensions/plugi-ext-blog/routes/savePost.php
new file mode 100644
index 0000000..12ac4f9
--- /dev/null
+++ b/extensions/plugi-ext-blog/routes/savePost.php
@@ -0,0 +1,31 @@
+ 'ext--blog', 'action' => 'posts']);
+$fields = ['title', 'slug', 'excerpt', 'body', 'category_id', 'status', 'published_at'];
+$data = array_intersect_key($_POST, array_flip($fields));
+try {
+ if (isset($_POST['id']) && is_string($_POST['id']) && ctype_digit($_POST['id']) && (int) $_POST['id'] > 0) {
+ $saved = $postsRepository->update(['id' => (int) $_POST['id']], $data);
+ } elseif (!isset($_POST['id'])) {
+ $saved = $postsRepository->create($data);
+ } else {
+ $saved = false;
+ }
+ $message = $saved ? ['message-type' => 'success', 'message' => 'Post saved.'] : ['message-type' => 'error', 'message' => 'The post could not be saved.'];
+} catch (\InvalidArgumentException $exception) {
+ $message = ['message-type' => 'error', 'message' => $exception->getMessage()];
+} catch (\Throwable) {
+ $message = ['message-type' => 'error', 'message' => 'The post could not be saved. Check that the slug is unique.'];
+}
+phpb_redirect($redirectUrl, $message);
\ No newline at end of file
diff --git a/extensions/plugi-ext-blog/tests/BlogRepositoriesTest.php b/extensions/plugi-ext-blog/tests/BlogRepositoriesTest.php
new file mode 100644
index 0000000..989f5b0
--- /dev/null
+++ b/extensions/plugi-ext-blog/tests/BlogRepositoriesTest.php
@@ -0,0 +1,239 @@
+pdo = new PDO('sqlite::memory:');
+ $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $this->databaseName = 'blog_test';
+ }
+
+ public function createSchema(): void
+ {
+ $this->query('CREATE TABLE test_ext__blog_posts (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ title VARCHAR(255) NOT NULL,
+ slug VARCHAR(191) NOT NULL UNIQUE,
+ excerpt TEXT NOT NULL,
+ body TEXT NOT NULL,
+ category_id INTEGER NULL,
+ status VARCHAR(20) NOT NULL,
+ published_at DATETIME NULL,
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
+ )');
+ $this->query('CREATE TABLE test_ext__blog_categories (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ name VARCHAR(255) NOT NULL,
+ slug VARCHAR(191) NOT NULL UNIQUE,
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
+ )');
+ }
+}
+
+class BlogTestTheme implements \Plugi\Contracts\ThemeContract
+{
+ public function getThemeBlocks()
+ {
+ return [];
+ }
+
+ public function getThemeLayouts()
+ {
+ return [];
+ }
+
+ public function getFolder()
+ {
+ return '';
+ }
+}
+
+function assertBlog(bool $condition, string $message): void
+{
+ if (!$condition) {
+ throw new RuntimeException($message);
+ }
+}
+
+function expectBlogValidation(callable $callback, string $message): void
+{
+ try {
+ $callback();
+ } catch (InvalidArgumentException) {
+ return;
+ }
+ throw new RuntimeException($message);
+}
+
+function renderBlogBlock(string $slug): string
+{
+ $theme = new BlogTestTheme();
+ $page = new \Plugi\Page();
+ $themeBlock = new \Plugi\ThemeBlock($theme, \Plugi\Extensions::getBlock($slug), true, $slug);
+ return (new \Plugi\Modules\GrapesJS\Block\BlockRenderer($theme, $page))->render($themeBlock);
+}
+
+function renderBlogListing(): string
+{
+ return renderBlogBlock('post-list');
+}
+
+function renderBlogDetail(?string $slug): string
+{
+ $GLOBALS['phpb_route_parameters'] = $slug === null ? [] : ['slug' => $slug];
+ return renderBlogBlock('post-detail');
+}
+
+$_ENV['DATABASE_PREFIX'] = 'test_';
+$phpb_db = new BlogTestDatabase();
+$phpb_db->createSchema();
+assertBlog(isset($phpb_db->getDBDefinition()['ext__blog_posts'], $phpb_db->getDBDefinition()['ext__blog_categories']), 'The blog tables should be included in the core database definition.');
+assertBlog(isset(\Plugi\Extensions::getBackends()['ext--blog']), 'The blog admin backend should be registered.');
+assertBlog(count(array_intersect(['/ext--blog/save-post', '/ext--blog/delete-post', '/ext--blog/save-category', '/ext--blog/delete-category'], array_keys(\Plugi\Extensions::getRoutes()))) === 4, 'Blog CRUD routes should be registered.');
+assertBlog(\Plugi\Extensions::getBlock('post-list') === dirname(__DIR__) . '/blocks/post-list', 'The published-post block should be registered.');
+assertBlog((require dirname(__DIR__) . '/blocks/post-list/config.php')['cache'] === false, 'The listing block should avoid stale cached post data.');
+assertBlog(\Plugi\Extensions::getBlock('post-detail') === dirname(__DIR__) . '/blocks/post-detail', 'The post-detail block should be registered.');
+assertBlog((require dirname(__DIR__) . '/blocks/post-detail/config.php')['cache'] === false, 'The detail block should avoid stale route-specific output.');
+$posts = new PostsRepository();
+$categories = new CategoriesRepository();
+$category = $categories->create(['name' => 'News', 'slug' => 'news']);
+assertBlog(is_array($category) && (int) $category['id'] > 0, 'Category creation should return the new category.');
+
+$post = $posts->create([
+ 'title' => 'First post',
+ 'slug' => 'first-post',
+ 'excerpt' => 'A short summary',
+ 'body' => 'Post body',
+ 'category_id' => $category['id'],
+ 'status' => 'published',
+ 'published_at' => '',
+]);
+assertBlog(is_array($post) && $post['status'] === 'published', 'A valid published post should be created.');
+assertBlog($post['published_at'] !== null, 'Publishing without a date should set the current publication timestamp.');
+
+$olderPost = $posts->create([
+ 'title' => 'Older published post',
+ 'slug' => 'older-published-post',
+ 'excerpt' => 'Older excerpt',
+ 'body' => '&safe',
+ 'category_id' => '',
+ 'status' => 'published',
+ 'published_at' => '2020-01-01T00:00',
+]);
+$pastPublication = date('Y-m-d\\TH:i', time() - 3600);
+$datedPost = $posts->create([
+ 'title' => 'Dated draft',
+ 'slug' => 'dated-draft',
+ 'excerpt' => '',
+ 'body' => " Draft body\n",
+ 'category_id' => '',
+ 'status' => 'draft',
+ 'published_at' => $pastPublication,
+]);
+$secondPost = $posts->create([
+ 'title' => 'Second post',
+ 'slug' => 'second-post',
+ 'excerpt' => '',
+ 'body' => '',
+ 'category_id' => $category['id'],
+ 'status' => 'draft',
+ 'published_at' => '',
+]);
+$publishedPosts = $posts->getPublishedForListing();
+assertBlog(count($publishedPosts) === 2, 'The public listing should exclude drafts.');
+assertBlog($publishedPosts[0]['slug'] === $post['slug'] && $publishedPosts[0]['category_name'] === 'News', 'Published posts should be newest first and include their category name.');
+assertBlog($publishedPosts[1]['slug'] === $olderPost['slug'], 'Older published posts should follow newer posts.');
+
+$_ENV['BASE_URL'] = 'https://blog.example';
+$listingHtml = renderBlogListing();
+assertBlog(str_contains($listingHtml, 'href="https://blog.example/blog/first-post"'), 'The listing should link to the configured post detail URL.');
+assertBlog(strpos($listingHtml, 'first-post') < strpos($listingHtml, 'older-published-post'), 'The rendered listing should preserve newest-first order.');
+assertBlog(!str_contains($listingHtml, 'dated-draft') && !str_contains($listingHtml, 'second-post'), 'Draft posts should not appear in the rendered listing.');
+$detailHtml = renderBlogDetail('older-published-post');
+assertBlog(str_contains($detailHtml, '
Older published post
'), 'The detail block should resolve a published post from the named slug route parameter.');
+assertBlog(str_contains($detailHtml, '<script>alert("x")</script>&safe') && !str_contains($detailHtml, '