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