📁 File Manager
Editing:
/home/wvl7mof7402e/www/chem/admin/add_blog.php
<?php /** * Pure Chemical Admin * File: admin/add_blog.php * Modern Add Blog Post UI */ if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); } require_once __DIR__ . '/includes/db.php'; foreach (['auth.php', 'functions.php', 'csrf.php'] as $file) { $path = __DIR__ . '/includes/' . $file; if (is_file($path)) { require_once $path; } } if (function_exists('requireAdmin')) { requireAdmin(); } if (!isset($pdo) || !$pdo instanceof PDO) { http_response_code(500); exit('PDO database connection not found.'); } if (!function_exists('e')) { function e(mixed $value): string { return htmlspecialchars( (string)$value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' ); } } if (!function_exists('csrf_field')) { function csrf_field(): string { if (function_exists('csrf_token')) { $token = csrf_token(); } else { if (empty($_SESSION['_csrf_token'])) { $_SESSION['_csrf_token'] = bin2hex(random_bytes(32)); } $token = $_SESSION['_csrf_token']; } return '<input type="hidden" name="_csrf" value="' . e($token) . '">'; } } function pc_verify_csrf(): bool { if (function_exists('csrf_verify')) { return csrf_verify() !== false; } $submitted = (string)($_POST['_csrf'] ?? ''); $stored = (string)($_SESSION['_csrf_token'] ?? ''); return $submitted !== '' && $stored !== '' && hash_equals($stored, $submitted); } function pc_column_exists(PDO $pdo, string $table, string $column): bool { $stmt = $pdo->prepare( 'SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?' ); $stmt->execute([$table, $column]); return (int)$stmt->fetchColumn() > 0; } function pc_table_exists(PDO $pdo, string $table): bool { $stmt = $pdo->prepare( 'SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ?' ); $stmt->execute([$table]); return (int)$stmt->fetchColumn() > 0; } function pc_slug(string $value): string { $value = trim($value); if (function_exists('iconv')) { $converted = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value); if ($converted !== false) { $value = $converted; } } $value = strtolower($value); $value = preg_replace('/[^a-z0-9]+/', '-', $value) ?? ''; $value = trim($value, '-'); return $value !== '' ? $value : 'blog-' . bin2hex(random_bytes(5)); } function pc_datetime(?string $value): string|false|null { $value = trim((string)$value); if ($value === '') { return null; } $date = DateTime::createFromFormat('!Y-m-d\TH:i', $value); $errors = DateTime::getLastErrors(); if ( !$date || ($errors !== false && ($errors['warning_count'] > 0 || $errors['error_count'] > 0)) || $date->format('Y-m-d\TH:i') !== $value ) { return false; } return $date->format('Y-m-d H:i:s'); } function pc_enum(PDO $pdo, string $table, string $column, array $fallback): array { $stmt = $pdo->query("SHOW COLUMNS FROM `$table`"); $columns = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($columns as $item) { if (($item['Field'] ?? '') !== $column) { continue; } $type = (string)($item['Type'] ?? ''); if (preg_match('/^enum\((.*)\)$/i', $type, $match)) { preg_match_all("/'((?:[^'\\\\]|\\\\.|'')*)'/", $match[1], $matches); if (!empty($matches[1])) { return array_map( static fn($v) => str_replace("''", "'", $v), $matches[1] ); } } } return $fallback; } $adminName = $_SESSION['ADMIN_NAME'] ?? $_SESSION['admin_name'] ?? $_SESSION['name'] ?? 'Administrator'; $errors = []; $form = [ 'title' => '', 'slug' => '', 'category_id' => '', 'author_id' => '', 'excerpt' => '', 'content' => '', 'featured_image_id' => '', 'video_url' => '', 'status' => 'draft', 'featured' => 0, 'published_at' => '', ]; $statuses = pc_enum( $pdo, 'blog_posts', 'status', ['draft', 'published', 'inactive'] ); $categories = []; $authors = []; $mediaItems = []; if (pc_table_exists($pdo, 'blog_categories')) { $categoryColumn = null; foreach (['name', 'title', 'category_name'] as $candidate) { if (pc_column_exists($pdo, 'blog_categories', $candidate)) { $categoryColumn = $candidate; break; } } if ($categoryColumn !== null) { $categories = $pdo->query( "SELECT id, `$categoryColumn` AS label FROM blog_categories ORDER BY `$categoryColumn`" )->fetchAll(PDO::FETCH_ASSOC); } } if (pc_table_exists($pdo, 'users')) { $authorColumn = null; foreach (['name', 'full_name', 'username', 'email'] as $candidate) { if (pc_column_exists($pdo, 'users', $candidate)) { $authorColumn = $candidate; break; } } if ($authorColumn !== null) { $authors = $pdo->query( "SELECT id, `$authorColumn` AS label FROM users ORDER BY `$authorColumn`" )->fetchAll(PDO::FETCH_ASSOC); } } /* Existing active image records from the media library */ if ( pc_table_exists($pdo, 'media') && pc_column_exists($pdo, 'media', 'id') && pc_column_exists($pdo, 'media', 'file_path') && pc_column_exists($pdo, 'media', 'mime_type') ) { $hasStatus = pc_column_exists($pdo, 'media', 'status'); $hasType = pc_column_exists($pdo, 'media', 'file_type'); $where = []; if ($hasStatus) { $where[] = 'status = 1'; } if ($hasType) { $where[] = "file_type = 'image'"; } else { $where[] = "mime_type LIKE 'image/%'"; } $mediaItems = $pdo->query( 'SELECT id, file_path, mime_type' . (pc_column_exists($pdo, 'media', 'alt_text') ? ', alt_text' : '') . ' FROM media WHERE ' . implode(' AND ', $where) . ' ORDER BY id DESC LIMIT 100' )->fetchAll(PDO::FETCH_ASSOC); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { foreach ($form as $key => $default) { if ($key === 'featured') { $form[$key] = isset($_POST[$key]) ? 1 : 0; } else { $form[$key] = trim((string)($_POST[$key] ?? $default)); } } if (!pc_verify_csrf()) { $errors[] = 'Security token invalid. Refresh the page and try again.'; } if ($form['title'] === '') { $errors[] = 'Blog title is required.'; } elseif (mb_strlen($form['title'], 'UTF-8') > 500) { $errors[] = 'Title must not exceed 500 characters.'; } $form['slug'] = pc_slug( $form['slug'] !== '' ? $form['slug'] : $form['title'] ); if (mb_strlen($form['slug'], 'UTF-8') > 500) { $errors[] = 'Slug must not exceed 500 characters.'; } if (!in_array($form['status'], $statuses, true)) { $errors[] = 'Invalid status selected.'; } foreach (['category_id', 'author_id', 'featured_image_id'] as $key) { if ($form[$key] !== '' && !ctype_digit($form[$key])) { $errors[] = 'Invalid value for ' . str_replace('_', ' ', $key) . '.'; } } $categoryId = $form['category_id'] !== '' ? (int)$form['category_id'] : null; $authorId = $form['author_id'] !== '' ? (int)$form['author_id'] : null; $imageId = $form['featured_image_id'] !== '' ? (int)$form['featured_image_id'] : null; if ($categoryId !== null) { $check = $pdo->prepare('SELECT id FROM blog_categories WHERE id = ?'); $check->execute([$categoryId]); if (!$check->fetchColumn()) { $errors[] = 'Selected category does not exist.'; } } if ($authorId !== null) { $check = $pdo->prepare('SELECT id FROM users WHERE id = ?'); $check->execute([$authorId]); if (!$check->fetchColumn()) { $errors[] = 'Selected author does not exist.'; } } if ($imageId !== null) { $check = $pdo->prepare( "SELECT id FROM media WHERE id = ? AND mime_type LIKE 'image/%'" . (pc_column_exists($pdo, 'media', 'status') ? ' AND status = 1' : '') . ' LIMIT 1' ); $check->execute([$imageId]); if (!$check->fetchColumn()) { $errors[] = 'Selected featured image is unavailable or invalid.'; } } if ($form['video_url'] !== '') { $url = filter_var($form['video_url'], FILTER_VALIDATE_URL); $scheme = strtolower((string)parse_url($form['video_url'], PHP_URL_SCHEME)); if (!$url || !in_array($scheme, ['http', 'https'], true)) { $errors[] = 'Enter a valid HTTP or HTTPS video URL.'; } elseif (mb_strlen($form['video_url'], 'UTF-8') > 1000) { $errors[] = 'Video URL must not exceed 1000 characters.'; } } $publishedAt = pc_datetime($form['published_at']); if ($publishedAt === false) { $errors[] = 'Invalid publishing date/time.'; } if ($form['status'] === 'published' && $publishedAt === null) { $publishedAt = date('Y-m-d H:i:s'); $form['published_at'] = date('Y-m-d\TH:i'); } if (empty($errors)) { $check = $pdo->prepare( 'SELECT id FROM blog_posts WHERE slug = ? LIMIT 1' ); $check->execute([$form['slug']]); if ($check->fetchColumn()) { $errors[] = 'This slug already exists. Choose a different slug.'; } } if (empty($errors)) { try { $pdo->beginTransaction(); $stmt = $pdo->prepare( 'INSERT INTO blog_posts (category_id, author_id, title, slug, excerpt, content, featured_image_id, video_url, status, featured, published_at, views) VALUES (:category_id, :author_id, :title, :slug, :excerpt, :content, :featured_image_id, :video_url, :status, :featured, :published_at, 0)' ); $stmt->execute([ ':category_id' => $categoryId, ':author_id' => $authorId, ':title' => $form['title'], ':slug' => $form['slug'], ':excerpt' => $form['excerpt'] !== '' ? $form['excerpt'] : null, ':content' => $form['content'] !== '' ? $form['content'] : null, ':featured_image_id' => $imageId, ':video_url' => $form['video_url'] !== '' ? $form['video_url'] : null, ':status' => $form['status'], ':featured' => $form['featured'], ':published_at' => $publishedAt, ]); $newId = (int)$pdo->lastInsertId(); $pdo->commit(); $_SESSION['success_message'] = 'Blog post created successfully.'; header('Location: blog_posts.php'); exit; } catch (Throwable $exception) { if ($pdo->inTransaction()) { $pdo->rollBack(); } error_log('Add blog failed: ' . $exception->getMessage()); $errors[] = 'Could not save this blog post. Check server logs.'; } } } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Add Blog Post | Pure Chemical Admin</title> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css"> <style> :root{--green:#116b4c;--green2:#084b37;--pale:#e9f7f0;--bg:#f4f7f5;--ink:#172821;--muted:#74837b;--line:#e3eae5;--red:#bd3343;--shadow:0 12px 38px #173f2b0b} *{box-sizing:border-box} body{margin:0;background:var(--bg);color:var(--ink);font:14px Inter,system-ui,sans-serif} button,input,textarea,select{font:inherit} a{text-decoration:none;color:inherit} .pc-page{margin-left:280px;min-height:100vh} .topbar{height:76px;position:sticky;top:0;z-index:20;background:#ffffffed;backdrop-filter:blur(15px);border-bottom:1px solid var(--line);display:flex;align-items:center;justify-content:space-between;padding:0 30px} .brand{display:flex;align-items:center;gap:12px} .brand-icon{width:43px;height:43px;border-radius:14px;display:grid;place-items:center;color:#fff;background:linear-gradient(135deg,#16835e,#075039);box-shadow:0 8px 18px #116b4c27} .brand strong{font-size:16px;display:block} .brand small{font-size:11px;color:var(--muted);display:block;margin-top:4px} .top-actions{display:flex;gap:10px;align-items:center} .btn{display:inline-flex;align-items:center;justify-content:center;gap:8px;padding:11px 15px;border:1px solid var(--line);border-radius:11px;background:white;color:#34473d;font-size:12px;font-weight:750;cursor:pointer;transition:.18s} .btn:hover{transform:translateY(-1px)} .btn-primary{background:var(--green);border-color:var(--green);color:#fff} .btn-primary:hover{background:var(--green2)} .content{max-width:1500px;margin:auto;padding:29px} .eyebrow{color:var(--green);font-size:10px;font-weight:800;text-transform:uppercase;letter-spacing:1.5px;margin-bottom:9px} .heading{display:flex;align-items:flex-end;justify-content:space-between;gap:15px;margin-bottom:23px} .heading h1{font-size:29px;letter-spacing:-1px;margin:0} .heading p{font-size:12px;color:var(--muted);margin:8px 0 0} .errors{padding:15px 18px;border-radius:13px;border:1px solid #f0c8ce;background:#fff1f2;color:#922b37;margin-bottom:20px;font-size:12px;line-height:1.7} .errors ul{margin:5px 0 0;padding-left:20px} .layout{display:grid;grid-template-columns:minmax(0,1fr) 320px;gap:20px;align-items:start} .card{background:white;border:1px solid var(--line);border-radius:17px;box-shadow:var(--shadow);overflow:hidden} .card+.card{margin-top:17px} .card-head{padding:19px 21px;border-bottom:1px solid var(--line);display:flex;align-items:center;gap:11px} .head-icon{width:37px;height:37px;border-radius:11px;display:grid;place-items:center;color:var(--green);background:var(--pale)} .card-head strong{font-size:13px;display:block} .card-head small{font-size:10px;color:var(--muted);display:block;margin-top:4px} .card-body{padding:22px} .grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:19px} .field{min-width:0} .full{grid-column:1/-1} label{display:block;font-size:11px;font-weight:750;margin-bottom:8px;color:#34453b} .req{color:var(--red)} .control{width:100%;height:44px;padding:0 12px;border:1px solid #dce5df;border-radius:10px;background:#fff;color:var(--ink);outline:none;transition:.2s} textarea.control{height:auto;min-height:115px;padding:12px;line-height:1.65;resize:vertical} textarea.content-control{min-height:310px} .control:focus{border-color:#58a586;box-shadow:0 0 0 4px #116b4c12} .help{font-size:10px;color:#89968f;line-height:1.5;margin-top:6px} .slug-wrap{position:relative} .slug-wrap input{padding-right:43px} .icon-button{position:absolute;right:6px;top:6px;width:32px;height:32px;border:0;border-radius:8px;background:var(--pale);color:var(--green);cursor:pointer} .counter{display:flex;justify-content:flex-end;font-size:10px;color:#8a9690;margin-top:5px} .image-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px;margin-top:13px} .image-choice{position:relative;border:2px solid var(--line);border-radius:11px;overflow:hidden;cursor:pointer;background:#f6f9f7;aspect-ratio:1/1} .image-choice input{position:absolute;opacity:0;pointer-events:none} .image-choice img{width:100%;height:100%;object-fit:cover;display:block} .image-choice .image-id{position:absolute;left:5px;bottom:5px;padding:4px 6px;border-radius:6px;background:#13271fd9;color:white;font-size:9px} .image-choice:has(input:checked){border-color:var(--green);box-shadow:0 0 0 2px #116b4c22} .media-empty{border:1px dashed #ccd9d1;padding:17px;border-radius:11px;font-size:11px;color:var(--muted);line-height:1.6} .media-empty a{color:var(--green);font-weight:800} .status-box{padding:14px;border:1px solid var(--line);background:#fafcfb;border-radius:12px} .status-label{font-size:10px;color:var(--muted);text-transform:uppercase;letter-spacing:1px;font-weight:800;margin-bottom:10px} .status-pill{display:inline-flex;align-items:center;gap:7px;padding:7px 10px;border-radius:99px;background:#fff;border:1px solid var(--line);font-size:11px;font-weight:800} .dot{width:7px;height:7px;border-radius:50%;background:#d99a20} .status-pill.published .dot{background:#198657} .status-pill.inactive .dot{background:#c44747} .note{margin-top:11px;background:#f5f8f6;border-radius:10px;padding:11px;font-size:10px;line-height:1.6;color:#6e7c73} .toggle{display:flex;align-items:center;justify-content:space-between;gap:12px;border:1px solid var(--line);border-radius:12px;padding:13px} .toggle strong{display:block;font-size:12px} .toggle small{display:block;color:var(--muted);font-size:10px;margin-top:4px} .switch{position:relative;width:43px;height:25px;flex:0 0 auto} .switch input{position:absolute;opacity:0;width:1px;height:1px} .switch span{position:absolute;inset:0;border-radius:99px;background:#ccd6d0;cursor:pointer;transition:.2s} .switch span:before{content:"";position:absolute;width:19px;height:19px;left:3px;top:3px;border-radius:50%;background:#fff;box-shadow:0 2px 5px #0002;transition:.2s} .switch input:checked+span{background:var(--green)} .switch input:checked+span:before{transform:translateX(18px)} .tips{padding:0;margin:0;list-style:none} .tips li{display:flex;gap:9px;padding:10px 0;color:#6e7d74;font-size:10px;line-height:1.6} .tips li+li{border-top:1px dashed var(--line)} .tips i{color:var(--green);margin-top:3px} .actions{display:flex;justify-content:flex-end;gap:10px;padding-top:20px;margin-top:21px;border-top:1px solid var(--line)} @media(max-width:1150px){.pc-page{margin-left:0}.layout{grid-template-columns:minmax(0,1fr) 280px}} @media(max-width:850px){.layout{grid-template-columns:1fr}.side-column{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:15px}.side-column .card+.card{margin-top:0}} @media(max-width:600px){.topbar{padding:12px 15px;height:auto;min-height:67px}.brand small{display:none}.brand strong{font-size:13px}.brand-icon{width:38px;height:38px}.top-actions .back-text{display:none}.content{padding:20px 13px}.heading h1{font-size:25px}.grid{grid-template-columns:1fr}.full{grid-column:auto}.card-body{padding:16px}.card-head{padding:16px}.side-column{grid-template-columns:1fr}.actions{flex-direction:column-reverse}.actions .btn{width:100%}.image-grid{grid-template-columns:repeat(3,minmax(0,1fr))}} </style> </head> <body> <?php if (is_file(__DIR__ . '/sidebar.php')) { require_once __DIR__ . '/sidebar.php'; } ?> <div class="pc-page"> <header class="topbar"> <div class="brand"> <div class="brand-icon"><i class="fa-solid fa-pen-nib"></i></div> <div> <strong>Pure Chemical</strong> <small>Admin / Blog Management</small> </div> </div> <div class="top-actions"> <a class="btn" href="blog_posts.php"> <i class="fa-solid fa-arrow-left"></i> <span class="back-text">All Blogs</span> </a> </div> </header> <main class="content"> <div class="heading"> <div> <div class="eyebrow"><i class="fa-solid fa-layer-group"></i> Content Studio</div> <h1>Create a new blog</h1> <p>Write, organize and publish content for your website.</p> </div> </div> <?php if ($errors): ?> <div class="errors"> <strong><i class="fa-solid fa-circle-exclamation"></i> Please review the following:</strong> <ul> <?php foreach ($errors as $error): ?> <li><?= e($error) ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <form method="post" id="blogForm"> <?= csrf_field() ?> <div class="layout"> <div class="main-column"> <section class="card"> <div class="card-head"> <div class="head-icon"><i class="fa-solid fa-file-lines"></i></div> <div><strong>Blog information</strong><small>Title, URL and article content</small></div> </div> <div class="card-body"> <div class="grid"> <div class="field full"> <label for="title">Blog title <span class="req">*</span></label> <input class="control" id="title" name="title" maxlength="500" required placeholder="Enter an engaging blog title" value="<?= e($form['title']) ?>"> <div class="counter"><span id="titleCount">0</span>/500</div> </div> <div class="field full"> <label for="slug">SEO-friendly URL slug <span class="req">*</span></label> <div class="slug-wrap"> <input class="control" id="slug" name="slug" maxlength="500" required placeholder="your-blog-url" value="<?= e($form['slug']) ?>"> <button class="icon-button" id="generateSlug" type="button" title="Generate slug"> <i class="fa-solid fa-wand-magic-sparkles"></i> </button> </div> <div class="help">Use lowercase letters, numbers and hyphens.</div> </div> <div class="field"> <label for="category_id">Category</label> <select class="control" id="category_id" name="category_id"> <option value="">Choose category</option> <?php foreach ($categories as $category): ?> <option value="<?= (int)$category['id'] ?>" <?= (string)$form['category_id'] === (string)$category['id'] ? 'selected' : '' ?>> <?= e($category['label']) ?> </option> <?php endforeach; ?> </select> </div> <div class="field"> <label for="author_id">Author</label> <select class="control" id="author_id" name="author_id"> <option value="">Choose author</option> <?php foreach ($authors as $author): ?> <option value="<?= (int)$author['id'] ?>" <?= (string)$form['author_id'] === (string)$author['id'] ? 'selected' : '' ?>> <?= e($author['label']) ?> </option> <?php endforeach; ?> </select> </div> <div class="field full"> <label for="excerpt">Short description</label> <textarea class="control" id="excerpt" name="excerpt" rows="4" placeholder="Write a short summary for blog cards and search results..."><?= e($form['excerpt']) ?></textarea> <div class="counter"><span id="excerptCount">0</span> characters</div> </div> <div class="field full"> <label for="content">Article content</label> <textarea class="control content-control" id="content" name="content" placeholder="Write your complete article here..."><?= e($form['content']) ?></textarea> <div class="help">This field stores the article text. Rich-text editing can be integrated separately if your project uses an editor.</div> </div> <div class="field full"> <label>Featured image</label> <?php if ($mediaItems): ?> <div class="media-empty"> Select an image from your existing media library. To upload new files, use the secure media manager. </div> <div class="image-grid"> <?php foreach ($mediaItems as $media): ?> <label class="image-choice" title="Media ID <?= (int)$media['id'] ?>"> <input type="radio" name="featured_image_id" value="<?= (int)$media['id'] ?>" <?= (string)$form['featured_image_id'] === (string)$media['id'] ? 'checked' : '' ?>> <img src="<?= e($media['file_path']) ?>" alt="<?= e($media['alt_text'] ?? '') ?>" loading="lazy"> <span class="image-id">#<?= (int)$media['id'] ?></span> </label> <?php endforeach; ?> </div> <div style="margin-top:12px"> <label for="manual_image_id">Or enter a media ID</label> <input class="control" id="manual_image_id" inputmode="numeric" placeholder="Media ID" value="<?= e($form['featured_image_id']) ?>"> <div class="help">Entering an ID selects the matching image when it is in the list.</div> </div> <?php else: ?> <div class="media-empty"> <i class="fa-regular fa-images"></i> No active image records were found in the media library. <br> <a href="media_manager.php">Open Media Library →</a> <div style="margin-top:12px"> <label for="manual_image_id">Existing image media ID</label> <input class="control" id="manual_image_id" inputmode="numeric" placeholder="Media ID" value="<?= e($form['featured_image_id']) ?>"> </div> </div> <?php endif; ?> </div> <div class="field full"> <label for="video_url">Video URL</label> <input class="control" id="video_url" name="video_url" type="url" maxlength="1000" placeholder="https://..." value="<?= e($form['video_url']) ?>"> </div> </div> </div> </section> </div> <aside class="side-column"> <section class="card"> <div class="card-head"> <div class="head-icon"><i class="fa-solid fa-paper-plane"></i></div> <div><strong>Publish settings</strong><small>Control visibility and timing</small></div> </div> <div class="card-body"> <div class="field"> <label for="status">Publication status</label> <select class="control" id="status" name="status"> <?php foreach ($statuses as $status): ?> <option value="<?= e($status) ?>" <?= $form['status'] === $status ? 'selected' : '' ?>> <?= e(ucfirst($status)) ?> </option> <?php endforeach; ?> </select> </div> <div class="status-box" style="margin-top:15px"> <div class="status-label">Current status</div> <span class="status-pill" id="statusPill"> <span class="dot"></span><span id="statusText"><?= e(ucfirst($form['status'])) ?></span> </span> <div class="note" id="statusNote">Your article status will be saved when you submit this form.</div> </div> <div class="field" style="margin-top:18px"> <label for="published_at">Publish date & time</label> <input class="control" type="datetime-local" id="published_at" name="published_at" value="<?= e($form['published_at']) ?>"> <div class="help">If left empty, the current time is used when publishing.</div> </div> <div class="toggle" style="margin-top:18px"> <div> <strong>Featured article</strong> <small>Highlight this blog on your website.</small> </div> <label class="switch" aria-label="Featured article"> <input type="checkbox" name="featured" value="1" <?= $form['featured'] ? 'checked' : '' ?>> <span></span> </label> </div> <div class="actions" style="flex-direction:column"> <button class="btn btn-primary" type="submit" style="width:100%" id="submitButton"> <i class="fa-solid fa-floppy-disk"></i> Save blog post </button> <a class="btn" href="blog_posts.php" style="width:100%">Cancel</a> </div> </div> </section> <section class="card"> <div class="card-head"> <div class="head-icon"><i class="fa-solid fa-lightbulb"></i></div> <div><strong>Content checklist</strong><small>Before saving your article</small></div> </div> <div class="card-body"> <ul class="tips"> <li><i class="fa-solid fa-check-circle"></i><span>Use a descriptive, readable blog title.</span></li> <li><i class="fa-solid fa-check-circle"></i><span>Keep the URL slug short and unique.</span></li> <li><i class="fa-solid fa-check-circle"></i><span>Add a useful short description.</span></li> <li><i class="fa-solid fa-check-circle"></i><span>Choose a relevant featured image.</span></li> <li><i class="fa-solid fa-check-circle"></i><span>Review the publication status before saving.</span></li> </ul> </div> </section> </aside> </div> </form> </main> </div> <script> (() => { const title = document.getElementById('title'); const slug = document.getElementById('slug'); const excerpt = document.getElementById('excerpt'); const status = document.getElementById('status'); const statusPill = document.getElementById('statusPill'); const statusText = document.getElementById('statusText'); const statusNote = document.getElementById('statusNote'); const manualImage = document.getElementById('manual_image_id'); const form = document.getElementById('blogForm'); const submit = document.getElementById('submitButton'); let slugEdited = slug.value.trim() !== ''; function makeSlug(value) { return value.normalize('NFD') .replace(/[\u0300-\u036f]/g, '') .toLowerCase() .trim() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, ''); } function updateCount(input, output, max = null) { const el = document.getElementById(output); if (!el) return; el.textContent = input.value.length; if (max) el.style.color = input.value.length >= max ? '#bd3343' : ''; } title.addEventListener('input', () => { updateCount(title, 'titleCount', 500); if (!slugEdited) slug.value = makeSlug(title.value); }); slug.addEventListener('input', () => { slugEdited = slug.value.trim() !== ''; }); document.getElementById('generateSlug').addEventListener('click', () => { slug.value = makeSlug(title.value); slugEdited = true; }); excerpt.addEventListener('input', () => updateCount(excerpt, 'excerptCount')); function updateStatus() { const value = status.value; statusText.textContent = value.charAt(0).toUpperCase() + value.slice(1); statusPill.className = 'status-pill ' + value; const messages = { draft: 'This post will be saved as a draft.', published: 'This post will be marked as published.', inactive: 'This post will be saved as inactive.' }; statusNote.textContent = messages[value] || 'Status will be saved with your post.'; } status.addEventListener('change', updateStatus); if (manualImage) { manualImage.addEventListener('input', () => { const radios = document.querySelectorAll('input[name="featured_image_id"][type="radio"]'); radios.forEach(radio => { radio.checked = radio.value === manualImage.value.trim(); }); }); } document.querySelectorAll('.image-choice input').forEach(radio => { radio.addEventListener('change', () => { if (manualImage) manualImage.value = radio.value; }); }); form.addEventListener('submit', () => { const checkedImage = document.querySelector('input[name="featured_image_id"][type="radio"]:checked'); if (checkedImage && manualImage) manualImage.value = checkedImage.value; if (submit) { submit.disabled = true; submit.innerHTML = '<i class="fa-solid fa-spinner fa-spin"></i> Saving blog...'; } }); updateCount(title, 'titleCount', 500); updateCount(excerpt, 'excerptCount'); updateStatus(); })(); </script> </body> </html>
Save
Cancel