| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.0 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2021 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 |
public function __construct() |
| 22 |
{ |
| 23 |
$this->setupAjax(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Setup ajax requests |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function setupAjax() |
| 32 |
{ |
| 33 |
// 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']); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Box Track Event |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public function fb_trackevent() |
| 44 |
{ |
| 45 |
$nonce = isset($_GET['nonce']) ? $_GET['nonce'] : ''; |
| 46 |
|
| 47 |
// verify nonce |
| 48 |
if (!$verify = wp_verify_nonce($nonce, 'fbox_js_nonce')) |
| 49 |
{ |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
$event = isset($_GET['event']) ? sanitize_text_field($_GET['event']) : ''; |
| 54 |
$box_id = isset($_GET['box']) ? sanitize_text_field($_GET['box']) : ''; |
| 55 |
$page = ''; |
| 56 |
$referrer = ''; |
| 57 |
$box_log_id = ''; |
| 58 |
|
| 59 |
$response['success'] = false; |
| 60 |
|
| 61 |
// ensure non-empty values |
| 62 |
if (empty($event) || empty($box_id)) |
| 63 |
{ |
| 64 |
echo json_encode($response); |
| 65 |
wp_die(); |
| 66 |
} |
| 67 |
|
| 68 |
// ensure valid event |
| 69 |
if (!in_array($event, ['open', 'close'])) |
| 70 |
{ |
| 71 |
echo json_encode($response); |
| 72 |
wp_die(); |
| 73 |
} |
| 74 |
|
| 75 |
// If its a close event, it should also provide the box log ID in order to add close date of box |
| 76 |
if ($event == 'close') |
| 77 |
{ |
| 78 |
if (!isset($_GET['box_log_id'])) |
| 79 |
{ |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$box_log_id = sanitize_text_field($_GET['box_log_id']); |
| 84 |
} |
| 85 |
else if ($event == 'open') |
| 86 |
{ |
| 87 |
if (!isset($_GET['page']) && !isset($_GET['referrer'])) |
| 88 |
{ |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$page = sanitize_text_field($_GET['page']); |
| 93 |
$referrer = sanitize_text_field($_GET['referrer']); |
| 94 |
} |
| 95 |
|
| 96 |
// Load box settings |
| 97 |
if (!$box = firebox()->boxes->getBox()->get($box_id)) |
| 98 |
{ |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Trigger Open & Close Event. |
| 104 |
*/ |
| 105 |
do_action('firebox/box/on_' . $event, $box); |
| 106 |
|
| 107 |
// get options |
| 108 |
$options = isset($_GET['options']) ? wp_unslash($_GET['options']) : []; |
| 109 |
|
| 110 |
$response['success'] = true; |
| 111 |
|
| 112 |
if ($event == 'open') |
| 113 |
{ |
| 114 |
$response['box_log_id'] = $this->handleOpenEvent($box, $box_id, $page, $referrer); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
// Log impression in the database |
| 119 |
if ($event == 'close') |
| 120 |
{ |
| 121 |
$this->handleCloseEvent($box, $box_id, $box_log_id, $options, $response); |
| 122 |
} |
| 123 |
|
| 124 |
echo json_encode($response); |
| 125 |
wp_die(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Handle Open Event |
| 130 |
* |
| 131 |
* @param object $box |
| 132 |
* @param int $box_id |
| 133 |
* @param string $page |
| 134 |
* @param string $referrer |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
protected function handleOpenEvent($box, $box_id, $page, $referrer) |
| 139 |
{ |
| 140 |
// Do not track when box is on test mode |
| 141 |
if (!$box->params->get('testmode')) |
| 142 |
{ |
| 143 |
return firebox()->boxes->getBox()->logOpenEvent($box_id, $page, $referrer); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Handle Close Event |
| 149 |
* |
| 150 |
* @param object $box |
| 151 |
* @param int $box_id |
| 152 |
* @param int $box_log_id |
| 153 |
* @param array $options |
| 154 |
* @param array $response |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
protected function handleCloseEvent($box, $box_id, $box_log_id, $options, &$response) |
| 159 |
{ |
| 160 |
// Do not track when box is on test mode |
| 161 |
if (!$box->params->get('testmode')) |
| 162 |
{ |
| 163 |
$response['box_log_id'] = firebox()->boxes->getBox()->logCloseEvent($box_id, $box_log_id); |
| 164 |
} |
| 165 |
|
| 166 |
// Do not set any cookie if box is on test mode |
| 167 |
if (!$box->params->get('testmode') && !isset($options['temporary'])) |
| 168 |
{ |
| 169 |
$cookie = new \FireBox\Core\FB\Cookie($box_id); |
| 170 |
$cookie->set(); |
| 171 |
|
| 172 |
if ($cookie->exist()) |
| 173 |
{ |
| 174 |
$response['action'] = 'stop'; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |