ameliabooking
/
vendor
/
melograno
/
usage-tracker
/
src
/
Collectors
/
Plugin
/
AmeliaCollector.php
AmeliaCollector.php
2 months ago
IvyFormsCollector.php
2 months ago
WpDataTablesCollector.php
2 months ago
AmeliaCollector.php
173 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AmeliaVendor\Melograno\UsageTracker\Collectors\Plugin; |
| 6 | |
| 7 | use AmeliaVendor\Melograno\UsageTracker\Collectors\BaseCollector; |
| 8 | use AmeliaVendor\Melograno\UsageTracker\Collectors\ConsentNoticeCollectorInterface; |
| 9 | |
| 10 | class AmeliaCollector extends BaseCollector implements ConsentNoticeCollectorInterface |
| 11 | { |
| 12 | /** @var array<string, string> */ |
| 13 | private const LICENSE_TIER_MAP = [ |
| 14 | 'lite' => 'lite', |
| 15 | 'starter' => 'starter', |
| 16 | 'basic' => 'standard', |
| 17 | 'pro' => 'pro', |
| 18 | 'developer' => 'elite', |
| 19 | ]; |
| 20 | |
| 21 | public function getPluginSlug(): string |
| 22 | { |
| 23 | return 'ameliabooking'; |
| 24 | } |
| 25 | |
| 26 | public function getConsentOptionName(): string |
| 27 | { |
| 28 | return 'amelia_usage_tracking_consent'; |
| 29 | } |
| 30 | |
| 31 | public function getNoticeOptionName(): string |
| 32 | { |
| 33 | return 'amelia_show_usage_tracking_notice'; |
| 34 | } |
| 35 | |
| 36 | public function getOptInMigrationVersion(): ?string |
| 37 | { |
| 38 | return '2.4.2'; |
| 39 | } |
| 40 | |
| 41 | public function shouldEnableConsentByDefault(): bool |
| 42 | { |
| 43 | return \AmeliaBooking\Infrastructure\Licence\Licence::isPremium(); |
| 44 | } |
| 45 | |
| 46 | public function shouldShowAdminNotice(): bool |
| 47 | { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | public function shouldMigrateConsentOnUpgrade(): bool |
| 52 | { |
| 53 | $licence = \AmeliaBooking\Infrastructure\Licence\Licence::getLicence(); |
| 54 | |
| 55 | return is_string($licence) |
| 56 | && strcasecmp($licence, \AmeliaBooking\Infrastructure\Licence\LicenceConstants::LITE) === 0; |
| 57 | } |
| 58 | |
| 59 | public function getConsentNoticeAjaxPrefix(): string |
| 60 | { |
| 61 | return 'amelia'; |
| 62 | } |
| 63 | |
| 64 | public function renderConsentAdminNotice(): void |
| 65 | { |
| 66 | if (!$this->isOnAmeliaAdminPage() || $this->isOnWelcomePage()) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | $resourcesUrl = AMELIA_URL . 'vendor/melograno/usage-tracker/resources'; |
| 71 | |
| 72 | wp_enqueue_style( |
| 73 | 'amelia-usage-tracking-notice-css', |
| 74 | $resourcesUrl . '/usage-tracking-notice.css', |
| 75 | [], |
| 76 | AMELIA_VERSION |
| 77 | ); |
| 78 | |
| 79 | $usageTrackingCollector = $this; |
| 80 | $usageTrackingResourcesUrl = $resourcesUrl; |
| 81 | include dirname(dirname(dirname(__DIR__))) . '/resources/usage-tracking-notice.php'; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Maps Amelia activation.licence values to telemetry license slugs. |
| 86 | */ |
| 87 | public static function normalizeLicenseTier(?string $ameliaLicence): ?string |
| 88 | { |
| 89 | if ($ameliaLicence === null) { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | $trimmed = trim($ameliaLicence); |
| 94 | if ($trimmed === '') { |
| 95 | return 'elite'; |
| 96 | } |
| 97 | |
| 98 | $key = strtolower($trimmed); |
| 99 | |
| 100 | return self::LICENSE_TIER_MAP[$key] ?? null; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return array<string, mixed> |
| 105 | */ |
| 106 | protected function pluginPayload(): array |
| 107 | { |
| 108 | $container = require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php'; |
| 109 | $customerBookingRepository = $container->get('domain.booking.customerBooking.repository'); |
| 110 | $appointmentRepository = $container->get('domain.booking.appointment.repository'); |
| 111 | |
| 112 | $data = [ |
| 113 | 'plugin_version' => AMELIA_VERSION, |
| 114 | ]; |
| 115 | |
| 116 | $rawLicence = $this->resolveAmeliaLicence(); |
| 117 | $licenseTier = self::normalizeLicenseTier($rawLicence); |
| 118 | if ($licenseTier !== null) { |
| 119 | $data['license'] = $licenseTier; |
| 120 | } |
| 121 | |
| 122 | $minCreated = $customerBookingRepository->getEarliestCreatedAt(); |
| 123 | if ($minCreated !== null && $minCreated !== '') { |
| 124 | $timestamp = strtotime($minCreated); |
| 125 | if ($timestamp !== false) { |
| 126 | $data['first_booking_created_at'] = gmdate('c', $timestamp); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | $data['appointment_bookings_count'] = $customerBookingRepository->getAppointmentBookingsCount(); |
| 131 | $data['appointments_count'] = $appointmentRepository->getAppointmentsCount(); |
| 132 | $data['event_bookings_count'] = $customerBookingRepository->getEventBookingsCount(); |
| 133 | $data['approved_appointment_bookings_count'] = $customerBookingRepository->getApprovedAppointmentBookingsCount(); |
| 134 | $data['approved_appointments_count'] = $appointmentRepository->getApprovedAppointmentsCount(); |
| 135 | |
| 136 | return array_filter($data, static function ($value) { |
| 137 | return $value !== null; |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | protected function resolveAmeliaLicence(): ?string |
| 142 | { |
| 143 | $licence = \AmeliaBooking\Infrastructure\Licence\Licence::getLicence(); |
| 144 | |
| 145 | return is_string($licence) ? $licence : null; |
| 146 | } |
| 147 | |
| 148 | private function isOnAmeliaAdminPage(): bool |
| 149 | { |
| 150 | $page = $this->currentAdminPageSlug(); |
| 151 | |
| 152 | if ($page !== '' && strpos($page, 'wpamelia') === 0) { |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | $screen = function_exists('get_current_screen') ? get_current_screen() : null; |
| 157 | |
| 158 | return $screen !== null && strpos($screen->id, 'wpamelia') !== false; |
| 159 | } |
| 160 | |
| 161 | private function isOnWelcomePage(): bool |
| 162 | { |
| 163 | return $this->currentAdminPageSlug() === 'wpamelia-welcome'; |
| 164 | } |
| 165 | |
| 166 | private function currentAdminPageSlug(): string |
| 167 | { |
| 168 | return isset($_GET['page']) |
| 169 | ? sanitize_text_field(wp_unslash((string) $_GET['page'])) |
| 170 | : ''; |
| 171 | } |
| 172 | } |
| 173 |