PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.1.14
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.1.14
3.1.14 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 All 123 releases
← All changes | Inc/Core/FB/Log.php +96 -44 1.0.8 → 3.1.14 View file →
@@ -1,12 +1,12 @@
1 1 <?php
2 2 /**
3 3 * @package FireBox
4 - * @version 1.0.8 Free
4 + * @version 3.1.14 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 © 2026 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,19 +17,32 @@
17 17 }
18 18
19 19 use FPFramework\Libs\Registry;
20 20 use FireBox\Core\Helpers\BoxHelper;
21 -use FireBox\Core\Helpers\DBHelper;
22 21
23 22 class Log
24 23 {
25 24 /**
26 25 * Factory
27 - *
26 + *
28 27 * @var Factory
29 28 */
30 29 private $factory;
31 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 +
32 45 public function __construct($factory = null)
33 46 {
34 47 if (!$factory)
35 48 {
@@ -36,9 +49,48 @@
36 49 $factory = new \FPFramework\Base\Factory();
37 50 }
38 51
39 52 $this->factory = $factory;
53 +
54 + add_action(self::CLEANUP_HOOK, [$this, 'cleanupCron']);
55 +
56 + self::maybeScheduleCleanup();
40 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 + }
41 93
42 94 /**
43 95 * Logs box events to the database
44 96 *
@@ -106,26 +158,25 @@
106 158 try
107 159 {
108 160 $log_id = firebox()->tables->boxlog->insert($passed_data);
109 161
110 - // clean up
111 - $this->clean();
112 162 return $log_id;
113 - }
114 - catch (Exception $e)
115 - {
116 163 }
164 + catch (\Exception $e)
165 + {}
117 166 }
118 167
119 168 /**
120 169 * Handle the logging of other events
121 170 *
122 - * @param int $box_log_id
123 - * @param int $event_id
171 + * @param int $box_log_id
172 + * @param int $event_id
173 + * @param string $event_source
174 + * @param string $event_label
124 175 *
125 176 * @return mixed
126 177 */
127 - private function handleOtherEvents($box_log_id, $event_id)
178 + private function handleOtherEvents($box_log_id, $event_id, $event_source = '', $event_label = '')
128 179 {
129 180 if (!$box_log_id)
130 181 {
131 182 return;
@@ -136,19 +187,18 @@
136 187 {
137 188 $data = [
138 189 'log_id' => $box_log_id,
139 190 'event' => $this->parseEvent($event_id),
140 - 'params' => '',
191 + 'event_source' => $event_source,
192 + 'event_label' => $event_label,
141 193 'date' => $this->factory->getDate()->format('Y-m-d H:i:s')
142 194 ];
143 195
144 196 firebox()->tables->boxlogdetails->insert($data);
145 197
146 - // clean up
147 - $this->clean();
148 198 return;
149 199 }
150 - catch (Exception $e)
200 + catch (\Exception $e)
151 201 {
152 202 }
153 203 }
154 204
@@ -170,8 +220,11 @@
170 220 break;
171 221 case 2:
172 222 $name = 'close';
173 223 break;
224 + case 3:
225 + $name = 'conversion';
226 + break;
174 227 }
175 228
176 229 return $name;
177 230 }
@@ -176,49 +229,48 @@
176 229 return $name;
177 230 }
178 231
179 232 /**
180 - * Removes old rows from the logs table
181 - * Runs every 12 hours with a self-check
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).
182 235 *
183 236 * @return void
184 237 */
185 - private function clean()
238 + public function cleanupCron()
186 239 {
187 - // cache key
188 - $hash = md5('fireboxclean');
189 -
190 - // check cache
191 - if ($params = wp_cache_get($hash))
192 - {
193 - return;
194 - }
195 -
196 240 // Removes rows older than x days
197 241 $stats = \FireBox\Core\Helpers\BoxHelper::getParams();
198 242 $stats = new Registry($stats);
199 243
200 - $days = $stats->get('statsdays', 180);
244 + $days = $stats->get('statsdays', 730);
201 245
202 - if ($days == 'custom')
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++)
203 252 {
204 - $days = $stats->get('statsdays_custom', 1);
205 - $days = $days < 0 ? 1 : $days;
206 - }
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 + ));
207 257
208 - global $wpdb;
258 + if (!$ids)
259 + {
260 + break;
261 + }
209 262
210 - $sql = 'DELETE bl, bld
211 - FROM `' . firebox()->tables->boxlog->getFullTableName() . '` as bl
212 - LEFT JOIN `' . firebox()->tables->boxlogdetails->getFullTableName() . '` as bld
213 - ON bld.log_id = bl.id
214 - WHERE
215 - bl.date < DATE_SUB(NOW(), INTERVAL ' . $wpdb->prepare('%d', $days) . ' DAY)';
263 + $ids_placeholder = implode(',', array_map('intval', $ids));
216 264
217 - firebox()->tables->boxlog->executeRaw($sql);
218 -
219 - // set cache
220 - wp_cache_set($hash, $params, $hash, 720);
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 + }
221 273
222 274 return true;
223 275 }
224 276 }