<?php
/**
* QuickWP - Quick Edit by ID Controller (Editorial 2-Column UX)
*/
require_once __DIR__ . '/partials/bootstrap.php';
use QuickWP\QuickWP;
use QuickWP\Http\CsrfProtection;
$message = null;
$error = null;
$loadedItem = null;
$action = $_POST['action'] ?? ($_GET['action'] ?? '');
$itemType = $_POST['item_type'] ?? ($_GET['item_type'] ?? 'post');
$cptSlug = trim((string) ($_POST['cpt_slug'] ?? ($_GET['cpt_slug'] ?? '')));
$itemId = (int) ($_POST['item_id'] ?? ($_GET['item_id'] ?? 0));
function getServiceForType(QuickWP $qwp, string $itemType, string $cptSlug = ''): array
{
if ($itemType === 'cpt' && $cptSlug !== '') {
return ['service' => $qwp->cpt(), 'slug' => $cptSlug];
}
if ($itemType === 'page') {
return ['service' => $qwp->pages(), 'slug' => null];
}
return ['service' => $qwp->posts(), 'slug' => null];
}
// Load item for editing
if ($action === 'load' && $itemId > 0) {
$svcInfo = getServiceForType($qwp, $itemType, $cptSlug);
$result = $svcInfo['slug'] !== null
? $svcInfo['service']->get($svcInfo['slug'], $itemId)
: $svcInfo['service']->get($itemId);
if (QuickWP::isSuccess($result)) {
$loadedItem = $result['json'];
} else {
$error = 'Could not load item: ' . QuickWP::getError($result);
}
}
// Save changes
if ($action === 'save' && $itemId > 0 && $_SERVER['REQUEST_METHOD'] === 'POST') {
if (!CsrfProtection::validate()) {
$error = $t->get('common.csrf_error');
} else {
$data = [];
if (isset($_POST['title'])) $data['title'] = trim($_POST['title']);
if (isset($_POST['content'])) $data['content'] = trim($_POST['content']);
if (isset($_POST['excerpt'])) $data['excerpt'] = trim($_POST['excerpt']);
if (isset($_POST['status'])) $data['status'] = $_POST['status'];
if (isset($_POST['slug']) && trim($_POST['slug']) !== '') $data['slug'] = trim($_POST['slug']);
if (isset($_POST['featured_media'])) {
$data['featured_media'] = (int) $_POST['featured_media'];
}
$publishDate = trim($_POST['date'] ?? '');
if ($publishDate !== '') {
$data['date'] = strlen($publishDate) === 16 ? $publishDate . ':00' : $publishDate;
}
if (isset($_POST['author']) && (int) $_POST['author'] > 0) {
$data['author'] = (int) $_POST['author'];
}
if (isset($_POST['comment_status']) && in_array($_POST['comment_status'], ['open', 'closed'], true)) {
$data['comment_status'] = $_POST['comment_status'];
}
if ($itemType === 'post') {
$data['sticky'] = !empty($_POST['sticky']);
}
if (isset($_POST['template'])) {
$template = trim($_POST['template']);
if ($template === '__custom__') {
$template = trim($_POST['template_custom'] ?? '');
}
$data['template'] = $template;
}
if ($itemType === 'post' || $itemType === 'cpt') {
if (isset($_POST['categories'])) {
$data['categories'] = trim($_POST['categories']);
}
if (isset($_POST['tags'])) {
$data['tags'] = trim($_POST['tags']);
}
}
if ($itemType === 'page') {
if (isset($_POST['parent'])) $data['parent'] = (int) $_POST['parent'];
if (isset($_POST['menu_order'])) $data['menu_order'] = (int) $_POST['menu_order'];
}
$metaJson = trim($_POST['meta_json'] ?? '');
if ($metaJson !== '') {
$decoded = json_decode($metaJson, true);
if (!is_array($decoded)) {
$error = 'Custom Meta JSON is not valid JSON object.';
} else {
$data['meta'] = $decoded;
}
}
$acfJson = trim($_POST['acf_json'] ?? '');
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;
}
}
$svcInfo = getServiceForType($qwp, $itemType, $cptSlug);
if ($error === null) {
$result = $svcInfo['slug'] !== null
? $svcInfo['service']->update($svcInfo['slug'], $itemId, $data)
: $svcInfo['service']->update($itemId, $data);
if (QuickWP::isSuccess($result)) {
$safeLabel = htmlspecialchars(ucfirst($itemType === 'cpt' ? $cptSlug : $itemType), ENT_QUOTES, 'UTF-8');
$message = $safeLabel . ' #' . $itemId . ' updated successfully.';
$loadedItem = $result['json'];
} else {
$error = 'Update failed: ' . QuickWP::getError($result);
}
}
if ($loadedItem === null) {
$loadResult = $svcInfo['slug'] !== null
? $svcInfo['service']->get($svcInfo['slug'], $itemId)
: $svcInfo['service']->get($itemId);
if (QuickWP::isSuccess($loadResult)) {
$loadedItem = $loadResult['json'];
}
}
}
}
// Delete item
if ($action === 'delete' && $itemId > 0 && $_SERVER['REQUEST_METHOD'] === 'POST') {
if (!CsrfProtection::validate()) {
$error = $t->get('common.csrf_error');
} else {
$svcInfo = getServiceForType($qwp, $itemType, $cptSlug);
$force = isset($_POST['force_delete']) && $_POST['force_delete'] === '1';
$result = $svcInfo['slug'] !== null
? $svcInfo['service']->delete($svcInfo['slug'], $itemId, $force)
: $svcInfo['service']->delete($itemId, $force);
if (QuickWP::isSuccess($result)) {
$safeLabel = htmlspecialchars(ucfirst($itemType === 'cpt' ? $cptSlug : $itemType), ENT_QUOTES, 'UTF-8');
$message = $safeLabel . ' deleted successfully.' . ($force ? ' (permanently)' : ' (trashed)');
$loadedItem = null;
$itemId = 0;
} else {
$error = 'Delete failed: ' . QuickWP::getError($result);
$loadResult = $svcInfo['slug'] !== null
? $svcInfo['service']->get($svcInfo['slug'], $itemId)
: $svcInfo['service']->get($itemId);
if (QuickWP::isSuccess($loadResult)) {
$loadedItem = $loadResult['json'];
}
}
}
}
$authors = [];
$usersResult = $qwp->users()->list(['per_page' => 50, '_fields' => 'id,name']);
if (QuickWP::isSuccess($usersResult) && is_array($usersResult['json'])) {
$authors = $usersResult['json'];
}
// Fetch selectable items for the active type (with optional status & title search)
$pickerSearch = trim((string) ($_GET['q'] ?? ''));
$pickerStatus = trim((string) ($_GET['filter_status'] ?? 'any'));
$allowedPickerStatuses = ['any', 'draft', 'publish', 'future', 'pending', 'private'];
if (!in_array($pickerStatus, $allowedPickerStatuses, true)) {
$pickerStatus = 'any';
}
$pickerItems = [];
$pickerError = null;
if ($itemType === 'post' || $itemType === 'page' || ($itemType === 'cpt' && $cptSlug !== '')) {
$pickerParams = [
'per_page' => 100,
'status' => $pickerStatus,
'context' => 'edit',
'_fields' => 'id,title,status',
];
if ($pickerSearch !== '') {
$pickerParams['search'] = $pickerSearch;
}
$pickerSvc = getServiceForType($qwp, $itemType, $cptSlug);
$pickerRes = $pickerSvc['slug'] !== null
? $pickerSvc['service']->list($pickerSvc['slug'], $pickerParams)
: $pickerSvc['service']->list($pickerParams);
if (QuickWP::isSuccess($pickerRes) && is_array($pickerRes['json'])) {
$pickerItems = $pickerRes['json'];
if ($pickerStatus !== 'any') {
$pickerItems = array_values(array_filter(
$pickerItems,
static fn (array $item): bool => (string) ($item['status'] ?? '') === $pickerStatus
));
}
} else {
$pickerError = 'Could not load list: ' . QuickWP::getError($pickerRes);
}
}
$pageTitle = $t->get('edit.heading');
$heading = $t->get('edit.heading');
$activeNav = 'edit';
$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; ?>
<?php if ($pickerError): ?>
<div class="message error"><?= htmlspecialchars($pickerError, ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>
<!-- Load / Search & Select Form -->
<div class="form-card" style="margin-bottom: 20px;">
<form method="get" id="load-item-form" class="filters" style="margin: 0; padding: 0; background: transparent;">
<input type="hidden" name="action" value="load">
<?php if ($siteKey !== ''): ?>
<input type="hidden" name="site" value="<?= htmlspecialchars($siteKey, ENT_QUOTES, 'UTF-8') ?>">
<?php endif; ?>
<div class="filter-group" style="max-width: 150px;">
<label for="item_type"><?= $t->e('edit.type') ?></label>
<select id="item_type" name="item_type" onchange="handleTypeChange(this.value)">
<option value="post" <?= $itemType === 'post' ? 'selected' : '' ?>><?= $t->e('nav.post') ?></option>
<option value="page" <?= $itemType === 'page' ? 'selected' : '' ?>><?= $t->e('nav.page') ?></option>
<option value="cpt" <?= $itemType === 'cpt' ? 'selected' : '' ?>><?= $t->e('dashboard.cpt') ?></option>
</select>
</div>
<div class="filter-group" style="max-width: 145px;">
<label for="filter_status"><?= $t->e('common.status') ?></label>
<select id="filter_status" name="filter_status" onchange="handleStatusFilterChange()">
<option value="any" <?= $pickerStatus === 'any' ? 'selected' : '' ?>><?= $t->e('status.all') ?></option>
<option value="draft" <?= $pickerStatus === 'draft' ? 'selected' : '' ?>><?= $t->e('status.draft') ?></option>
<option value="publish" <?= $pickerStatus === 'publish' ? 'selected' : '' ?>><?= $t->e('status.published') ?></option>
<option value="future" <?= $pickerStatus === 'future' ? 'selected' : '' ?>><?= $t->e('status.future') ?></option>
<option value="pending" <?= $pickerStatus === 'pending' ? 'selected' : '' ?>><?= $t->e('status.pending') ?></option>
<option value="private" <?= $pickerStatus === 'private' ? 'selected' : '' ?>><?= $t->e('status.private') ?></option>
</select>
</div>
<div class="filter-group" id="cpt_slug_wrap" style="max-width: 160px; <?= $itemType === 'cpt' ? '' : 'display:none;' ?>">
<label for="cpt_slug"><?= $t->e('edit.cpt_slug') ?></label>
<input type="text" id="cpt_slug" name="cpt_slug" placeholder="e.g. product" value="<?= htmlspecialchars($cptSlug, ENT_QUOTES, 'UTF-8') ?>">
</div>
<div class="filter-group" style="min-width: 180px;">
<label for="q"><?= $t->e('edit.search_title') ?></label>
<input type="search" id="q" name="q" placeholder="<?= $t->e('edit.search_placeholder') ?>" value="<?= htmlspecialchars($pickerSearch, ENT_QUOTES, 'UTF-8') ?>" oninput="filterPickerLive(this.value)">
</div>
<div class="filter-group" style="flex: 2; min-width: 240px;">
<label for="item_picker"><?= $t->e('edit.select_item', ['type' => ucfirst($itemType === 'cpt' && $cptSlug !== '' ? $cptSlug : $itemType)]) ?> <span id="picker_count" class="hint">(<?= count($pickerItems) ?>)</span></label>
<select id="item_picker" onchange="selectPickerItem(this.value)">
<option value=""><?= $t->e('edit.choose_from', ['count' => count($pickerItems)]) ?></option>
<?php foreach ($pickerItems as $pi): ?>
<?php
$piId = (int) ($pi['id'] ?? 0);
$piTitle = is_array($pi['title'] ?? null) ? ($pi['title']['rendered'] ?? '#' . $piId) : ($pi['title'] ?? '#' . $piId);
$piTitle = html_entity_decode(strip_tags((string) $piTitle), ENT_QUOTES, 'UTF-8');
?>
<option value="<?= $piId ?>" <?= $piId === $itemId ? 'selected' : '' ?>>
#<?= $piId ?> [<?= htmlspecialchars((string) ($pi['status'] ?? ''), ENT_QUOTES, 'UTF-8') ?>] <?= htmlspecialchars($piTitle, ENT_QUOTES, 'UTF-8') ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="filter-group" style="max-width: 100px;">
<label for="item_id"><?= $t->e('edit.or_id') ?></label>
<input type="number" id="item_id" name="item_id" min="1" value="<?= $itemId > 0 ? $itemId : '' ?>" placeholder="ID">
</div>
<button type="submit"><?= $t->e('edit.load_search') ?></button>
</form>
<?php if ($pickerSearch !== ''): ?>
<div style="margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border);">
<div class="hint" style="margin-bottom: 6px;">
<?= $t->e('edit.search_results') ?> <strong>"<?= htmlspecialchars($pickerSearch, ENT_QUOTES, 'UTF-8') ?>"</strong> (<?= count($pickerItems) ?>):
</div>
<?php if (empty($pickerItems)): ?>
<span class="hint"><?= $t->e('edit.no_matches') ?></span>
<?php else: ?>
<div class="term-list">
<?php foreach ($pickerItems as $pi): ?>
<?php
$piId = (int) ($pi['id'] ?? 0);
$piTitle = is_array($pi['title'] ?? null) ? ($pi['title']['rendered'] ?? '#' . $piId) : ($pi['title'] ?? '#' . $piId);
$piTitle = html_entity_decode(strip_tags((string) $piTitle), ENT_QUOTES, 'UTF-8');
$piUrl = 'quick-edit.php?action=load&item_type=' . urlencode($itemType)
. ($cptSlug !== '' ? '&cpt_slug=' . urlencode($cptSlug) : '')
. '&item_id=' . $piId
. ($siteKey !== '' ? '&site=' . urlencode($siteKey) : '');
?>
<a href="<?= htmlspecialchars($piUrl, ENT_QUOTES, 'UTF-8') ?>" class="term-item" style="text-decoration: none; color: var(--primary);">
<span class="id">#<?= $piId ?></span> <?= htmlspecialchars($piTitle, ENT_QUOTES, 'UTF-8') ?>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<?php if (is_array($loadedItem)): ?>
<?php
$titleVal = is_array($loadedItem['title'] ?? null) ? ($loadedItem['title']['raw'] ?? $loadedItem['title']['rendered'] ?? '') : ($loadedItem['title'] ?? '');
$contentVal = is_array($loadedItem['content'] ?? null) ? ($loadedItem['content']['raw'] ?? $loadedItem['content']['rendered'] ?? '') : ($loadedItem['content'] ?? '');
$excerptVal = is_array($loadedItem['excerpt'] ?? null) ? ($loadedItem['excerpt']['raw'] ?? $loadedItem['excerpt']['rendered'] ?? '') : ($loadedItem['excerpt'] ?? '');
$statusVal = (string) ($loadedItem['status'] ?? 'draft');
$slugVal = (string) ($loadedItem['slug'] ?? '');
$featuredVal = (int) ($loadedItem['featured_media'] ?? 0);
$templateVal = (string) ($loadedItem['template'] ?? '');
$dateVal = !empty($loadedItem['date']) ? substr((string) $loadedItem['date'], 0, 16) : '';
$authorVal = (int) ($loadedItem['author'] ?? 0);
$commentStatusVal = (string) ($loadedItem['comment_status'] ?? 'open');
$stickyVal = !empty($loadedItem['sticky']);
$catsVal = is_array($loadedItem['categories'] ?? null) ? implode(', ', $loadedItem['categories']) : '';
$tagsVal = is_array($loadedItem['tags'] ?? null) ? implode(', ', $loadedItem['tags']) : '';
$parentVal = (int) ($loadedItem['parent'] ?? 0);
$menuOrderVal = (int) ($loadedItem['menu_order'] ?? 0);
$itemLink = (string) ($loadedItem['link'] ?? '');
$templatesList = $itemType === 'page' ? $qwp->templates()->getPageTemplates() : $qwp->templates()->getPostTemplates();
$metaInitial = !empty($loadedItem['meta']) && is_array($loadedItem['meta']) ? json_encode($loadedItem['meta'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) : '';
$acfInitial = !empty($loadedItem['acf']) && is_array($loadedItem['acf']) ? json_encode($loadedItem['acf'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) : '';
?>
<form method="post">
<?= CsrfProtection::field() ?>
<input type="hidden" name="action" value="save">
<input type="hidden" name="item_type" value="<?= htmlspecialchars($itemType, ENT_QUOTES, 'UTF-8') ?>">
<input type="hidden" name="cpt_slug" value="<?= htmlspecialchars($cptSlug, ENT_QUOTES, 'UTF-8') ?>">
<input type="hidden" name="item_id" value="<?= $itemId ?>">
<?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">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px;">
<span class="hint"><?= htmlspecialchars(strtoupper($itemType === 'cpt' ? $cptSlug : $itemType), ENT_QUOTES, 'UTF-8') ?> #<?= $itemId ?></span>
<?php if ($itemLink !== ''): ?>
<a href="<?= htmlspecialchars($itemLink, ENT_QUOTES, 'UTF-8') ?>" target="_blank" style="font-size: 0.85rem;"><?= $t->e('common.view_on_site') ?></a>
<?php endif; ?>
</div>
<label for="title"><?= $t->e('common.title') ?></label>
<input type="text" id="title" name="title" value="<?= htmlspecialchars((string) $titleVal, ENT_QUOTES, 'UTF-8') ?>">
<label for="content"><?= $t->e('common.content') ?></label>
<textarea id="content" name="content" data-quickwp-editor><?= htmlspecialchars((string) $contentVal, ENT_QUOTES, 'UTF-8') ?></textarea>
<label for="excerpt"><?= $t->e('common.excerpt') ?></label>
<textarea id="excerpt" name="excerpt" style="min-height: 80px;"><?= htmlspecialchars((string) $excerptVal, ENT_QUOTES, 'UTF-8') ?></textarea>
<?php if ($itemType === 'post' || $itemType === 'cpt'): ?>
<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" value="<?= htmlspecialchars($catsVal, ENT_QUOTES, 'UTF-8') ?>" placeholder="IDs: 1, 2">
</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" value="<?= htmlspecialchars($tagsVal, ENT_QUOTES, 'UTF-8') ?>" placeholder="IDs: 3, 4">
</div>
</div>
<?php endif; ?>
<details class="collapsible-card">
<summary><?= $t->e('common.custom_fields') ?></summary>
<div class="row" style="margin-top: 10px;">
<div>
<label for="meta_json">Meta (JSON)</label>
<textarea id="meta_json" name="meta_json" style="min-height: 90px; font-family: monospace;"><?= htmlspecialchars((string) $metaInitial, 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;"><?= htmlspecialchars((string) $acfInitial, 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 (['publish' => $t->get('status.published'), 'draft' => $t->get('status.draft'), 'future' => $t->get('status.future'), 'pending' => $t->get('status.pending'), 'private' => $t->get('status.private')] as $val => $lbl): ?>
<option value="<?= $val ?>" <?= $statusVal === $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($dateVal, ENT_QUOTES, 'UTF-8') ?>">
<label for="slug"><?= $t->e('common.slug') ?></label>
<input type="text" id="slug" name="slug" value="<?= htmlspecialchars($slugVal, ENT_QUOTES, 'UTF-8') ?>">
<?php if (!empty($authors)): ?>
<label for="author"><?= $t->e('common.author') ?></label>
<select id="author" name="author">
<?php foreach ($authors as $a): ?>
<option value="<?= (int) $a['id'] ?>" <?= $authorVal === (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="open" <?= $commentStatusVal === 'open' ? 'selected' : '' ?>><?= $t->e('common.allow_comments') ?></option>
<option value="closed" <?= $commentStatusVal === 'closed' ? 'selected' : '' ?>><?= $t->e('common.disable_comments') ?></option>
</select>
<?php if ($itemType === 'post'): ?>
<label class="checkbox-inline">
<input type="checkbox" name="sticky" value="1" <?= $stickyVal ? 'checked' : '' ?>>
<?= $t->e('post.sticky') ?>
</label>
<?php endif; ?>
<button type="submit" style="width: 100%; margin-top: 14px;"><?= $t->e('common.save_changes') ?></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" value="<?= $featuredVal ?>">
<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>
<?php if ($itemType === 'page'): ?>
<div class="sidebar-card">
<h3><?= $t->e('page.attributes') ?></h3>
<label for="parent"><?= $t->e('page.parent') ?> ID</label>
<input type="number" id="parent" name="parent" min="0" value="<?= $parentVal ?>">
<label for="menu_order"><?= $t->e('page.menu_order') ?></label>
<input type="number" id="menu_order" name="menu_order" min="0" value="<?= $menuOrderVal ?>">
</div>
<?php endif; ?>
<div class="sidebar-card">
<h3><?= $t->e('common.template') ?></h3>
<?php if (count($templatesList) > 1): ?>
<select id="template" name="template" onchange="toggleCustomTemplate(this)">
<?php foreach ($templatesList as $tplFile => $tplName): ?>
<option value="<?= htmlspecialchars((string) $tplFile, ENT_QUOTES, 'UTF-8') ?>" <?= $templateVal === (string) $tplFile ? 'selected' : '' ?>>
<?= htmlspecialchars((string) $tplName, ENT_QUOTES, 'UTF-8') ?>
</option>
<?php endforeach; ?>
<option value="__custom__" <?= ($templateVal !== '' && !isset($templatesList[$templateVal])) ? 'selected' : '' ?>><?= $t->e('common.custom_template') ?></option>
</select>
<input type="text" id="template_custom" name="template_custom"
value="<?= ($templateVal !== '' && !isset($templatesList[$templateVal])) ? htmlspecialchars($templateVal, ENT_QUOTES, 'UTF-8') : '' ?>"
style="margin-top: 6px; <?= ($templateVal !== '' && !isset($templatesList[$templateVal])) ? '' : 'display: none;' ?>">
<?php else: ?>
<input type="text" id="template" name="template" value="<?= htmlspecialchars($templateVal, ENT_QUOTES, 'UTF-8') ?>" placeholder="template.php">
<?php endif; ?>
</div>
</aside>
</div>
</form>
<!-- Separate Delete Form -->
<div class="form-card" style="margin-top: 20px; border-color: #f5c6cb; background: #fff8f8;">
<form method="post" onsubmit="return confirm('Are you sure you want to delete this <?= htmlspecialchars($itemType === 'cpt' ? $cptSlug : $itemType, ENT_QUOTES, 'UTF-8') ?>?');" style="display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px;">
<?= CsrfProtection::field() ?>
<input type="hidden" name="action" value="delete">
<input type="hidden" name="item_type" value="<?= htmlspecialchars($itemType, ENT_QUOTES, 'UTF-8') ?>">
<input type="hidden" name="cpt_slug" value="<?= htmlspecialchars($cptSlug, ENT_QUOTES, 'UTF-8') ?>">
<input type="hidden" name="item_id" value="<?= $itemId ?>">
<?php if ($siteKey !== ''): ?>
<input type="hidden" name="site" value="<?= htmlspecialchars($siteKey, ENT_QUOTES, 'UTF-8') ?>">
<?php endif; ?>
<label class="checkbox-inline" style="margin: 0;">
<input type="checkbox" name="force_delete" value="1">
<?= $t->e('edit.delete_permanent') ?>
</label>
<button type="submit" style="margin: 0; background: var(--error); padding: 8px 16px; font-size: 0.9rem;"><?= $t->e('edit.delete_btn') ?></button>
</form>
</div>
<?php endif; ?>
<script>
function handleStatusFilterChange() {
var idInput = document.getElementById('item_id');
if (idInput) idInput.value = '';
var form = document.getElementById('load-item-form');
if (form) form.submit();
}
function handleTypeChange(val) {
var wrap = document.getElementById('cpt_slug_wrap');
if (wrap) wrap.style.display = val === 'cpt' ? 'block' : 'none';
if (val !== 'cpt') {
var idInput = document.getElementById('item_id');
if (idInput) idInput.value = '';
var form = document.getElementById('load-item-form');
if (form) form.submit();
}
}
function selectPickerItem(id) {
if (!id) return;
var idInput = document.getElementById('item_id');
if (idInput) idInput.value = id;
var form = document.getElementById('load-item-form');
if (form) form.submit();
}
function filterPickerLive(query) {
var picker = document.getElementById('item_picker');
var countEl = document.getElementById('picker_count');
if (!picker) return;
var q = (query || '').toLowerCase().trim();
var visible = 0;
for (var i = 1; i < picker.options.length; i++) {
var opt = picker.options[i];
var match = q === '' || opt.text.toLowerCase().indexOf(q) !== -1;
opt.hidden = !match;
if (match) visible++;
}
if (countEl) countEl.textContent = '(' + visible + ')';
}
function toggleCustomTemplate(select) {
var customInput = document.getElementById('template_custom');
if (customInput) customInput.style.display = select.value === '__custom__' ? 'block' : 'none';
}
<?php if (is_array($loadedItem) && ($itemType === 'post' || $itemType === 'cpt')): ?>
TaxonomyPicker.init({
categoriesEndpoint: <?= json_encode($config->getCategoriesEndpoint()) ?>,
tagsEndpoint: <?= json_encode($config->getTagsEndpoint()) ?>
});
<?php endif; ?>
QuickWPEditor.init({
siteKey: <?= json_encode($siteKey) ?>,
labels: <?= json_encode($editorLabels) ?>
});
</script>
<?php require __DIR__ . '/partials/footer.php'; ?>
|