PHP Classes

File: public/quick-post.php

Recommend this page to a friend!
  Packages of Adrian M   QuickWP   public/quick-post.php   Download  
File: public/quick-post.php
Role: Application script
Content type: text/plain
Description: Application script
Class: QuickWP
Manage WordPress sites using the REST API
Author: By
Last change: feat(i18n): add zero-dependency UI localization (EN/RO) and language switcher
v3.0-a: multi-site CLI, outbound webhooks (HMAC), Markdown parser, and cross-site sync
v2.4: editorial 2-column UI, HTML toolbar with live preview, visual Media Picker modal, and publish options
v2.2: refactor services, add retry/pagination, shared UI layout, PHP 8.3+, PHPStan L8, and CI
Date: 12 hours ago
Size: 14,199 bytes
 

 

Contents

Class file image Download
<?php /** * QuickWP - Create Post Controller */ require_once __DIR__ . '/partials/bootstrap.php'; use QuickWP\QuickWP; use QuickWP\Http\CsrfProtection; $message = null; $error = null; // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!CsrfProtection::validate()) { $error = $t->get('common.csrf_error'); } else { $title = trim($_POST['title'] ?? ''); $content = trim($_POST['content'] ?? ''); $excerpt = trim($_POST['excerpt'] ?? ''); $status = $_POST['status'] ?? 'draft'; $slug = trim($_POST['slug'] ?? ''); $categories = trim($_POST['categories'] ?? ''); $tags = trim($_POST['tags'] ?? ''); $featuredMedia = (int) ($_POST['featured_media'] ?? 0); $publishDate = trim($_POST['date'] ?? ''); $authorId = (int) ($_POST['author'] ?? 0); $commentStatus = $_POST['comment_status'] ?? ''; $sticky = !empty($_POST['sticky']); $metaJson = trim($_POST['meta_json'] ?? ''); $acfJson = trim($_POST['acf_json'] ?? ''); $template = trim($_POST['template'] ?? ''); if ($template === '__custom__') { $template = trim($_POST['template_custom'] ?? ''); } if ($title === '' || $content === '') { $error = $t->get('common.title_content_required'); } else { $data = [ 'title' => $title, 'content' => $content, 'status' => $status, ]; if ($excerpt !== '') $data['excerpt'] = $excerpt; if ($slug !== '') $data['slug'] = $slug; if ($categories !== '') $data['categories'] = $categories; if ($tags !== '') $data['tags'] = $tags; if ($featuredMedia > 0) $data['featured_media'] = $featuredMedia; if ($template !== '') $data['template'] = $template; if ($publishDate !== '') $data['date'] = strlen($publishDate) === 16 ? $publishDate . ':00' : $publishDate; if ($authorId > 0) $data['author'] = $authorId; if (in_array($commentStatus, ['open', 'closed'], true)) $data['comment_status'] = $commentStatus; if ($sticky) $data['sticky'] = true; if ($metaJson !== '') { $decoded = json_decode($metaJson, true); if (!is_array($decoded)) { $error = 'Custom Meta JSON is not valid JSON object.'; } else { $data['meta'] = $decoded; } } if ($error === null && $acfJson !== '') { $decoded = json_decode($acfJson, true); if (!is_array($decoded)) { $error = 'ACF JSON is not valid JSON object.'; } else { $data['acf'] = $decoded; } } if ($error === null) { $result = $qwp->posts()->create($data); if (QuickWP::isSuccess($result)) { $postId = QuickWP::getId($result); $postLink = QuickWP::getLink($result); if ($status === 'publish') { $qwp->webhooks()->dispatch('post.published', [ 'id' => $postId, 'title' => $title, 'link' => $postLink, 'site' => $siteKey, ]); } if ($postLink) { $message = 'Post created: <a href="' . htmlspecialchars($postLink, ENT_QUOTES, 'UTF-8') . '" target="_blank">View Post</a> (ID: ' . (int) $postId . ')'; } elseif ($postId) { $message = 'Post created. ID: ' . $postId; } else { $message = 'Post created successfully.'; } } else { $error = 'Error: ' . QuickWP::getError($result); } } } } } // Fetch authors list (graceful fallback if restricted) $authors = []; $usersResult = $qwp->users()->list(['per_page' => 50, '_fields' => 'id,name']); if (QuickWP::isSuccess($usersResult) && is_array($usersResult['json'])) { $authors = $usersResult['json']; } $postTemplates = $qwp->templates()->getPostTemplates(); $selectedTemplate = $_POST['template'] ?? ''; $selectedStatus = $_POST['status'] ?? 'draft'; $pageTitle = $t->get('post.heading'); $heading = $t->get('post.heading'); $activeNav = 'post'; $containerClass = 'wide'; $extraHead = '<script src="assets/taxonomy-picker.js"></script><script src="assets/editor.js"></script>'; require __DIR__ . '/partials/header.php'; ?> <?php if ($message): ?> <div class="message success"><?= $message ?></div> <?php endif; ?> <?php if ($error): ?> <div class="message error"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></div> <?php endif; ?> <form method="post"> <?= CsrfProtection::field() ?> <?php if ($siteKey !== ''): ?> <input type="hidden" name="site" value="<?= htmlspecialchars($siteKey, ENT_QUOTES, 'UTF-8') ?>"> <?php endif; ?> <div class="editor-layout"> <!-- Main Editorial Column --> <div class="editor-main"> <label for="title"><?= $t->e('common.title') ?> *</label> <input type="text" id="title" name="title" required placeholder="<?= $t->e('post.title_placeholder') ?>" value="<?= htmlspecialchars($_POST['title'] ?? '', ENT_QUOTES, 'UTF-8') ?>"> <label for="content"><?= $t->e('common.content') ?> *</label> <textarea id="content" name="content" required data-quickwp-editor><?= htmlspecialchars($_POST['content'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea> <label for="excerpt"><?= $t->e('common.excerpt') ?></label> <textarea id="excerpt" name="excerpt" style="min-height: 80px;"><?= htmlspecialchars($_POST['excerpt'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea> <div class="row"> <div> <label for="categories"><?= $t->e('common.categories') ?></label> <div id="categories-picker" class="picker-container"> <small class="picker-empty">Loading...</small> </div> <input type="text" id="categories" name="categories" placeholder="IDs: 1, 2, 3" value="<?= htmlspecialchars($_POST['categories'] ?? '', ENT_QUOTES, 'UTF-8') ?>"> </div> <div> <label for="tags"><?= $t->e('common.tags') ?></label> <div id="tags-picker" class="picker-container"> <small class="picker-empty">Loading...</small> </div> <input type="text" id="tags" name="tags" placeholder="IDs: 4, 5, 6" value="<?= htmlspecialchars($_POST['tags'] ?? '', ENT_QUOTES, 'UTF-8') ?>"> </div> </div> <details class="collapsible-card"> <summary><?= $t->e('common.custom_fields') ?></summary> <div class="row" style="margin-top: 10px;"> <div> <label for="meta_json">Post Meta (JSON)</label> <textarea id="meta_json" name="meta_json" style="min-height: 90px; font-family: monospace;" placeholder='{"custom_key": "value"}'><?= htmlspecialchars($_POST['meta_json'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea> </div> <div> <label for="acf_json">ACF Fields (JSON)</label> <textarea id="acf_json" name="acf_json" style="min-height: 90px; font-family: monospace;" placeholder='{"field_name": "value"}'><?= htmlspecialchars($_POST['acf_json'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea> </div> </div> </details> </div> <!-- Right Publish Sidebar --> <aside class="editor-sidebar"> <div class="sidebar-card"> <h3><?= $t->e('common.publish_box') ?></h3> <label for="status"><?= $t->e('common.status') ?></label> <select id="status" name="status"> <?php foreach (['draft' => $t->get('status.draft'), 'publish' => $t->get('status.publish'), 'future' => $t->get('status.future'), 'pending' => $t->get('status.pending'), 'private' => $t->get('status.private')] as $val => $lbl): ?> <option value="<?= $val ?>" <?= $selectedStatus === $val ? 'selected' : '' ?>><?= htmlspecialchars($lbl, ENT_QUOTES, 'UTF-8') ?></option> <?php endforeach; ?> </select> <label for="date"><?= $t->e('common.date') ?></label> <input type="datetime-local" id="date" name="date" value="<?= htmlspecialchars($_POST['date'] ?? '', ENT_QUOTES, 'UTF-8') ?>"> <label for="slug"><?= $t->e('common.slug') ?></label> <input type="text" id="slug" name="slug" placeholder="optional-url-slug" value="<?= htmlspecialchars($_POST['slug'] ?? '', ENT_QUOTES, 'UTF-8') ?>"> <?php if (!empty($authors)): ?> <label for="author"><?= $t->e('common.author') ?></label> <select id="author" name="author"> <option value="0"><?= $t->e('common.default_author') ?></option> <?php foreach ($authors as $a): ?> <option value="<?= (int) $a['id'] ?>" <?= ((int) ($_POST['author'] ?? 0) === (int) $a['id']) ? 'selected' : '' ?>> <?= htmlspecialchars((string) ($a['name'] ?? '#' . $a['id']), ENT_QUOTES, 'UTF-8') ?> </option> <?php endforeach; ?> </select> <?php endif; ?> <label for="comment_status"><?= $t->e('common.comments') ?></label> <select id="comment_status" name="comment_status"> <option value=""><?= $t->e('common.default') ?></option> <option value="open" <?= (($_POST['comment_status'] ?? '') === 'open') ? 'selected' : '' ?>><?= $t->e('common.allow_comments') ?></option> <option value="closed" <?= (($_POST['comment_status'] ?? '') === 'closed') ? 'selected' : '' ?>><?= $t->e('common.disable_comments') ?></option> </select> <label class="checkbox-inline"> <input type="checkbox" name="sticky" value="1" <?= !empty($_POST['sticky']) ? 'checked' : '' ?>> <?= $t->e('post.sticky') ?> </label> <button type="submit" style="width: 100%; margin-top: 14px;"><?= $t->e('post.submit') ?></button> </div> <div class="sidebar-card" data-featured-picker="featured_media"> <h3><?= $t->e('common.featured_image') ?></h3> <div class="featured-preview-box"></div> <input type="number" id="featured_media" name="featured_media" min="0" placeholder="Media ID (0 = none)" value="<?= htmlspecialchars($_POST['featured_media'] ?? '0', ENT_QUOTES, 'UTF-8') ?>"> <div class="featured-actions"> <button type="button" class="featured-choose-btn"><?= $t->e('common.choose_media') ?></button> <button type="button" class="featured-remove-btn"><?= $t->e('common.remove') ?></button> </div> </div> <div class="sidebar-card"> <h3><?= $t->e('common.template') ?></h3> <?php if (count($postTemplates) > 1): ?> <select id="template" name="template" onchange="toggleCustomTemplate(this)"> <?php foreach ($postTemplates as $tplFile => $tplName): ?> <option value="<?= htmlspecialchars((string) $tplFile, ENT_QUOTES, 'UTF-8') ?>" <?= $selectedTemplate === (string) $tplFile ? 'selected' : '' ?>> <?= htmlspecialchars((string) $tplName, ENT_QUOTES, 'UTF-8') ?> </option> <?php endforeach; ?> <option value="__custom__" <?= $selectedTemplate === '__custom__' ? 'selected' : '' ?>><?= $t->e('common.custom_template') ?></option> </select> <input type="text" id="template_custom" name="template_custom" placeholder="e.g. single-custom.php" value="<?= htmlspecialchars($_POST['template_custom'] ?? '', ENT_QUOTES, 'UTF-8') ?>" style="margin-top: 6px; <?= $selectedTemplate === '__custom__' ? '' : 'display: none;' ?>"> <?php else: ?> <input type="text" id="template" name="template" placeholder="e.g. single-full-width.php" value="<?= htmlspecialchars($selectedTemplate, ENT_QUOTES, 'UTF-8') ?>"> <?php endif; ?> </div> </aside> </div> </form> <script> function toggleCustomTemplate(select) { var customInput = document.getElementById('template_custom'); if (customInput) { customInput.style.display = select.value === '__custom__' ? 'block' : 'none'; } } TaxonomyPicker.init({ categoriesEndpoint: <?= json_encode($config->getCategoriesEndpoint()) ?>, tagsEndpoint: <?= json_encode($config->getTagsEndpoint()) ?> }); QuickWPEditor.init({ siteKey: <?= json_encode($siteKey) ?>, labels: <?= json_encode($editorLabels) ?> }); </script> <?php require __DIR__ . '/partials/footer.php'; ?>