$schedules * @return array */ public static function intervals(array $schedules, Config $config): array { $schedules[self::RECURRENCE] = [ 'interval' => $config->interval, 'display' => __('Freshet Backup interval', 'freshet-backup'), ]; return $schedules; } public static function sync(Config $config): void { $event = wp_get_scheduled_event(self::HOOK); if (!$config->enabled) { if (defined('WP_CLI') && WP_CLI && !isset($_SERVER['HTTP_HOST'])) { // A command run without --url names no host, so a connection guarded by one reads as // off. That says nothing about the site; leave its schedule for a request that does. return; } if ($event !== false) { wp_clear_scheduled_hook(self::HOOK); } return; } if ($event !== false && $event->schedule === self::RECURRENCE && (int) $event->interval === $config->interval) { return; } if ($event !== false) { wp_clear_scheduled_hook(self::HOOK); } wp_schedule_event(time() + MINUTE_IN_SECONDS, self::RECURRENCE, self::HOOK); update_option(self::SET_OPTION, time(), false); } /** * Move the event one interval out from now. * * Only WP-Cron reschedules a recurring event, so a WP-Cron that has stopped * leaves the due time frozen in the past. After a catch-up run has made the * backup that due time was waiting for, this puts the event where it would * have been — otherwise the screen keeps reporting an overdue run that has * in fact just been made. */ public static function advance(Config $config): void { if (!$config->enabled) { return; } wp_clear_scheduled_hook(self::HOOK); wp_schedule_event(time() + $config->interval, self::RECURRENCE, self::HOOK); } public static function describe(int $seconds): string { return match (true) { $seconds === DAY_IN_SECONDS => __('every day', 'freshet-backup'), $seconds === WEEK_IN_SECONDS => __('every week', 'freshet-backup'), $seconds === HOUR_IN_SECONDS => __('every hour', 'freshet-backup'), $seconds % HOUR_IN_SECONDS === 0 => sprintf( /* translators: %d: number of hours */ _n('every %d hour', 'every %d hours', intdiv($seconds, HOUR_IN_SECONDS), 'freshet-backup'), intdiv($seconds, HOUR_IN_SECONDS) ), default => sprintf( /* translators: %d: number of minutes */ _n('every %d minute', 'every %d minutes', intdiv($seconds, MINUTE_IN_SECONDS), 'freshet-backup'), intdiv($seconds, MINUTE_IN_SECONDS) ), }; } }