PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Analytics / AnalyticsEventDto.php
wp-staging / Framework / Analytics Last commit date
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
AnalyticsEventDto.php
270 lines
1 <?php
2
3 namespace WPStaging\Framework\Analytics;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Experiments\ExperimentManager;
7 use WPStaging\Framework\Filesystem\DebugLogReader;
8
9 abstract class AnalyticsEventDto implements \JsonSerializable
10 {
11 use WithAnalyticsSiteInfo;
12
13
14 protected $event;
15
16
17 protected $job_identifier;
18
19
20 protected $event_hash;
21
22
23 protected $is_finished = false;
24
25
26 protected $is_stale = false;
27
28
29 protected $is_error = false;
30
31
32 protected $is_cancelled = false;
33
34
35 protected $is_requirement_check_fail = false;
36
37
38 protected $requirement_fail_reason = '';
39
40
41 protected $error_message;
42
43
44 protected $error_code;
45
46
47 protected $last_debug_logs;
48
49
50 protected $ready_to_send = false;
51
52
53 protected $start_time;
54
55
56 protected $end_time;
57
58
59 protected $duration;
60
61
62 protected $site_info;
63
64
65 protected $experiment;
66
67
68 protected $variant;
69
70 public function __construct()
71 {
72 $this->event = $this->getEventAction();
73 $this->site_info = $this->getAnalyticsSiteInfo();
74
75
76
77 $attribution = WPStaging::make(ExperimentManager::class)->getAttribution();
78
79 $this->experiment = isset($attribution['experiment']) ? $attribution['experiment'] : null;
80 $this->variant = isset($attribution['variant']) ? $attribution['variant'] : null;
81 }
82
83 #[\ReturnTypeWillChange]
84 public function jsonSerialize()
85 {
86 return get_object_vars($this);
87 }
88
89
90
91
92 abstract public function getEventAction();
93
94 public function enqueueStartEvent($jobId, $eventData)
95 {
96 $this->job_identifier = $jobId;
97 $this->start_time = time();
98 $this->event_hash = microtime(true) . rand();
99
100 try {
101 $this->saveEvent($jobId, $this);
102 } catch (\Exception $e) {
103 \WPStaging\functions\debug_log("WP STAGING: Could not register start event analytics data for job ID $jobId.", 'debug', false);
104 }
105 }
106
107 public function enqueueFinishEvent($jobId, $eventData, $eventOverrides = [])
108 {
109 try {
110 $event = $this->getEventByJobId($jobId);
111 } catch (\Exception $e) {
112 \WPStaging\functions\debug_log("WP STAGING: Could not register finish event analytics data for job ID $jobId", 'debug', false);
113
114 return;
115 }
116
117 $event->is_finished = true;
118 $event->end_time = time();
119 $event->duration = time() - $event->start_time;
120 $event->ready_to_send = true;
121
122
123 foreach ($eventOverrides as $key => $value) {
124 $event->$key = $value;
125 }
126
127 try {
128 $this->saveEvent($jobId, $event);
129 } catch (\Exception $e) {
130 \WPStaging\functions\debug_log("WP STAGING: Could not save finish event analytics data for job ID $jobId.", 'debug', false);
131 }
132 }
133
134
135
136
137 public static function enqueueCancelEvent($jobId)
138 {
139 try {
140 $event = static::getEventByJobId($jobId);
141 } catch (\Exception $e) {
142 \WPStaging\functions\debug_log("WP STAGING: Could not register cancel event analytics data for job ID $jobId", 'debug', false);
143
144 return;
145 }
146
147
148 if ($event->is_cancelled) {
149 return;
150 }
151
152
153
154
155
156
157 if ($event->is_error) {
158 return;
159 }
160
161 $event->is_finished = false;
162 $event->is_cancelled = true;
163 $event->end_time = time();
164 $event->duration = time() - $event->start_time;
165 $event->ready_to_send = true;
166
167 try {
168 static::saveEvent($jobId, $event);
169 } catch (\Exception $e) {
170 \WPStaging\functions\debug_log("WP STAGING: Could not save cancel event analytics data for job ID $jobId.", 'debug', false);
171 }
172 }
173
174
175
176
177 public static function enqueueErrorEvent($jobId, $errorMessage, string $errorCode = '')
178 {
179 try {
180 $event = static::getEventByJobId($jobId);
181 } catch (\Exception $e) {
182 \WPStaging\functions\debug_log("WP STAGING: Could not register cancel event analytics data for job ID $jobId", 'debug', false);
183
184 return;
185 }
186
187 $lastDebugLogErrors = WPStaging::make(DebugLogReader::class)->getLastLogEntries(8 * KB_IN_BYTES);
188
189 $event->is_finished = false;
190 $event->is_error = true;
191 $event->error_message = $errorMessage;
192 $event->error_code = ErrorCode::sanitize($errorCode);
193 $event->last_debug_logs = $lastDebugLogErrors;
194 $event->end_time = time();
195 $event->duration = time() - $event->start_time;
196 $event->ready_to_send = true;
197
198 try {
199 static::saveEvent($jobId, $event);
200 } catch (\Exception $e) {
201 \WPStaging\functions\debug_log("WP STAGING: Could not save cancel event analytics data for job ID $jobId.", 'debug', false);
202 }
203 }
204
205 protected static function getEventByJobId($jobId)
206 {
207 if (!$event = get_option("wpstg_analytics_event_$jobId")) {
208 throw new \UnexpectedValueException();
209 }
210
211 $event = json_decode($event);
212
213 if (empty($event) || !is_object($event)) {
214 throw new \UnexpectedValueException();
215 }
216
217 return $event;
218 }
219
220 protected static function saveEvent($jobId, $event)
221 {
222 $event = wp_json_encode($event);
223
224 if (!update_option("wpstg_analytics_event_$jobId", $event, false)) {
225 throw new \UnexpectedValueException();
226 }
227 }
228
229
230
231
232
233
234
235 protected function getEventDataValue($eventData, string $key, $default = null)
236 {
237 if (is_array($eventData) && array_key_exists($key, $eventData)) {
238 return $eventData[$key];
239 }
240
241 if (!is_object($eventData)) {
242 return $default;
243 }
244
245 $publicProperties = get_object_vars($eventData);
246 if (array_key_exists($key, $publicProperties)) {
247 return $publicProperties[$key];
248 }
249
250 $getter = 'get' . ucfirst($key);
251 if (method_exists($eventData, $getter)) {
252 return $eventData->{$getter}();
253 }
254
255 $isser = 'is' . ucfirst($key);
256 if (method_exists($eventData, $isser)) {
257 return $eventData->{$isser}();
258 }
259
260 return $default;
261 }
262
263 protected function getStagingEngine($eventData): string
264 {
265 $engine = $this->getEventDataValue($eventData, 'stagingEngine', 'legacy');
266
267 return in_array($engine, ['legacy', 'next_gen'], true) ? $engine : 'legacy';
268 }
269 }
270