WebOrbiton
v1.1.0.0

StocketBase

162 lines · 8.1 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/includes/config.php';
  6. require_once __DIR__ . '/includes/database.php';
  7. require_once __DIR__ . '/includes/auth.php';
  8. require_once __DIR__ . '/includes/csrf.php';
  9. require_once __DIR__ . '/includes/activity-log.php';
  10. require_once __DIR__ . '/includes/site-front.php';
  11. ​
  12. Auth::boot();
  13. Auth::requireRoleAtLeast(Auth::ROLE_STORE_OWNER);
  14. ​
  15. $currentUser = Auth::user();
  16. $currentRole = Auth::role();
  17. $db = Database::site();
  18. ​
  19. $flashMessage = null;
  20. $flashType = 'success';
  21. ​
  22. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  23. if (!Csrf::verify($_POST['csrf_token'] ?? null)) {
  24. $flashMessage = 'Your session expired. Please try again.';
  25. $flashType = 'error';
  26. } else {
  27. $analyticsError = null;
  28. require __DIR__ . '/dashboard-actions/manage-analytics.php';
  29. if ($analyticsError !== null) {
  30. $flashMessage = $analyticsError;
  31. $flashType = 'error';
  32. } else {
  33. ActivityLog::record('analytics.' . (string) ($_POST['action'] ?? ''));
  34. $flashMessage = 'Changes saved.';
  35. }
  36. }
  37. }
  38. ​
  39. $editId = (int) ($_GET['edit'] ?? 0) ?: null;
  40. $editingScript = null;
  41. if ($editId !== null) {
  42. $statement = $db->prepare('SELECT * FROM analytics_scripts WHERE id = :id LIMIT 1');
  43. $statement->execute(['id' => $editId]);
  44. $editingScript = $statement->fetch() ?: null;
  45. }
  46. ​
  47. $scripts = $db->query('SELECT * FROM analytics_scripts ORDER BY created_at DESC')->fetchAll();
  48. ​
  49. SiteFront::resetSettings();
  50. $consentSettings = SiteFront::settings();
  51. $consentEnabled = ($consentSettings['consent_manager_enabled'] ?? '0') === '1';
  52. $consentFooterIconEnabled = ($consentSettings['consent_footer_icon_enabled'] ?? '0') === '1';
  53. $consentManageEnabled = ($consentSettings['consent_manage_enabled'] ?? '1') === '1';
  54. $consentShown = SiteFront::bannerConsentCategories();
  55. ​
  56. $consentCategoryLabels = [
  57. 'analytics' => 'Analytics (like Google Analytics)',
  58. 'marketing' => 'Marketing (like a Meta or TikTok pixel)',
  59. 'preferences' => 'Preferences (remembering choices, chat widgets)',
  60. ];
  61. $scriptRunLabel = static function (array $script) use ($consentCategoryLabels): string {
  62. if (!(int) $script['requires_consent']) {
  63. return 'Always';
  64. }
  65. $category = (string) ($script['consent_category'] ?? 'analytics');
  66. ​
  67. return 'After consent: ' . strtok($consentCategoryLabels[$category] ?? $consentCategoryLabels['analytics'], ' ');
  68. };
  69. $editingRun = $editingScript === null
  70. ? 'analytics'
  71. : ((int) $editingScript['requires_consent'] ? (string) ($editingScript['consent_category'] ?? 'analytics') : 'always');
  72. ​
  73. $dashActivePage = 'analytics';
  74. $dashPageTitle = 'Analytics';
  75. ​
  76. require __DIR__ . '/includes/dash-header.php';
  77. ​
  78. ?>
  79. ​
  80. <?php if ($flashMessage !== null): ?>
  81. <div class="dash-flash dash-flash-<?= htmlspecialchars($flashType) ?>"><?= Icons::icon($flashType === 'error' ? 'x' : 'check', 'icon icon-sm') ?><?= htmlspecialchars($flashMessage) ?></div>
  82. <?php endif; ?>
  83. ​
  84. <h1 class="dash-title"><?= Icons::icon("analytics", "icon icon-lg") ?>Analytics</h1>
  85. ​
  86. <div class="sidebar-box" style="max-width:640px;margin-bottom:24px;">
  87. <form method="post">
  88. <?= Csrf::field() ?>
  89. <input type="hidden" name="action" value="save_consent_settings">
  90. <label><input type="checkbox" name="consent_manager_enabled" <?= $consentEnabled ? 'checked' : '' ?>> Ask visitors for cookie consent (scripts marked below only run after they agree)</label>
  91. <label><input type="checkbox" name="consent_footer_icon_enabled" <?= $consentFooterIconEnabled ? 'checked' : '' ?>> Show a cookie icon in the footer so visitors can change their mind</label>
  92. <label><input type="checkbox" name="consent_manage_enabled" <?= $consentManageEnabled ? 'checked' : '' ?>> Add a "Manage" button so visitors can choose which kinds of cookies they allow</label>
  93. <p style="font-size:12.5px;color:var(--muted);margin:14px 0 6px;">Choices shown after "Manage" (only tick what your store actually uses). "Necessary" is always shown.</p>
  94. <?php foreach ($consentCategoryLabels as $categoryKey => $categoryLabel): ?>
  95. <label><input type="checkbox" name="consent_show_<?= htmlspecialchars($categoryKey) ?>" <?= in_array($categoryKey, $consentShown, true) ? 'checked' : '' ?>> <?= htmlspecialchars($categoryLabel) ?></label>
  96. <?php endforeach; ?>
  97. <button type="submit" class="dash-btn dash-btn-primary" style="margin-top:12px;"><?= Icons::icon('check', 'icon icon-sm') ?>Save</button>
  98. </form>
  99. </div>
  100. ​
  101. <table class="dash-table">
  102. <thead><tr><th><?= Icons::icon('heading', 'icon icon-sm') ?>Name</th><th><?= Icons::icon('layout', 'icon icon-sm') ?>Placement</th><th><?= Icons::icon('shield', 'icon icon-sm') ?>Runs</th><th><?= Icons::icon('toggle', 'icon icon-sm') ?>Active</th><th></th></tr></thead>
  103. <tbody>
  104. <?php foreach ($scripts as $script): ?>
  105. <tr>
  106. <td><?= htmlspecialchars($script['name']) ?></td>
  107. <td><?= htmlspecialchars(['head' => 'Page head', 'body_start' => 'Top of the page', 'body_end' => 'Bottom of the page'][$script['placement']] ?? (string) $script['placement']) ?></td>
  108. <td><?= htmlspecialchars($scriptRunLabel($script)) ?></td>
  109. <td><?= $script['is_active'] ? 'Yes' : 'No' ?></td>
  110. <td class="dash-table-actions">
  111. <a href="analytics.php?edit=<?= (int) $script['id'] ?>" class="dash-btn-small"><?= Icons::icon('edit', 'icon icon-sm') ?>Edit</a>
  112. <form method="post" style="display:inline;" onsubmit="return confirm('Delete this script? It will stop running on your store.');">
  113. <?= Csrf::field() ?>
  114. <input type="hidden" name="action" value="delete_analytics_script">
  115. <input type="hidden" name="script_id" value="<?= (int) $script['id'] ?>">
  116. <button type="submit" class="dash-btn-small dash-btn-danger"><?= Icons::icon('trash', 'icon icon-sm') ?>Delete</button>
  117. </form>
  118. </td>
  119. </tr>
  120. <?php endforeach; ?>
  121. <?php if (empty($scripts)): ?>
  122. <tr><td colspan="5">No tracking scripts yet. Add one for Google Analytics, a Meta pixel and so on.</td></tr>
  123. <?php endif; ?>
  124. </tbody>
  125. </table>
  126. ​
  127. <h2 class="dash-subtitle"><?= Icons::icon('plus', 'icon icon-sm') ?><?= $editingScript ? 'Edit script' : 'New script' ?></h2>
  128. <form method="post">
  129. <?= Csrf::field() ?>
  130. <input type="hidden" name="action" value="save_analytics_script">
  131. <?php if ($editingScript): ?>
  132. <input type="hidden" name="script_id" value="<?= (int) $editingScript['id'] ?>">
  133. <?php endif; ?>
  134. ​
  135. <label>Name</label>
  136. <input type="text" name="name" value="<?= htmlspecialchars($editingScript['name'] ?? '') ?>" required>
  137. ​
  138. <label>Code (paste it exactly as the service gives it to you)</label>
  139. <textarea name="script_code" rows="8" class="be-code"><?= htmlspecialchars($editingScript['script_code'] ?? '') ?></textarea>
  140. ​
  141. <label>Where to add it</label>
  142. <select name="placement">
  143. <option value="head" <?= ($editingScript['placement'] ?? 'head') === 'head' ? 'selected' : '' ?>>Page head (&lt;head&gt;, most common)</option>
  144. <option value="body_start" <?= ($editingScript['placement'] ?? '') === 'body_start' ? 'selected' : '' ?>>Top of the page (&lt;body&gt; start)</option>
  145. <option value="body_end" <?= ($editingScript['placement'] ?? '') === 'body_end' ? 'selected' : '' ?>>Bottom of the page (&lt;body&gt; end)</option>
  146. </select>
  147. ​
  148. <label>When should it run?</label>
  149. <select name="consent_run">
  150. <option value="always" <?= $editingRun === 'always' ? 'selected' : '' ?>>Always, no consent needed (only for things the store can't work without)</option>
  151. <?php foreach ($consentCategoryLabels as $categoryKey => $categoryLabel): ?>
  152. <option value="<?= htmlspecialchars($categoryKey) ?>" <?= $editingRun === $categoryKey ? 'selected' : '' ?>>After the visitor allows: <?= htmlspecialchars($categoryLabel) ?><?= in_array($categoryKey, $consentShown, true) ? '' : ' (not shown under Manage, only "Accept" turns it on)' ?></option>
  153. <?php endforeach; ?>
  154. </select>
  155. <p style="font-size:12px;color:var(--muted);margin:6px 0 0;">Visitors who use "Manage" can allow some kinds and block others. Pick the kind that fits this script best.</p>
  156. <label><input type="checkbox" name="is_active" <?= ($editingScript['is_active'] ?? 1) ? 'checked' : '' ?>> Turned on</label>
  157. ​
  158. <button type="submit" class="dash-btn dash-btn-primary" style="margin-top:16px;"><?= Icons::icon('check', 'icon icon-sm') ?>Save script</button>
  159. </form>
  160. ​
  161. <?php require __DIR__ . '/includes/dash-footer.php'; ?>
  162. ​