| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 3.1.7 Free | |
| 4 | + * @version 3.1.14 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <[email protected]> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | 8 | * @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| @@ -22,13 +22,27 @@ | ||
| 22 | 22 | class Log |
| 23 | 23 | { |
| 24 | 24 | /** |
| 25 | 25 | * Factory |
| 26 | - * | |
| 26 | + * | |
| 27 | 27 | * @var Factory |
| 28 | 28 | */ |
| 29 | 29 | private $factory; |
| 30 | 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 | + | |
| 31 | 45 | public function __construct($factory = null) |
| 32 | 46 | { |
| 33 | 47 | if (!$factory) |
| 34 | 48 | { |
| @@ -35,9 +49,48 @@ | ||
| 35 | 49 | $factory = new \FPFramework\Base\Factory(); |
| 36 | 50 | } |
| 37 | 51 | |
| 38 | 52 | $this->factory = $factory; |
| 53 | + | |
| 54 | + add_action(self::CLEANUP_HOOK, [$this, 'cleanupCron']); | |
| 55 | + | |
| 56 | + self::maybeScheduleCleanup(); | |
| 39 | 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 | + } | |
| 40 | 93 | |
| 41 | 94 | /** |
| 42 | 95 | * Logs box events to the database |
| 43 | 96 | * |
| @@ -105,13 +158,11 @@ | ||
| 105 | 158 | try |
| 106 | 159 | { |
| 107 | 160 | $log_id = firebox()->tables->boxlog->insert($passed_data); |
| 108 | 161 | |
| 109 | - // clean up | |
| 110 | - $this->clean(); | |
| 111 | 162 | return $log_id; |
| 112 | - } | |
| 113 | - catch (Exception $e) | |
| 163 | + } | |
| 164 | + catch (\Exception $e) | |
| 114 | 165 | {} |
| 115 | 166 | } |
| 116 | 167 | |
| 117 | 168 | /** |
| @@ -143,13 +194,11 @@ | ||
| 143 | 194 | ]; |
| 144 | 195 | |
| 145 | 196 | firebox()->tables->boxlogdetails->insert($data); |
| 146 | 197 | |
| 147 | - // clean up | |
| 148 | - $this->clean(); | |
| 149 | 198 | return; |
| 150 | 199 | } |
| 151 | - catch (Exception $e) | |
| 200 | + catch (\Exception $e) | |
| 152 | 201 | { |
| 153 | 202 | } |
| 154 | 203 | } |
| 155 | 204 | |
| @@ -180,24 +229,15 @@ | ||
| 180 | 229 | return $name; |
| 181 | 230 | } |
| 182 | 231 | |
| 183 | 232 | /** |
| 184 | - * Removes old rows from the logs table | |
| 185 | - * 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). | |
| 186 | 235 | * |
| 187 | 236 | * @return void |
| 188 | 237 | */ |
| 189 | - private function clean() | |
| 238 | + public function cleanupCron() | |
| 190 | 239 | { |
| 191 | - // cache key | |
| 192 | - $hash = md5('fireboxclean'); | |
| 193 | - | |
| 194 | - // check cache | |
| 195 | - if ($params = wp_cache_get($hash)) | |
| 196 | - { | |
| 197 | - return; | |
| 198 | - } | |
| 199 | - | |
| 200 | 240 | // Removes rows older than x days |
| 201 | 241 | $stats = \FireBox\Core\Helpers\BoxHelper::getParams(); |
| 202 | 242 | $stats = new Registry($stats); |
| 203 | 243 | |
| @@ -204,19 +244,33 @@ | ||
| 204 | 244 | $days = $stats->get('statsdays', 730); |
| 205 | 245 | |
| 206 | 246 | global $wpdb; |
| 207 | 247 | |
| 208 | - $sql = 'DELETE bl, bld | |
| 209 | - FROM `' . firebox()->tables->boxlog->getFullTableName() . '` as bl | |
| 210 | - LEFT JOIN `' . firebox()->tables->boxlogdetails->getFullTableName() . '` as bld | |
| 211 | - ON bld.log_id = bl.id | |
| 212 | - WHERE | |
| 213 | - bl.date < DATE_SUB(NOW(), INTERVAL ' . $wpdb->prepare('%d', $days) . ' DAY)'; | |
| 248 | + $logTable = firebox()->tables->boxlog->getFullTableName(); | |
| 249 | + $logDetailsTable = firebox()->tables->boxlogdetails->getFullTableName(); | |
| 214 | 250 | |
| 215 | - firebox()->tables->boxlog->executeRaw($sql); | |
| 216 | - | |
| 217 | - // set cache | |
| 218 | - wp_cache_set($hash, $stats, $hash, 720); | |
| 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 | + } | |
| 219 | 273 | |
| 220 | 274 | return true; |
| 221 | 275 | } |
| 222 | 276 | } |