PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Core / AnalyticsRetention.php

AnalyticsRetention.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.8.0, at includes/Core/AnalyticsRetention.php

85 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Core;
4
5 use WPDeveloper\BetterDocs\Utils\Base;
6
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.
14 *
15 * 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.
18 */
19 class AnalyticsRetention extends Base {
20 const CRON_HOOK = 'betterdocs_analytics_cleanup';
21
22 public function __construct() {
23 add_action( 'init', [ $this, 'maybe_schedule_cleanup' ] );
24 add_action( self::CRON_HOOK, [ $this, 'run_cleanup' ] );
25
26 if ( defined( 'BETTERDOCS_PLUGIN_FILE' ) ) {
27 register_deactivation_hook( BETTERDOCS_PLUGIN_FILE, [ $this, 'clear_cleanup' ] );
28 }
29 }
30
31 /**
32 * Schedule the daily purge once if it isn't already scheduled.
33 */
34 public function maybe_schedule_cleanup() {
35 if ( ! wp_next_scheduled( self::CRON_HOOK ) ) {
36 wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_HOOK );
37 }
38 }
39
40 public function clear_cleanup() {
41 wp_clear_scheduled_hook( self::CRON_HOOK );
42 }
43
44 /**
45 * Free keeps a rolling 12 months of analytics by default.
46 */
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 public function run_cleanup() {
55 if ( betterdocs()->is_pro_active() ) {
56 return;
57 }
58
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 );
64 if ( $days < 1 ) {
65 return; // filtered to 0 => keep forever.
66 }
67
68 global $wpdb;
69
70 $wpdb->query(
71 $wpdb->prepare(
72 "DELETE FROM {$wpdb->prefix}betterdocs_analytics WHERE created_at < DATE_SUB( CURDATE(), INTERVAL %d DAY )",
73 $days
74 )
75 );
76
77 $wpdb->query(
78 $wpdb->prepare(
79 "DELETE FROM {$wpdb->prefix}betterdocs_search_log WHERE created_at < DATE_SUB( CURDATE(), INTERVAL %d DAY )",
80 $days
81 )
82 );
83 }
84 }
85