$value) { if (str_starts_with($key, 'layout_field__')) { $fieldName = substr($key, strlen('layout_field__')); $layoutFields[$fieldName] = trim((string) $value); } } $_SESSION['preview'][$previewKey] = [ 'markdown' => $_POST['markdown'] ?? '', 'title' => trim($_POST['title'] ?? ''), 'date' => trim($_POST['date'] ?? ''), 'tags' => trim($_POST['tags'] ?? ''), 'description' => trim($_POST['description'] ?? ''), 'layout' => trim($_POST['layout'] ?? ''), 'layout_fields' => $layoutFields, 'editor_type' => $editorType, 'slug' => $slug, 'created_at' => time(), ]; $redirect = base_path() . '/admin/preview.php?type=' . urlencode($editorType); if ($slug !== '') { $redirect .= '&slug=' . urlencode($slug); } header('Location: ' . $redirect); exit; } $editorType = trim($_GET['type'] ?? 'post'); $slug = trim($_GET['slug'] ?? ''); $previewKey = $editorType . ':' . ($slug !== '' ? $slug : 'new'); $preview = $_SESSION['preview'][$previewKey] ?? null; $previewTime = $preview ? (int) ($preview['created_at'] ?? 0) : 0; $markdown = ''; $title = ''; $date = ''; $tags = ''; $description = ''; $useSnapshot = $preview !== null; if ($slug !== '') { if ($editorType === 'page') { $live = get_page_by_slug($slug, true); } else { $live = get_post_by_slug($slug, true); } if ($live) { $path = $live['path'] ?? ''; $fileTime = $path && is_file($path) ? (int) filemtime($path) : 0; if ($previewTime <= $fileTime) { $useSnapshot = false; $title = (string) ($live['title'] ?? ''); $date = (string) ($live['date'] ?? ''); $tags = is_array($live['tags'] ?? null) ? implode(', ', $live['tags']) : ''; $description = (string) ($live['description'] ?? ''); $markdown = (string) ($live['content'] ?? ''); } } } $layout = ''; $extraFields = []; if ($useSnapshot && $preview) { $markdown = $preview['markdown'] ?? ''; $title = trim($preview['title'] ?? ''); $date = trim($preview['date'] ?? ''); $tags = trim($preview['tags'] ?? ''); $description = trim($preview['description'] ?? ''); $layout = trim($preview['layout'] ?? ''); $extraFields = is_array($preview['layout_fields'] ?? null) ? $preview['layout_fields'] : []; $editorType = trim($preview['editor_type'] ?? $editorType); } elseif (!$useSnapshot && isset($live)) { $layout = (string) ($live['layout'] ?? ''); $knownKeys = ['title', 'slug', 'date', 'status', 'tags', 'description', 'content', 'layout', 'path', 'timestamp', 'categories']; foreach ($live as $k => $v) { if (!in_array($k, $knownKeys, true)) { $extraFields[$k] = (string) $v; } } } $config = load_config(); $fontStack = font_stack_css($config['theme']['font_stack'] ?? 'sans'); $pageTitle = $title !== '' ? $title : t('admin.editor.preview_title'); $metaDescription = $description; header('Cache-Control: no-store, no-cache, must-revalidate'); header('Pragma: no-cache'); if ($editorType === 'page') { $page = [ 'title' => $title !== '' ? $title : t('admin.editor.preview_title'), 'slug' => $slug, 'status' => 'draft', 'description' => $description, 'include_in_nav' => false, 'content' => $markdown, ]; require __DIR__ . '/../page.php'; exit; } $post = array_merge($extraFields, [ 'title' => $title !== '' ? $title : t('admin.editor.preview_title'), 'slug' => $slug, 'date' => $date, 'status' => 'draft', 'tags' => $tags !== '' ? array_map('trim', explode(',', $tags)) : [], 'description' => $description, 'content' => $markdown, 'layout' => $layout, ]); require __DIR__ . '/../post.php';