<?php
/**
* QuickWP - Create Page 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'] ?? '');
$parent = (int) ($_POST['parent_id'] ?? 0);
$menuOrder = (int) ($_POST['menu_order'] ?? 0);
$featuredMedia = (int) ($_POST['featured_media'] ?? 0);
$publishDate = trim($_POST['date'] ?? '');
$authorId = (int) ($_POST['author'] ?? 0);
$commentStatus = $_POST['comment_status'] ?? '';
$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 ($parent > 0) $data['parent'] = $parent;
if ($menuOrder > 0) $data['menu_order'] = $menuOrder;
if ($template !== '') $data['template'] = $template;
if ($featuredMedia > 0) $data['featured_media'] = $featuredMedia;
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 ($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->pages()->create($data);
if (QuickWP::isSuccess($result)) {
$pageId = QuickWP::getId($result);
$pageLink = QuickWP::getLink($result);
if ($status === 'publish') {
$qwp->webhooks()->dispatch('page.published', [
'id' => $pageId,
'title' => $title,
'link' => $pageLink,
'site' => $siteKey,
]);
}
if ($pageLink) {
$message = 'Page created: <a href="' . htmlspecialchars($pageLink, ENT_QUOTES, 'UTF-8') . '" target="_blank">View Page</a> (ID: ' . (int) $pageId . ')';
} elseif ($pageId) {
$message = 'Page created. ID: ' . $pageId;
} else {
$message = 'Page created successfully.';
}
} else {
$error = 'Error: ' . QuickWP::getError($result);
}
}
}
}
}
$authors = [];
$usersResult = $qwp->users()->list(['per_page' => 50, '_fields' => 'id,name']);
if (QuickWP::isSuccess($usersResult) && is_array($usersResult['json'])) {
$authors = $usersResult['json'];
}
$pageTemplates = $qwp->templates()->getPageTemplates();
$selectedTemplate = $_POST['template'] ?? '';
$selectedStatus = $_POST['status'] ?? 'draft';
$pageTitle = $t->get('page.heading');
$heading = $t->get('page.heading');
$activeNav = 'page';
$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">
<div class="editor-main">
<label for="title"><?= $t->e('common.title') ?> *</label>
<input type="text" id="title" name="title" required placeholder="<?= $t->e('page.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>
<details class="collapsible-card">
<summary><?= $t->e('common.custom_fields') ?></summary>
<div class="row" style="margin-top: 10px;">
<div>
<label for="meta_json">Page 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>
<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>
<button type="submit" style="width: 100%; margin-top: 14px;"><?= $t->e('page.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('page.attributes') ?></h3>
<label for="parent_id"><?= $t->e('page.parent') ?></label>
<div id="parent-picker" class="picker-container">
<small class="picker-empty">Loading...</small>
</div>
<input type="number" id="parent_id" name="parent_id" min="0" value="<?= htmlspecialchars($_POST['parent_id'] ?? '0', ENT_QUOTES, 'UTF-8') ?>">
<label for="menu_order"><?= $t->e('page.menu_order') ?></label>
<input type="number" id="menu_order" name="menu_order" min="0" value="<?= htmlspecialchars($_POST['menu_order'] ?? '0', ENT_QUOTES, 'UTF-8') ?>">
<label for="template"><?= $t->e('common.template') ?></label>
<?php if (count($pageTemplates) > 1): ?>
<select id="template" name="template" onchange="toggleCustomTemplate(this)">
<?php foreach ($pageTemplates 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. template-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. template-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({
pagesEndpoint: <?= json_encode($config->getPagesEndpoint()) ?>
});
QuickWPEditor.init({
siteKey: <?= json_encode($siteKey) ?>,
labels: <?= json_encode($editorLabels) ?>
});
</script>
<?php require __DIR__ . '/partials/footer.php'; ?>
|