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
← All changes | Inc/Core/Admin/Analytics/Metrics.php +69 -151 1.0.112.0.12 View file →
@@ -1,12 +1,12 @@
1 1 <?php
2 2 /**
3 3 * @package FireBox
4 - * @version 1.0.11 Free
4 + * @version 2.0.12 Free
5 5 *
6 6 * @author FirePlugins <info@fireplugins.com>
7 7 * @link https://www.fireplugins.com
8 - * @copyright Copyright © 2022 FirePlugins All Rights Reserved
8 + * @copyright Copyright © 2023 FirePlugins All Rights Reserved
9 9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 10 */
11 11
12 12 namespace FireBox\Core\Admin\Analytics;
@@ -29,70 +29,48 @@
29 29 'impressions'
30 30 ];
31 31
32 32 /**
33 - * Toolbar
33 + * Default Metrics.
34 34 *
35 35 * @var array
36 36 */
37 - private $toolbar;
37 + private $metrics_map = [
38 + 'impressions' => [
39 + 'class' => 'Impressions',
40 + ],
41 + 'averagetimeopen' => [
42 + 'class' => 'AverageTimeOpen'
43 + ],
44 + 'submissions' => [
45 + 'class' => 'Submissions'
46 + ],
47 + 'conversionrate' => [
48 + 'class' => 'ConversionRate'
49 + ]
50 + ];
38 51
39 52 /**
40 - * Analytics Metrics
53 + * Metrics.
41 54 *
42 55 * @var array
43 56 */
44 - private $metrics;
57 + private $metrics = [];
45 58
46 - /**
47 - * Metrics Data
48 - *
49 - * @var array
50 - */
51 - private $metrics_data;
59 + private $toolbar;
52 60
53 - /**
54 - * Metrics Constructor
55 - *
56 - * @param array $toolbar
57 - * @param array $metrics
58 - *
59 - * @return void
60 - */
61 - public function __construct($toolbar = null, $metrics = null)
61 + public function __construct($toolbar = null)
62 62 {
63 - if (!$toolbar)
64 - {
65 - $toolbar = new Toolbar();
66 - }
67 63 $this->toolbar = $toolbar;
68 -
69 - if (!$metrics)
70 - {
71 - // initialize the metrics
72 - $metrics = $this->initializeMetrics();
73 - }
74 - $this->metrics = $metrics;
75 64 }
76 65
77 66 /**
78 - * Init metrics
67 + * Render metrics.
79 68 *
80 69 * @return void
81 70 */
82 - public function init()
71 + public function render()
83 72 {
84 - // hook to render metrics
85 - add_action('firebox/analytics/metrics', [$this, 'renderMetrics']);
86 - }
87 -
88 - /**
89 - * Render metrics
90 - *
91 - * @return void
92 - */
93 - public function renderMetrics()
94 - {
95 73 firebox()->renderer->admin->render('analytics/metrics/tmpl');
96 74 }
97 75
98 76 /**
@@ -99,19 +77,54 @@
99 77 * Gets all metric data with chart-related data
100 78 *
101 79 * @return array
102 80 */
103 - public function getMetricsData()
81 + public function getMetrics()
104 82 {
105 - if ($this->metrics_data)
83 + if (count($this->metrics))
106 84 {
107 - return $this->metrics_data;
85 + return $this->metrics;
108 86 }
109 87
110 - // set the metrics data
111 - $this->setMetricsData();
88 + $this->initializeMetrics();
89 +
90 + return $this->metrics;
91 + }
92 +
93 + public function getMetricsData($update = true)
94 + {
95 + $data = [];
96 +
97 + $metrics = $this->getMetrics();
98 +
99 + foreach ($metrics as $key => $metric)
100 + {
101 + // Metrics that require other metrics to generate data
102 + if ($metric->getDataSource() === 'metrics')
103 + {
104 + $found_metrics = array_intersect_key($metrics, array_flip($metric->getDataSourceInput()));
105 +
106 + if ($found_metrics)
107 + {
108 + // Skip fill dates
109 + $metric_opts = $metric->getOptionsRaw();
110 + $metric_opts['skip_filling_dates'] = true;
111 + $metric->setOptions($metric_opts);
112 +
113 + // Set custom metrics
114 + $metric->setCustomMetrics($found_metrics);
115 + }
116 + }
112 117
113 - return $this->metrics_data;
118 + if ($update)
119 + {
120 + $metric->update();
121 + }
122 +
123 + $data[$key] = $metric->getData();
124 + }
125 +
126 + return $data;
114 127 }
115 128
116 129 /**
117 130 * Returns the default active metrics
@@ -131,9 +144,9 @@
131 144 public function getMetricsLabels()
132 145 {
133 146 $metrics = [];
134 147
135 - foreach ($this->metrics as $key => $metric)
148 + foreach ($this->getMetrics() as $key => $metric)
136 149 {
137 150 if (!$metric instanceof Metric)
138 151 {
139 152 continue;
@@ -138,10 +151,8 @@
138 151 {
139 152 continue;
140 153 }
141 154
142 - $metric->setBasicData();
143 -
144 155 $metrics[$key] = $metric->getCleanLabel();
145 156 }
146 157
147 158 return $metrics;
@@ -147,57 +158,8 @@
147 158 return $metrics;
148 159 }
149 160
150 161 /**
151 - * Creates and sets an array of each metric and their data
152 - *
153 - * @return void
154 - */
155 - public function setMetricsData()
156 - {
157 - $compareLabels = $this->toolbar->getComparedLabelsFromSelectedToolbarItems();
158 -
159 - // get all db-related metrics
160 - $extra_data = [
161 - 'data_source' => [ 'db' ]
162 - ];
163 -
164 - // get the metric data
165 - $data = Helpers\AnalyticsHelper::getData($this->getMetricsKeys(), $this->toolbar->getSelectedToolbarItems(), $extra_data);
166 -
167 - // loop all metrics that depend on other metrics to calculate their data
168 - foreach ($this->metrics as $key => $metric)
169 - {
170 - // skip non-metrics items
171 - if ($metric->getDataSource() != 'metrics')
172 - {
173 - continue;
174 - }
175 -
176 - // if the metric does not depend on any other metric, skip
177 - if (empty($metric->getDataSourceInput()))
178 - {
179 - continue;
180 - }
181 -
182 - // get all dependend metrics data
183 - $metrics_data = [];
184 - foreach ($metric->getDataSourceInput() as $_metric)
185 - {
186 - $metrics_data[$_metric] = $data[$_metric];
187 - }
188 -
189 - // get the data from the metric
190 - $newdata = $metric->getMetricDataFromOtherMetrics($metrics_data);
191 -
192 - // merge them with the current data
193 - $data[$key] = array_merge_recursive($data[$key], $newdata);
194 - }
195 -
196 - $this->metrics_data = $data;
197 - }
198 -
199 - /**
200 162 * Initialize all metrics items
201 163 *
202 164 * @return array
203 165 */
@@ -202,62 +164,18 @@
202 164 * @return array
203 165 */
204 166 private function initializeMetrics()
205 167 {
206 - // get all Metric Namespace Files
207 - if (!$files = \scandir(__DIR__ . '/Metrics'))
168 + foreach ($this->metrics_map as $metric => $value)
208 169 {
209 - return [];
210 - }
170 + $class = '\FireBox\Core\Admin\Analytics\Metrics\\' . $value['class'];
211 171
212 - $items = [];
213 -
214 - foreach ($files as $key => $file)
215 - {
216 - // ignore unimportant files
217 - if (in_array($file, ['index.php', '.', '..']))
172 + if (!class_exists($class))
218 173 {
219 174 continue;
220 175 }
221 176
222 - $file = basename($file, '.php');
223 -
224 - // instantiate class
225 - $className = '\FireBox\Core\Admin\Analytics\Metrics\\' . $file;
226 -
227 - if (!class_exists($className))
228 - {
229 - continue;
230 - }
231 -
232 177 // init metric and fetch its data
233 - $metric = new $className($this->toolbar);
234 -
235 - // set metric data
236 - $items[strtolower($file)] = $metric;
178 + $this->metrics[$metric] = new $class($this->toolbar);
237 179 }
238 -
239 - return $items;
240 - }
241 -
242 - public function getMetricsKeys()
243 - {
244 - $keys = [];
245 -
246 - foreach ($this->metrics as $key => $metric)
247 - {
248 - $keys[] = $key;
249 - }
250 -
251 - return $keys;
252 - }
253 -
254 - /**
255 - * Return the metrics
256 - *
257 - * @return array
258 - */
259 - public function getMetrics()
260 - {
261 - return $this->metrics;
262 180 }
263 181 }