| @@ -4,18 +4,20 @@ | ||
| 4 | 4 | |
| 5 | 5 | use WPDeveloper\BetterDocs\Utils\Base; |
| 6 | 6 | |
| 7 | 7 | /** |
| 8 | - * Free analytics retention: a daily native-cron purge of the simple aggregate | |
| 9 | - * tables. Free keeps a rolling 12 months (365 days) of analytics by default — | |
| 10 | - * enough history to be useful, while unlimited retention stays a Pro perk. A | |
| 11 | - * site can shorten the window with the data_retention_days setting, or change / | |
| 12 | - * disable it (0 = keep forever) via the betterdocs_analytics_retention_days | |
| 13 | - * filter. | |
| 8 | + * Free analytics retention. | |
| 14 | 9 | * |
| 10 | + * Free keeps every analytics row forever by default; nothing is purged. A site | |
| 11 | + * that wants a rolling window can opt in through the | |
| 12 | + * betterdocs_analytics_retention_days filter (number of days, 0 = keep forever), | |
| 13 | + * and a daily native-cron purge of the aggregate tables then trims older rows. | |
| 14 | + * | |
| 15 | + * Earlier versions purged anything older than 12 months on every Free-only | |
| 16 | + * install. That schedule is cleared on the next request when no window is set. | |
| 17 | + * | |
| 15 | 18 | * Pro-safe: when Pro is active the purge is skipped entirely, because Pro |
| 16 | - * manages its own retention (unlimited by default) on top of these tables. | |
| 17 | - * Mirrors the scheduling pattern in Pro's RelatedDocsTracker. | |
| 19 | + * manages its own retention (data_retention_days) on top of these tables. | |
| 18 | 20 | */ |
| 19 | 21 | class AnalyticsRetention extends Base { |
| 20 | 22 | const CRON_HOOK = 'betterdocs_analytics_cleanup'; |
| 21 | 23 | |
| @@ -28,12 +30,31 @@ | ||
| 28 | 30 | } |
| 29 | 31 | } |
| 30 | 32 | |
| 31 | 33 | /** |
| 32 | - * Schedule the daily purge once if it isn't already scheduled. | |
| 34 | + * Retention window in days. 0 (the default) keeps everything. | |
| 35 | + * | |
| 36 | + * @return int | |
| 33 | 37 | */ |
| 38 | + public function retention_days() { | |
| 39 | + return max( 0, (int) apply_filters( 'betterdocs_analytics_retention_days', 0 ) ); | |
| 40 | + } | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Schedule the daily purge only while a window is set; otherwise make sure | |
| 44 | + * no purge is left scheduled (including the one older versions added). | |
| 45 | + */ | |
| 34 | 46 | public function maybe_schedule_cleanup() { |
| 35 | - if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { | |
| 47 | + $scheduled = wp_next_scheduled( self::CRON_HOOK ); | |
| 48 | + | |
| 49 | + if ( betterdocs()->is_pro_active() || $this->retention_days() < 1 ) { | |
| 50 | + if ( $scheduled ) { | |
| 51 | + $this->clear_cleanup(); | |
| 52 | + } | |
| 53 | + return; | |
| 54 | + } | |
| 55 | + | |
| 56 | + if ( ! $scheduled ) { | |
| 36 | 57 | wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_HOOK ); |
| 37 | 58 | } |
| 38 | 59 | } |
| 39 | 60 | |
| @@ -41,29 +62,19 @@ | ||
| 41 | 62 | wp_clear_scheduled_hook( self::CRON_HOOK ); |
| 42 | 63 | } |
| 43 | 64 | |
| 44 | 65 | /** |
| 45 | - * Free keeps a rolling 12 months of analytics by default. | |
| 66 | + * Delete aggregate rows older than the opted-in retention window. No-op when | |
| 67 | + * Pro is active or no window is set. | |
| 46 | 68 | */ |
| 47 | - const DEFAULT_RETENTION_DAYS = 365; | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Delete aggregate rows older than the retention window. No-op when Pro is | |
| 51 | - * active so Pro's longer (unlimited-by-default) retention is never trimmed | |
| 52 | - * by Free. | |
| 53 | - */ | |
| 54 | 69 | public function run_cleanup() { |
| 55 | 70 | if ( betterdocs()->is_pro_active() ) { |
| 56 | 71 | return; |
| 57 | 72 | } |
| 58 | 73 | |
| 59 | - // A site may shorten the window via the setting; the filter can lengthen | |
| 60 | - // it or disable pruning entirely (0 => keep forever). Default is 12 months. | |
| 61 | - $setting = (int) betterdocs()->settings->get( 'data_retention_days', 0 ); | |
| 62 | - $window = $setting > 0 ? $setting : self::DEFAULT_RETENTION_DAYS; | |
| 63 | - $days = (int) apply_filters( 'betterdocs_analytics_retention_days', $window ); | |
| 74 | + $days = $this->retention_days(); | |
| 64 | 75 | if ( $days < 1 ) { |
| 65 | - return; // filtered to 0 => keep forever. | |
| 76 | + return; | |
| 66 | 77 | } |
| 67 | 78 | |
| 68 | 79 | global $wpdb; |
| 69 | 80 | |