<?php
/**
* QuickWP v2 - Custom Post Type 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 {
$cptSlug = trim($_POST['cpt_slug'] ?? $config->getDefaultCptSlug());
$customEndpoint = trim($_POST['cpt_endpoint'] ?? '');
$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'] ?? '');
if ($cptSlug === '') {
$error = 'CPT slug is required.';
} elseif ($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;
$result = $qwp->cpt()->create(
$cptSlug,
$data,
$customEndpoint !== '' ? $customEndpoint : null
);
if (QuickWP::isSuccess($result)) {
$itemId = QuickWP::getId($result);
$itemLink = QuickWP::getLink($result);
$safeLabel = htmlspecialchars(ucfirst($cptSlug), ENT_QUOTES, 'UTF-8');
if ($itemLink) {
$message = $safeLabel . ' created: <a href="' . htmlspecialchars($itemLink, ENT_QUOTES, 'UTF-8') . '" target="_blank">View</a>';
} elseif ($itemId) {
$message = $safeLabel . ' created. ID: ' . $itemId;
} else {
$message = $safeLabel . ' created successfully.';
}
} else {
$error = 'Error: ' . QuickWP::getError($result);
}
}
}
}
$pageTitle = $t->get('dashboard.cpt');
$heading = $t->get('cpt.heading');
$activeNav = 'cpt';
$extraHead = '<script src="assets/taxonomy-picker.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) ?></div>
<?php endif; ?>
<form method="post">
<?= CsrfProtection::field() ?>
<?php if ($siteKey !== ''): ?>
<input type="hidden" name="site" value="<?= htmlspecialchars($siteKey) ?>">
<?php endif; ?>
<div class="cpt-section" style="margin-top: 0; padding-top: 0; border-top: none;">
<div class="row">
<div>
<label for="cpt_slug"><?= $t->e('cpt.slug_label') ?></label>
<input type="text" id="cpt_slug" name="cpt_slug" required
placeholder="e.g. product, portfolio, event"
value="<?= htmlspecialchars($_POST['cpt_slug'] ?? $config->getDefaultCptSlug()) ?>">
<p class="hint"><?= $t->e('cpt.slug_hint') ?></p>
</div>
<div>
<label for="cpt_endpoint"><?= $t->e('cpt.endpoint_label') ?></label>
<input type="text" id="cpt_endpoint" name="cpt_endpoint"
placeholder="https://..."
value="<?= htmlspecialchars($_POST['cpt_endpoint'] ?? '') ?>">
<p class="hint"><?= $t->e('cpt.endpoint_hint') ?></p>
</div>
</div>
</div>
<label for="title"><?= $t->e('common.title') ?> *</label>
<input type="text" id="title" name="title" required value="<?= htmlspecialchars($_POST['title'] ?? '') ?>">
<label for="content"><?= $t->e('common.content') ?> *</label>
<textarea id="content" name="content" required><?= htmlspecialchars($_POST['content'] ?? '') ?></textarea>
<label for="excerpt"><?= $t->e('common.excerpt') ?></label>
<textarea id="excerpt" name="excerpt" style="min-height: 80px;"><?= htmlspecialchars($_POST['excerpt'] ?? '') ?></textarea>
<div class="row">
<div>
<label for="status"><?= $t->e('common.status') ?></label>
<select id="status" name="status">
<option value="draft" <?= ($_POST['status'] ?? 'draft') === 'draft' ? 'selected' : '' ?>><?= $t->e('status.draft') ?></option>
<option value="publish" <?= ($_POST['status'] ?? '') === 'publish' ? 'selected' : '' ?>><?= $t->e('status.publish') ?></option>
<option value="pending" <?= ($_POST['status'] ?? '') === 'pending' ? 'selected' : '' ?>><?= $t->e('status.pending') ?></option>
<option value="private" <?= ($_POST['status'] ?? '') === 'private' ? 'selected' : '' ?>><?= $t->e('status.private') ?></option>
</select>
</div>
<div>
<label for="slug"><?= $t->e('common.slug') ?></label>
<input type="text" id="slug" name="slug" value="<?= htmlspecialchars($_POST['slug'] ?? '') ?>">
</div>
</div>
<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="e.g. 1, 5, 12" value="<?= htmlspecialchars($_POST['categories'] ?? '') ?>">
<p class="hint"><?= $t->e('cpt.cats_hint') ?></p>
</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="e.g. 3, 7" value="<?= htmlspecialchars($_POST['tags'] ?? '') ?>">
<p class="hint"><?= $t->e('cpt.tags_hint') ?></p>
</div>
</div>
<button type="submit"><?= $t->e('cpt.submit') ?></button>
</form>
</div>
<script>
TaxonomyPicker.init({
categoriesEndpoint: <?= json_encode($config->getCategoriesEndpoint()) ?>,
tagsEndpoint: <?= json_encode($config->getTagsEndpoint()) ?>
});
</script>
</body>
</html>
|