PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 2.0.12
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v2.0.12
3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 122 releases
firebox / Inc / Core / Admin / Analytics / Metric.php

Metric.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 2.0.12, at Inc/Core/Admin/Analytics/Metric.php

601 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FireBox
4 * @version 2.0.12 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2023 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\Admin\Analytics;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FPFramework\Libs\Registry;
20
21 class Metric
22 {
23 /**
24 * Where the data are coming from.
25 * Options :
26 *
27 * [db] : By default they should be fetched from the DB via the selected Toolbar Items.
28 * [metrics] : It is also possible to produce metric data by combining other metrics data
29 *
30 * @return string
31 */
32 protected $data_source = 'db';
33
34 /**
35 * If we have set `data_source` to "metrics". We need some metric names to manipulate
36 * their values in order to produce metric data
37 *
38 * @var array
39 */
40 protected $data_source_input = [];
41
42 /**
43 * Type of metric
44 *
45 * @var string
46 */
47 protected $type;
48
49 /**
50 * Suffix that appears next to the metric total, chart values
51 *
52 * @var string
53 */
54 protected $suffix;
55
56 /**
57 * Results decimal points
58 *
59 * @var int
60 */
61 protected $decimal_points;
62
63 /**
64 * Label of metric
65 *
66 * @var string
67 */
68 private $label;
69
70 /**
71 * Whether we are comparing
72 *
73 * @var boolean
74 */
75 private $is_compare;
76
77 /**
78 * Metric color
79 *
80 * @var string
81 */
82 private $color;
83
84 /**
85 * Metric Hover color
86 *
87 * @var string
88 */
89 private $hover_color;
90
91 /**
92 * Clean Label of metric
93 *
94 * @var string
95 */
96 private $clean_label;
97
98 /**
99 * Current period data
100 *
101 * @var array
102 */
103 protected $current_period_data;
104
105 /**
106 * Current period total
107 *
108 * @var array
109 */
110 protected $current_period_total;
111
112 /**
113 * Last period data
114 *
115 * @var array
116 */
117 protected $last_period_data;
118
119 /**
120 * Last period total
121 *
122 * @var array
123 */
124 protected $last_period_total;
125
126 /**
127 * Tooltip of metric
128 *
129 * @var string
130 */
131 private $tooltip;
132
133 /**
134 * Read more URL of metric
135 *
136 * @var string
137 */
138 private $read_more_link;
139
140 /**
141 * The toolbar items from which the metric will not appear in the search results.
142 *
143 * @var string
144 */
145 private $hide_from_results_toolbar_items = [];
146
147 /**
148 * Percentage Change of metric
149 *
150 * @var float
151 */
152 protected $percentage_change;
153
154 /**
155 * Toolbar
156 *
157 * @var Toolbar
158 */
159 protected $toolbar;
160
161 /**
162 * Payload data
163 *
164 * @var array
165 */
166 private $payload;
167
168 /**
169 * Metric Data
170 *
171 * @var MetricData
172 */
173 protected $metric_data;
174
175 /**
176 * These metrics are used to calculate the metric value
177 * when other metrics are used.
178 *
179 * @var array
180 */
181 private $custom_metrics = [];
182
183 /**
184 * Options Raw
185 *
186 * @var array
187 */
188 protected $options_raw;
189
190 /**
191 * Options
192 *
193 * @var object
194 */
195 protected $options;
196
197 public function __construct($toolbar = null, $metric_data = null, $payload = null, $options = [])
198 {
199 if (!$toolbar)
200 {
201 $toolbar = new Toolbar();
202 }
203 $this->toolbar = $toolbar;
204
205 if (method_exists($this, 'onInit'))
206 {
207 $this->onInit();
208 }
209
210 if (!$payload && method_exists($this, 'getMetricSpecificData'))
211 {
212 $payload = $this->getMetricSpecificData();
213 }
214 $this->payload = new Registry($payload);
215
216 if (!$metric_data)
217 {
218 $metric_data = new MetricData($this, $this->toolbar);
219 }
220 $this->metric_data = $metric_data;
221
222 $this->options_raw = $options;
223 $this->options = new Registry($options);
224
225 $this->setupBasicData();
226 }
227
228 public function setupBasicData()
229 {
230 // set basic metric data
231 $this->type = $this->payload->get('type');
232 $this->decimal_points = $this->payload->get('decimal_points');
233 $this->color = $this->payload->get('color');
234 $this->hover_color = $this->payload->get('hover_color');
235 $this->label = $this->payload->get('label');
236 $this->clean_label = $this->payload->get('clean_label');
237 $this->tooltip = $this->payload->get('tooltip');
238 $this->read_more_link = $this->payload->get('read_more_link');
239 $this->hide_from_results_toolbar_items = $this->payload->get('hide_from_results_toolbar_items', []);
240 }
241
242 /**
243 * Updates metric.
244 *
245 * @return void
246 */
247 public function update()
248 {
249 $this->setupBasicData();
250
251 $skip_filling_dates = (bool) $this->options->get('skip_filling_dates', false);
252 $skip_compare_check_to_retrieve_previous_data = (bool) $this->options->get('skip_compare_check_to_retrieve_previous_data', false);
253
254 // current period data
255 $this->current_period_data = $this->getPeriodData(false);
256 $this->current_period_total = Helpers\ResultsHelper::sumTotalFromResults($this->current_period_data);
257
258 $toolbarItems = $this->toolbar->getToolbarItems();
259 $fillDatesPayload = [
260 'start_date' => $toolbarItems['date']->getStartDate(),
261 'total_days' => $toolbarItems['date']->getTotalDays(),
262
263 'options_key' => $toolbarItems['date']->getOptionsKey()
264 ];
265
266 if (!$skip_filling_dates)
267 {
268 // fill missing current data dates
269 $this->current_period_data = Helpers\MetricHelper::fillMissingData($this->current_period_data, $fillDatesPayload, 'd/m/Y');
270 }
271
272 // Parse the metric total via the metric itself
273 // This is useful if the metric needs to run extra logic prior to showing the total
274 if (method_exists($this, 'parseMetricTotal'))
275 {
276 $this->current_period_total = $this->parseMetricTotal($this->current_period_total, $this->current_period_data);
277 }
278
279
280 $this->is_compare = false;
281
282
283
284 $percentage_change = 0;
285 // if we don't have a compare selected toolbar item, then get percentage from previous period
286 if (!$skip_compare_check_to_retrieve_previous_data && !$this->is_compare)
287 {
288 // get last period data only for db related metrics
289 // non-db related metrics handle their percentage change in Metrics.php method setMetricsData()
290 if ($this->getDataSource() === 'db')
291 {
292 $percentage_change = $this->metric_data->getPercentageChangeWithPreviousPeriod($this->current_period_total);
293 }
294 else
295 {
296 // TODO: Adds last period data for custom metrics
297 }
298 }
299 else
300 {
301 // last period data
302 $this->last_period_data = $this->getPeriodData(true);
303 $this->last_period_total = Helpers\ResultsHelper::sumTotalFromResults($this->last_period_data);
304
305 // check if the metric needs to parse the metric total
306 if (method_exists($this, 'parseMetricTotal'))
307 {
308 $this->last_period_total = $this->parseMetricTotal($this->last_period_total, $this->last_period_data, true);
309 }
310
311 if (!$skip_filling_dates)
312 {
313 // We are comparing and we have re-calculated the start date, so fetch it and set it so display accurate data on x axis
314 $fillDatesPayload['start_date'] = $toolbarItems['date']->getStartDate();
315
316 // fill missing last period data dates
317 $this->last_period_data = Helpers\MetricHelper::fillMissingData($this->last_period_data, $fillDatesPayload, 'd/m/Y');
318 }
319
320 // Calculate a local percentage change from the current and last period
321 $percentage_change = $this->getLocalMetricPercentageChange($this->current_period_total, $this->last_period_total);
322 }
323 $this->percentage_change = $percentage_change;
324
325 return $this;
326 }
327
328 /**
329 * Returns the metric period data.
330 *
331 * @param bool $previous_period Whether to get the data for the previous period.
332 *
333 * @return array
334 */
335 private function getPeriodData($previous_period = false)
336 {
337 return $this->getDataSource() === 'db' ? $this->metric_data->getData($previous_period) : $this->getCustomMetricData($previous_period);
338 }
339
340 /**
341 * Returns all metric data
342 *
343 * @return array
344 */
345 public function getData()
346 {
347 $selected_toolbar_items = $this->toolbar->getSelectedToolbarItems();
348
349
350
351 return [
352 'type' => $this->type,
353 'period_key' => $selected_toolbar_items['date']['options_key'],
354 'data_source' => $this->data_source,
355 'data_source_input' => $this->data_source_input,
356 'suffix' => $this->suffix,
357 'decimal_points' => $this->decimal_points,
358 'label' => $this->label,
359 'color' => $this->color,
360 'hover_color' => $this->hover_color,
361 'clean_label' => $this->clean_label,
362 'compare' => $this->is_compare,
363 'current_period' => [
364 'data' => $this->current_period_data,
365 'total' => $this->current_period_total,
366
367 ],
368 'last_period' => [
369 'data' => $this->last_period_data,
370 'total' => $this->last_period_total,
371
372 ],
373 'tooltip' => $this->tooltip,
374 'read_more_link' => $this->read_more_link,
375 'hide_from_results_toolbar_items' => $this->hide_from_results_toolbar_items,
376 'percentage_change' => $this->percentage_change,
377 'toolbar_items' => $selected_toolbar_items,
378 'toolbar_items_url_safe' => Helpers\ToolbarHelper::dataToURLParam('toolbar_items', $selected_toolbar_items),
379 'metric' => $this
380 ];
381 }
382
383 /**
384 * Returns the local metric percentage change
385 *
386 * @param int $current_period_total
387 * @param int $last_period_total
388 *
389 * @return float
390 */
391 public function getLocalMetricPercentageChange($current_period_total, $last_period_total)
392 {
393 if (!$last_period_total)
394 {
395 return 0;
396 }
397
398 $percentage = (($current_period_total - $last_period_total) / $last_period_total) * 100;
399
400 return $percentage;
401 }
402
403 public function getMetricData()
404 {
405 return $this->metric_data;
406 }
407
408 public function onResults()
409 {}
410
411 /**
412 * Get type
413 *
414 * @return string
415 */
416 public function getType()
417 {
418 return $this->type;
419 }
420
421 /**
422 * Get Label
423 *
424 * @return string
425 */
426 public function getLabel()
427 {
428 return $this->label;
429 }
430
431 /**
432 * Get Tooltip
433 *
434 * @return string
435 */
436 public function getTooltip()
437 {
438 return $this->tooltip;
439 }
440
441 /**
442 * Get Read More Link
443 *
444 * @return string
445 */
446 public function getReadMoreLink()
447 {
448 return $this->read_more_link;
449 }
450
451 /**
452 * Get Precentage Change
453 *
454 * @return float
455 */
456 public function getPercentageChange()
457 {
458 return $this->percentage_change;
459 }
460
461 /**
462 * Get Current Period Metric Total
463 *
464 * @return float
465 */
466 public function getCurrentPeriodTotal()
467 {
468 return $this->current_period_total;
469 }
470
471 /**
472 * Get Current Period Metric Data
473 *
474 * @return float
475 */
476 public function getCurrentPeriodData()
477 {
478 return $this->current_period_data;
479 }
480
481 /**
482 * Get Last Period Metric Value
483 *
484 * @return float
485 */
486 public function getLastPeriodTotal()
487 {
488 return $this->last_period_total;
489 }
490
491 /**
492 * Get Last Period Metric Data
493 *
494 * @return float
495 */
496 public function getLastPeriodData()
497 {
498 return $this->last_period_data;
499 }
500
501 /**
502 * Get color
503 *
504 * @return float
505 */
506 public function getColor()
507 {
508 return $this->color;
509 }
510
511 /**
512 * Get hover color
513 *
514 * @return float
515 */
516 public function getHoverColor()
517 {
518 return $this->hover_color;
519 }
520
521 /**
522 * Returns the clean label
523 *
524 * @return boolean
525 */
526 public function getCleanLabel()
527 {
528 return $this->clean_label;
529 }
530
531 public function getDataSource()
532 {
533 return $this->data_source;
534 }
535
536 public function getDataSourceInput()
537 {
538 return $this->data_source_input;
539 }
540
541 public function getSuffix()
542 {
543 return $this->suffix;
544 }
545
546 public function getDecimalPoints()
547 {
548 return $this->decimal_points;
549 }
550
551 public function getToolbar()
552 {
553 return $this->toolbar;
554 }
555
556 protected function getConfiguration()
557 {
558 return $this->toolbar->getConfiguration();
559 }
560
561 public function setOptions($options)
562 {
563 $this->options = new Registry($options);
564 }
565
566 public function getOptionsRaw()
567 {
568 return $this->options_raw;
569 }
570
571 public function getOptions()
572 {
573 return $this->options;
574 }
575
576 public function getPayload()
577 {
578 return $this->payload;
579 }
580
581 public function setToolbar($toolbar)
582 {
583 $this->toolbar = $toolbar;
584 $this->metric_data->setToolbar($toolbar);
585 }
586
587 public function getHideFromResultsToolbarItems()
588 {
589 return $this->hide_from_results_toolbar_items;
590 }
591
592 public function setCustomMetrics($custom_metrics)
593 {
594 $this->custom_metrics = $custom_metrics;
595 }
596
597 public function getCustomMetrics()
598 {
599 return $this->custom_metrics;
600 }
601 }