register_services(); } /** * Register available AI services. */ private function register_services() { $this->services = array( 'dall_e_3' => array( 'name' => __('DALL-E 3', 'ai-thumbnails-maker'), 'class' => 'AITM_AI_DALL_E', 'description' => __('OpenAI DALL-E 3 image generation service.', 'ai-thumbnails-maker'), 'enabled' => true, ), 'midjourney' => array( 'name' => __('Midjourney', 'ai-thumbnails-maker'), 'class' => 'AITM_AI_Midjourney', 'description' => __('Midjourney image generation service.', 'ai-thumbnails-maker'), 'enabled' => true, ), ); /** * Filter available AI services. * * @param array $services Available services. */ $this->services = apply_filters('aitm_ai_services', $this->services); } /** * Get all available services. * * @return array */ public function get_services() { return $this->services; } /** * Get enabled services only. * * @return array */ public function get_enabled_services() { return array_filter($this->services, function($service) { return $service['enabled']; }); } /** * Get a specific service instance. * * @param string $service_name Service name. * @return AITM_AI_Service_Interface|WP_Error */ public function get_service($service_name) { if (!isset($this->services[$service_name])) { return new WP_Error( 'aitm_service_not_found', // translators: %s is the name of the AI service sprintf(__('AI service "%s" not found.', 'ai-thumbnails-maker'), $service_name) ); } $service_config = $this->services[$service_name]; if (!$service_config['enabled']) { return new WP_Error( 'aitm_service_disabled', // translators: %s is the name of the AI service sprintf(__('AI service "%s" is disabled.', 'ai-thumbnails-maker'), $service_name) ); } if (!class_exists($service_config['class'])) { return new WP_Error( 'aitm_service_class_not_found', // translators: %s is the class name of the AI service sprintf(__('AI service class "%s" not found.', 'ai-thumbnails-maker'), $service_config['class']) ); } $service_instance = new $service_config['class'](); if (!$service_instance instanceof AITM_AI_Service_Interface) { return new WP_Error( 'aitm_service_invalid_interface', // translators: %s is the name of the AI service sprintf(__('AI service "%s" does not implement the required interface.', 'ai-thumbnails-maker'), $service_name) ); } return $service_instance; } /** * Test a specific AI service. * * @param string $service_name Service name. * @return bool|WP_Error */ public function test_service($service_name) { $service = $this->get_service($service_name); if (is_wp_error($service)) { return $service; } return $service->test_connection(); } /** * Get service configuration. * * @param string $service_name Service name. * @return array|WP_Error */ public function get_service_config($service_name) { if (!isset($this->services[$service_name])) { return new WP_Error( 'aitm_service_not_found', // translators: %s is the name of the AI service sprintf(__('AI service "%s" not found.', 'ai-thumbnails-maker'), $service_name) ); } return $this->services[$service_name]; } /** * Enable or disable a service. * * @param string $service_name Service name. * @param bool $enabled Whether to enable the service. * @return bool */ public function set_service_enabled($service_name, $enabled) { if (!isset($this->services[$service_name])) { return false; } $this->services[$service_name]['enabled'] = (bool) $enabled; // Save to options $settings = get_option('aitm_settings', array()); $settings['ai_services'][$service_name]['enabled'] = (bool) $enabled; update_option('aitm_settings', $settings); return true; } /** * Get service settings. * * @param string $service_name Service name. * @return array */ public function get_service_settings($service_name) { $settings = get_option('aitm_settings', array()); return isset($settings['ai_services'][$service_name]) ? $settings['ai_services'][$service_name] : array(); } /** * Save service settings. * * @param string $service_name Service name. * @param array $settings Service settings. * @return bool */ public function save_service_settings($service_name, $settings) { $current_settings = get_option('aitm_settings', array()); $current_settings['ai_services'][$service_name] = $settings; return update_option('aitm_settings', $current_settings); } /** * Generate thumbnail using AI service. * * @param string $service_name Service name. * @param string $prompt Image prompt. * @param int $width Image width. * @param int $height Image height. * @param bool $crop Whether to crop the image. * @return array|WP_Error */ public function generate_thumbnail($service_name, $prompt, $width, $height, $crop = false) { $service = $this->get_service($service_name); if (is_wp_error($service)) { return $service; } return $service->generate_thumbnail($prompt, $width, $height, $crop); } /** * Get service usage statistics. * * @param string $service_name Service name. * @param int $days Number of days to look back. * @return array */ public function get_service_usage($service_name, $days = 30) { global $wpdb; $table_name = $wpdb->prefix . 'aitm_statistics'; $start_date = gmdate('Y-m-d H:i:s', strtotime("-{$days} days")); $cache_key = 'aitm_service_usage_' . $service_name . '_' . $days; $results = wp_cache_get($cache_key, 'aitm_statistics'); if (false === $results) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table query with caching $results = $wpdb->get_results( $wpdb->prepare( "SELECT COUNT(*) as total_requests, SUM(CASE WHEN success = 1 THEN 1 ELSE 0 END) as successful_requests, SUM(CASE WHEN success = 0 THEN 1 ELSE 0 END) as failed_requests FROM %i WHERE generation_method = 'ai' AND ai_service = %s AND created_at >= %s", $table_name, $service_name, $start_date ) ); wp_cache_set($cache_key, $results, 'aitm_statistics', 300); // Cache for 5 minutes } if (empty($results)) { return array( 'total_requests' => 0, 'successful_requests' => 0, 'failed_requests' => 0, 'success_rate' => 0, ); } $stats = $results[0]; $success_rate = $stats->total_requests > 0 ? ($stats->successful_requests / $stats->total_requests) * 100 : 0; return array( 'total_requests' => (int) $stats->total_requests, 'successful_requests' => (int) $stats->successful_requests, 'failed_requests' => (int) $stats->failed_requests, 'success_rate' => round($success_rate, 2), ); } /** * Get all services usage statistics. * * @param int $days Number of days to look back. * @return array */ public function get_all_services_usage($days = 30) { $usage = array(); foreach (array_keys($this->services) as $service_name) { $usage[$service_name] = $this->get_service_usage($service_name, $days); } return $usage; } }