| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.9 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 |
use FPFramework\Libs\Registry; |
| 20 |
use FireBox\Core\Helpers\BoxHelper; |
| 21 |
|
| 22 |
class Log |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Factory |
| 26 |
* |
| 27 |
* @var Factory |
| 28 |
*/ |
| 29 |
private $factory; |
| 30 |
|
| 31 |
/** |
| 32 |
* The hook name used to run the log retention cleanup via WP-Cron. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
const CLEANUP_HOOK = 'firebox_logs_cleanup'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Max number of batches (of 1000 rows) deleted per cron run, to avoid long lock holds. |
| 40 |
* |
| 41 |
* @var int |
| 42 |
*/ |
| 43 |
const CLEANUP_MAX_BATCHES = 20; |
| 44 |
|
| 45 |
public function __construct($factory = null) |
| 46 |
{ |
| 47 |
if (!$factory) |
| 48 |
{ |
| 49 |
$factory = new \FPFramework\Base\Factory(); |
| 50 |
} |
| 51 |
|
| 52 |
$this->factory = $factory; |
| 53 |
|
| 54 |
add_action(self::CLEANUP_HOOK, [$this, 'cleanupCron']); |
| 55 |
|
| 56 |
$this->maybeScheduleCleanup(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Schedules the daily log retention cleanup, if not already scheduled. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function maybeScheduleCleanup() |
| 65 |
{ |
| 66 |
if (wp_next_scheduled(self::CLEANUP_HOOK)) |
| 67 |
{ |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
wp_schedule_event(time(), 'daily', self::CLEANUP_HOOK); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Unschedules the log retention cleanup. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function unscheduleCleanup() |
| 80 |
{ |
| 81 |
$timestamp = wp_next_scheduled(self::CLEANUP_HOOK); |
| 82 |
|
| 83 |
if ($timestamp) |
| 84 |
{ |
| 85 |
wp_unschedule_event($timestamp, self::CLEANUP_HOOK); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Logs box events to the database |
| 91 |
* |
| 92 |
* @param integer $boxid The box id |
| 93 |
* @param string $page The page the box appeared |
| 94 |
* @param string $referrer The referrer |
| 95 |
* @param integer $event_id Event id: 1=Open, 2=Close |
| 96 |
* @param integer $box_log_id The box log ID of the open event that we are closing |
| 97 |
* |
| 98 |
* @return bool Returns a boolean indicating if the event logged successfully |
| 99 |
*/ |
| 100 |
public function track($boxid, $event_id = 1, $box_log_id = null, $page = '', $referrer = '') |
| 101 |
{ |
| 102 |
// Making sure we have a valid Boxid |
| 103 |
if (!$boxid) |
| 104 |
{ |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// open event |
| 109 |
if ($event_id == 1) |
| 110 |
{ |
| 111 |
// Get visitor's token id |
| 112 |
if (!$visitorID = $this->factory->getVisitorID()) |
| 113 |
{ |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
$country_value = ''; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
// Everything seems OK. Let's save data to db. |
| 124 |
$data = (object) [ |
| 125 |
'sessionid' => $this->factory->getSession()->getSessionID(), |
| 126 |
'visitorid' => $visitorID, |
| 127 |
'user' => $this->factory->getUser()->ID, |
| 128 |
'box' => $boxid, |
| 129 |
'page' => $page, |
| 130 |
'country' => $country_value, |
| 131 |
'device' => $this->factory->getDevice(), |
| 132 |
'referrer' => $referrer, |
| 133 |
'date' => $this->factory->getDate()->format('Y-m-d H:i:s') |
| 134 |
]; |
| 135 |
|
| 136 |
return $this->handleOpen($data); |
| 137 |
} |
| 138 |
|
| 139 |
// other events |
| 140 |
return $this->handleOtherEvents($box_log_id, $event_id); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Handle the logging when opening a box |
| 145 |
* |
| 146 |
* @param array $passed_data |
| 147 |
* |
| 148 |
* @return mixed |
| 149 |
*/ |
| 150 |
private function handleOpen($passed_data) |
| 151 |
{ |
| 152 |
// Insert the object into the firebox logs table |
| 153 |
try |
| 154 |
{ |
| 155 |
$log_id = firebox()->tables->boxlog->insert($passed_data); |
| 156 |
|
| 157 |
return $log_id; |
| 158 |
} |
| 159 |
catch (\Exception $e) |
| 160 |
{} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Handle the logging of other events |
| 165 |
* |
| 166 |
* @param int $box_log_id |
| 167 |
* @param int $event_id |
| 168 |
* @param string $event_source |
| 169 |
* @param string $event_label |
| 170 |
* |
| 171 |
* @return mixed |
| 172 |
*/ |
| 173 |
private function handleOtherEvents($box_log_id, $event_id, $event_source = '', $event_label = '') |
| 174 |
{ |
| 175 |
if (!$box_log_id) |
| 176 |
{ |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
// Insert the object into the firebox logs details table |
| 181 |
try |
| 182 |
{ |
| 183 |
$data = [ |
| 184 |
'log_id' => $box_log_id, |
| 185 |
'event' => $this->parseEvent($event_id), |
| 186 |
'event_source' => $event_source, |
| 187 |
'event_label' => $event_label, |
| 188 |
'date' => $this->factory->getDate()->format('Y-m-d H:i:s') |
| 189 |
]; |
| 190 |
|
| 191 |
firebox()->tables->boxlogdetails->insert($data); |
| 192 |
|
| 193 |
return; |
| 194 |
} |
| 195 |
catch (\Exception $e) |
| 196 |
{ |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Translates the event ID to name |
| 202 |
* |
| 203 |
* @param int $id |
| 204 |
* |
| 205 |
* @return string |
| 206 |
*/ |
| 207 |
private function parseEvent($id) |
| 208 |
{ |
| 209 |
$name = ''; |
| 210 |
|
| 211 |
switch ($id) |
| 212 |
{ |
| 213 |
case 1: |
| 214 |
$name = 'open'; |
| 215 |
break; |
| 216 |
case 2: |
| 217 |
$name = 'close'; |
| 218 |
break; |
| 219 |
case 3: |
| 220 |
$name = 'conversion'; |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
return $name; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Removes old rows from the logs table in small batches, to avoid holding |
| 229 |
* long locks on large tables. Runs daily via WP-Cron (see self::CLEANUP_HOOK). |
| 230 |
* |
| 231 |
* @return void |
| 232 |
*/ |
| 233 |
public function cleanupCron() |
| 234 |
{ |
| 235 |
// Removes rows older than x days |
| 236 |
$stats = \FireBox\Core\Helpers\BoxHelper::getParams(); |
| 237 |
$stats = new Registry($stats); |
| 238 |
|
| 239 |
$days = $stats->get('statsdays', 730); |
| 240 |
|
| 241 |
global $wpdb; |
| 242 |
|
| 243 |
$logTable = firebox()->tables->boxlog->getFullTableName(); |
| 244 |
$logDetailsTable = firebox()->tables->boxlogdetails->getFullTableName(); |
| 245 |
|
| 246 |
for ($i = 0; $i < self::CLEANUP_MAX_BATCHES; $i++) |
| 247 |
{ |
| 248 |
$ids = $wpdb->get_col($wpdb->prepare( |
| 249 |
"SELECT id FROM `$logTable` WHERE date < DATE_SUB(NOW(), INTERVAL %d DAY) ORDER BY id LIMIT 1000", |
| 250 |
$days |
| 251 |
)); |
| 252 |
|
| 253 |
if (!$ids) |
| 254 |
{ |
| 255 |
break; |
| 256 |
} |
| 257 |
|
| 258 |
$ids_placeholder = implode(',', array_map('intval', $ids)); |
| 259 |
|
| 260 |
$wpdb->query("DELETE FROM `$logDetailsTable` WHERE log_id IN ($ids_placeholder)"); |
| 261 |
$wpdb->query("DELETE FROM `$logTable` WHERE id IN ($ids_placeholder)"); |
| 262 |
|
| 263 |
if (count($ids) < 1000) |
| 264 |
{ |
| 265 |
break; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return true; |
| 270 |
} |
| 271 |
} |