PluginProbe
Notibar – Notification Bar for WordPress / trunk
Notibar – Notification Bar for WordPress vtrunk
3.2.2 3.2.1.1 3.2.1 3.1.5 3.1.4 3.1.1 3.1.0 trunk 1.9.2 1.9.3 2.0 2.1.1 2.1.4 2.1.5 2.1.6 2.1.8 2.1.9
notibar / includes / NotificationBar / TrackingSettings.php

TrackingSettings.php in Notibar – Notification Bar for WordPress trunk, at includes/NotificationBar/TrackingSettings.php

79 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Tracking settings store.
4 *
5 * Holds operator-tunable tracking options in a single small autoloaded
6 * `notibar_tracking` option. Currently just retention_days — how long raw
7 * events in {prefix}notibar_events are kept before the daily prune cron
8 * (TrackingCron) deletes them.
9 *
10 * setRetentionDays() is the sanitize chokepoint: a future Settings UI must
11 * route writes through it (with manage_options + nonce at the REST/form layer).
12 *
13 * @package NjtNotificationBar\NotificationBar
14 * @since 3.1.2
15 */
16
17 namespace NjtNotificationBar\NotificationBar;
18
19 defined( 'ABSPATH' ) || exit;
20
21 class TrackingSettings {
22
23 /** @var string Option name (autoloaded; tiny row). */
24 const OPTION = 'notibar_tracking';
25
26 /** @var int Default retention window in days. */
27 const DEFAULT_RETENTION = 90;
28
29 /** @var int Clamp bounds for retention_days. */
30 const MIN_RETENTION = 1;
31 const MAX_RETENTION = 3650;
32
33 /**
34 * Retention window in days, clamped to [MIN, MAX]. Falls back to default
35 * when unset or invalid.
36 */
37 public static function getRetentionDays(): int {
38 $opt = get_option( self::OPTION );
39 $raw = is_array( $opt ) && isset( $opt['retention_days'] )
40 ? $opt['retention_days']
41 : self::DEFAULT_RETENTION;
42 return self::sanitizeDays( $raw );
43 }
44
45 /**
46 * Persist retention_days (sanitized + clamped). Merges into the option so
47 * future keys are preserved.
48 *
49 * @return int The clamped value actually stored.
50 */
51 public static function setRetentionDays( $days ): int {
52 $clamped = self::sanitizeDays( $days );
53 $opt = get_option( self::OPTION );
54 if ( ! is_array( $opt ) ) {
55 $opt = [];
56 }
57 $opt['retention_days'] = $clamped;
58 // autoload=yes (unlike EventCounter's counters option which is autoload=no):
59 // this row is a single int read on every request by the cron-wiring path,
60 // so keeping it autoloaded is cheaper than a separate query.
61 update_option( self::OPTION, $opt, true );
62 return $clamped;
63 }
64
65 /**
66 * Coerce to int. Values below MIN_RETENTION (0, negative, garbage) are
67 * treated as unset and fall back to DEFAULT_RETENTION rather than clamping
68 * to 1 — avoids an accidental near-zero retention nuking all history.
69 * Values above MAX_RETENTION clamp down to MAX_RETENTION.
70 */
71 public static function sanitizeDays( $days ): int {
72 $n = (int) $days;
73 if ( $n < self::MIN_RETENTION ) {
74 return self::DEFAULT_RETENTION;
75 }
76 return min( $n, self::MAX_RETENTION );
77 }
78 }
79