PHP Classes

File: public/quick-taxonomy.php

Recommend this page to a friend!
  Packages of Adrian M   QuickWP   public/quick-taxonomy.php   Download  
File: public/quick-taxonomy.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
v2.2: refactor services, add retry/pagination, shared UI layout, PHP 8.3+, PHPStan L8, and CI
Date: 12 hours ago
Size: 6,817 bytes
 

 

Contents

Class file image Download
<?php
/**
 * QuickWP v2 - Taxonomy (Categories/Tags) Controller
 */

require_once __DIR__ . '/partials/bootstrap.php';

use QuickWP\QuickWP;
use QuickWP\Http\CsrfProtection;

$message = null;
$error = null;
$categories = [];
$tags = [];

// Action handling
$action = $_POST['action'] ?? ($_GET['action'] ?? 'list');

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!CsrfProtection::validate()) {
        $error = $t->get('common.csrf_error');
    } elseif ($action === 'create_category') {
        $name = trim($_POST['category_name'] ?? '');
        $slug = trim($_POST['category_slug'] ?? '');
        $description = trim($_POST['category_description'] ?? '');
        $parent = (int)($_POST['category_parent'] ?? 0);

        if ($name === '') {
            $error = 'Category name is required.';
        } else {
            $data = ['name' => $name];
            if ($slug !== '') $data['slug'] = $slug;
            if ($description !== '') $data['description'] = $description;
            if ($parent > 0) $data['parent'] = $parent;

            $result = $qwp->taxonomy()->createCategory($data);

            if (QuickWP::isSuccess($result)) {
                $termId = QuickWP::getId($result);
                $message = 'Category created. ID: ' . $termId;
            } else {
                $error = 'Error: ' . QuickWP::getError($result);
            }
        }
    } elseif ($action === 'create_tag') {
        $name = trim($_POST['tag_name'] ?? '');
        $slug = trim($_POST['tag_slug'] ?? '');
        $description = trim($_POST['tag_description'] ?? '');

        if ($name === '') {
            $error = 'Tag name is required.';
        } else {
            $data = ['name' => $name];
            if ($slug !== '') $data['slug'] = $slug;
            if ($description !== '') $data['description'] = $description;

            $result = $qwp->taxonomy()->createTag($data);

            if (QuickWP::isSuccess($result)) {
                $termId = QuickWP::getId($result);
                $message = 'Tag created. ID: ' . $termId;
            } else {
                $error = 'Error: ' . QuickWP::getError($result);
            }
        }
    }
}

// Fetch existing categories and tags for reference
$categoriesResult = $qwp->taxonomy()->listCategories(['per_page' => 100]);
if (QuickWP::isSuccess($categoriesResult)) {
    $categories = $categoriesResult['json'] ?? [];
}

$tagsResult = $qwp->taxonomy()->listTags(['per_page' => 100]);
if (QuickWP::isSuccess($tagsResult)) {
    $tags = $tagsResult['json'] ?? [];
}

$pageTitle = $t->get('nav.taxonomy');
$heading = $t->get('taxonomy.heading');
$activeNav = 'taxonomy';
$containerClass = 'wide';
$extraHead = '<style>textarea { min-height: 60px; } .list-section { margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--border); }</style>';
require __DIR__ . '/partials/header.php';
?>

    <?php if ($message): ?>
        <div class="message success"><?= htmlspecialchars($message) ?></div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="message error"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <div class="grid-2">
        <!-- Create Category Form -->
        <div class="form-card">
            <h2><?= $t->e('taxonomy.create_category') ?></h2>
            <form method="post">
                <?= CsrfProtection::field() ?>
                <input type="hidden" name="action" value="create_category">
                <?php if ($siteKey !== ''): ?>
                    <input type="hidden" name="site" value="<?= htmlspecialchars($siteKey) ?>">
                <?php endif; ?>

                <label for="category_name"><?= $t->e('taxonomy.name') ?></label>
                <input type="text" id="category_name" name="category_name" required>

                <label for="category_slug"><?= $t->e('common.slug') ?></label>
                <input type="text" id="category_slug" name="category_slug">

                <label for="category_description"><?= $t->e('media.description') ?></label>
                <textarea id="category_description" name="category_description"></textarea>

                <label for="category_parent"><?= $t->e('taxonomy.parent_id') ?></label>
                <input type="number" id="category_parent" name="category_parent" min="0" value="0">

                <button type="submit"><?= $t->e('taxonomy.create_category') ?></button>
            </form>
        </div>

        <!-- Create Tag Form -->
        <div class="form-card">
            <h2><?= $t->e('taxonomy.create_tag') ?></h2>
            <form method="post">
                <?= CsrfProtection::field() ?>
                <input type="hidden" name="action" value="create_tag">
                <?php if ($siteKey !== ''): ?>
                    <input type="hidden" name="site" value="<?= htmlspecialchars($siteKey) ?>">
                <?php endif; ?>

                <label for="tag_name"><?= $t->e('taxonomy.name') ?></label>
                <input type="text" id="tag_name" name="tag_name" required>

                <label for="tag_slug"><?= $t->e('common.slug') ?></label>
                <input type="text" id="tag_slug" name="tag_slug">

                <label for="tag_description"><?= $t->e('media.description') ?></label>
                <textarea id="tag_description" name="tag_description"></textarea>

                <button type="submit"><?= $t->e('taxonomy.create_tag') ?></button>
            </form>
        </div>
    </div>

    <!-- Existing Terms -->
    <div class="list-section">
        <h2><?= $t->e('taxonomy.existing_categories') ?></h2>
        <?php if (!empty($categories)): ?>
            <div class="term-list">
                <?php foreach ($categories as $cat): ?>
                    <span class="term-item">
                        <?= htmlspecialchars($cat['name'] ?? 'Untitled') ?>
                        <span class="id">(ID: <?= (int)($cat['id'] ?? 0) ?>)</span>
                    </span>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <p style="color: var(--text-muted);"><?= $t->e('taxonomy.no_categories') ?></p>
        <?php endif; ?>

        <h2><?= $t->e('taxonomy.existing_tags') ?></h2>
        <?php if (!empty($tags)): ?>
            <div class="term-list">
                <?php foreach ($tags as $tag): ?>
                    <span class="term-item">
                        <?= htmlspecialchars($tag['name'] ?? 'Untitled') ?>
                        <span class="id">(ID: <?= (int)($tag['id'] ?? 0) ?>)</span>
                    </span>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <p style="color: var(--text-muted);"><?= $t->e('taxonomy.no_tags') ?></p>
        <?php endif; ?>
    </div>
</div>
</body>
</html>