sync_approved (cortexcommerce_sync_approved in production), every hour, starts SyncRun. */ final class SyncSchedule { const HOOK = Flavor::HOOK_PREFIX . 'sync_approved'; const GROUP = Flavor::GROUP; const INTERVAL = 3600; public function register(): void { add_action( self::HOOK, array( self::class, 'on_hourly' ) ); } /** * While a run holds the lock this does nothing; the timer simply waits for the next hour. */ public static function on_hourly(): void { SyncRun::start( 'hourly' ); } /** * Schedules the hourly timer unless it already is. The first run is an hour after connect: nothing is approved * before the store's catalog reached CortexCommerce. */ public static function schedule(): void { if ( function_exists( 'as_schedule_recurring_action' ) && ! as_has_scheduled_action( self::HOOK, null, self::GROUP ) ) { as_schedule_recurring_action( time() + self::INTERVAL, self::INTERVAL, self::HOOK, array(), self::GROUP ); } } /** * Removes the hourly timer. */ public static function unschedule(): void { if ( function_exists( 'as_unschedule_all_actions' ) ) { // @phpstan-ignore-next-line argument.type -- null means "any args" to Action Scheduler (see OrderEvents::unschedule_all()). as_unschedule_all_actions( self::HOOK, null, self::GROUP ); } } /** * When the hourly timer next fires, or null when it is not scheduled. */ public static function next(): ?int { if ( ! function_exists( 'as_next_scheduled_action' ) ) { return null; } $next = as_next_scheduled_action( self::HOOK, null, self::GROUP ); return is_int( $next ) ? $next : null; } }