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 / FB / Track.php

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

289 lines 7.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 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\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[$key]) ? json_decode(wp_unslash($_COOKIE[$key]), 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($_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($_GET['event']) : '';
123 $box_id = isset($_GET['box']) ? sanitize_text_field($_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($_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 = sanitize_text_field($_GET['page']);
162 $referrer = sanitize_text_field($_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']) ? wp_unslash($_GET['options']) : [];
195
196 $this->handleCloseEvent($box, $box_id, $box_log_id, $options, $response);
197 }
198
199 echo wp_json_encode($response);
200 wp_die();
201 }
202
203 /**
204 * Handle Open Event
205 *
206 * @param object $box
207 * @param int $box_id
208 * @param string $page
209 * @param string $referrer
210 *
211 * @return void
212 */
213 protected function handleOpenEvent($box, $box_id, $page, $referrer)
214 {
215 // Do not track when box is on test mode
216 if (!$box->params->get('testmode'))
217 {
218 return firebox()->box->logOpenEvent($box_id, $page, $referrer);
219 }
220 }
221
222 /**
223 * Handle Close Event
224 *
225 * @param object $box
226 * @param int $box_id
227 * @param int $box_log_id
228 * @param array $options
229 * @param array $response
230 *
231 * @return void
232 */
233 protected function handleCloseEvent($box, $box_id, $box_log_id, $options, &$response)
234 {
235 // Do not track when box is on test mode
236 if (!$box->params->get('testmode'))
237 {
238 firebox()->box->logCloseEvent($box_id, $box_log_id);
239 }
240
241 // Do not set any cookie if box is on test mode
242 if (!$box->params->get('testmode') && !isset($options['temporary']))
243 {
244 // allow to prevent cookie from being set
245 if (!apply_filters('firebox/box/close/cookie_set', true, $box_id))
246 {
247 return;
248 }
249
250 if ($this->cookieExist($box_id))
251 {
252 $response['action'] = 'stop';
253 }
254 }
255 }
256
257 /**
258 * Check whether the cookie exists.
259 *
260 * @param int $box_id
261 *
262 * @return bool
263 */
264 protected function cookieExist($box_id)
265 {
266 $cookie = new \FireBox\Core\FB\Cookie(firebox()->box->get($box_id));
267 $cookie->set();
268
269 return $cookie->exist();
270 }
271
272 /**
273 * Box Track Conversion
274 *
275 * @return string
276 */
277 public function firebox_trackconversion()
278 {
279 $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
280
281 // verify nonce
282 if (!$verify = wp_verify_nonce($nonce, 'fbox_js_nonce'))
283 {
284 return false;
285 }
286
287 $this->trackConversions();
288 }
289 }