init(); } /** * Initialize AI provider functionality */ public function init() { // Admin hooks if (is_admin()) { add_action('admin_post_update_ai_provider_settings', array($this, 'handle_form_submission')); add_action('wp_ajax_tigerstyle_test_api_key', array($this, 'ajax_test_api_key')); add_action('wp_ajax_tigerstyle_refresh_models', array($this, 'ajax_refresh_models')); add_action('wp_ajax_tigerstyle_delete_api_key', array($this, 'ajax_delete_api_key')); add_action('wp_ajax_tigerstyle_ai_chat', array($this, 'ajax_ai_chat')); } } /** * Get all configured API providers */ public function get_api_providers() { return get_option('tigerstyle_ai_providers', array()); } /** * Get API provider by name */ public function get_api_provider($name) { $providers = $this->get_api_providers(); return isset($providers[$name]) ? $providers[$name] : null; } /** * Add or update API provider */ public function save_api_provider($name, $config) { $providers = $this->get_api_providers(); // Encrypt API key $config['api_key'] = $this->encrypt_api_key($config['api_key']); $config['created_at'] = time(); $config['last_tested'] = null; $config['test_status'] = 'pending'; $config['models'] = array(); $providers[$name] = $config; update_option('tigerstyle_ai_providers', $providers); return true; } /** * Delete API provider */ public function delete_api_provider($name) { $providers = $this->get_api_providers(); if (isset($providers[$name])) { unset($providers[$name]); update_option('tigerstyle_ai_providers', $providers); return true; } return false; } /** * Encrypt API key for secure storage */ private function encrypt_api_key($api_key) { if (!function_exists('openssl_encrypt')) { // Fallback to base64 encoding if OpenSSL not available return base64_encode($api_key); } $encryption_key = $this->get_encryption_key(); $iv = openssl_random_pseudo_bytes(16); $encrypted = openssl_encrypt($api_key, 'AES-256-CBC', $encryption_key, 0, $iv); return base64_encode($iv . $encrypted); } /** * Mask API key for display (show only first and last 4 characters) */ private function mask_api_key($api_key) { if (strlen($api_key) <= 8) { // If key is too short, show first 2 and last 2 characters return substr($api_key, 0, 2) . str_repeat('*', max(4, strlen($api_key) - 4)) . substr($api_key, -2); } // Show first 4 and last 4 characters with asterisks in between return substr($api_key, 0, 4) . str_repeat('*', max(8, strlen($api_key) - 8)) . substr($api_key, -4); } /** * Get masked API key for display purposes */ public function get_masked_api_key($name) { $provider = $this->get_api_provider($name); if (!$provider) { return null; } $decrypted_key = $this->decrypt_api_key($provider['api_key']); return $this->mask_api_key($decrypted_key); } /** * Decrypt API key */ private function decrypt_api_key($encrypted_key) { if (!function_exists('openssl_decrypt')) { // Fallback from base64 encoding return base64_decode($encrypted_key); } $encryption_key = $this->get_encryption_key(); $data = base64_decode($encrypted_key); $iv = substr($data, 0, 16); $encrypted = substr($data, 16); return openssl_decrypt($encrypted, 'AES-256-CBC', $encryption_key, 0, $iv); } /** * Get encryption key for API keys */ private function get_encryption_key() { $key = get_option('tigerstyle_ai_encryption_key'); if (!$key) { $key = wp_generate_password(32, false); update_option('tigerstyle_ai_encryption_key', $key); } return $key; } /** * Test API key by listing models */ public function test_api_key($name) { $provider = $this->get_api_provider($name); if (!$provider) { return array('success' => false, 'error' => 'Provider not found'); } $api_key = $this->decrypt_api_key($provider['api_key']); $base_url = $provider['base_url']; // Test by listing models $response = wp_remote_get($base_url . '/models', array( 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', 'User-Agent' => 'TigerStyle-SEO/' . TIGERSTYLE_HEAT_VERSION ), 'timeout' => 30 )); if (is_wp_error($response)) { return array( 'success' => false, 'error' => $response->get_error_message() ); } $status_code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); if ($status_code !== 200) { return array( 'success' => false, 'error' => 'HTTP ' . $status_code . ': ' . $body ); } $data = json_decode($body, true); if (!$data || !isset($data['data'])) { return array( 'success' => false, 'error' => 'Invalid response format' ); } // Update provider with test results $providers = $this->get_api_providers(); $providers[$name]['last_tested'] = time(); $providers[$name]['test_status'] = 'success'; $providers[$name]['models'] = $this->process_models($data['data']); update_option('tigerstyle_ai_providers', $providers); return array( 'success' => true, 'models' => $providers[$name]['models'], 'message' => 'API key tested successfully! Found ' . count($providers[$name]['models']) . ' models.' ); } /** * Process models from API response */ private function process_models($models_data) { $models = array(); foreach ($models_data as $model) { if (isset($model['id'])) { $models[] = array( 'id' => $model['id'], 'object' => $model['object'] ?? 'model', 'created' => $model['created'] ?? time(), 'owned_by' => $model['owned_by'] ?? 'unknown', 'enabled' => true // Default to enabled ); } } return $models; } /** * Get OpenAI-compatible client for a provider */ public function get_client($provider_name, $model_id = null) { $provider = $this->get_api_provider($provider_name); if (!$provider) { throw new Exception('Provider not found: ' . $provider_name); } if ($model_id && !$this->is_model_enabled($provider_name, $model_id)) { throw new Exception('Model not enabled: ' . $model_id); } $api_key = $this->decrypt_api_key($provider['api_key']); return new TigerStyleSEO_AI_Client($provider['base_url'], $api_key, $model_id); } /** * Check if model is enabled for provider */ public function is_model_enabled($provider_name, $model_id) { $provider = $this->get_api_provider($provider_name); if (!$provider || !isset($provider['models'])) { return false; } foreach ($provider['models'] as $model) { if ($model['id'] === $model_id) { return $model['enabled'] ?? true; } } return false; } /** * Toggle model enabled/disabled status */ public function toggle_model($provider_name, $model_id, $enabled) { $providers = $this->get_api_providers(); if (!isset($providers[$provider_name])) { return false; } foreach ($providers[$provider_name]['models'] as &$model) { if ($model['id'] === $model_id) { $model['enabled'] = $enabled; break; } } update_option('tigerstyle_ai_providers', $providers); return true; } /** * Get enabled models for provider */ public function get_enabled_models($provider_name) { $provider = $this->get_api_provider($provider_name); if (!$provider || !isset($provider['models'])) { return array(); } return array_filter($provider['models'], function($model) { return $model['enabled'] ?? true; }); } /** * AJAX handler for testing API key */ public function ajax_test_api_key() { check_ajax_referer('tigerstyle_ai_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_die('Unauthorized access'); } $provider_name = sanitize_text_field($_POST['provider_name']); $result = $this->test_api_key($provider_name); wp_send_json($result); } /** * AJAX handler for refreshing models */ public function ajax_refresh_models() { check_ajax_referer('tigerstyle_ai_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_die('Unauthorized access'); } $provider_name = sanitize_text_field($_POST['provider_name']); $result = $this->test_api_key($provider_name); // Refresh models by re-testing wp_send_json($result); } /** * AJAX handler for deleting API key */ public function ajax_delete_api_key() { check_ajax_referer('tigerstyle_ai_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_die('Unauthorized access'); } $provider_name = sanitize_text_field($_POST['provider_name']); $success = $this->delete_api_provider($provider_name); wp_send_json(array( 'success' => $success, 'message' => $success ? 'Provider deleted successfully!' : 'Failed to delete provider.' )); } /** * Handle form submission */ public function handle_form_submission() { // Verify nonce if (!wp_verify_nonce($_POST['ai_provider_nonce'], 'update_ai_provider_settings')) { wp_die('Nonce verification failed'); } // Check user permissions if (!current_user_can('manage_options')) { wp_die('Insufficient permissions'); } $action = sanitize_text_field($_POST['ai_action'] ?? ''); if ($action === 'add_provider') { $name = sanitize_text_field($_POST['provider_name']); $api_key = sanitize_text_field($_POST['api_key']); $base_url = esc_url_raw($_POST['base_url']); $description = sanitize_textarea_field($_POST['description'] ?? ''); if (empty($name) || empty($api_key) || empty($base_url)) { wp_redirect(add_query_arg(array( 'page' => 'tigerstyle-heat', 'tab' => 'ai-provider', 'message' => 'error' ), admin_url('admin.php'))); exit; } $config = array( 'base_url' => rtrim($base_url, '/'), 'api_key' => $api_key, 'description' => $description, 'provider_type' => 'openai' // Default to OpenAI compatible ); $this->save_api_provider($name, $config); wp_redirect(add_query_arg(array( 'page' => 'tigerstyle-heat', 'tab' => 'ai-provider', 'message' => 'provider_added' ), admin_url('admin.php'))); } elseif ($action === 'toggle_model') { $provider_name = sanitize_text_field($_POST['provider_name']); $model_id = sanitize_text_field($_POST['model_id']); $enabled = isset($_POST['enabled']) ? 1 : 0; $this->toggle_model($provider_name, $model_id, $enabled); wp_redirect(add_query_arg(array( 'page' => 'tigerstyle-heat', 'tab' => 'ai-provider', 'message' => 'model_updated' ), admin_url('admin.php'))); } exit; } /** * Handle AI chat AJAX requests */ public function ajax_ai_chat() { // Verify nonce if (!check_ajax_referer('tigerstyle_ai_nonce', 'nonce', false)) { wp_send_json_error('Invalid nonce'); return; } $provider_name = sanitize_text_field($_POST['provider_name'] ?? ''); $model_id = sanitize_text_field($_POST['model_id'] ?? ''); $message = sanitize_textarea_field($_POST['message'] ?? ''); if (empty($provider_name) || empty($model_id) || empty($message)) { wp_send_json_error('Missing required parameters'); return; } try { // Get AI client $client = $this->get_client($provider_name, $model_id); // Create SEO-focused system prompt $system_prompt = "You are an expert SEO consultant and web optimization specialist. " . "Provide helpful, accurate advice about search engine optimization, content strategy, " . "meta tags, website performance, and digital marketing best practices. " . "Keep responses concise but informative. Focus on actionable advice."; // Send chat request $response = $client->simple_chat($message, $system_prompt, $model_id); $ai_response = $client->extract_text($response); wp_send_json_success(array( 'response' => wp_kses_post($ai_response) )); } catch (Exception $e) { wp_send_json_error('AI Error: ' . $e->getMessage()); } } /** * Render admin page */ public function render_admin_page() { $providers = $this->get_api_providers(); ?>

$config): ?>

get_masked_api_key($name)); ?>



available


// Get client for a specific provider and model
$client = tigerstyle_heat()->get_module('ai_provider')->get_client('openai-main', 'gpt-4');

// Generate meta description
$response = $client->chat_completion([
    'messages' => [
        ['role' => 'user', 'content' => 'Generate an SEO meta description for: ' . $post_title]
    ],
    'max_tokens' => 160
]);

// Use with any enabled model
$providers = tigerstyle_heat()->get_module('ai_provider')->get_api_providers();
foreach ($providers as $name => $config) {
    $enabled_models = tigerstyle_heat()->get_module('ai_provider')->get_enabled_models($name);
    // Use the models...
}

TigerStyle Life9 plugin for enhanced security and modularity.', 'tigerstyle-heat'); ?>

but servers don\'t!"', 'tigerstyle-heat'); ?>


📦

TigerStyle Dash plugin for specialized speed optimization.', 'tigerstyle-heat'); ?>

with lightning speed!"', 'tigerstyle-heat'); ?>


🏃‍♀️

TigerStyle Scent plugin for secure access control.', 'tigerstyle-heat'); ?>


👃

👃