PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.5
4.9.5 4.9.4 4.9.3 4.9.2 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 All 203 releases
betterdocs / includes / Core / AnalyticsRetention.php

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

96 lines 2.5 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.
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 *
18 * Pro-safe: when Pro is active the purge is skipped entirely, because Pro
19 * manages its own retention (data_retention_days) on top of these tables.
20 */
21 class AnalyticsRetention extends Base {
22 const CRON_HOOK = 'betterdocs_analytics_cleanup';
23
24 public function __construct() {
25 add_action( 'init', [ $this, 'maybe_schedule_cleanup' ] );
26 add_action( self::CRON_HOOK, [ $this, 'run_cleanup' ] );
27
28 if ( defined( 'BETTERDOCS_PLUGIN_FILE' ) ) {
29 register_deactivation_hook( BETTERDOCS_PLUGIN_FILE, [ $this, 'clear_cleanup' ] );
30 }
31 }
32
33 /**
34 * Retention window in days. 0 (the default) keeps everything.
35 *
36 * @return int
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 */
46 public function maybe_schedule_cleanup() {
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 ) {
57 wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_HOOK );
58 }
59 }
60
61 public function clear_cleanup() {
62 wp_clear_scheduled_hook( self::CRON_HOOK );
63 }
64
65 /**
66 * Delete aggregate rows older than the opted-in retention window. No-op when
67 * Pro is active or no window is set.
68 */
69 public function run_cleanup() {
70 if ( betterdocs()->is_pro_active() ) {
71 return;
72 }
73
74 $days = $this->retention_days();
75 if ( $days < 1 ) {
76 return;
77 }
78
79 global $wpdb;
80
81 $wpdb->query(
82 $wpdb->prepare(
83 "DELETE FROM {$wpdb->prefix}betterdocs_analytics WHERE created_at < DATE_SUB( CURDATE(), INTERVAL %d DAY )",
84 $days
85 )
86 );
87
88 $wpdb->query(
89 $wpdb->prepare(
90 "DELETE FROM {$wpdb->prefix}betterdocs_search_log WHERE created_at < DATE_SUB( CURDATE(), INTERVAL %d DAY )",
91 $days
92 )
93 );
94 }
95 }
96