| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.3 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 | * |
| @@ -66,9 +118,11 @@ | ||
| 66 | 118 | { |
| 67 | 119 | return; |
| 68 | 120 | } |
| 69 | 121 | |
| 122 | + | |
| 70 | 123 | $country_value = ''; |
| 124 | + | |
| 71 | 125 | |
| 72 | 126 | |
| 73 | 127 | |
| 74 | 128 | // Everything seems OK. Let's save data to db. |
| @@ -104,26 +158,25 @@ | ||
| 104 | 158 | try |
| 105 | 159 | { |
| 106 | 160 | $log_id = firebox()->tables->boxlog->insert($passed_data); |
| 107 | 161 | |
| 108 | - // clean up | |
| 109 | - $this->clean(); | |
| 110 | 162 | return $log_id; |
| 111 | - } | |
| 112 | - catch (Exception $e) | |
| 113 | - { | |
| 114 | 163 | } |
| 164 | + catch (\Exception $e) | |
| 165 | + {} | |
| 115 | 166 | } |
| 116 | 167 | |
| 117 | 168 | /** |
| 118 | 169 | * Handle the logging of other events |
| 119 | 170 | * |
| 120 | - * @param int $box_log_id | |
| 121 | - * @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 | |
| 122 | 175 | * |
| 123 | 176 | * @return mixed |
| 124 | 177 | */ |
| 125 | - private function handleOtherEvents($box_log_id, $event_id) | |
| 178 | + private function handleOtherEvents($box_log_id, $event_id, $event_source = '', $event_label = '') | |
| 126 | 179 | { |
| 127 | 180 | if (!$box_log_id) |
| 128 | 181 | { |
| 129 | 182 | return; |
| @@ -134,19 +187,18 @@ | ||
| 134 | 187 | { |
| 135 | 188 | $data = [ |
| 136 | 189 | 'log_id' => $box_log_id, |
| 137 | 190 | 'event' => $this->parseEvent($event_id), |
| 138 | - 'params' => '', | |
| 191 | + 'event_source' => $event_source, | |
| 192 | + 'event_label' => $event_label, | |
| 139 | 193 | 'date' => $this->factory->getDate()->format('Y-m-d H:i:s') |
| 140 | 194 | ]; |
| 141 | 195 | |
| 142 | 196 | firebox()->tables->boxlogdetails->insert($data); |
| 143 | 197 | |
| 144 | - // clean up | |
| 145 | - $this->clean(); | |
| 146 | 198 | return; |
| 147 | 199 | } |
| 148 | - catch (Exception $e) | |
| 200 | + catch (\Exception $e) | |
| 149 | 201 | { |
| 150 | 202 | } |
| 151 | 203 | } |
| 152 | 204 | |
| @@ -168,8 +220,11 @@ | ||
| 168 | 220 | break; |
| 169 | 221 | case 2: |
| 170 | 222 | $name = 'close'; |
| 171 | 223 | break; |
| 224 | + case 3: | |
| 225 | + $name = 'conversion'; | |
| 226 | + break; | |
| 172 | 227 | } |
| 173 | 228 | |
| 174 | 229 | return $name; |
| 175 | 230 | } |
| @@ -174,49 +229,48 @@ | ||
| 174 | 229 | return $name; |
| 175 | 230 | } |
| 176 | 231 | |
| 177 | 232 | /** |
| 178 | - * Removes old rows from the logs table | |
| 179 | - * 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). | |
| 180 | 235 | * |
| 181 | 236 | * @return void |
| 182 | 237 | */ |
| 183 | - private function clean() | |
| 238 | + public function cleanupCron() | |
| 184 | 239 | { |
| 185 | - // cache key | |
| 186 | - $hash = md5('fireboxclean'); | |
| 187 | - | |
| 188 | - // check cache | |
| 189 | - if ($params = wp_cache_get($hash)) | |
| 190 | - { | |
| 191 | - return; | |
| 192 | - } | |
| 193 | - | |
| 194 | 240 | // Removes rows older than x days |
| 195 | 241 | $stats = \FireBox\Core\Helpers\BoxHelper::getParams(); |
| 196 | 242 | $stats = new Registry($stats); |
| 197 | 243 | |
| 198 | - $days = $stats->get('statsdays', 180); | |
| 244 | + $days = $stats->get('statsdays', 730); | |
| 199 | 245 | |
| 200 | - 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++) | |
| 201 | 252 | { |
| 202 | - $days = $stats->get('statsdays_custom', 1); | |
| 203 | - $days = $days < 0 ? 1 : $days; | |
| 204 | - } | |
| 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 | + )); | |
| 205 | 257 | |
| 206 | - global $wpdb; | |
| 258 | + if (!$ids) | |
| 259 | + { | |
| 260 | + break; | |
| 261 | + } | |
| 207 | 262 | |
| 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)'; | |
| 263 | + $ids_placeholder = implode(',', array_map('intval', $ids)); | |
| 214 | 264 | |
| 215 | - firebox()->tables->boxlog->executeRaw($sql); | |
| 216 | - | |
| 217 | - // set cache | |
| 218 | - 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 | + } | |
| 219 | 273 | |
| 220 | 274 | return true; |
| 221 | 275 | } |
| 222 | 276 | } |