syncRunner = $syncRunner; $this->progress = $progress; $this->reader = $reader; $this->settings = $settings; $this->monitor = $monitor; } public function register(): void { \add_action( 'wp_ajax_' . self::START_ACTION, [ $this, 'handleStart' ] ); \add_action( 'wp_ajax_' . self::STATUS_ACTION, [ $this, 'handleStatus' ] ); } public function handleStart(): void { if ( ! \current_user_can( SettingsPage::CAPABILITY ) ) { \wp_send_json_error( [ 'message' => \__( 'Forbidden.', 'agent001-support-assistant-for-woocommerce' ) ], 403 ); } \check_ajax_referer( self::NONCE, 'nonce' ); if ( ! $this->settings->hasApiKey() ) { \wp_send_json_error( [ 'message' => \__( 'Connect your store before syncing.', 'agent001-support-assistant-for-woocommerce' ) ], 409 ); } if ( ! $this->settings->hasSelectedAgents() ) { \wp_send_json_error( [ 'message' => \__( 'Select at least one agent before syncing.', 'agent001-support-assistant-for-woocommerce' ) ], 409 ); } if ( $this->progress->isRunning() ) { \wp_send_json_error( [ 'message' => \__( 'Sync is already running.', 'agent001-support-assistant-for-woocommerce' ), 'progress' => $this->progress->current(), ], 409 ); } $total = null; try { $total = $this->reader->count( $this->settings->syncCategories() ); } catch ( Throwable $e ) { $total = null; } $this->syncRunner->schedule( $total ); \status_header( 202 ); \wp_send_json_success( [ 'status' => 'queued', 'progress' => $this->progress->current(), ] ); } public function handleStatus(): void { if ( ! \current_user_can( SettingsPage::CAPABILITY ) ) { \wp_send_json_error( [ 'message' => \__( 'Forbidden.', 'agent001-support-assistant-for-woocommerce' ) ], 403 ); } \check_ajax_referer( self::NONCE, 'nonce' ); $this->monitor->check(); \wp_send_json_success( $this->progress->current() ); } }