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