'', 'widget_key' => '', 'connected_site' => '', 'account_name' => '', 'sync_categories' => [], 'selected_agent_ids' => [], 'selected_agent_embed_token' => '', 'selected_agent_active' => true, ]; private ?array $cached = null; public function all(): array { if ( $this->cached === null ) { $stored = \get_option( self::OPTION_NAME, [] ); $this->cached = array_merge( self::DEFAULTS, is_array( $stored ) ? $stored : [] ); } return $this->cached; } public function save( array $data ): void { \update_option( self::OPTION_NAME, $data ); $this->cached = null; } public function apiUrl(): string { $url = \defined( 'AGENT001_API_URL' ) ? (string) \AGENT001_API_URL : self::API_URL; return (string) \apply_filters( 'agent001_api_url', $url ); } public function connectUrl(): string { $url = \rtrim( $this->backendBaseUrl(), '/' ) . '/connect'; return (string) \apply_filters( 'agent001_connect_url', $url ); } public function exchangeUrl(): string { $url = \rtrim( $this->backendBaseUrl(), '/' ) . '/connect/exchange'; return (string) \apply_filters( 'agent001_exchange_url', $url ); } public function productEndpoint( int $productId ): string { $url = \rtrim( $this->backendBaseUrl(), '/' ) . '/api/v1/ingest/products/' . \rawurlencode( (string) $productId ); return (string) \apply_filters( 'agent001_product_endpoint', $url, $productId ); } public function syncSessionUrl( string $sessionId ): string { $url = \rtrim( $this->apiUrl(), '/' ) . '/sessions/' . \rawurlencode( $sessionId ); return (string) \apply_filters( 'agent001_sync_session_url', $url, $sessionId ); } public function agentsUrl(): string { $url = \rtrim( $this->backendBaseUrl(), '/' ) . '/api/v1/integrations/me/agents'; return (string) \apply_filters( 'agent001_agents_url', $url ); } public function agentDomainUrl( string $agentId ): string { $url = \rtrim( $this->backendBaseUrl(), '/' ) . '/api/v1/integrations/me/agents/' . \rawurlencode( AgentId::normalize( $agentId ) ) . '/domain'; return (string) \apply_filters( 'agent001_agent_domain_url', $url, $agentId ); } public function agentActiveUrl( string $agentId ): string { $url = \rtrim( $this->backendBaseUrl(), '/' ) . '/api/v1/integrations/me/agents/' . \rawurlencode( AgentId::normalize( $agentId ) ) . '/active'; return (string) \apply_filters( 'agent001_agent_active_url', $url, $agentId ); } public function apiKey(): string { if ( \defined( 'AGENT001_API_KEY' ) ) { return (string) \AGENT001_API_KEY; } return (string) \apply_filters( 'agent001_api_key', (string) $this->all()['api_key'] ); } public function setApiKey( string $apiKey ): void { $current = $this->all(); $current['api_key'] = $apiKey; $this->save( $current ); } public function connectedSite(): string { return (string) ( $this->all()['connected_site'] ?? '' ); } public function setConnectedSite( string $site ): void { $current = $this->all(); $current['connected_site'] = $site; $this->save( $current ); } public function accountName(): string { return (string) ( $this->all()['account_name'] ?? '' ); } public function setAccountName( string $name ): void { $current = $this->all(); $current['account_name'] = $name; $this->save( $current ); } /** * @return int[] */ public function syncCategories(): array { $raw = $this->all()['sync_categories'] ?? []; if ( ! is_array( $raw ) ) { return []; } $ids = []; foreach ( $raw as $value ) { $id = (int) $value; if ( $id > 0 ) { $ids[] = $id; } } return array_values( array_unique( $ids ) ); } /** * @param int[] $ids */ public function setSyncCategories( array $ids ): void { $clean = []; foreach ( $ids as $value ) { $id = (int) $value; if ( $id > 0 ) { $clean[] = $id; } } $current = $this->all(); $current['sync_categories'] = array_values( array_unique( $clean ) ); $this->save( $current ); } /** * @return list */ public function selectedAgentIds(): array { $raw = $this->all()['selected_agent_ids'] ?? []; return is_array( $raw ) ? AgentId::normalizeList( $raw ) : []; } /** * @param array $ids */ public function setSelectedAgentIds( array $ids ): void { $current = $this->all(); $current['selected_agent_ids'] = AgentId::normalizeList( $ids ); $this->save( $current ); } public function hasSelectedAgents(): bool { return $this->selectedAgentIds() !== []; } public function selectedAgentEmbedToken(): string { if ( ! $this->hasSelectedAgents() ) { return ''; } return (string) ( $this->all()['selected_agent_embed_token'] ?? '' ); } public function setSelectedAgentEmbedToken( string $token ): void { $current = $this->all(); $current['selected_agent_embed_token'] = $token; $this->save( $current ); } public function selectedAgentIsActive(): bool { return (bool) ( $this->all()['selected_agent_active'] ?? true ); } public function setSelectedAgentActive( bool $active ): void { $current = $this->all(); $current['selected_agent_active'] = $active; $this->save( $current ); } public function widgetLoaderUrl(): string { $url = \rtrim( $this->backendBaseUrl(), '/' ) . '/embed/loader.js'; return (string) \apply_filters( 'agent001_widget_loader_url', $url ); } public function widgetKey(): string { $key = (string) ( $this->all()['widget_key'] ?? '' ); if ( $key !== '' ) { return $key; } try { $key = \bin2hex( \random_bytes( 32 ) ); } catch ( \Throwable $e ) { $key = \wp_generate_password( 64, false, false ); } $current = $this->all(); $current['widget_key'] = $key; $this->save( $current ); return $key; } public function setWidgetKey( string $key ): void { $current = $this->all(); $current['widget_key'] = $key; $this->save( $current ); } public function backendBaseUrl(): string { $apiUrl = $this->apiUrl(); $parts = \wp_parse_url( $apiUrl ); if ( ! is_array( $parts ) || empty( $parts['scheme'] ) || empty( $parts['host'] ) ) { return 'https://app.agent001.ai'; } $base = $parts['scheme'] . '://' . $parts['host']; if ( ! empty( $parts['port'] ) ) { $base .= ':' . $parts['port']; } return $base; } public function apiKeyIsLocked(): bool { return \defined( 'AGENT001_API_KEY' ); } public function hasApiKey(): bool { return $this->apiKey() !== ''; } }