Anonymizer.php
4 weeks ago
ConsentManager.php
4 weeks ago
ConsentNoticeService.php
4 weeks ago
HttpClient.php
4 weeks ago
NoticeManager.php
4 weeks ago
UsageTracker.php
4 weeks ago
ConsentNoticeService.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AmeliaVendor\Melograno\UsageTracker\Core; |
| 6 | |
| 7 | use AmeliaVendor\Melograno\UsageTracker\Collectors\ConsentNoticeCollectorInterface; |
| 8 | |
| 9 | /** |
| 10 | * Decides consent/notice policy and owns the admin-notice option. |
| 11 | * |
| 12 | * This service never mutates consent state or touches cron scheduling; those |
| 13 | * side effects belong to UsageTracker. It only answers questions ("should we |
| 14 | * migrate?", "what is the default consent?", "should the notice show?") and |
| 15 | * arms/dismisses the notice option it owns. |
| 16 | */ |
| 17 | class ConsentNoticeService |
| 18 | { |
| 19 | private ConsentNoticeCollectorInterface $collector; |
| 20 | |
| 21 | private ConsentManager $consentManager; |
| 22 | |
| 23 | private NoticeManager $noticeManager; |
| 24 | |
| 25 | public function __construct(ConsentNoticeCollectorInterface $collector) |
| 26 | { |
| 27 | $this->collector = $collector; |
| 28 | $this->consentManager = new ConsentManager($collector->getConsentOptionName()); |
| 29 | $this->noticeManager = new NoticeManager($collector->getNoticeOptionName()); |
| 30 | } |
| 31 | |
| 32 | public function shouldRunUpgradeMigration(string $savedVersion, string $currentVersion): bool |
| 33 | { |
| 34 | if (!$this->collector->shouldMigrateConsentOnUpgrade()) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | $migrationVersion = $this->collector->getOptInMigrationVersion(); |
| 39 | |
| 40 | if ($migrationVersion === null) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return version_compare($savedVersion, $migrationVersion, '<') |
| 45 | && version_compare($currentVersion, $migrationVersion, '>='); |
| 46 | } |
| 47 | |
| 48 | public function defaultConsent(): bool |
| 49 | { |
| 50 | return $this->collector->shouldEnableConsentByDefault(); |
| 51 | } |
| 52 | |
| 53 | public function armNotice(): void |
| 54 | { |
| 55 | $this->noticeManager->arm(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Arms the notice for a fresh install only when telemetry stays off by default |
| 60 | * and the collector opts into showing the notice. |
| 61 | */ |
| 62 | public function armNoticeForNewInstallation(bool $consentEnabled): void |
| 63 | { |
| 64 | if (!$consentEnabled && $this->collector->shouldShowAdminNotice()) { |
| 65 | $this->noticeManager->arm(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public function canCurrentUserSeeAdminNotice(): bool |
| 70 | { |
| 71 | return is_admin() && current_user_can('manage_options'); |
| 72 | } |
| 73 | |
| 74 | public function shouldShowNotice(): bool |
| 75 | { |
| 76 | if (!$this->collector->shouldShowAdminNotice()) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if (!$this->noticeManager->isArmed()) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | return !$this->consentManager->isEnabled(); |
| 85 | } |
| 86 | |
| 87 | public function dismissNotice(): void |
| 88 | { |
| 89 | $this->noticeManager->dismiss(); |
| 90 | } |
| 91 | |
| 92 | public function dismissNoticeIfArmed(): void |
| 93 | { |
| 94 | if ($this->noticeManager->isArmed()) { |
| 95 | $this->noticeManager->dismiss(); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 |