PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 1.0.0
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v1.0.0
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 1.0.0, at Inc/Core/Admin/Analytics/Metric.php

566 lines 13.2 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 1.0.0 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2021 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 private $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 private $current_period_data;
104
105 /**
106 * Current period total
107 *
108 * @var array
109 */
110 private $current_period_total;
111
112 /**
113 * Last period data
114 *
115 * @var array
116 */
117 private $last_period_data;
118
119 /**
120 * Last period total
121 *
122 * @var array
123 */
124 private $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 private $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 * Options
177 *
178 * @var array
179 */
180 protected $options;
181
182 public function __construct($toolbar = null, $metric_data = null, $payload = null, $options = [])
183 {
184 if (!$toolbar)
185 {
186 $toolbar = new Toolbar();
187 }
188 $this->toolbar = $toolbar;
189
190 if (method_exists($this, 'onInit'))
191 {
192 $this->onInit();
193 }
194
195 if (!$payload && method_exists($this, 'getMetricSpecificData'))
196 {
197 $payload = $this->getMetricSpecificData();
198 }
199 $this->payload = $payload;
200
201 if (!$metric_data)
202 {
203 $metric_data = new MetricData($this, $this->toolbar);
204 }
205 $this->metric_data = $metric_data;
206
207 $this->options = new Registry($options);
208 }
209
210 /**
211 * Sets the metric basic data
212 *
213 * @return void
214 */
215 public function setBasicData()
216 {
217 $payload = new Registry($this->payload);
218
219 // set metric data
220 $this->type = $payload->get('type');
221 $this->decimal_points = $payload->get('decimal_points');
222 $this->color = $payload->get('color');
223 $this->hover_color = $payload->get('hover_color');
224 $this->label = $payload->get('label');
225 $this->clean_label = $payload->get('clean_label');
226 $this->tooltip = $payload->get('tooltip');
227 $this->read_more_link = $payload->get('read_more_link');
228 $this->hide_from_results_toolbar_items = $payload->get('hide_from_results_toolbar_items');
229 }
230
231 /**
232 * Sets the metric data
233 *
234 * @return void
235 */
236 public function setData()
237 {
238 $skip_filling_dates = (bool) $this->options->get('skip_filling_dates', false);
239 $skip_compare_check_to_retrieve_previous_data = (bool) $this->options->get('skip_compare_check_to_retrieve_previous_data', false);
240
241 $payload = new Registry($this->payload);
242
243 // set the basic data
244 $this->setBasicData();
245
246 // current period data
247 $this->current_period_data = $this->metric_data->getCurrent();
248 $this->current_period_total = Helpers\ResultsHelper::sumTotalFromResults($this->current_period_data);
249
250 $toolbarItems = $this->toolbar->getToolbarItems();
251 $fillDatesPayload = [
252 'start_date' => $toolbarItems['date']->getStartDate(),
253 'total_days' => $toolbarItems['date']->getTotalDays(),
254
255 'options_key' => $toolbarItems['date']->getOptionsKey()
256 ];
257
258 if (!$skip_filling_dates)
259 {
260 // fill missing current data dates
261 $this->current_period_data = Helpers\MetricHelper::fillMissingData($this->current_period_data, $fillDatesPayload);
262 }
263
264 // Parse the metric total via the metric itself
265 // This is useful if the metric needs to run extra logic prior to showing the total
266 if (method_exists($this, 'parseMetricTotal'))
267 {
268 $this->current_period_total = $this->parseMetricTotal($this->current_period_total, $this->current_period_data);
269 }
270
271 $this->is_compare = false;
272
273
274 $percentage_change = 0;
275 // if we don't have a compare selected toolbar item, then get percentage from previous period
276 if (!$skip_compare_check_to_retrieve_previous_data && !$this->is_compare)
277 {
278 // get last period data only for db related metrics
279 // non-db related metrics handle their percentage change in Metrics.php method setMetricsData()
280 if ($this->getDataSource() == 'db')
281 {
282 $percentage_change = $this->metric_data->getPercentageChangeWithPreviousPeriod($this->current_period_total);
283 }
284 }
285 else
286 {
287 // last period data
288 $this->last_period_data = $this->metric_data->getLastPeriod();
289 $this->last_period_total = Helpers\ResultsHelper::sumTotalFromResults($this->last_period_data);
290
291 // check if the metric needs to parse the metric total
292 if (method_exists($this, 'parseMetricTotal'))
293 {
294 $this->last_period_total = $this->parseMetricTotal($this->last_period_total, $this->last_period_data);
295 }
296
297 if (!$skip_filling_dates)
298 {
299 // We are comparing and we have re-calculated the start date, so fetch it and set it so display accurate data on x axis
300 $fillDatesPayload['start_date'] = $toolbarItems['date']->getStartDate();
301
302 // fill missing last period data dates
303 $this->last_period_data = Helpers\MetricHelper::fillMissingData($this->last_period_data, $fillDatesPayload);
304 }
305
306 // otherwise if we are comparing, calculate a local percentage change from the 2 actual metrics(current and last period)
307 $percentage_change = $this->getLocalMetricPercentageChange($this->current_period_total, $this->last_period_total);
308 }
309 $this->percentage_change = $percentage_change;
310 }
311
312 /**
313 * Returns all metric data
314 *
315 * @return array
316 */
317 public function getData()
318 {
319 $selected_toolbar_items = $this->toolbar->getSelectedToolbarItems();
320
321
322
323 return [
324 'type' => $this->type,
325 'period_key' => $selected_toolbar_items['date']['options_key'],
326 'data_source' => $this->data_source,
327 'data_source_input' => $this->data_source_input,
328 'suffix' => $this->suffix,
329 'decimal_points' => $this->decimal_points,
330 'label' => $this->label,
331 'color' => $this->color,
332 'hover_color' => $this->hover_color,
333 'clean_label' => $this->clean_label,
334 'compare' => $this->is_compare,
335 'current_period' => [
336 'data' => $this->current_period_data,
337 'total' => $this->current_period_total,
338
339 ],
340 'last_period' => [
341 'data' => $this->last_period_data,
342 'total' => $this->last_period_total,
343
344 ],
345 'tooltip' => $this->tooltip,
346 'read_more_link' => $this->read_more_link,
347 'hide_from_results_toolbar_items' => $this->hide_from_results_toolbar_items,
348 'percentage_change' => $this->percentage_change,
349 'toolbar_items' => $selected_toolbar_items,
350 'toolbar_items_url_safe' => Helpers\ToolbarHelper::dataToURLParam('toolbar_items', $selected_toolbar_items),
351 'metric' => $this
352 ];
353 }
354
355 /**
356 * Returns the local metric percentage change
357 *
358 * @param int $current_period_total
359 * @param int $last_period_total
360 *
361 * @return float
362 */
363 public function getLocalMetricPercentageChange($current_period_total, $last_period_total)
364 {
365 if (!$last_period_total)
366 {
367 return 0;
368 }
369
370 $percentage = (($current_period_total - $last_period_total) / $last_period_total) * 100;
371
372 return $percentage;
373 }
374
375 /**
376 * If we are fetching the metric data from other metrics data then let
377 * each metric handle the process
378 *
379 * @param array $metrics_data
380 *
381 * @return array
382 */
383 public function getMetricDataFromOtherMetrics($metrics_data)
384 {
385 $data = [];
386
387 $data = apply_filters('firebox/analytics/metrics/' . $this->type . '/filter/source_type/metrics', $metrics_data);
388
389 return $data;
390 }
391
392 public function getMetricData()
393 {
394 return $this->metric_data;
395 }
396
397 /**
398 * Get type
399 *
400 * @return string
401 */
402 public function getType()
403 {
404 return $this->type;
405 }
406
407 /**
408 * Get Label
409 *
410 * @return string
411 */
412 public function getLabel()
413 {
414 return $this->label;
415 }
416
417 /**
418 * Get Tooltip
419 *
420 * @return string
421 */
422 public function getTooltip()
423 {
424 return $this->tooltip;
425 }
426
427 /**
428 * Get Read More Link
429 *
430 * @return string
431 */
432 public function getReadMoreLink()
433 {
434 return $this->read_more_link;
435 }
436
437 /**
438 * Get Precentage Change
439 *
440 * @return float
441 */
442 public function getPercentageChange()
443 {
444 return $this->percentage_change;
445 }
446
447 /**
448 * Get Current Period Metric Total
449 *
450 * @return float
451 */
452 public function getCurrentPeriodTotal()
453 {
454 return $this->current_period_total;
455 }
456
457 /**
458 * Get Current Period Metric Data
459 *
460 * @return float
461 */
462 public function getCurrentPeriodData()
463 {
464 return $this->current_period_data;
465 }
466
467 /**
468 * Get Last Period Metric Value
469 *
470 * @return float
471 */
472 public function getLastPeriodTotal()
473 {
474 return $this->last_period_total;
475 }
476
477 /**
478 * Get Last Period Metric Data
479 *
480 * @return float
481 */
482 public function getLastPeriodData()
483 {
484 return $this->last_period_data;
485 }
486
487 /**
488 * Get color
489 *
490 * @return float
491 */
492 public function getColor()
493 {
494 return $this->color;
495 }
496
497 /**
498 * Get hover color
499 *
500 * @return float
501 */
502 public function getHoverColor()
503 {
504 return $this->hover_color;
505 }
506
507 /**
508 * Returns the clean label
509 *
510 * @return boolean
511 */
512 public function getCleanLabel()
513 {
514 return $this->clean_label;
515 }
516
517 public function getDataSource()
518 {
519 return $this->data_source;
520 }
521
522 public function getDataSourceInput()
523 {
524 return $this->data_source_input;
525 }
526
527 public function getSuffix()
528 {
529 return $this->suffix;
530 }
531
532 public function getDecimalPoints()
533 {
534 return $this->decimal_points;
535 }
536
537 public function getToolbar()
538 {
539 return $this->toolbar;
540 }
541
542 protected function getConfiguration()
543 {
544 return $this->toolbar->getConfiguration();
545 }
546
547 public function setOptions($options)
548 {
549 $this->options = new Registry($options);
550 }
551
552 protected function getOptions()
553 {
554 return $this->options;
555 }
556
557 public function getPayload()
558 {
559 return $this->payload;
560 }
561
562 public function setToolbar($toolbar)
563 {
564 $this->toolbar = $toolbar;
565 }
566 }