v1.0.0.9
Publisium
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/includes/config.php';
- require_once __DIR__ . '/includes/database.php';
- require_once __DIR__ . '/includes/auth.php';
- require_once __DIR__ . '/includes/csrf.php';
- require_once __DIR__ . '/includes/activity-log.php';
- require_once __DIR__ . '/includes/site-front.php';
-
- Auth::boot();
- Auth::requireRoleAtLeast(Auth::ROLE_EDITOR_IN_CHIEF);
-
- $currentUser = Auth::user();
- $currentRole = Auth::role();
- $db = Database::site();
-
- $flashMessage = null;
- $flashType = 'success';
-
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- if (!Csrf::verify($_POST['csrf_token'] ?? null)) {
- $flashMessage = 'Your session expired. Please try again.';
- $flashType = 'error';
- } else {
- require __DIR__ . '/dashboard-actions/manage-analytics.php';
- ActivityLog::record('analytics.' . (string) ($_POST['action'] ?? ''));
- $flashMessage = 'Saved.';
- }
- }
-
- $editId = (int) ($_GET['edit'] ?? 0) ?: null;
- $editingScript = null;
- if ($editId !== null) {
- $statement = $db->prepare('SELECT * FROM analytics_scripts WHERE id = :id LIMIT 1');
- $statement->execute(['id' => $editId]);
- $editingScript = $statement->fetch() ?: null;
- }
-
- $scripts = $db->query('SELECT * FROM analytics_scripts ORDER BY created_at DESC')->fetchAll();
-
- $db->prepare('INSERT IGNORE INTO site_settings (setting_key, setting_value) VALUES (:key, :value)')
- ->execute(['key' => 'consent_footer_icon_enabled', 'value' => '0']);
-
- $consentSetting = $db->prepare('SELECT setting_value FROM site_settings WHERE setting_key = :key LIMIT 1');
- $consentSetting->execute(['key' => 'consent_manager_enabled']);
- $consentEnabled = ($consentSetting->fetch()['setting_value'] ?? '0') === '1';
- $consentSetting->execute(['key' => 'consent_footer_icon_enabled']);
- $consentFooterIconEnabled = ($consentSetting->fetch()['setting_value'] ?? '0') === '1';
- SiteFront::resetSettings();
- $bannerSettings = SiteFront::settings();
- $bannerCategories = SiteFront::consentCategories();
-
- $dashActivePage = 'analytics';
- $dashPageTitle = 'Analytics';
-
- require __DIR__ . '/includes/dash-header.php';
-
- ?>
-
- <?php if ($flashMessage !== null): ?>
- <div class="dash-flash dash-flash-<?= htmlspecialchars($flashType) ?>"><?= Icons::icon($flashType === 'error' ? 'x' : 'check', 'icon icon-sm') ?><?= htmlspecialchars($flashMessage) ?></div>
- <?php endif; ?>
-
- <h1 class="dash-title"><?= Icons::icon("analytics", "icon icon-lg") ?>Analytics</h1>
-
- <div class="sidebar-box" style="max-width:640px;margin-bottom:24px;">
- <form method="post">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="save_consent_settings">
- <label><input type="checkbox" name="consent_manager_enabled" <?= $consentEnabled ? 'checked' : '' ?>> Ask visitors for cookie consent (scripts that need consent wait until they accept)</label>
- <label><input type="checkbox" name="consent_footer_icon_enabled" <?= $consentFooterIconEnabled ? 'checked' : '' ?>> Show a cookie icon in the footer so visitors can change their choice later</label>
- <?php foreach (['analytics' => 'Analytics', 'marketing' => 'Marketing'] as $bannerCategory => $bannerLabel): ?>
- <label><?= $bannerLabel ?> in the cookie banner</label>
- <select name="consent_show_<?= $bannerCategory ?>">
- <?php foreach (SiteFront::CONSENT_SHOW as $showKey => $showLabel): ?>
- <option value="<?= $showKey ?>" <?= ($bannerSettings['consent_show_' . $bannerCategory] ?? 'auto') === $showKey ? 'selected' : '' ?>><?= htmlspecialchars($showLabel) ?></option>
- <?php endforeach; ?>
- </select>
- <?php endforeach; ?>
- <p class="consent-status" style="font-size:13px;color:var(--muted);margin:12px 0 0;">
- <?php if (!$consentEnabled): ?>
- Cookie consent is off, so no banner is shown and all scripts run right away.
- <?php elseif ($bannerCategories === []): ?>
- Nothing on the site needs consent right now, so the banner and the cookie icon are hidden.
- <?php else: ?>
- Right now the banner asks about: <strong><?= htmlspecialchars(implode(', ', array_map('ucfirst', $bannerCategories))) ?></strong>.
- <?php endif; ?>
- </p>
- <button type="submit" class="dash-btn dash-btn-primary" style="margin-top:12px;"><?= Icons::icon('check', 'icon icon-sm') ?>Save</button>
- </form>
- </div>
-
- <table class="dash-table">
- <thead><tr><th><?= Icons::icon('heading', 'icon icon-sm') ?>Name</th><th><?= Icons::icon('layout', 'icon icon-sm') ?>Where</th><th><?= Icons::icon('shield', 'icon icon-sm') ?>Needs consent</th><th><?= Icons::icon('toggle', 'icon icon-sm') ?>Active</th><th></th></tr></thead>
- <tbody>
- <?php foreach ($scripts as $script): ?>
- <tr>
- <td><?= htmlspecialchars($script['name']) ?></td>
- <td><?= htmlspecialchars(['head' => 'In the head', 'body_start' => 'Start of the page', 'body_end' => 'End of the page'][$script['placement']] ?? $script['placement']) ?></td>
- <td><?= !$script['requires_consent'] ? 'No' : (['marketing' => 'Marketing', 'both' => 'Analytics and marketing'][$script['consent_category'] ?? 'analytics'] ?? 'Analytics') ?></td>
- <td><?= $script['is_active'] ? 'Yes' : 'No' ?></td>
- <td class="dash-table-actions">
- <a href="analytics.php?edit=<?= (int) $script['id'] ?>" class="dash-btn-small"><?= Icons::icon('edit', 'icon icon-sm') ?>Edit</a>
- <form method="post" style="display:inline;" onsubmit="return confirm('Delete this script? It stops running on the site right away.');">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="delete_analytics_script">
- <input type="hidden" name="script_id" value="<?= (int) $script['id'] ?>">
- <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('trash', 'icon icon-sm') ?>Delete</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- <?php if (empty($scripts)): ?>
- <tr><td colspan="5">No tracking scripts yet. Add one below, for example Google Analytics.</td></tr>
- <?php endif; ?>
- </tbody>
- </table>
-
- <h2 class="dash-subtitle"><?= Icons::icon('plus', 'icon icon-sm') ?><?= $editingScript ? 'Edit script' : 'New script' ?></h2>
- <form method="post">
- <?= Csrf::field() ?>
- <input type="hidden" name="action" value="save_analytics_script">
- <?php if ($editingScript): ?>
- <input type="hidden" name="script_id" value="<?= (int) $editingScript['id'] ?>">
- <?php endif; ?>
-
- <label>Name</label>
- <input type="text" name="name" value="<?= htmlspecialchars($editingScript['name'] ?? '') ?>" required>
-
- <label>Code (paste it exactly as your analytics service gives it to you)</label>
- <textarea name="script_code" rows="8" class="be-code"><?= htmlspecialchars($editingScript['script_code'] ?? '') ?></textarea>
-
- <label>Where to add it</label>
- <select name="placement">
- <option value="head" <?= ($editingScript['placement'] ?? 'head') === 'head' ? 'selected' : '' ?>>In the head (most tools ask for this)</option>
- <option value="body_start" <?= ($editingScript['placement'] ?? '') === 'body_start' ? 'selected' : '' ?>>Start of the page</option>
- <option value="body_end" <?= ($editingScript['placement'] ?? '') === 'body_end' ? 'selected' : '' ?>>End of the page</option>
- </select>
-
- <?php $consentValue = !($editingScript['requires_consent'] ?? 1) ? 'none' : (in_array($editingScript['consent_category'] ?? 'analytics', ['marketing', 'both'], true) ? $editingScript['consent_category'] : 'analytics'); ?>
- <label>Cookie consent</label>
- <select name="consent">
- <option value="analytics" <?= $consentValue === 'analytics' ? 'selected' : '' ?>>Run only after the visitor allows analytics</option>
- <option value="marketing" <?= $consentValue === 'marketing' ? 'selected' : '' ?>>Run only after the visitor allows marketing</option>
- <option value="both" <?= $consentValue === 'both' ? 'selected' : '' ?>>Run only after the visitor allows both analytics and marketing</option>
- <option value="none" <?= $consentValue === 'none' ? 'selected' : '' ?>>Always run (only for code that doesn’t track anyone)</option>
- </select>
- <label><input type="checkbox" name="is_active" <?= ($editingScript['is_active'] ?? 1) ? 'checked' : '' ?>> Active</label>
-
- <button type="submit" class="dash-btn dash-btn-primary" style="margin-top:16px;"><?= Icons::icon('check', 'icon icon-sm') ?>Save script</button>
- </form>
-
- <?php require __DIR__ . '/includes/dash-footer.php'; ?>
-