Pruning_Scheduler.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Data_Pruning; |
| 4 | |
| 5 | use IAWPSCOPED\Carbon\CarbonImmutable; |
| 6 | use IAWP\Illuminate_Builder; |
| 7 | use IAWP\Query; |
| 8 | use IAWP\Utils\Timezone; |
| 9 | use IAWP\Utils\WordPress_Site_Date_Format_Pattern; |
| 10 | /** @internal */ |
| 11 | class Pruning_Scheduler |
| 12 | { |
| 13 | 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']]; |
| 14 | public function cutoff_options() : array |
| 15 | { |
| 16 | return self::$cutoff_options; |
| 17 | } |
| 18 | public function is_enabled() : bool |
| 19 | { |
| 20 | return \get_option('iawp_pruning_cutoff', 'disabled') !== 'disabled'; |
| 21 | } |
| 22 | public function get_pruning_cutoff() : string |
| 23 | { |
| 24 | return \get_option('iawp_pruning_cutoff', 'disabled'); |
| 25 | } |
| 26 | public function get_pruning_cutoff_as_datetime() : ?\DateTime |
| 27 | { |
| 28 | if (!$this->is_enabled()) { |
| 29 | return null; |
| 30 | } |
| 31 | return $this->convert_cutoff_to_date($this->get_pruning_cutoff()); |
| 32 | } |
| 33 | public function status_message() : ?string |
| 34 | { |
| 35 | if (!$this->is_enabled()) { |
| 36 | return null; |
| 37 | } |
| 38 | $scheduled_at = new \DateTime(); |
| 39 | $scheduled_at->setTimezone(Timezone::site_timezone()); |
| 40 | $scheduled_at->setTimestamp(\wp_next_scheduled('iawp_prune')); |
| 41 | $day = $scheduled_at->format(\get_option('date_format')); |
| 42 | $time = $scheduled_at->format(\IAWPSCOPED\iawp()->get_option('time_format', 'g:i a')); |
| 43 | return \sprintf(\__('Next data pruning scheduled for %s at %s.', 'independent-analytics'), '<span>' . $day . '</span>', '<span>' . $time . '</span>'); |
| 44 | } |
| 45 | public function get_pruning_description(string $cutoff) : string |
| 46 | { |
| 47 | $date = $this->convert_cutoff_to_date($cutoff, \true); |
| 48 | $utc_date = $this->convert_cutoff_to_date($cutoff); |
| 49 | $formatted_date = $date->format(WordPress_Site_Date_Format_Pattern::for_php()); |
| 50 | $estimates = $this->get_pruning_estimates($utc_date); |
| 51 | return \sprintf(\__("All data from before %1\$s will be deleted immediately. This will remove %2\$s of your %3\$s tracked sessions. \n\n This process will repeat daily at midnight.", 'independent-analytics'), $formatted_date, \number_format_i18n($estimates['sessions_to_be_deleted']), \number_format_i18n($estimates['sessions'])); |
| 52 | } |
| 53 | /** |
| 54 | * @return array{sessions: int, sessions_to_be_deleted: int} |
| 55 | */ |
| 56 | public function get_pruning_estimates(\DateTime $cutoff_date) : array |
| 57 | { |
| 58 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 59 | $sessions = Illuminate_Builder::new()->from($sessions_table)->selectRaw('COUNT(*) as sessions')->value('sessions'); |
| 60 | $sessions_to_be_deleted = Illuminate_Builder::new()->from($sessions_table)->selectRaw('COUNT(*) as sessions')->where('created_at', '<', $cutoff_date)->value('sessions'); |
| 61 | return ['sessions' => $sessions, 'sessions_to_be_deleted' => $sessions_to_be_deleted]; |
| 62 | } |
| 63 | public function update_pruning_cutoff(string $cutoff) : bool |
| 64 | { |
| 65 | $is_valid_cutoff = \false; |
| 66 | foreach (self::$cutoff_options as $option) { |
| 67 | if ($option[0] === $cutoff) { |
| 68 | $is_valid_cutoff = \true; |
| 69 | } |
| 70 | } |
| 71 | if (!$is_valid_cutoff) { |
| 72 | return \false; |
| 73 | } |
| 74 | \update_option('iawp_pruning_cutoff', $cutoff, \true); |
| 75 | $this->schedule_pruning($cutoff); |
| 76 | return \true; |
| 77 | } |
| 78 | /** |
| 79 | * Attempt to schedule pruning based on whatever existing cutoff option is saved |
| 80 | * |
| 81 | * @return void |
| 82 | */ |
| 83 | public function schedule() : void |
| 84 | { |
| 85 | $cutoff = \get_option('iawp_pruning_cutoff', ''); |
| 86 | $this->update_pruning_cutoff($cutoff); |
| 87 | } |
| 88 | public function unschedule() : void |
| 89 | { |
| 90 | $scheduled_at_timestamp = \wp_next_scheduled('iawp_prune'); |
| 91 | if (\is_int($scheduled_at_timestamp)) { |
| 92 | \wp_unschedule_event($scheduled_at_timestamp, 'iawp_prune'); |
| 93 | } |
| 94 | } |
| 95 | private function schedule_pruning(string $cutoff) : void |
| 96 | { |
| 97 | $this->unschedule(); |
| 98 | if ($cutoff === 'disabled') { |
| 99 | return; |
| 100 | } |
| 101 | $tomorrow = new \DateTime('tomorrow', Timezone::site_timezone()); |
| 102 | \wp_schedule_event($tomorrow->getTimestamp(), 'daily', 'iawp_prune'); |
| 103 | } |
| 104 | private function convert_cutoff_to_date(string $cutoff, bool $as_site_timezone = \false) : \DateTime |
| 105 | { |
| 106 | $beginning_of_today = new CarbonImmutable('today', Timezone::site_timezone()); |
| 107 | if (!$as_site_timezone) { |
| 108 | $beginning_of_today = $beginning_of_today->setTimezone(Timezone::utc_timezone()); |
| 109 | } |
| 110 | switch ($cutoff) { |
| 111 | case 'thirty-days': |
| 112 | return $beginning_of_today->subDays(30)->toDate(); |
| 113 | case 'sixty-days': |
| 114 | return $beginning_of_today->subDays(60)->toDate(); |
| 115 | case 'ninety-days': |
| 116 | return $beginning_of_today->subDays(90)->toDate(); |
| 117 | case 'one-hundred-and-eighty-days': |
| 118 | return $beginning_of_today->subDays(180)->toDate(); |
| 119 | case 'one-year': |
| 120 | return $beginning_of_today->subYearsNoOverflow(1)->toDate(); |
| 121 | case 'two-years': |
| 122 | return $beginning_of_today->subYearsNoOverflow(2)->toDate(); |
| 123 | case 'three-years': |
| 124 | return $beginning_of_today->subYearsNoOverflow(3)->toDate(); |
| 125 | case 'four-years': |
| 126 | return $beginning_of_today->subYearsNoOverflow(4)->toDate(); |
| 127 | } |
| 128 | return $beginning_of_today->toDate(); |
| 129 | } |
| 130 | } |
| 131 |