PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.1.8
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.1.8
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 / FB / Track.php

Track.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 3.1.8, at Inc/Core/FB/Track.php

288 lines 7.5 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 3.1.8 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2026 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\FB;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 class Track
20 {
21 /**
22 * Factory
23 *
24 * @var Factory
25 */
26 private $factory;
27
28 public function __construct($factory = null)
29 {
30 if (!$factory)
31 {
32 $factory = new \FPFramework\Base\Factory();
33 }
34 $this->factory = $factory;
35
36 $this->trackConversions();
37
38 $this->setupAjax();
39 }
40
41 /**
42 * Setup ajax requests
43 *
44 * @return void
45 */
46 public function setupAjax()
47 {
48 // FB Track event AJAX
49 add_action('wp_ajax_firebox_trackevent', [$this, 'firebox_trackevent']);
50 add_action('wp_ajax_nopriv_firebox_trackevent', [$this, 'firebox_trackevent']);
51
52 // FB Track conversion AJAX
53 add_action('wp_ajax_firebox_trackconversion', [$this, 'firebox_trackconversion']);
54 add_action('wp_ajax_nopriv_firebox_trackconversion', [$this, 'firebox_trackconversion']);
55 }
56
57 /**
58 * Track Conversions
59 *
60 * @return void
61 */
62 public function trackConversions()
63 {
64 $key = 'firebox_conversions_tracker';
65
66 // Get conversions
67 $conversions = isset($_COOKIE['firebox_conversions_tracker']) ? json_decode(sanitize_text_field(wp_unslash($_COOKIE['firebox_conversions_tracker'])), true) : false;
68 if (!$conversions)
69 {
70 return;
71 }
72
73 // Delete cookie
74 unset($_COOKIE[$key]);
75 setcookie($key, '', time() - 3600, "/");
76
77 // Track conversions in db
78 foreach ($conversions as $campaign_id => $data)
79 {
80 if (!isset($data['box_log_id']) || !$data['box_log_id'])
81 {
82 continue;
83 }
84
85 if (!isset($data['source']))
86 {
87 continue;
88 }
89
90 if (!isset($data['label']))
91 {
92 continue;
93 }
94
95 $data = [
96 'log_id' => $data['box_log_id'],
97 'event' => 'conversion',
98 'event_source' => $data['source'],
99 'event_label' => $data['label'],
100 'date' => $this->factory->getDate()->format('Y-m-d H:i:s')
101 ];
102
103 firebox()->tables->boxlogdetails->insert($data);
104 }
105 }
106 /**
107 * Box Track Event
108 *
109 * @return string
110 */
111 public function firebox_trackevent()
112 {
113 $nonce = isset($_GET['nonce']) ? sanitize_text_field(wp_unslash($_GET['nonce'])) : '';
114
115 // verify nonce
116 if (!$verify = wp_verify_nonce($nonce, 'fbox_js_nonce'))
117 {
118 return false;
119 }
120
121 $event = isset($_GET['event']) ? sanitize_text_field(wp_unslash($_GET['event'])) : '';
122 $box_id = isset($_GET['box']) ? sanitize_text_field(wp_unslash($_GET['box'])) : '';
123 $page = '';
124 $referrer = '';
125 $box_log_id = '';
126
127 $response['success'] = false;
128
129 // ensure non-empty values
130 if (empty($event) || empty($box_id))
131 {
132 echo wp_json_encode($response);
133 wp_die();
134 }
135
136 // ensure valid event
137 if (!in_array($event, ['open', 'close']))
138 {
139 echo wp_json_encode($response);
140 wp_die();
141 }
142
143 // If its a close event, it should also provide the box log ID in order to add close date of box
144 if ($event == 'close')
145 {
146 if (!isset($_GET['box_log_id']))
147 {
148 return;
149 }
150
151 $box_log_id = sanitize_text_field(wp_unslash($_GET['box_log_id']));
152 }
153 else if ($event == 'open')
154 {
155 if (!isset($_GET['page']) && !isset($_GET['referrer']))
156 {
157 return;
158 }
159
160 $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';
161 $referrer = isset($_GET['referrer']) ? sanitize_text_field(wp_unslash($_GET['referrer'])) : '';
162 }
163
164 // Load box settings
165 if (!$box = firebox()->box->get($box_id))
166 {
167 return;
168 }
169
170 // Don't track events on unpublished campaigns
171 $track_unpublished = apply_filters('firebox/track_unpublished', false);
172 if (!$track_unpublished && $box->post_status !== 'publish')
173 {
174 return;
175 }
176
177 /**
178 * Trigger Open & Close Event.
179 */
180 do_action('firebox/box/on_' . $event, $box);
181
182 $response['success'] = true;
183 if ($event == 'open')
184 {
185 $response['box_log_id'] = $this->handleOpenEvent($box, $box_id, $page, $referrer);
186 }
187
188 // Log impression in the database
189 if ($event == 'close')
190 {
191 // get options
192 $options = (isset($_GET['options']) && is_array($_GET['options']))
193 ? array_map('sanitize_text_field', wp_unslash($_GET['options']))
194 : [];
195 $this->handleCloseEvent($box, $box_id, $box_log_id, $options, $response);
196 }
197
198 echo wp_json_encode($response);
199 wp_die();
200 }
201
202 /**
203 * Handle Open Event
204 *
205 * @param object $box
206 * @param int $box_id
207 * @param string $page
208 * @param string $referrer
209 *
210 * @return void
211 */
212 protected function handleOpenEvent($box, $box_id, $page, $referrer)
213 {
214 // Do not track when box is on test mode
215 if (!$box->params->get('testmode'))
216 {
217 return firebox()->box->logOpenEvent($box_id, $page, $referrer);
218 }
219 }
220
221 /**
222 * Handle Close Event
223 *
224 * @param object $box
225 * @param int $box_id
226 * @param int $box_log_id
227 * @param array $options
228 * @param array $response
229 *
230 * @return void
231 */
232 protected function handleCloseEvent($box, $box_id, $box_log_id, $options, &$response)
233 {
234 // Do not track when box is on test mode
235 if (!$box->params->get('testmode'))
236 {
237 firebox()->box->logCloseEvent($box_id, $box_log_id);
238 }
239
240 // Do not set any cookie if box is on test mode
241 if (!$box->params->get('testmode') && !isset($options['temporary']))
242 {
243 // allow to prevent cookie from being set
244 if (!apply_filters('firebox/box/close/cookie_set', true, $box_id))
245 {
246 return;
247 }
248
249 if ($this->cookieExist($box_id))
250 {
251 $response['action'] = 'stop';
252 }
253 }
254 }
255
256 /**
257 * Check whether the cookie exists.
258 *
259 * @param int $box_id
260 *
261 * @return bool
262 */
263 protected function cookieExist($box_id)
264 {
265 $cookie = new \FireBox\Core\FB\Cookie(firebox()->box->get($box_id));
266 $cookie->set();
267
268 return $cookie->exist();
269 }
270
271 /**
272 * Box Track Conversion
273 *
274 * @return string
275 */
276 public function firebox_trackconversion()
277 {
278 $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
279
280 // verify nonce
281 if (!$verify = wp_verify_nonce($nonce, 'fbox_js_nonce'))
282 {
283 return false;
284 }
285
286 $this->trackConversions();
287 }
288 }