synchronizer = $synchronizer; $this->settings = $settings; $this->progress = $progress ?? new SyncProgress(); } public function run(): SyncResult { if ( ! $this->settings->hasSelectedAgents() ) { return new SyncResult( false, \__( 'Select at least one agent before syncing.', 'agent001-support-assistant-for-woocommerce' ), 0, 0, time() ); } if ( \get_transient( self::LOCK_TRANSIENT ) ) { return new SyncResult( false, \__( 'Previous sync is still running.', 'agent001-support-assistant-for-woocommerce' ), 0, 0, time() ); } \set_transient( self::LOCK_TRANSIENT, 1, self::LOCK_TTL_SECONDS ); try { $result = $this->synchronizer->sync(); \update_option( self::LAST_RESULT_OPTION, $result->toArray(), false ); return $result; } finally { \delete_transient( self::LOCK_TRANSIENT ); } } public function register(): void { \add_action( self::SYNC_HOOK, [ $this, 'runQueued' ] ); } public function schedule( ?int $total = null ): void { $this->progress->start( $total ); if ( \function_exists( 'as_has_scheduled_action' ) && \as_has_scheduled_action( self::SYNC_HOOK, [], ProductHooks::ACTION_GROUP ) ) { return; } if ( ! \function_exists( 'as_enqueue_async_action' ) || ! \as_enqueue_async_action( self::SYNC_HOOK, [], ProductHooks::ACTION_GROUP, true ) ) { $this->progress->fail( \__( 'Could not queue the synchronization. Please try again.', 'agent001-support-assistant-for-woocommerce' ) ); } } public function runQueued(): void { try { $result = $this->run(); if ( ! $result->isSuccess() ) { $this->progress->fail( $result->message() ); } } catch ( \Throwable $e ) { $this->progress->fail( \__( 'Synchronization failed. Please try again.', 'agent001-support-assistant-for-woocommerce' ) ); } } }