scanner = $scanner; $this->budget = $budget; } /** * Hook the event that continues a scan. */ public function register(): void { add_action( self::CHUNK_HOOK, array( $this, 'run' ) ); } /** * Run one chunk of the scan, and schedule the next when the scan is not complete and none is scheduled yet, which * keeps a run that lost the lock to another from queuing a second event. */ public function run(): void { if ( ! $this->scanner->advance( $this->budget ) && $this->scanner->running() && false === wp_next_scheduled( self::CHUNK_HOOK ) ) { wp_schedule_single_event( time() + self::CHUNK_DELAY, self::CHUNK_HOOK ); } } /** * Remove the recurring event left over from an earlier schedule. */ public function remove_recurring(): void { if ( false !== wp_next_scheduled( self::SCAN_HOOK ) ) { wp_unschedule_hook( self::SCAN_HOOK ); } } /** * Remove both events. */ public function unschedule(): void { wp_unschedule_hook( self::SCAN_HOOK ); wp_unschedule_hook( self::CHUNK_HOOK ); } }