| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.2 Free | |
| 4 | + * @version 2.1.4 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <[email protected]> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2021 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2024 FirePlugins All Rights Reserved | |
| 9 | 9 | * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace FireBox\Core\FB; |
| @@ -17,10 +17,25 @@ | ||
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | class Track |
| 20 | 20 | { |
| 21 | - public function __construct() | |
| 22 | - { | |
| 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 | + | |
| 23 | 38 | $this->setupAjax(); |
| 24 | 39 | } |
| 25 | 40 | |
| 26 | 41 | /** |
| @@ -30,11 +45,65 @@ | ||
| 30 | 45 | */ |
| 31 | 46 | public function setupAjax() |
| 32 | 47 | { |
| 33 | 48 | // FB Track event AJAX |
| 34 | - add_action('wp_ajax_fb_trackevent', [$this, 'fb_trackevent']); | |
| 35 | - add_action('wp_ajax_nopriv_fb_trackevent', [$this, 'fb_trackevent']); | |
| 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']); | |
| 36 | 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 | + } | |
| 37 | 106 | |
| 38 | 107 | /** |
| 39 | 108 | * Box Track Event |
| 40 | 109 | * |
| @@ -39,11 +108,11 @@ | ||
| 39 | 108 | * Box Track Event |
| 40 | 109 | * |
| 41 | 110 | * @return string |
| 42 | 111 | */ |
| 43 | - public function fb_trackevent() | |
| 112 | + public function firebox_trackevent() | |
| 44 | 113 | { |
| 45 | - $nonce = isset($_GET['nonce']) ? $_GET['nonce'] : ''; | |
| 114 | + $nonce = isset($_GET['nonce']) ? sanitize_text_field($_GET['nonce']) : ''; | |
| 46 | 115 | |
| 47 | 116 | // verify nonce |
| 48 | 117 | if (!$verify = wp_verify_nonce($nonce, 'fbox_js_nonce')) |
| 49 | 118 | { |
| @@ -93,21 +162,25 @@ | ||
| 93 | 162 | $referrer = sanitize_text_field($_GET['referrer']); |
| 94 | 163 | } |
| 95 | 164 | |
| 96 | 165 | // Load box settings |
| 97 | - if (!$box = firebox()->boxes->getBox()->get($box_id, null)) | |
| 166 | + if (!$box = firebox()->box->get($box_id)) | |
| 98 | 167 | { |
| 99 | 168 | return; |
| 100 | 169 | } |
| 101 | 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 | + | |
| 102 | 178 | /** |
| 103 | 179 | * Trigger Open & Close Event. |
| 104 | 180 | */ |
| 105 | 181 | do_action('firebox/box/on_' . $event, $box); |
| 106 | 182 | |
| 107 | - // get options | |
| 108 | - $options = isset($_GET['options']) ? wp_unslash($_GET['options']) : []; | |
| 109 | - | |
| 110 | 183 | $response['success'] = true; |
| 111 | 184 | |
| 112 | 185 | if ($event == 'open') |
| 113 | 186 | { |
| @@ -116,11 +189,14 @@ | ||
| 116 | 189 | |
| 117 | 190 | // Log impression in the database |
| 118 | 191 | if ($event == 'close') |
| 119 | 192 | { |
| 193 | + // get options | |
| 194 | + $options = isset($_GET['options']) ? wp_unslash($_GET['options']) : []; | |
| 195 | + | |
| 120 | 196 | $this->handleCloseEvent($box, $box_id, $box_log_id, $options, $response); |
| 121 | 197 | } |
| 122 | - | |
| 198 | + | |
| 123 | 199 | echo json_encode($response); |
| 124 | 200 | wp_die(); |
| 125 | 201 | } |
| 126 | 202 | |
| @@ -138,9 +214,9 @@ | ||
| 138 | 214 | { |
| 139 | 215 | // Do not track when box is on test mode |
| 140 | 216 | if (!$box->params->get('testmode')) |
| 141 | 217 | { |
| 142 | - return firebox()->boxes->getBox()->logOpenEvent($box_id, $page, $referrer); | |
| 218 | + return firebox()->box->logOpenEvent($box_id, $page, $referrer); | |
| 143 | 219 | } |
| 144 | 220 | } |
| 145 | 221 | |
| 146 | 222 | /** |
| @@ -158,9 +234,9 @@ | ||
| 158 | 234 | { |
| 159 | 235 | // Do not track when box is on test mode |
| 160 | 236 | if (!$box->params->get('testmode')) |
| 161 | 237 | { |
| 162 | - $response['box_log_id'] = firebox()->boxes->getBox()->logCloseEvent($box_id, $box_log_id); | |
| 238 | + firebox()->box->logCloseEvent($box_id, $box_log_id); | |
| 163 | 239 | } |
| 164 | 240 | |
| 165 | 241 | // Do not set any cookie if box is on test mode |
| 166 | 242 | if (!$box->params->get('testmode') && !isset($options['temporary'])) |
| @@ -169,15 +245,45 @@ | ||
| 169 | 245 | if (!apply_filters('firebox/box/close/cookie_set', true, $box_id)) |
| 170 | 246 | { |
| 171 | 247 | return; |
| 172 | 248 | } |
| 173 | - | |
| 174 | - $cookie = new \FireBox\Core\FB\Cookie($box_id); | |
| 175 | - $cookie->set(); | |
| 176 | 249 | |
| 177 | - if ($cookie->exist()) | |
| 250 | + if ($this->cookieExist($box_id)) | |
| 178 | 251 | { |
| 179 | 252 | $response['action'] = 'stop'; |
| 180 | 253 | } |
| 181 | 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(); | |
| 182 | 288 | } |
| 183 | 289 | } |