PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 2.1.37
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v2.1.37
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 2.1.37, at Inc/Core/FB/Track.php

288 lines 7.4 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.37 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2025 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 /**
108 * Box Track Event
109 *
110 * @return string
111 */
112 public function firebox_trackevent()
113 {
114 $nonce = isset($_GET['nonce']) ? sanitize_text_field(wp_unslash($_GET['nonce'])) : '';
115
116 // verify nonce
117 if (!$verify = wp_verify_nonce($nonce, 'fbox_js_nonce'))
118 {
119 return false;
120 }
121
122 $event = isset($_GET['event']) ? sanitize_text_field(wp_unslash($_GET['event'])) : '';
123 $box_id = isset($_GET['box']) ? sanitize_text_field(wp_unslash($_GET['box'])) : '';
124 $page = '';
125 $referrer = '';
126 $box_log_id = '';
127
128 $response['success'] = false;
129
130 // ensure non-empty values
131 if (empty($event) || empty($box_id))
132 {
133 echo wp_json_encode($response);
134 wp_die();
135 }
136
137 // ensure valid event
138 if (!in_array($event, ['open', 'close']))
139 {
140 echo wp_json_encode($response);
141 wp_die();
142 }
143
144 // If its a close event, it should also provide the box log ID in order to add close date of box
145 if ($event == 'close')
146 {
147 if (!isset($_GET['box_log_id']))
148 {
149 return;
150 }
151
152 $box_log_id = sanitize_text_field(wp_unslash($_GET['box_log_id']));
153 }
154 else if ($event == 'open')
155 {
156 if (!isset($_GET['page']) && !isset($_GET['referrer']))
157 {
158 return;
159 }
160
161 $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';
162 $referrer = isset($_GET['referrer']) ? sanitize_text_field(wp_unslash($_GET['referrer'])) : '';
163 }
164
165 // Load box settings
166 if (!$box = firebox()->box->get($box_id))
167 {
168 return;
169 }
170
171 // Don't track events on unpublished campaigns
172 $track_unpublished = apply_filters('firebox/track_unpublished', false);
173 if (!$track_unpublished && $box->post_status !== 'publish')
174 {
175 return;
176 }
177
178 /**
179 * Trigger Open & Close Event.
180 */
181 do_action('firebox/box/on_' . $event, $box);
182
183 $response['success'] = true;
184
185 if ($event == 'open')
186 {
187 $response['box_log_id'] = $this->handleOpenEvent($box, $box_id, $page, $referrer);
188 }
189
190 // Log impression in the database
191 if ($event == 'close')
192 {
193 // get options
194 $options = isset($_GET['options']) ? array_map('sanitize_text_field', wp_unslash($_GET['options'])) : [];
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 }