PHP Classes

File: public/quick-comments.php

Recommend this page to a friend!
  Packages of Adrian M   QuickWP   public/quick-comments.php   Download  
File: public/quick-comments.php
Role: Example script
Content type: text/plain
Description: Example script
Class: QuickWP
Manage WordPress sites using the REST API
Author: By
Last change:
Date: 12 hours ago
Size: 6,243 bytes
 

 

Contents

Class file image Download
<?php
/**
 * QuickWP - Comments Moderation Controller
 */

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

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

$message = null;
$error = null;

$status = $_GET['status'] ?? 'hold';
$search = trim($_GET['search'] ?? '');
$page = max(1, (int) ($_GET['page'] ?? 1));
$perPage = 20;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!CsrfProtection::validate()) {
        $error = $t->get('common.csrf_error');
    } else {
        $action = $_POST['action'] ?? '';
        $commentId = (int) ($_POST['comment_id'] ?? 0);

        if ($commentId > 0) {
            $res = match ($action) {
                'approve'   => $qwp->comments()->approve($commentId),
                'unapprove' => $qwp->comments()->unapprove($commentId),
                'spam'      => $qwp->comments()->spam($commentId),
                'delete'    => $qwp->comments()->delete($commentId, false),
                default     => null,
            };

            if ($res !== null) {
                if (QuickWP::isSuccess($res)) {
                    $message = 'Comment #' . $commentId . ' updated (' . htmlspecialchars($action, ENT_QUOTES, 'UTF-8') . ').';
                } else {
                    $error = 'Action failed: ' . QuickWP::getError($res);
                }
            }
        }
    }
}

$params = [
    'per_page' => $perPage,
    'page' => $page,
    'status' => $status,
];
if ($search !== '') {
    $params['search'] = $search;
}

$comments = [];
$listResult = $qwp->comments()->list($params);
if (QuickWP::isSuccess($listResult) && is_array($listResult['json'])) {
    $comments = $listResult['json'];
} elseif (!$error) {
    $error = 'Could not load comments: ' . QuickWP::getError($listResult);
}

$pageTitle = $t->get('nav.comments');
$heading = $t->get('comments.heading');
$activeNav = 'comments';
$containerClass = 'wide';
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="get" class="filters">
        <?php if ($siteKey !== ''): ?>
            <input type="hidden" name="site" value="<?= htmlspecialchars($siteKey, ENT_QUOTES, 'UTF-8') ?>">
        <?php endif; ?>
        <div class="filter-group">
            <label for="status"><?= $t->e('common.status') ?></label>
            <select id="status" name="status">
                <?php foreach (['hold' => $t->get('status.pending'), 'approve' => $t->get('status.approved'), 'spam' => $t->get('status.spam'), 'all' => $t->get('status.all')] as $val => $lbl): ?>
                    <option value="<?= $val ?>" <?= $status === $val ? 'selected' : '' ?>><?= htmlspecialchars($lbl, ENT_QUOTES, 'UTF-8') ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="filter-group">
            <label for="search"><?= $t->e('common.search') ?></label>
            <input type="search" id="search" name="search" value="<?= htmlspecialchars($search, ENT_QUOTES, 'UTF-8') ?>" placeholder="<?= $t->e('common.search') ?>...">
        </div>
        <button type="submit"><?= $t->e('common.filter') ?></button>
    </form>

    <?php if (empty($comments)): ?>
        <div class="empty-state">
            <p><?= $t->e('comments.no_comments') ?></p>
        </div>
    <?php else: ?>
        <?php foreach ($comments as $comment): ?>
            <?php
            $cid = (int) ($comment['id'] ?? 0);
            $author = (string) ($comment['author_name'] ?? 'Anonymous');
            $cStatus = (string) ($comment['status'] ?? '');
            $cDate = (string) ($comment['date'] ?? '');
            $cContent = is_array($comment['content'] ?? null)
                ? strip_tags((string) ($comment['content']['rendered'] ?? ''))
                : strip_tags((string) ($comment['content'] ?? ''));
            ?>
            <div class="form-card" style="margin-bottom: 12px;">
                <div style="display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap;">
                    <div>
                        <strong><?= htmlspecialchars($author, ENT_QUOTES, 'UTF-8') ?></strong>
                        <span class="hint">#<?= $cid ?> · Post #<?= (int) ($comment['post'] ?? 0) ?> · <?= htmlspecialchars($cDate, ENT_QUOTES, 'UTF-8') ?></span>
                    </div>
                    <span class="status-badge status-<?= $cStatus === 'approved' ? 'publish' : ($cStatus === 'hold' ? 'pending' : 'private') ?>">
                        <?= htmlspecialchars($cStatus, ENT_QUOTES, 'UTF-8') ?>
                    </span>
                </div>
                <p style="margin: 10px 0;"><?= nl2br(htmlspecialchars(trim($cContent), ENT_QUOTES, 'UTF-8')) ?></p>
                <div style="display: flex; gap: 8px; flex-wrap: wrap;">
                    <?php foreach (['approve' => $t->get('comments.approve'), 'unapprove' => $t->get('comments.hold'), 'spam' => $t->get('comments.spam'), 'delete' => $t->get('comments.trash')] as $act => $btnLabel): ?>
                        <form method="post" style="display: inline;">
                            <?= CsrfProtection::field() ?>
                            <input type="hidden" name="action" value="<?= $act ?>">
                            <input type="hidden" name="comment_id" value="<?= $cid ?>">
                            <?php if ($siteKey !== ''): ?>
                                <input type="hidden" name="site" value="<?= htmlspecialchars($siteKey, ENT_QUOTES, 'UTF-8') ?>">
                            <?php endif; ?>
                            <button type="submit" style="margin: 0; padding: 6px 12px; font-size: 0.85rem; <?= in_array($act, ['spam', 'delete'], true) ? 'background: var(--error);' : '' ?>">
                                <?= $btnLabel ?>
                            </button>
                        </form>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>

<?php require __DIR__ . '/partials/footer.php'; ?>