Actions
3 years ago
AnalyticsCleanup.php
4 years ago
AnalyticsConsent.php
2 years ago
AnalyticsEventDto.php
3 years ago
AnalyticsSender.php
2 years ago
WithAnalyticsAPI.php
4 years ago
WithAnalyticsSiteInfo.php
2 years ago
AnalyticsCleanup.php
32 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Analytics; |
| 4 | |
| 5 | class AnalyticsCleanup |
| 6 | { |
| 7 | public function cleanupOldAnalytics() |
| 8 | { |
| 9 | global $wpdb; |
| 10 | |
| 11 | $results = $wpdb->get_results("SELECT `option_id`, `option_name` FROM $wpdb->options WHERE `option_name` LIKE 'wpstg_analytics_event_%' ORDER BY `option_id` ASC"); |
| 12 | |
| 13 | // No site should have more than 100 events in the database. |
| 14 | if (count($results) < 100) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | // If they do, we will delete the first 20 events. |
| 19 | $idsToDelete = array_map(function ($option) { |
| 20 | return $option->option_id; |
| 21 | }, array_slice($results, 0, 20)); |
| 22 | |
| 23 | $idsToDelete = implode(',', $idsToDelete); |
| 24 | |
| 25 | $deleteResult = $wpdb->query("DELETE FROM $wpdb->options WHERE `option_id` IN ($idsToDelete)"); |
| 26 | |
| 27 | if ($deleteResult != 20) { |
| 28 | \WPStaging\functions\debug_log(sprintf('WPSTAGING Analytics Cleanup tried to delete 20 events but couldn\'t. wpdb last query: %s wpdb last error: %s', $wpdb->last_query, $wpdb->last_error)); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 |