PayloadBuilder.php
6 days ago
RegisterSite.php
6 days ago
Scheduler.php
6 days ago
Sender.php
6 days ago
Scheduler.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Schedules the weekly usage tracking cron for Reviews Feed. |
| 5 | * |
| 6 | * @package SmashBalloon\Reviews\Common\UsageTracking\Core |
| 7 | * @since 1.0 |
| 8 | */ |
| 9 | |
| 10 | namespace SmashBalloon\Reviews\Common\UsageTracking\Core; |
| 11 | |
| 12 | use SmashBalloon\Reviews\Common\UsageTracking\Config; |
| 13 | |
| 14 | if (! defined('ABSPATH')) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | class Scheduler { |
| 19 | /** |
| 20 | * Ensure weekly cron is scheduled when tracking is enabled. |
| 21 | * Call on init or when enabling the option. |
| 22 | */ |
| 23 | public function schedule(): void |
| 24 | { |
| 25 | $this->cleanup_legacy(); |
| 26 | |
| 27 | if (! Config::is_enabled()) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | if (wp_next_scheduled(Config::CRON_HOOK)) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | $tracking = array( |
| 36 | 'day' => wp_rand(0, 6), |
| 37 | 'hour' => wp_rand(0, 23), |
| 38 | 'minute' => wp_rand(0, 59), |
| 39 | 'second' => wp_rand(0, 59), |
| 40 | ); |
| 41 | $tracking['offset'] = ($tracking['day'] * DAY_IN_SECONDS) |
| 42 | + ($tracking['hour'] * HOUR_IN_SECONDS) |
| 43 | + ($tracking['minute'] * MINUTE_IN_SECONDS) |
| 44 | + $tracking['second']; |
| 45 | |
| 46 | $last_sunday = strtotime('next sunday') - (7 * DAY_IN_SECONDS); |
| 47 | if (($last_sunday + $tracking['offset']) > time() + 6 * HOUR_IN_SECONDS) { |
| 48 | $tracking['initsend'] = $last_sunday + $tracking['offset']; |
| 49 | } else { |
| 50 | $tracking['initsend'] = strtotime('next sunday') + $tracking['offset']; |
| 51 | } |
| 52 | |
| 53 | wp_schedule_event($tracking['initsend'], 'weekly', Config::CRON_HOOK); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Clear the removed legacy tracking cron and delete its superseded config |
| 58 | * option. Guarded so steady-state cost is a no-op. Runs on every schedule() |
| 59 | * call, including the direct call from SettingsManagerService on consent |
| 60 | * change. Does not touch Config::OPTION_TRACKING, which stores last_send. |
| 61 | */ |
| 62 | private function cleanup_legacy(): void |
| 63 | { |
| 64 | if (wp_next_scheduled('sbr_usage_tracking_cron')) { |
| 65 | wp_clear_scheduled_hook('sbr_usage_tracking_cron'); |
| 66 | } |
| 67 | if (get_option('sbr_usage_tracking_config', null) !== null) { |
| 68 | delete_option('sbr_usage_tracking_config'); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add weekly schedule if not already present. |
| 74 | * |
| 75 | * @param array $schedules Existing cron schedules. |
| 76 | * @return array |
| 77 | */ |
| 78 | public function add_schedules($schedules) |
| 79 | { |
| 80 | if (! isset($schedules['weekly'])) { |
| 81 | $schedules['weekly'] = array( |
| 82 | 'interval' => 604800, |
| 83 | 'display' => __('Once Weekly', 'reviews-feed'), |
| 84 | ); |
| 85 | } |
| 86 | return $schedules; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Unschedule the cron when tracking is disabled. |
| 91 | */ |
| 92 | public function unschedule(): void |
| 93 | { |
| 94 | wp_clear_scheduled_hook(Config::CRON_HOOK); |
| 95 | } |
| 96 | } |
| 97 |