PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.7.3
Independent Analytics – WordPress Analytics Plugin v2.7.3
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Data_Pruning / Pruning_Scheduler.php
independent-analytics / IAWP / Data_Pruning Last commit date
Pruner.php 2 years ago Pruning_Scheduler.php 2 years ago
Pruning_Scheduler.php
117 lines
1 <?php
2
3 namespace IAWP\Data_Pruning;
4
5 use IAWPSCOPED\Carbon\CarbonImmutable;
6 use IAWP\Utils\WordPress_Site_Date_Format_Pattern;
7 use IAWPSCOPED\Proper\Timezone;
8 /** @internal */
9 class Pruning_Scheduler
10 {
11 public static $cutoff_options = [['disabled', 'Keep data forever'], ['thirty-days', 'Keep data for 30 days'], ['sixty-days', 'Keep data for 60 days'], ['ninety-days', 'Keep data for 90 days'], ['one-hundred-and-eighty-days', 'Keep data for 180 days'], ['one-year', 'Keep data for 1 year'], ['two-years', 'Keep data for 2 years'], ['three-years', 'Keep data for 3 years'], ['four-years', 'Keep data for 4 years']];
12 public function cutoff_options() : array
13 {
14 return self::$cutoff_options;
15 }
16 public function is_enabled() : bool
17 {
18 return \get_option('iawp_pruning_cutoff', 'disabled') !== 'disabled';
19 }
20 public function get_pruning_cutoff() : string
21 {
22 return \get_option('iawp_pruning_cutoff', 'disabled');
23 }
24 public function get_pruning_cutoff_as_datetime() : ?\DateTime
25 {
26 if (!$this->is_enabled()) {
27 return null;
28 }
29 return $this->convert_cutoff_to_date($this->get_pruning_cutoff());
30 }
31 public function status_message() : ?string
32 {
33 if (!$this->is_enabled()) {
34 return null;
35 }
36 $scheduled_at = new \DateTime();
37 $scheduled_at->setTimezone(Timezone::site_timezone());
38 $scheduled_at->setTimestamp(\wp_next_scheduled('iawp_prune'));
39 $day = $scheduled_at->format(\get_option('date_format'));
40 $time = $scheduled_at->format(\get_option('time_format'));
41 return \sprintf(\__('Next data pruning scheduled for %s at %s.', 'independent-analytics'), '<span>' . $day . '</span>', '<span>' . $time . '</span>');
42 }
43 public function get_pruning_description(string $cutoff) : string
44 {
45 $date = $this->convert_cutoff_to_date($cutoff, \true);
46 $formatted_date = $date->format(WordPress_Site_Date_Format_Pattern::for_php());
47 return \sprintf(\__('All data from before %1$s will be immediately deleted. This process will repeat daily at midnight.', 'independent-analytics'), $formatted_date);
48 }
49 public function update_pruning_cutoff(string $cutoff) : bool
50 {
51 $is_valid_cutoff = \false;
52 foreach (self::$cutoff_options as $option) {
53 if ($option[0] === $cutoff) {
54 $is_valid_cutoff = \true;
55 }
56 }
57 if (!$is_valid_cutoff) {
58 return \false;
59 }
60 \update_option('iawp_pruning_cutoff', $cutoff, \true);
61 $this->schedule_pruning($cutoff);
62 return \true;
63 }
64 /**
65 * Attempt to schedule pruning based on whatever existing cutoff option is saved
66 *
67 * @return void
68 */
69 public function schedule() : void
70 {
71 $cutoff = \get_option('iawp_pruning_cutoff', '');
72 $this->update_pruning_cutoff($cutoff);
73 }
74 public function unschedule() : void
75 {
76 $scheduled_at_timestamp = \wp_next_scheduled('iawp_prune');
77 if (\is_int($scheduled_at_timestamp)) {
78 \wp_unschedule_event($scheduled_at_timestamp, 'iawp_prune');
79 }
80 }
81 private function schedule_pruning(string $cutoff) : void
82 {
83 $this->unschedule();
84 if ($cutoff === 'disabled') {
85 return;
86 }
87 $tomorrow = new \DateTime('tomorrow', Timezone::site_timezone());
88 \wp_schedule_event($tomorrow->getTimestamp(), 'daily', 'iawp_prune');
89 }
90 private function convert_cutoff_to_date(string $cutoff, bool $as_site_timezone = \false) : \DateTime
91 {
92 $beginning_of_today = new CarbonImmutable('today', Timezone::site_timezone());
93 if (!$as_site_timezone) {
94 $beginning_of_today = $beginning_of_today->setTimezone(Timezone::utc_timezone());
95 }
96 switch ($cutoff) {
97 case 'thirty-days':
98 return $beginning_of_today->subDays(30)->toDate();
99 case 'sixty-days':
100 return $beginning_of_today->subDays(60)->toDate();
101 case 'ninety-days':
102 return $beginning_of_today->subDays(90)->toDate();
103 case 'one-hundred-and-eighty-days':
104 return $beginning_of_today->subDays(180)->toDate();
105 case 'one-year':
106 return $beginning_of_today->subYearsNoOverflow(1)->toDate();
107 case 'two-years':
108 return $beginning_of_today->subYearsNoOverflow(2)->toDate();
109 case 'three-years':
110 return $beginning_of_today->subYearsNoOverflow(3)->toDate();
111 case 'four-years':
112 return $beginning_of_today->subYearsNoOverflow(4)->toDate();
113 }
114 return $beginning_of_today->toDate();
115 }
116 }
117