v1.0.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- if (count(get_included_files()) === 1) {
- http_response_code(403);
- exit;
- }
-
- if (!Auth::hasRoleAtLeast(Auth::ROLE_STORE_MANAGER)) {
- exit;
- }
-
- $title = trim((string) ($_POST['title'] ?? ''));
- $pageId = (int) ($_POST['page_id'] ?? 0) ?: null;
- $status = ($_POST['status'] ?? 'draft') === 'published' ? 'published' : 'draft';
- $showInMenu = isset($_POST['show_in_menu']) ? 1 : 0;
- $blocksJson = BlockEditor::sanitize((string) ($_POST['blocks_json'] ?? '{"blocks":[]}'), true);
-
- if ($title === '') {
- exit;
- }
-
- require_once __DIR__ . '/../includes/slugger.php';
-
- $slug = Slugger::make($title);
- $db = Database::site();
-
- if ($pageId !== null) {
- $previousMenu = $db->prepare('SELECT show_in_menu FROM pages WHERE id = :id LIMIT 1');
- $previousMenu->execute(['id' => $pageId]);
- $wasInMenu = (int) $previousMenu->fetchColumn() === 1;
-
- $statement = $db->prepare(
- 'UPDATE pages SET title = :title, slug = :slug, content_blocks = :content, status = :status, show_in_menu = :show_in_menu WHERE id = :id'
- );
- $statement->execute([
- 'title' => $title,
- 'slug' => $slug,
- 'content' => $blocksJson,
- 'status' => $status,
- 'show_in_menu' => $showInMenu,
- 'id' => $pageId,
- ]);
-
- if ($showInMenu === 1 && !$wasInMenu) {
- SiteFront::appendToCustomMenu('page', $pageId);
- }
- } else {
- $statement = $db->prepare(
- 'INSERT INTO pages (author_id, title, slug, content_blocks, status, show_in_menu) VALUES (:author_id, :title, :slug, :content, :status, :show_in_menu)'
- );
- $statement->execute([
- 'author_id' => $currentUser['id'],
- 'title' => $title,
- 'slug' => $slug,
- 'content' => $blocksJson,
- 'status' => $status,
- 'show_in_menu' => $showInMenu,
- ]);
-
- if ($showInMenu === 1) {
- SiteFront::appendToCustomMenu('page', (int) $db->lastInsertId());
- }
- }
-