PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 2.1.14
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v2.1.14
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 / Analytics / Metrics / Metric.php

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

288 lines 7.7 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.1.14 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2024 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\Analytics\Metrics;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 abstract class Metric
20 {
21 protected $start_date = null;
22
23 protected $end_date = null;
24
25 protected $filters = [];
26
27 protected $offset = null;
28
29 protected $limit = null;
30
31 protected $type = 'list';
32
33 protected $query_placeholders = [];
34
35 protected $sql_filters = '';
36
37 protected $wpdb;
38
39 protected $options = [];
40
41 protected $is_single_day = false;
42
43 /**
44 * @param $type Can be either "list" or "count"
45 */
46 public function __construct($start_date = null, $end_date = null, $type = 'list', $options = [])
47 {
48 global $wpdb;
49 $this->wpdb = $wpdb;
50
51 $this->start_date = $start_date;
52 $this->end_date = $end_date;
53 $this->type = $type;
54 $this->query_placeholders = [
55 $this->start_date,
56 $this->end_date
57 ];
58 $this->options = $options;
59 $this->is_single_day = \FireBox\Core\Analytics\Helpers\Date::isSingleDay($this->start_date, $this->end_date);
60 }
61
62 protected function applyFilters()
63 {
64 $table_name = 'l';
65
66 if (get_class($this) === 'FireBox\Core\Analytics\Metrics\Conversions')
67 {
68 $table_name = 'bl';
69 }
70
71 // Campaign
72 if (array_key_exists('campaign', $this->filters) && isset($this->filters['campaign']['value']) && is_array($this->filters['campaign']['value']))
73 {
74 $this->sql_filters .= 'AND ' . $table_name . '.box IN (' . implode(',', array_map('intval', $this->filters['campaign']['value'])) . ')';
75 }
76
77 // Country
78 if (array_key_exists('country', $this->filters) && isset($this->filters['country']['value']) && is_array($this->filters['country']['value']))
79 {
80 $this->sql_filters .= 'AND ' . $table_name . '.country IN (' . implode(',', array_fill(0, count($this->filters['country']['value']), array_merge($this->query_placeholders, $this->filters['country']['value']))) . ')';
81 }
82
83 // Device
84 if (array_key_exists('device', $this->filters) && isset($this->filters['device']['value']) && is_array($this->filters['device']['value']))
85 {
86 $this->sql_filters .= 'AND ' . $table_name . '.device IN (' . implode(',', array_fill(0, count($this->filters['device']['value']), array_merge($this->query_placeholders, $this->filters['device']['value']))) . ')';
87 }
88
89 // Event
90 if (array_key_exists('event', $this->filters) && isset($this->filters['event']['value']) && is_array($this->filters['event']['value']))
91 {
92 $this->sql_filters .= 'AND ld.event IN (' . implode(',', array_fill(0, count($this->filters['event']['value']), array_merge($this->query_placeholders, $this->filters['event']['value']))) . ')';
93 }
94
95 $allowed_types = ['contains', 'not_contains', 'equals'];
96
97 // Page
98 if (array_key_exists('page', $this->filters) && isset($this->filters['page']['value']) && isset($this->filters['page']['type']) && is_scalar($this->filters['page']['type']) && array_intersect([$this->filters['page']['type']], $allowed_types) && is_array($this->filters['page']['value']))
99 {
100 $type = $this->filters['page']['type'];
101 foreach ($this->filters['page']['value'] as $page)
102 {
103 if ($type === 'contains')
104 {
105 $this->sql_filters .= 'AND ' . $table_name . '.page LIKE %s';
106 $this->query_placeholders[] = '%' . $page . '%';
107 }
108 else if ($type === 'not_contains')
109 {
110 $this->sql_filters .= 'AND ' . $table_name . '.page NOT LIKE %s';
111 $this->query_placeholders[] = '%' . $page . '%';
112 }
113 else if ($type === 'equals')
114 {
115 $this->sql_filters .= 'AND ' . $table_name . '.page = %s';
116 $this->query_placeholders[] = $page;
117 }
118 }
119 }
120
121 // Referrer
122 if (array_key_exists('referrer', $this->filters) && isset($this->filters['referrer']['value']) && isset($this->filters['referrer']['type']) && is_scalar($this->filters['referrer']['type']) && array_intersect([$this->filters['referrer']['type']], $allowed_types) && is_array($this->filters['referrer']['value']))
123 {
124 $type = $this->filters['referrer']['type'];
125 foreach ($this->filters['referrer']['value'] as $referrer)
126 {
127 if ($type === 'contains')
128 {
129 $this->sql_filters .= 'AND ' . $table_name . '.referrer LIKE %s';
130 $this->query_placeholders[] = '%' . $referrer . '%';
131 }
132 else if ($type === 'not_contains')
133 {
134 $this->sql_filters .= 'AND ' . $table_name . '.referrer NOT LIKE %s';
135 $this->query_placeholders[] = '%' . $referrer . '%';
136 }
137 else if ($type === 'equals')
138 {
139 $this->sql_filters .= 'AND ' . $table_name . '.referrer = %s';
140 $this->query_placeholders[] = $referrer;
141 }
142 }
143 }
144 }
145
146 protected function getJOINs()
147 {
148 if (get_class($this) !== 'FireBox\Core\Analytics\Metrics\Views')
149 {
150 return;
151 }
152
153 $joins = '';
154
155 if ((array_key_exists('event', $this->filters) && isset($this->filters['event']['value']) && is_array($this->filters['event']['value']) && count($this->filters['event']['value'])) || $this->type === 'events')
156 {
157 $joins .= "LEFT JOIN {$this->wpdb->prefix}firebox_logs_details as ld
158 ON ld.log_id = l.id";
159 }
160
161 return $joins;
162 }
163
164 abstract public function getData();
165
166 protected function getWherePeriod($prefix = 'l')
167 {
168 if (!$this->start_date || !$this->end_date)
169 {
170 return;
171 }
172
173 return ' AND ' . $prefix . '.date BETWEEN outer_query.start_date AND outer_query.end_date';
174 }
175
176 public function onAfterGetData(&$data = [])
177 {
178 // Fix timezone on dates
179 if ($this->isSingleDay() || in_array($this->type, ['popular_view_times']))
180 {
181 \FireBox\Core\Analytics\Helpers\Date::fixTimezoneInHourlyData($data);
182 }
183
184 // Transform country codes to names when the country filter is used
185 if ($this->type === 'countries')
186 {
187 foreach ($data as &$item)
188 {
189 $item->code = $item->label;
190 $item->label = \FPFramework\Helpers\CountriesHelper::getCountryName($item->label) ?? $item->label;
191 }
192 }
193
194 // Set the device name
195 if ($this->type === 'devices')
196 {
197 foreach ($data as &$item)
198 {
199 $item->label = fpframework()->_('FPF_' . strtoupper($item->label));
200 }
201 }
202
203 // Set the event name
204 if ($this->type === 'events')
205 {
206 foreach ($data as &$item)
207 {
208 $item->label = fpframework()->_('FPF_' . strtoupper($item->label) . '_EVENT');
209 }
210 }
211
212 // Remove https://www. from URLs
213 if ($this->type === 'referrers')
214 {
215 $regex = '/^(https?:\/\/(www\.)?|www\.)/i';
216
217 foreach ($data as &$item)
218 {
219 $item->full_label = $item->label;
220 $item->label = rtrim(preg_replace($regex, '', $item->label), '/');
221 }
222 }
223
224 // Remove site from URLs
225 if ($this->type === 'pages')
226 {
227 $site_url = get_site_url();
228
229 foreach ($data as &$item)
230 {
231 $item->full_label = $item->label;
232 $item->label = str_replace($site_url, '', $item->label);
233 }
234 }
235
236 // Make top campaigns linkable
237 if ($this->type === 'top_campaign')
238 {
239 $baseURL = admin_url('admin.php?page=firebox-analytics&campaign=');
240 foreach ($data as &$item)
241 {
242 $item->link = $baseURL . $item->id;
243 }
244 }
245 }
246
247 protected function getLimit()
248 {
249 if (!is_scalar($this->limit))
250 {
251 return;
252 }
253
254 $this->query_placeholders[] = $this->limit;
255 return 'LIMIT %d';
256 }
257
258 protected function getOffset()
259 {
260 if (!is_scalar($this->offset))
261 {
262 return;
263 }
264
265 $this->query_placeholders[] = $this->offset;
266 return 'OFFSET %d';
267 }
268
269 public function setFilters($filters = [])
270 {
271 $this->filters = $filters;
272 }
273
274 public function setOffset($offset = null)
275 {
276 $this->offset = (int) $offset;
277 }
278
279 public function setLimit($limit = null)
280 {
281 $this->limit = (int) $limit;
282 }
283
284 public function isSingleDay()
285 {
286 return $this->is_single_day;
287 }
288 }