Files
plugi/extensions/plugi-ext-blog/routes/savePost.php
T
2026-12-02 10:20:36 +00:00

31 lines
1.3 KiB
PHP

<?php
phpb_requires_authenticated();
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
http_response_code(405);
exit();
}
if (!phpb_validate_csrf()) {
http_response_code(403);
exit();
}
$postsRepository = new \Plugi\Extensions\Blog\PostsRepository();
$redirectUrl = phpb_url('website_manager', ['tab' => '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);