'', 'slug' => '', 'date' => current_site_datetime_for_storage($config), 'status' => 'draft', 'tags' => [], 'description' => '', 'content' => '', ]; $originalSlug = ''; if ($isEditing) { $existing = get_post_by_slug($slugParam, true); if ($existing !== null && $existing !== false) { $post = [ 'title' => $existing['title'] ?? '', 'slug' => $existing['slug'] ?? '', 'date' => $existing['date'] ?? current_site_datetime_for_storage($config), 'status' => $existing['status'] ?? 'draft', 'tags' => $existing['tags'] ?? [], 'description' => $existing['description'] ?? '', 'content' => $existing['content'] ?? '', 'layout' => $existing['layout'] ?? '', 'feature_image' => $existing['feature_image'] ?? '', ]; $originalSlug = $existing['slug'] ?? ''; $originalExisting = $existing; } else { $errors[] = t('admin.editor.error_not_found_post'); $isEditing = false; $originalExisting = null; } } else { $originalExisting = null; } if ($action === 'new') { $isEditing = false; $originalSlug = ''; $post['layout'] = trim($_GET['layout'] ?? ''); } $autosaveData = null; if ($isEditing && $originalSlug !== '') { $autosavePath = PUREBLOG_BASE_PATH . '/content/autosaves/post-' . $originalSlug . '.json'; if (is_file($autosavePath)) { $raw = file_get_contents($autosavePath); $decoded = $raw !== false ? json_decode($raw, true) : null; if (is_array($decoded)) { if (($decoded['content'] ?? '') !== $post['content'] || ($decoded['title'] ?? '') !== $post['title']) { $autosaveData = $decoded; } else { @unlink($autosavePath); } } } } if ($_SERVER['REQUEST_METHOD'] === 'POST' && !isset($_POST['admin_action_id'])) { $post['title'] = trim($_POST['title'] ?? ''); $post['slug'] = trim($_POST['slug'] ?? ''); $post['date'] = trim($_POST['date'] ?? ''); $post['status'] = trim($_POST['status'] ?? 'draft'); $post['content'] = trim($_POST['content'] ?? ''); $post['description'] = trim($_POST['description'] ?? ''); $post['layout'] = trim($_POST['post_layout'] ?? ''); $post['feature_image'] = trim($_POST['feature_image'] ?? ''); $tagsInput = trim($_POST['tags'] ?? ''); $post['tags'] = $tagsInput === '' ? [] : array_values(array_filter(array_map('trim', explode(',', $tagsInput)))); $originalSlug = trim($_POST['original_slug'] ?? ''); $originalStatus = trim($_POST['original_status'] ?? ''); $originalDate = trim($_POST['original_date'] ?? ''); $post['layout_fields'] = []; foreach ($_POST as $key => $value) { if (str_starts_with($key, 'layout_field__')) { $fieldName = substr($key, strlen('layout_field__')); $post['layout_fields'][$fieldName] = trim((string) $value); } } $post = apply_filter('on_filter_post', $post); if ($post['title'] === '') { $errors[] = t('admin.editor.error_title_required'); } elseif ($post['slug'] === '' && slugify($post['title']) === '') { $errors[] = t('admin.editor.error_empty_slug'); } if (!in_array($post['status'], ['draft', 'published', 'scheduled'], true)) { $errors[] = t('admin.editor.error_invalid_status'); } if (!$errors) { if ($post['status'] === 'published' && $originalStatus !== 'published') { $post['date'] = current_site_datetime_for_storage($config); } $saveError = ''; $saved = save_post( $post, $originalSlug === '' ? null : $originalSlug, $originalDate === '' ? null : $originalDate, $originalStatus === '' ? null : $originalStatus, $saveError ); if ($saved) { $redirectSlug = $post['slug'] === '' ? slugify($post['title']) : $post['slug']; $autosaveDir = PUREBLOG_BASE_PATH . '/content/autosaves'; @unlink($autosaveDir . '/post-' . $originalSlug . '.json'); @unlink($autosaveDir . '/post-' . $redirectSlug . '.json'); if (!empty($_POST['ajax_save'])) { header('Content-Type: application/json'); echo json_encode(['saved' => true, 'slug' => $redirectSlug, 'date' => $post['date']]); exit; } header('Location: ' . base_path() . '/admin/edit-post.php?slug=' . urlencode($redirectSlug) . '&saved=1'); exit; } $errors[] = $saveError !== '' ? $saveError : t('admin.editor.error_save_post'); } if (!empty($_POST['ajax_save'])) { header('Content-Type: application/json', true, 422); echo json_encode(['saved' => false, 'errors' => $errors]); exit; } } $imageFolder = ''; if ($post['slug'] !== '') { $imageFolder = __DIR__ . '/../content/images/' . $post['slug']; if (is_dir($imageFolder)) { $files = glob($imageFolder . '/*') ?: []; foreach ($files as $file) { if (is_file($file)) { $basename = basename($file); $altText = pathinfo($basename, PATHINFO_FILENAME) ?: 'image'; $url = base_path() . '/content/images/' . $post['slug'] . '/' . $basename; $images[] = [ 'filename' => $basename, 'markdown' => '![' . $altText . '](' . $url . ')', 'url' => $url, 'thumb_url' => get_image_thumb_url($post['slug'], $basename), ]; } } } } $allLayouts = get_layouts(); $currentLayout = trim($post['layout'] ?? ''); $layoutDef = null; foreach ($allLayouts as $l) { if ($l['name'] === $currentLayout) { $layoutDef = $l; break; } } $layoutFields = $layoutDef ? ($layoutDef['fields'] ?? []) : []; $hasCustomTitle = false; $hasCustomContent = false; if ($layoutFields) { foreach ($layoutFields as $field) { $name = (string) ($field['name'] ?? ''); if ($name === 'title') { $hasCustomTitle = true; } elseif ($name === 'content') { $hasCustomContent = true; } } } $adminTitle = t($isEditing ? 'admin.post_editor.edit_title' : 'admin.post_editor.new_title'); $codeMirror = 'markdown'; require __DIR__ . '/../includes/admin-head.php'; ?>