WebOrbiton
v2.0.0.0

Publisium

70 lines · 3.0 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. $db = Database::site();
  6. $action = $_POST['action'] ?? '';
  7. ​
  8. if ($action === 'save_analytics_script') {
  9. $scriptId = (int) ($_POST['script_id'] ?? 0) ?: null;
  10. $name = trim((string) ($_POST['name'] ?? ''));
  11. $scriptCode = (string) ($_POST['script_code'] ?? '');
  12. $placement = in_array($_POST['placement'] ?? '', ['head', 'body_start', 'body_end'], true) ? $_POST['placement'] : 'head';
  13. $consentChoice = (string) ($_POST['consent'] ?? 'analytics');
  14. $requiresConsent = in_array($consentChoice, ['analytics', 'marketing', 'both'], true) ? 1 : 0;
  15. $consentCategory = in_array($consentChoice, ['marketing', 'both'], true) ? $consentChoice : 'analytics';
  16. $isActive = isset($_POST['is_active']) ? 1 : 0;
  17. ​
  18. if ($name === '' || $scriptCode === '') {
  19. return;
  20. }
  21. ​
  22. if ($scriptId !== null) {
  23. $statement = $db->prepare(
  24. 'UPDATE analytics_scripts SET name = :name, script_code = :script_code, placement = :placement, requires_consent = :requires_consent, consent_category = :consent_category, is_active = :is_active WHERE id = :id'
  25. );
  26. $statement->execute([
  27. 'name' => $name,
  28. 'script_code' => $scriptCode,
  29. 'placement' => $placement,
  30. 'requires_consent' => $requiresConsent,
  31. 'consent_category' => $consentCategory,
  32. 'is_active' => $isActive,
  33. 'id' => $scriptId,
  34. ]);
  35. } else {
  36. $statement = $db->prepare(
  37. 'INSERT INTO analytics_scripts (name, script_code, placement, requires_consent, consent_category, is_active) VALUES (:name, :script_code, :placement, :requires_consent, :consent_category, :is_active)'
  38. );
  39. $statement->execute([
  40. 'name' => $name,
  41. 'script_code' => $scriptCode,
  42. 'placement' => $placement,
  43. 'requires_consent' => $requiresConsent,
  44. 'consent_category' => $consentCategory,
  45. 'is_active' => $isActive,
  46. ]);
  47. }
  48. }
  49. ​
  50. if ($action === 'delete_analytics_script') {
  51. $scriptId = (int) ($_POST['script_id'] ?? 0);
  52. if ($scriptId > 0) {
  53. $statement = $db->prepare('DELETE FROM analytics_scripts WHERE id = :id');
  54. $statement->execute(['id' => $scriptId]);
  55. }
  56. }
  57. ​
  58. if ($action === 'save_consent_settings') {
  59. $consentEnabled = isset($_POST['consent_manager_enabled']) ? '1' : '0';
  60. $statement = $db->prepare(
  61. 'INSERT INTO site_settings (setting_key, setting_value) VALUES (:key, :value) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)'
  62. );
  63. $statement->execute(['key' => 'consent_manager_enabled', 'value' => $consentEnabled]);
  64. $statement->execute(['key' => 'consent_footer_icon_enabled', 'value' => isset($_POST['consent_footer_icon_enabled']) ? '1' : '0']);
  65. foreach (SiteFront::CONSENT_CATEGORIES as $category) {
  66. $mode = (string) ($_POST['consent_show_' . $category] ?? 'auto');
  67. $statement->execute(['key' => 'consent_show_' . $category, 'value' => array_key_exists($mode, SiteFront::CONSENT_SHOW) ? $mode : 'auto']);
  68. }
  69. }
  70. ​