| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.5 Free | |
| 4 | + * @version 2.1.39 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 © 2025 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['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 | + } | |
| 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']) ? sanitize_text_field($_GET['nonce']) : ''; | |
| 114 | + $nonce = isset($_GET['nonce']) ? sanitize_text_field(wp_unslash($_GET['nonce'])) : ''; | |
| 46 | 115 | |
| 47 | 116 | // verify nonce |
| 48 | 117 | if (!$verify = wp_verify_nonce($nonce, 'fbox_js_nonce')) |
| 49 | 118 | { |
| @@ -49,10 +118,10 @@ | ||
| 49 | 118 | { |
| 50 | 119 | return false; |
| 51 | 120 | } |
| 52 | 121 | |
| 53 | - $event = isset($_GET['event']) ? sanitize_text_field($_GET['event']) : ''; | |
| 54 | - $box_id = isset($_GET['box']) ? sanitize_text_field($_GET['box']) : ''; | |
| 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'])) : ''; | |
| 55 | 124 | $page = ''; |
| 56 | 125 | $referrer = ''; |
| 57 | 126 | $box_log_id = ''; |
| 58 | 127 | |
| @@ -60,9 +129,9 @@ | ||
| 60 | 129 | |
| 61 | 130 | // ensure non-empty values |
| 62 | 131 | if (empty($event) || empty($box_id)) |
| 63 | 132 | { |
| 64 | - echo json_encode($response); | |
| 133 | + echo wp_json_encode($response); | |
| 65 | 134 | wp_die(); |
| 66 | 135 | } |
| 67 | 136 | |
| 68 | 137 | // ensure valid event |
| @@ -67,9 +136,9 @@ | ||
| 67 | 136 | |
| 68 | 137 | // ensure valid event |
| 69 | 138 | if (!in_array($event, ['open', 'close'])) |
| 70 | 139 | { |
| 71 | - echo json_encode($response); | |
| 140 | + echo wp_json_encode($response); | |
| 72 | 141 | wp_die(); |
| 73 | 142 | } |
| 74 | 143 | |
| 75 | 144 | // If its a close event, it should also provide the box log ID in order to add close date of box |
| @@ -79,9 +148,9 @@ | ||
| 79 | 148 | { |
| 80 | 149 | return; |
| 81 | 150 | } |
| 82 | 151 | |
| 83 | - $box_log_id = sanitize_text_field($_GET['box_log_id']); | |
| 152 | + $box_log_id = sanitize_text_field(wp_unslash($_GET['box_log_id'])); | |
| 84 | 153 | } |
| 85 | 154 | else if ($event == 'open') |
| 86 | 155 | { |
| 87 | 156 | if (!isset($_GET['page']) && !isset($_GET['referrer'])) |
| @@ -88,10 +157,10 @@ | ||
| 88 | 157 | { |
| 89 | 158 | return; |
| 90 | 159 | } |
| 91 | 160 | |
| 92 | - $page = sanitize_text_field($_GET['page']); | |
| 93 | - $referrer = sanitize_text_field($_GET['referrer']); | |
| 161 | + $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : ''; | |
| 162 | + $referrer = isset($_GET['referrer']) ? sanitize_text_field(wp_unslash($_GET['referrer'])) : ''; | |
| 94 | 163 | } |
| 95 | 164 | |
| 96 | 165 | // Load box settings |
| 97 | 166 | if (!$box = firebox()->box->get($box_id)) |
| @@ -98,16 +167,20 @@ | ||
| 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,12 +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']) ? array_map('sanitize_text_field', wp_unslash($_GET['options'])) : []; | |
| 120 | 195 | $this->handleCloseEvent($box, $box_id, $box_log_id, $options, $response); |
| 121 | 196 | } |
| 122 | 197 | |
| 123 | - echo json_encode($response); | |
| 198 | + echo wp_json_encode($response); | |
| 124 | 199 | wp_die(); |
| 125 | 200 | } |
| 126 | 201 | |
| 127 | 202 | /** |
| @@ -186,10 +261,28 @@ | ||
| 186 | 261 | * @return bool |
| 187 | 262 | */ |
| 188 | 263 | protected function cookieExist($box_id) |
| 189 | 264 | { |
| 190 | - $cookie = new \FireBox\Core\FB\Cookie($box_id); | |
| 265 | + $cookie = new \FireBox\Core\FB\Cookie(firebox()->box->get($box_id)); | |
| 191 | 266 | $cookie->set(); |
| 192 | 267 | |
| 193 | 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(); | |
| 194 | 287 | } |
| 195 | 288 | } |