Actions
1 day ago
AnalyticsCleanup.php
1 day ago
AnalyticsConsent.php
1 day ago
AnalyticsEventDto.php
1 day ago
AnalyticsEventWithTimeDto.php
1 day ago
AnalyticsGenericEventHandler.php
1 day ago
AnalyticsSender.php
1 day ago
ErrorCode.php
1 day ago
WithAnalyticsAPI.php
1 day ago
WithAnalyticsSiteInfo.php
1 day ago
AnalyticsSender.php
254 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Analytics; |
| 4 | |
| 5 | class AnalyticsSender |
| 6 | { |
| 7 | use WithAnalyticsAPI; |
| 8 | |
| 9 | protected $consent; |
| 10 | |
| 11 | private $corruptSettingsNotice; |
| 12 | |
| 13 | public function __construct(AnalyticsConsent $consent) |
| 14 | { |
| 15 | $this->consent = $consent; |
| 16 | } |
| 17 | |
| 18 | public function maybeSend() |
| 19 | { |
| 20 | |
| 21 | if (defined('DOING_AJAX') && DOING_AJAX) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | $settings = get_option("wpstg_settings", []); |
| 29 | |
| 30 | |
| 31 | if (is_object($settings)) { |
| 32 | $settings = json_decode(json_encode($settings), true); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | if (!is_array($settings)) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | $interval = ($this->isDev() ? 1 : 15) * MINUTE_IN_SECONDS; |
| 42 | |
| 43 | |
| 44 | if (isset($settings['lastAnalyticsSend']) && time() - $settings['lastAnalyticsSend'] - $interval < 0) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | $settings['lastAnalyticsSend'] = time(); |
| 49 | |
| 50 | if (!update_option('wpstg_settings', $settings)) { |
| 51 | \WPStaging\functions\debug_log('WP STAGING: Could not update Analytics last sent time.', 'debug'); |
| 52 | }; |
| 53 | |
| 54 | $this->sendAnalytics(); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | public function sendNow($event): bool |
| 76 | { |
| 77 | if (!$this->consent->hasUserConsent()) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | if ($this->isDev() && !$this->canDevSendAnalytics()) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | $response = wp_remote_post($this->getApiUrl('events'), [ |
| 86 | 'method' => 'POST', |
| 87 | 'headers' => ['Content-Type' => 'application/json; charset=utf-8'], |
| 88 | 'body' => wp_json_encode([ |
| 89 | 'events' => [$event], |
| 90 | 'site_hash' => $this->getSiteHash(), |
| 91 | ]), |
| 92 | 'data_format' => 'body', |
| 93 | 'timeout' => 1, |
| 94 | 'redirection' => 0, |
| 95 | 'httpversion' => '1.0', |
| 96 | 'blocking' => false, |
| 97 | 'sslverify' => false, |
| 98 | ]); |
| 99 | |
| 100 | return !is_wp_error($response); |
| 101 | } |
| 102 | |
| 103 | public function sendAnalytics() |
| 104 | { |
| 105 | global $wpdb; |
| 106 | |
| 107 | $eventOptions = $wpdb->get_results("SELECT * FROM $wpdb->options WHERE `option_name` LIKE 'wpstg_analytics_event_%' LIMIT 0, 20"); |
| 108 | |
| 109 | |
| 110 | if (empty($eventOptions)) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | if (!$this->consent->hasUserConsent()) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | $events = array_map(function ($eventOption) { |
| 121 | return json_decode($eventOption->option_value); |
| 122 | }, $eventOptions); |
| 123 | |
| 124 | $this->setStaleEvents($events); |
| 125 | |
| 126 | |
| 127 | $events = array_filter($events, function ($event) { |
| 128 | return $event->ready_to_send; |
| 129 | }); |
| 130 | |
| 131 | |
| 132 | if (empty($events)) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | foreach ($events as &$event) { |
| 138 | foreach ($event as $property => &$value) { |
| 139 | if (is_bool($value)) { |
| 140 | $event->$property = (int)$value; |
| 141 | } elseif ($property === 'site_info') { |
| 142 | $siteInfo = &$value; |
| 143 | |
| 144 | foreach ($siteInfo as $siteInfoProperty => &$siteInfoValue) { |
| 145 | if (is_bool($siteInfoValue)) { |
| 146 | $siteInfo->$siteInfoProperty = (int)$siteInfoValue; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | $idsToDelete = implode(',', array_map(function ($key) use ($eventOptions) { |
| 156 | return $eventOptions[$key]->option_id; |
| 157 | }, array_keys($events))); |
| 158 | |
| 159 | if (!$wpdb->query("DELETE FROM $wpdb->options WHERE `option_id` IN ($idsToDelete)")) { |
| 160 | \WPStaging\functions\debug_log('WP STAGING Analytics Delete Sent Events Error: ' . $wpdb->last_error); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | $events = array_values($events); |
| 165 | |
| 166 | $body = wp_json_encode([ |
| 167 | 'events' => $events, |
| 168 | 'site_hash' => $this->getSiteHash(), |
| 169 | ]); |
| 170 | |
| 171 | $url = $this->getApiUrl('events'); |
| 172 | |
| 173 | |
| 174 | if ($this->isDev() && !$this->canDevSendAnalytics()) { |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | $response = wp_remote_post($url, [ |
| 179 | 'method' => 'POST', |
| 180 | 'headers' => ['Content-Type' => 'application/json; charset=utf-8'], |
| 181 | 'body' => $body, |
| 182 | 'data_format' => 'body', |
| 183 | 'timeout' => 10, |
| 184 | 'redirection' => 5, |
| 185 | 'httpversion' => '1.0', |
| 186 | 'blocking' => true, |
| 187 | 'sslverify' => false, |
| 188 | ]); |
| 189 | |
| 190 | if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { |
| 191 | if (wp_remote_retrieve_response_code($response) == 412) { |
| 192 | |
| 193 | if ($this->consent->hasUserConsent()) { |
| 194 | try { |
| 195 | $this->consent->giveConsent(); |
| 196 | } catch (\Exception $e) { |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | $errorMessage = is_wp_error($response) ? $response->get_error_message() : wp_remote_retrieve_body($response); |
| 207 | \WPStaging\functions\debug_log('WP STAGING Analytics Send Error: ' . $errorMessage); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | protected function setStaleEvents(&$events) |
| 217 | { |
| 218 | $staleThreshold = time() - 1 * DAY_IN_SECONDS; |
| 219 | |
| 220 | foreach ($events as &$event) { |
| 221 | if ($event->ready_to_send) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | if (isset($event->start_at)) { |
| 227 | if ($event->start_at < $staleThreshold) { |
| 228 | $event->ready_to_send = true; |
| 229 | $event->stale_at = time(); |
| 230 | } |
| 231 | |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | |
| 237 | if (!empty($event->start_time) && $event->start_time < $staleThreshold) { |
| 238 | $event->ready_to_send = true; |
| 239 | $event->is_stale = true; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | protected function isDev(): bool |
| 245 | { |
| 246 | return defined('WPSTG_IS_DEV') && WPSTG_IS_DEV; |
| 247 | } |
| 248 | |
| 249 | protected function canDevSendAnalytics(): bool |
| 250 | { |
| 251 | return defined('WPSTG_DEV_SEND_ANALYTICS') && WPSTG_DEV_SEND_ANALYTICS; |
| 252 | } |
| 253 | } |
| 254 |