*/ public static function intervals(): array { return array( self::EXPIRE => 5 * MINUTE_IN_SECONDS, self::MESSAGES => 15 * MINUTE_IN_SECONDS, self::HOURLY => HOUR_IN_SECONDS, self::DAILY => DAY_IN_SECONDS, /** * How often imported iCal calendars are fetched; Pro makes it hourly. * * @param int $seconds Seconds between imports. */ self::CALENDARS => max( 15 * MINUTE_IN_SECONDS, (int) apply_filters( 'schedulereed_calendar_sync_interval', 6 * HOUR_IN_SECONDS ) ), ); } public function register(): void { add_filter( 'cron_schedules', array( $this, 'schedules' ) ); // phpcs:ignore WordPress.WP.CronInterval.ChangeDetected -- Every five and fifteen minutes; see schedules(). add_action( self::EXPIRE, static fn() => Services::checkout()->expire_unpaid() ); add_action( self::MESSAGES, static fn() => Services::notifier()->scheduled() ); add_action( self::HOURLY, array( $this, 'hourly' ) ); add_action( self::DAILY, array( $this, 'daily' ) ); add_action( self::CALENDARS, static fn() => Services::calendar_sync()->sync_all() ); add_action( 'init', array( $this, 'ensure' ), 20 ); } public function hourly(): void { Services::checkout()->complete_past(); Services::sessions()->cancel_under_minimum(); Services::tokens()->purge(); /** * Fires once an hour, for Pro's hourly work. */ do_action( 'schedulereed_hourly_tasks' ); } public function daily(): void { Services::sessions()->generate_all(); Services::privacy()->apply_retention(); /** * Fires once a day, for Pro's daily work. */ do_action( 'schedulereed_daily_tasks' ); } /** * WP-Cron intervals for the jobs that run more often than hourly. * * @param array $schedules Schedules. * @return array */ public function schedules( array $schedules ): array { foreach ( self::intervals() as $hook => $seconds ) { $schedules[ 'schedulereed_' . $seconds ] = array( 'interval' => $seconds, /* translators: 1: minutes, 2: plugin name. */ 'display' => sprintf( __( 'Every %1$d minutes (%2$s)', 'schedulereed' ), intdiv( $seconds, 60 ), Brand::name() ), ); } return $schedules; } /** * Make sure every job is scheduled, at its current interval. */ public function ensure(): void { $scheduler = self::has_action_scheduler(); $stored = (array) get_option( 'schedulereed_jobs', array() ); $wanted = self::intervals() + array( 'runner' => $scheduler ? 'as' : 'cron' ); if ( $stored === $wanted ) { return; } self::clear(); foreach ( self::intervals() as $hook => $seconds ) { if ( $scheduler ) { as_schedule_recurring_action( time() + 60, $seconds, $hook, array(), self::GROUP ); } else { wp_schedule_event( time() + 60, 'schedulereed_' . $seconds, $hook ); } } update_option( 'schedulereed_jobs', $wanted, false ); } /** * Unschedule every job, from both runners. */ public static function clear(): void { foreach ( array_keys( self::intervals() ) as $hook ) { wp_clear_scheduled_hook( $hook ); if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( $hook, array(), self::GROUP ); } } delete_option( 'schedulereed_jobs' ); } /** * Run a one-off task soon, in the background. * * @param string $hook Action. * @param array $args Arguments. * @param int $delay Seconds from now. */ public static function later( string $hook, array $args = array(), int $delay = 0 ): void { if ( self::has_action_scheduler() ) { as_schedule_single_action( time() + $delay, $hook, $args, self::GROUP ); return; } wp_schedule_single_event( time() + $delay, $hook, $args ); } private static function has_action_scheduler(): bool { /** * Whether to use Action Scheduler when it is available. * * @param bool $use Use it. */ return function_exists( 'as_schedule_recurring_action' ) && did_action( 'action_scheduler_init' ) && (bool) apply_filters( 'schedulereed_use_action_scheduler', true ); } }