container = $container; } /** * Boot the plugin: register services, then register hooks. * * Safe to call multiple times — second call is a no-op. * * @return void */ public function boot(): void { if ( $this->booted ) { return; } $this->register_services(); $this->register_hooks(); /** * Fires after AICommerce WP has fully booted. * * Third-party integrations can use this hook to extend the plugin, * knowing that all core services are available in the container. * * @param ServiceContainer $container The plugin service container. */ do_action( 'aicommerce_loaded', $this->container ); $this->booted = true; } /** * Register all service factories in the container. * * Services are lazy-loaded: they are not instantiated until first use. * * @return void */ private function register_services(): void { // Database service. $this->container->register( Database::class, static function (): Database { global $wpdb; return new Database( $wpdb ); } ); // Schema services. $this->container->register( ProductSchema::class, static function (): ProductSchema { return new ProductSchema(); } ); $this->container->register( EntityChain::class, static function (): EntityChain { return new EntityChain(); } ); $this->container->register( ConflictDetector::class, static function (): ConflictDetector { return new ConflictDetector(); } ); $this->container->register( Validator::class, static function (): Validator { return new Validator(); } ); $this->container->register( FaqSchema::class, static function (): FaqSchema { return new FaqSchema(); } ); $this->container->register( SpeakableSchema::class, static function (): SpeakableSchema { return new SpeakableSchema(); } ); $this->container->register( SchemaEngine::class, function (): SchemaEngine { return new SchemaEngine( $this->container->get( ProductSchema::class ), $this->container->get( EntityChain::class ), $this->container->get( ConflictDetector::class ), $this->container->get( FaqSchema::class ), $this->container->get( SpeakableSchema::class ) ); } ); $this->container->register( \AICommerce\Schema\SiteTypeDetector::class, static function (): \AICommerce\Schema\SiteTypeDetector { return new \AICommerce\Schema\SiteTypeDetector(); } ); $this->container->register( \AICommerce\Schema\SiteSchema::class, function (): \AICommerce\Schema\SiteSchema { return new \AICommerce\Schema\SiteSchema( $this->container->get( EntityChain::class ), $this->container->get( ConflictDetector::class ), $this->container->get( \AICommerce\Schema\SiteTypeDetector::class ) ); } ); $this->container->register( \AICommerce\Schema\ArticleSchema::class, static function (): \AICommerce\Schema\ArticleSchema { return new \AICommerce\Schema\ArticleSchema(); } ); $this->container->register( \AICommerce\Schema\WebPageSchema::class, static function (): \AICommerce\Schema\WebPageSchema { return new \AICommerce\Schema\WebPageSchema(); } ); $this->container->register( \AICommerce\Schema\ContentSchemaEngine::class, function (): \AICommerce\Schema\ContentSchemaEngine { return new \AICommerce\Schema\ContentSchemaEngine( $this->container->get( \AICommerce\Schema\ArticleSchema::class ), $this->container->get( \AICommerce\Schema\WebPageSchema::class ) ); } ); // Analytics services. $this->container->register( ReadinessScore::class, function (): ReadinessScore { global $wpdb; return new ReadinessScore( $wpdb, $this->container ); } ); $this->container->register( AttributionTracker::class, static function (): AttributionTracker { global $wpdb; return new AttributionTracker( $wpdb ); } ); // Rate Limiter. $this->container->register( \AICommerce\API\RateLimiter::class, static function (): \AICommerce\API\RateLimiter { return new \AICommerce\API\RateLimiter(); } ); // Encryption service. $this->container->register( Encryption::class, static function (): Encryption { return new Encryption(); } ); // AI provider services. $this->container->register( OpenAIProvider::class, function (): OpenAIProvider { return new OpenAIProvider( $this->container->get( Encryption::class ) ); } ); $this->container->register( AnthropicProvider::class, function (): AnthropicProvider { return new AnthropicProvider( $this->container->get( Encryption::class ) ); } ); $this->container->register( GeminiProvider::class, function (): GeminiProvider { return new GeminiProvider( $this->container->get( Encryption::class ) ); } ); // Content optimizer — uses the configured provider. $this->container->register( ContentOptimizer::class, function (): ContentOptimizer { $provider_slug = get_option( 'aicommerce_ai_provider', 'openai' ); $provider_map = array( 'openai' => OpenAIProvider::class, 'anthropic' => AnthropicProvider::class, 'gemini' => GeminiProvider::class, ); $provider_class = $provider_map[ $provider_slug ] ?? OpenAIProvider::class; return new ContentOptimizer( $this->container->get( $provider_class ), $this->container->get( ProductDataExtractor::class ) ); } ); // Bulk optimization queue. $this->container->register( BulkQueue::class, function (): BulkQueue { global $wpdb; return new BulkQueue( $wpdb, $this->container ); } ); // Image alt text optimizer. $this->container->register( ImageAltTextOptimizer::class, function (): ImageAltTextOptimizer { return new ImageAltTextOptimizer( $this->container->get( Encryption::class ) ); } ); // REST API. $this->container->register( RestController::class, function (): RestController { return new RestController( $this->container->get( ReadinessScore::class ), $this->container->get( SchemaEngine::class ), $this->container->get( Validator::class ), $this->container->get( FeedGenerator::class ), $this->container->get( CrawlerMonitor::class ), $this->container->get( RobotsManager::class ), $this->container->get( ManifestEndpoint::class ), $this->container->get( FeedHealthMonitor::class ), $this->container->get( ContentOptimizer::class ), $this->container, $this->container->get( BulkQueue::class ), $this->container->get( ImageAltTextOptimizer::class ), $this->container->get( AttributionTracker::class ) ); } ); // Admin services. $this->container->register( AdminMenu::class, static function (): AdminMenu { return new AdminMenu(); } ); $this->container->register( MetaBox::class, function (): MetaBox { return new MetaBox( $this->container->get( ReadinessScore::class ) ); } ); $this->container->register( GutenbergPanel::class, static function (): GutenbergPanel { return new GutenbergPanel(); } ); // Feed services. $this->container->register( ChangeDetector::class, static function (): ChangeDetector { global $wpdb; return new ChangeDetector( $wpdb ); } ); $this->container->register( ProductDataExtractor::class, static function (): ProductDataExtractor { return new ProductDataExtractor(); } ); $this->container->register( FeedFileManager::class, static function (): FeedFileManager { return new FeedFileManager(); } ); $this->container->register( FeedHealthMonitor::class, static function (): FeedHealthMonitor { return new FeedHealthMonitor(); } ); $this->container->register( GMCApiClient::class, static function (): GMCApiClient { global $wpdb; return new GMCApiClient( $wpdb ); } ); $this->container->register( FeedGenerator::class, function (): FeedGenerator { global $wpdb; $google_feed = new GoogleFeed(); $openai_feed = new OpenAIFeed(); $generator = new FeedGenerator( $this->container->get( ChangeDetector::class ), $this->container->get( ProductDataExtractor::class ), $this->container->get( FeedFileManager::class ), $this->container->get( FeedHealthMonitor::class ), $wpdb ); $bing_feed = new BingFeed(); $perplexity_feed = new PerplexityFeed(); // Register feed types — persists to wp_options. $generator->register_feed_type( $google_feed ); $generator->register_feed_type( $openai_feed ); $generator->register_feed_type( $bing_feed ); $generator->register_feed_type( $perplexity_feed ); // Register concrete FeedInterface generators. $generator->register_feed_generator( 'google', $google_feed ); $generator->register_feed_generator( 'openai', $openai_feed ); $generator->register_feed_generator( 'bing', $bing_feed ); $generator->register_feed_generator( 'perplexity', $perplexity_feed ); /** * Fires when feed types are being registered. * * Add-ons can register additional feed formats here. * * @param FeedGenerator $generator The feed generator. */ do_action( 'aicommerce_register_feeds', $generator ); return $generator; } ); $this->container->register( ManifestEndpoint::class, function (): ManifestEndpoint { global $wpdb; return new ManifestEndpoint( $this->container->get( FeedFileManager::class ), $wpdb ); } ); // Crawler services. $this->container->register( CrawlerMonitor::class, static function (): CrawlerMonitor { global $wpdb; return new CrawlerMonitor( $wpdb ); } ); $this->container->register( RobotsManager::class, static function (): RobotsManager { return new RobotsManager(); } ); $this->container->register( LlmsTxtGenerator::class, static function (): LlmsTxtGenerator { return new LlmsTxtGenerator(); } ); $this->container->register( BingApiClient::class, static function (): BingApiClient { return new BingApiClient(); } ); // White-label service. $this->container->register( WhiteLabel::class, static function (): WhiteLabel { return new WhiteLabel(); } ); // Weekly digest service. $this->container->register( WeeklyDigest::class, function (): WeeklyDigest { return new WeeklyDigest( $this->container ); } ); // Multilingual integration services. $this->container->register( WPMLIntegration::class, static function (): WPMLIntegration { return new WPMLIntegration(); } ); $this->container->register( PolylangIntegration::class, static function (): PolylangIntegration { return new PolylangIntegration(); } ); // MCP / Agentic services. $this->container->register( AgentAuthenticator::class, static function (): AgentAuthenticator { return new AgentAuthenticator(); } ); $this->container->register( StripeACP::class, function (): StripeACP { return new StripeACP( $this->container->get( Encryption::class ) ); } ); $this->container->register( InventoryReservation::class, static function (): InventoryReservation { return new InventoryReservation(); } ); $this->container->register( FraudDetector::class, static function (): FraudDetector { return new FraudDetector(); } ); $this->container->register( OrderCreator::class, function (): OrderCreator { return new OrderCreator( $this->container->get( InventoryReservation::class ) ); } ); $this->container->register( AgentBridge::class, function (): AgentBridge { return new AgentBridge( $this->container->get( StripeACP::class ), $this->container->get( InventoryReservation::class ), $this->container->get( FraudDetector::class ) ); } ); $this->container->register( MCPServer::class, function (): MCPServer { $auth = $this->container->get( AgentAuthenticator::class ); $server = new MCPServer( $auth ); $tools = new MCPTools( $server ); $tools->set_bridge( $this->container->get( AgentBridge::class ) ); $server->set_tools( $tools ); return $server; } ); // SOA analytics services. $this->container->register( SOATracker::class, function (): SOATracker { return new SOATracker( $this->container ); } ); $this->container->register( CompetitorTracker::class, function (): CompetitorTracker { return new CompetitorTracker( $this->container->get( SOATracker::class ) ); } ); // Hooks service. $this->container->register( Hooks::class, function (): Hooks { return new Hooks( $this->container ); } ); /** * Fires when the service container is being populated. * * Add-ons and integrations can register additional services here. * * @param ServiceContainer $container The plugin service container. */ do_action( 'aicommerce_register_services', $this->container ); } /** * Boot the hooks system. * * @return void */ private function register_hooks(): void { /** @var Hooks $hooks */ $hooks = $this->container->get( Hooks::class ); $hooks->register(); } /** * Get the service container. * * @return ServiceContainer */ public function get_container(): ServiceContainer { return $this->container; } /** * Check if the plugin has been booted. * * @return bool */ public function is_booted(): bool { return $this->booted; } }