Pruning_Scheduler.php
141 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\Format; |
| 9 | use IAWP\Utils\Timezone; |
| 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 | $value = \get_option('iawp_pruning_cutoff'); |
| 21 | if (!\is_string($value)) { |
| 22 | return \false; |
| 23 | } |
| 24 | if ($this->convert_cutoff_to_date($value) === null) { |
| 25 | return \false; |
| 26 | } |
| 27 | return \true; |
| 28 | } |
| 29 | public function get_pruning_cutoff() : string |
| 30 | { |
| 31 | return \get_option('iawp_pruning_cutoff', 'disabled'); |
| 32 | } |
| 33 | public function get_pruning_cutoff_as_datetime() : ?\DateTime |
| 34 | { |
| 35 | if (!$this->is_enabled()) { |
| 36 | return null; |
| 37 | } |
| 38 | return $this->convert_cutoff_to_date($this->get_pruning_cutoff()); |
| 39 | } |
| 40 | public function status_message() : ?string |
| 41 | { |
| 42 | if (!$this->is_enabled()) { |
| 43 | return null; |
| 44 | } |
| 45 | $scheduled_at = new \DateTime('now', Timezone::utc_timezone()); |
| 46 | $scheduled_at->setTimezone(Timezone::site_timezone()); |
| 47 | $scheduled_at->setTimestamp(\wp_next_scheduled('iawp_prune')); |
| 48 | $day = $scheduled_at->format(Format::date()); |
| 49 | $time = $scheduled_at->format(Format::time()); |
| 50 | return \sprintf(\__('Next data pruning scheduled for %s at %s.', 'independent-analytics'), '<strong>' . $day . '</strong>', '<strong>' . $time . '</strong>'); |
| 51 | } |
| 52 | public function get_confirmation_message(string $cutoff) : string |
| 53 | { |
| 54 | $utc_date = $this->convert_cutoff_to_date($cutoff); |
| 55 | if ($utc_date === null) { |
| 56 | return ''; |
| 57 | } |
| 58 | $date = $this->convert_cutoff_to_date($cutoff)->setTimezone(Timezone::site_timezone()); |
| 59 | $formatted_date = $date->format(Format::date()); |
| 60 | $estimates = $this->get_pruning_estimates($utc_date); |
| 61 | 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'])); |
| 62 | } |
| 63 | /** |
| 64 | * @return array{sessions: int, sessions_to_be_deleted: int} |
| 65 | */ |
| 66 | public function get_pruning_estimates(\DateTime $cutoff_date) : array |
| 67 | { |
| 68 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 69 | $sessions = Illuminate_Builder::new()->from($sessions_table)->selectRaw('COUNT(*) as sessions')->value('sessions'); |
| 70 | $sessions_to_be_deleted = Illuminate_Builder::new()->from($sessions_table)->selectRaw('COUNT(*) as sessions')->where('created_at', '<', $cutoff_date)->value('sessions'); |
| 71 | return ['sessions' => $sessions, 'sessions_to_be_deleted' => $sessions_to_be_deleted]; |
| 72 | } |
| 73 | public function update_pruning_cutoff(string $cutoff) : bool |
| 74 | { |
| 75 | $is_valid_cutoff = \false; |
| 76 | foreach (self::$cutoff_options as $option) { |
| 77 | if ($option[0] === $cutoff) { |
| 78 | $is_valid_cutoff = \true; |
| 79 | } |
| 80 | } |
| 81 | if (!$is_valid_cutoff) { |
| 82 | return \false; |
| 83 | } |
| 84 | \update_option('iawp_pruning_cutoff', $cutoff, \true); |
| 85 | $this->schedule_pruning($cutoff); |
| 86 | return \true; |
| 87 | } |
| 88 | /** |
| 89 | * Attempt to schedule pruning based on whatever existing cutoff option is saved |
| 90 | * |
| 91 | * @return void |
| 92 | */ |
| 93 | public function schedule() : void |
| 94 | { |
| 95 | $cutoff = \get_option('iawp_pruning_cutoff', ''); |
| 96 | $this->update_pruning_cutoff($cutoff); |
| 97 | } |
| 98 | private function schedule_pruning(string $cutoff) : void |
| 99 | { |
| 100 | \wp_unschedule_hook('iawp_prune'); |
| 101 | if ($cutoff === 'disabled') { |
| 102 | return; |
| 103 | } |
| 104 | $tomorrow = new \DateTime('tomorrow', Timezone::site_timezone()); |
| 105 | \wp_schedule_event($tomorrow->getTimestamp(), 'daily', 'iawp_prune'); |
| 106 | } |
| 107 | private function convert_cutoff_to_date(string $cutoff) : ?\DateTime |
| 108 | { |
| 109 | $date = new CarbonImmutable('today', Timezone::site_timezone()); |
| 110 | switch ($cutoff) { |
| 111 | case 'thirty-days': |
| 112 | $date = $date->subDays(30); |
| 113 | break; |
| 114 | case 'sixty-days': |
| 115 | $date = $date->subDays(60); |
| 116 | break; |
| 117 | case 'ninety-days': |
| 118 | $date = $date->subDays(90); |
| 119 | break; |
| 120 | case 'one-hundred-and-eighty-days': |
| 121 | $date = $date->subDays(180); |
| 122 | break; |
| 123 | case 'one-year': |
| 124 | $date = $date->subYearsNoOverflow(1); |
| 125 | break; |
| 126 | case 'two-years': |
| 127 | $date = $date->subYearsNoOverflow(2); |
| 128 | break; |
| 129 | case 'three-years': |
| 130 | $date = $date->subYearsNoOverflow(3); |
| 131 | break; |
| 132 | case 'four-years': |
| 133 | $date = $date->subYearsNoOverflow(4); |
| 134 | break; |
| 135 | default: |
| 136 | return null; |
| 137 | } |
| 138 | return $date->setTimezone(Timezone::utc_timezone())->toDate(); |
| 139 | } |
| 140 | } |
| 141 |