PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.1.11
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.1.11
3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 122 releases
firebox / Inc / Core / FB / Log.php

Log.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 3.1.11, at Inc/Core/FB/Log.php

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