api_key = $api_key; $this->model = $model; $this->timeout = $timeout; } /** * Get Prompt Builder instance * * @since 1.0.0 * * @return Prompt_Builder Prompt Builder instance */ private function get_prompt_builder(): Prompt_Builder { if (!$this->prompt_builder) { // Ensure Prompt Builder is loaded if (!class_exists('ThinkRank\\AI\\Prompt_Builder')) { require_once THINKRANK_PLUGIN_DIR . 'includes/ai/class-prompt-builder.php'; } $this->prompt_builder = new Prompt_Builder(); } return $this->prompt_builder; } /** * Generate completion using OpenAI * * @param string $prompt The prompt to send * @param array $options Additional options * @return array Response data * @throws \Exception If API request fails */ public function generate_completion(string $prompt, array $options = []): array { $default_options = [ 'model' => $this->model, 'max_tokens' => 1000, 'temperature' => 0.7, 'top_p' => 1, 'frequency_penalty' => 0, 'presence_penalty' => 0, ]; $options = array_merge($default_options, $options); $body = $this->build_chat_completion_body($prompt, $options); return $this->make_request('chat/completions', $body); } /** * Build the chat/completions request body for the given (merged) options. * * Extracted so the per-model-family parameter handling is unit-testable: * reasoning models take max_completion_tokens (and only the GPT-5 family * accepts reasoning_effort — o1/o3 reject it), while standard models take * temperature/top_p/penalties/max_tokens. Keeping this in one place stops a * future refactor from silently regressing the GPT-5-only guard (issue #286). * * @param string $prompt User prompt. * @param array $options Merged options (must include model, max_tokens, and * the sampling defaults; reasoning_effort optional). * @return array Request body for the chat/completions endpoint. */ private function build_chat_completion_body(string $prompt, array $options): array { $body = [ 'model' => $options['model'], 'messages' => [ [ 'role' => 'user', 'content' => $prompt, ] ], ]; // Get safe token limit for this model $safe_tokens = $this->get_safe_token_limit($options['model'], $options['max_tokens']); // Add parameters based on model type if ($this->is_reasoning_model($options['model'])) { // Reasoning models (o1/o3) have fixed parameters and restricted support // temperature, top_p, frequency_penalty, presence_penalty are not supported $body['max_completion_tokens'] = $safe_tokens; // GPT-5 models accept reasoning_effort ('minimal'…'high'). Callers // wanting a quick consumer-style answer (e.g. brand-visibility // probes) pass 'minimal' so hidden reasoning can't consume the // whole completion budget and return empty text. Only the GPT-5 // family gets it: o1 rejects the parameter outright. if (isset($options['reasoning_effort']) && str_starts_with($options['model'], 'gpt-5')) { $body['reasoning_effort'] = (string) $options['reasoning_effort']; } } else { // Standard models support all parameters $body['temperature'] = $options['temperature']; $body['top_p'] = $options['top_p']; $body['frequency_penalty'] = $options['frequency_penalty']; $body['presence_penalty'] = $options['presence_penalty']; $body['max_tokens'] = $safe_tokens; } return $body; } /** * Generate SEO metadata * * @param string $content Content to analyze * @param array $options Generation options * @return array Generated metadata * @throws \Exception If generation fails */ public function generate_seo_metadata(string $content, array $options = []): array { $target_keyword = $options['target_keyword'] ?? ''; $content_type = $options['content_type'] ?? 'blog_post'; $tone = $options['tone'] ?? 'professional'; $prompt_builder = $this->get_prompt_builder(); $language = is_string($options['language'] ?? null) ? $options['language'] : ''; $prompt = $prompt_builder->build_seo_prompt($content, $target_keyword, $content_type, $tone, 'openai', $language); $response = $this->generate_completion($prompt, [ 'max_tokens' => $this->get_recommended_tokens('seo_metadata'), 'temperature' => 0.3, // Lower temperature for more consistent SEO output ]); return $this->parse_seo_response($response); } /** * Analyze content for SEO optimization * * @param string $content Content to analyze * @param array $metadata Existing metadata * @return array Analysis results * @throws \Exception If analysis fails */ public function analyze_content(string $content, array $metadata = []): array { $prompt_builder = $this->get_prompt_builder(); $prompt = $prompt_builder->build_analysis_prompt($content, $metadata, 'openai'); $response = $this->generate_completion($prompt, [ 'max_tokens' => $this->get_recommended_tokens('analysis'), 'temperature' => 0.3, // Lower temperature for more consistent analysis ]); return $this->parse_analysis_response($response); } private function is_reasoning_model(string $model): bool { // Models that require max_completion_tokens and restrict parameters (no temperature/top_p) // Includes OpenAI o1/o3 series and GPT-5 family $reasoning_models = [ 'o1-preview', 'o1-mini', 'o3-mini', 'o3-2024-12-17', 'gpt-5', 'gpt-5-mini', 'gpt-5-nano', ]; // Check for exact matches or model prefixes foreach ($reasoning_models as $reasoning_model) { if ($model === $reasoning_model || strpos($model, $reasoning_model) === 0) { return true; } } return false; } /** * Get maximum completion tokens for a model * * @param string $model Model name * @return int Maximum completion tokens */ private function get_max_completion_tokens(string $model): int { // Model-specific token limits (completion tokens, not context) $token_limits = [ // GPT-5 series (optimized for reasoning + content tokens based on usage data) 'gpt-5' => 20480, // 20K tokens for full GPT-5 'gpt-5-mini' => 15360, // 15K tokens for mini variant (increased) 'gpt-5-nano' => 12288, // 12K tokens for nano (increased from 10K for better buffer) // GPT-4o series 'gpt-4o' => 4096, 'gpt-4o-2024-08-06' => 4096, 'gpt-4o-2024-05-13' => 4096, 'gpt-4o-mini' => 16384, 'gpt-4o-mini-2024-07-18' => 16384, // o1/o3 reasoning models (higher limits) 'o1-preview' => 32768, 'o1-mini' => 65536, 'o3-mini' => 65536, 'o3-2024-12-17' => 65536, ]; // Check for exact match first if (isset($token_limits[$model])) { return $token_limits[$model]; } // Check for partial matches (for versioned models) foreach ($token_limits as $known_model => $limit) { if (strpos($model, $known_model) === 0) { return $limit; } } // Default fallback for unknown models return 4096; } /** * Get safe token limit for a request * * @param string $model Model name * @param int $requested_tokens Requested token count * @return int Safe token count (capped at model limit) */ public function get_safe_token_limit(string $model, int $requested_tokens): int { $max_tokens = $this->get_max_completion_tokens($model); return min($requested_tokens, $max_tokens); } /** * Get recommended token limit for specific use cases * * @param string $use_case Use case (e.g., 'content_brief', 'seo_metadata', 'analysis') * @return int Recommended token limit */ public function get_recommended_tokens(string $use_case): int { $max_tokens = $this->get_max_completion_tokens($this->model); // For reasoning models (GPT-5, o1, o3), we need much higher token limits // because they use reasoning tokens + content tokens if ($this->is_reasoning_model($this->model)) { $reasoning_recommendations = [ 'content_brief' => 0.95, // 95% of max tokens (reasoning + content) 'seo_metadata' => 0.8, // 80% for reasoning models (safe buffer) 'analysis' => 0.85, // 85% for reasoning models (safe buffer) 'llms_txt' => 0.8, // 80% for reasoning models (safe buffer) 'optimization' => 0.9, // 90% for reasoning models (based on 5.9K usage + 50% buffer) ]; $percentage = $reasoning_recommendations[$use_case] ?? 0.3; } else { // Standard models - original percentages $standard_recommendations = [ 'content_brief' => 0.9, // 90% of max tokens for comprehensive briefs 'seo_metadata' => 0.15, // 15% of max tokens for metadata 'analysis' => 0.25, // 25% of max tokens for analysis 'llms_txt' => 0.5, // 50% of max tokens for llms.txt 'optimization' => 0.15, // 15% of max tokens for optimization ]; $percentage = $standard_recommendations[$use_case] ?? 0.15; } return (int) ($max_tokens * $percentage); } /** * Build request body for chat completions with model-specific parameters * * @param string $user_prompt User prompt * @param string|null $system_prompt System prompt (ignored for reasoning models) * @param int $max_tokens Maximum tokens * @param float $temperature Temperature (ignored for reasoning models) * @return array Request body */ private function build_chat_request(string $user_prompt, ?string $system_prompt = null, int $max_tokens = 600, float $temperature = 0.4): array { $body = [ 'model' => $this->model, ]; // Get safe token limit for this model $safe_tokens = $this->get_safe_token_limit($this->model, $max_tokens); // Build messages based on model type if ($this->is_reasoning_model($this->model)) { // Reasoning/GPT‑5 family: use max_completion_tokens and omit temperature $content = $user_prompt; // Some GPT‑5 responses may expect array content parts; we'll send string $body['messages'] = [ [ 'role' => 'user', 'content' => $content ] ]; $body['max_completion_tokens'] = $safe_tokens; } else { // Standard models support system messages and temperature $messages = []; if ($system_prompt) { $messages[] = [ 'role' => 'system', 'content' => $system_prompt ]; } $messages[] = [ 'role' => 'user', 'content' => $user_prompt ]; $body['messages'] = $messages; $body['temperature'] = $temperature; $body['max_tokens'] = $safe_tokens; } return $body; } /** * Get current model * * @return string Current model name */ public function get_model(): string { return $this->model; } /** * Test API connection * * @return bool True if connection successful */ public function test_connection(): bool { try { $response = $this->make_request('models'); return isset($response['data']) && is_array($response['data']); } catch (\Exception $e) { return false; } } /** * Make API request to OpenAI * * @param string $endpoint API endpoint * @param array $body Request body * @return array Response data * @throws \Exception If request fails */ private function make_request(string $endpoint, array $body = []): array { $url = self::API_BASE_URL . '/' . ltrim($endpoint, '/'); $args = [ 'timeout' => $this->timeout, 'headers' => [ 'Authorization' => 'Bearer ' . $this->api_key, 'Content-Type' => 'application/json', 'User-Agent' => 'ThinkRank/' . THINKRANK_VERSION, ], ]; if (!empty($body)) { $args['method'] = 'POST'; $args['body'] = wp_json_encode($body); } $response = $this->request_with_retry($url, $args); if (is_wp_error($response)) { throw new \Exception('API request failed: ' . esc_html($response->get_error_message())); } $status_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); if ($status_code >= 400) { $error_data = json_decode($response_body, true); $error_message = $error_data['error']['message'] ?? 'Unknown API error'; throw new \Exception(sprintf('OpenAI API error (%d): %s', (int) $status_code, esc_html($error_message))); } $data = json_decode($response_body, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception('Invalid JSON response from OpenAI API'); } // A valid-but-scalar body (null/number/string from a proxy/gateway on a // 2xx) would violate this method's : array return type; reject it here so // it surfaces as a catchable \Exception, not an uncatchable TypeError. if (!is_array($data)) { throw new \Exception('Unexpected non-array response from OpenAI API'); } return $data; } /** * Perform an HTTP request, retrying transient failures (429 / 5xx / network) * per the plugin's retry settings, honoring a Retry-After header when given. * * @param string $url Request URL * @param array $args wp_remote_request arguments * @return array|\WP_Error Final response (or last error after retries) */ private function request_with_retry(string $url, array $args) { $settings = \ThinkRank\Core\Settings::instance(); $retry_enabled = (bool) $settings->get('retry_failed_requests', true); $max_attempts = $retry_enabled ? max(1, (int) $settings->get('retry_attempts', 3)) : 1; $response = null; for ($attempt = 1; $attempt <= $max_attempts; $attempt++) { // Keep PHP alive for the whole blocking call (see method docblock). $this->raise_request_time_limit(); $response = wp_remote_request($url, $args); $is_transient = false; $retry_after = 0; if (is_wp_error($response)) { // A client-side timeout means the work genuinely needs longer // than the budget we allowed; re-running the identical prompt, // model and budget just times out again and multiplies the // wait (issue #288). Do not retry a timeout. Other WP_Error // results — DNS, connection refused, TLS — stay retryable. $is_transient = !$this->is_timeout_error($response); } else { $status = wp_remote_retrieve_response_code($response); if (429 === $status || $status >= 500) { $is_transient = true; $retry_after = (int) wp_remote_retrieve_header($response, 'retry-after'); } } if (!$is_transient || $attempt === $max_attempts) { break; } // Honor Retry-After, else exponential backoff (1s, 2s, 4s…), capped. $delay = $retry_after > 0 ? min($retry_after, 30) : min(2 ** ($attempt - 1), 8); sleep($delay); } return $response; } /** * Give PHP enough execution time to outlive a blocking AI HTTP request. * * The provider call blocks for up to $this->timeout seconds, but the web * SAPI's default max_execution_time (commonly 30s) is shorter — so PHP * fatally terminates the script mid-request (inside the cURL transport), * which the web server surfaces as a 502 Bad Gateway. Resetting the limit * before each attempt keeps the script alive for the full call; PHP-FPM's * request_terminate_timeout still caps the absolute maximum. No-op when * set_time_limit() is disabled (e.g. via disable_functions or safe mode). * * @return void */ private function raise_request_time_limit(): void { if (function_exists('set_time_limit')) { // Cover the request timeout plus a small buffer for connection // setup and response handling. @set_time_limit($this->timeout + 45); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- set_time_limit() warns when disabled by host policy; the guard is intentional. } } /** * Parse SEO response from OpenAI * * @param array $response OpenAI response * @return array Parsed metadata * @throws \Exception If parsing fails */ private function parse_seo_response(array $response): array { if (!isset($response['choices'][0]['message']['content'])) { throw new \Exception('Invalid response format from OpenAI'); } $content = $response['choices'][0]['message']['content']; $ai_text = $content; // Store the raw AI-generated text (Content Brief pattern) // Try to extract JSON from the response $json_start = strpos($content, '{'); $json_end = strrpos($content, '}'); if (false === $json_start || false === $json_end) { throw new \Exception('No valid JSON found in OpenAI response'); } $json_content = substr($content, $json_start, $json_end - $json_start + 1); $metadata = json_decode($json_content, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception('Failed to parse JSON from OpenAI response'); } // Validate required fields $required_fields = ['title', 'description', 'focus_keyword']; foreach ($required_fields as $field) { if (!isset($metadata[$field])) { throw new \Exception(sprintf('Missing required field: %s', esc_html($field))); } } return [ 'title' => sanitize_text_field($metadata['title']), 'description' => sanitize_text_field($metadata['description']), 'focus_keyword' => sanitize_text_field($metadata['focus_keyword']), 'suggestions' => array_map('sanitize_text_field', $metadata['suggestions'] ?? []), 'generated_at' => current_time('mysql'), 'tokens_used' => $response['usage']['total_tokens'] ?? 0, '_ai_text' => $ai_text, // Store the raw AI-generated text (Content Brief pattern) ]; } /** * Parse analysis response from OpenAI * * @param array $response OpenAI API response * @return array Parsed analysis data * @throws \Exception If parsing fails */ private function parse_analysis_response(array $response): array { if (!isset($response['choices'][0]['message']['content'])) { throw new \Exception('Invalid response format from OpenAI'); } $content = trim($response['choices'][0]['message']['content']); $ai_text = $content; // Store the raw AI-generated text (Content Brief pattern) // Extract JSON from response $json_start = strpos($content, '{'); $json_end = strrpos($content, '}'); if (false === $json_start || false === $json_end) { throw new \Exception('No valid JSON found in response'); } $json_content = substr($content, $json_start, $json_end - $json_start + 1); $analysis = json_decode($json_content, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception('Failed to parse JSON response: ' . esc_html(json_last_error_msg())); } // Validate and sanitize response return [ 'seo_score' => min(100, max(0, (int) ($analysis['seo_score'] ?? 0))), 'content_analysis' => [ 'word_count' => (int) ($analysis['content_analysis']['word_count'] ?? 0), 'readability' => sanitize_text_field($analysis['content_analysis']['readability'] ?? 'unknown'), 'keyword_density' => sanitize_text_field($analysis['content_analysis']['keyword_density'] ?? 'unknown'), 'structure' => sanitize_text_field($analysis['content_analysis']['structure'] ?? 'unknown'), ], 'suggestions' => array_map('sanitize_text_field', $analysis['suggestions'] ?? []), 'strengths' => array_map('sanitize_text_field', $analysis['strengths'] ?? []), 'weaknesses' => array_map('sanitize_text_field', $analysis['weaknesses'] ?? []), 'analyzed_at' => current_time('mysql'), 'tokens_used' => $response['usage']['total_tokens'] ?? 0, '_ai_text' => $ai_text, // Store the raw AI-generated text (Content Brief pattern) ]; } /** * Optimize site identity using OpenAI * * @since 1.0.0 * * @param array $site_data Site data to optimize * @param array $options Optimization options * @return array Optimization results * @throws \Exception If optimization fails */ public function optimize_site_identity(array $site_data, array $options = []): array { $business_type = $options['business_type'] ?? 'website'; $target_audience = $options['target_audience'] ?? 'general'; $tone = $options['tone'] ?? 'professional'; $prompt_builder = $this->get_prompt_builder(); $prompt = $prompt_builder->build_site_identity_prompt($site_data, $business_type, $target_audience, $tone, 'openai'); $body = $this->build_chat_request( $prompt, 'You are an expert SEO consultant specializing in site identity optimization. Provide actionable, specific recommendations in JSON format.', $this->get_recommended_tokens('optimization'), 0.4 ); $response = $this->make_request('chat/completions', $body); return $this->parse_site_identity_response($response); } /** * Parse site identity optimization response * * @param array $response OpenAI API response * @return array Parsed optimization data * @throws \Exception If parsing fails */ private function parse_site_identity_response(array $response): array { if (!isset($response['choices'][0]['message']['content'])) { throw new \Exception('Invalid response format from OpenAI'); } $content = trim($response['choices'][0]['message']['content']); $ai_text = $content; // Store the raw AI-generated text (Content Brief pattern) // Extract JSON from response $json_start = strpos($content, '{'); $json_end = strrpos($content, '}'); if (false === $json_start || false === $json_end) { throw new \Exception('No valid JSON found in response'); } $json_content = substr($content, $json_start, $json_end - $json_start + 1); $optimization = json_decode($json_content, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception('Failed to parse JSON response: ' . esc_html(json_last_error_msg())); } // Validate and sanitize response return [ 'optimized_data' => [ 'site_name' => sanitize_text_field($optimization['optimized_data']['site_name'] ?? ''), 'site_description' => sanitize_text_field($optimization['optimized_data']['site_description'] ?? ''), 'tagline' => sanitize_text_field($optimization['optimized_data']['tagline'] ?? ''), 'default_meta_description' => sanitize_text_field($optimization['optimized_data']['default_meta_description'] ?? ''), ], 'analysis' => sanitize_textarea_field($optimization['analysis'] ?? ''), 'suggestions' => array_map('sanitize_text_field', $optimization['suggestions'] ?? []), 'score' => min(100, max(0, (int) ($optimization['score'] ?? 0))), 'tokens_used' => $response['usage']['total_tokens'] ?? 0, '_ai_text' => $ai_text, // Store the raw AI-generated text (Content Brief pattern) ]; } /** * Optimize homepage meta content using AI (copying Site Identity pattern exactly) * * @since 1.0.0 * * @param array $content_data Meta content data to optimize * @param array $options Optimization options * @return array Optimization results * @throws \Exception If optimization fails */ public function optimize_homepage_meta(array $content_data, array $options = []): array { $business_type = $options['business_type'] ?? 'website'; $target_audience = $options['target_audience'] ?? 'general'; $tone = $options['tone'] ?? 'professional'; $context = $options['context'] ?? []; $prompt_builder = $this->get_prompt_builder(); $prompt = $prompt_builder->build_homepage_meta_prompt($content_data, $business_type, $target_audience, $tone, $context, 'openai'); $body = $this->build_chat_request( $prompt, 'You are an expert SEO consultant specializing in homepage meta optimization. Provide actionable, specific recommendations in JSON format.', $this->get_recommended_tokens('optimization'), 0.4 ); $response = $this->make_request('chat/completions', $body); return $this->parse_homepage_meta_response($response); } /** * Optimize homepage hero content using AI (copying Site Identity pattern exactly) * * @since 1.0.0 * * @param array $hero_data Hero content data to optimize * @param array $options Optimization options * @return array Optimization results * @throws \Exception If optimization fails */ public function optimize_homepage_hero(array $hero_data, array $options = []): array { $business_type = $options['business_type'] ?? 'website'; $target_audience = $options['target_audience'] ?? 'general'; $tone = $options['tone'] ?? 'professional'; $context = $options['context'] ?? []; $prompt_builder = $this->get_prompt_builder(); $prompt = $prompt_builder->build_homepage_hero_prompt($hero_data, $business_type, $target_audience, $tone, $context, 'openai'); $body = $this->build_chat_request( $prompt, 'You are an expert conversion optimization specialist specializing in homepage hero sections. Provide actionable, specific recommendations in JSON format.', $this->get_recommended_tokens('optimization'), 0.4 ); $response = $this->make_request('chat/completions', $body); return $this->parse_homepage_hero_response($response); } /** * Optimize LLMs.txt content using OpenAI * * @since 1.0.0 * * @param array $website_data Website data to optimize * @param array $options Optimization options * @return array Optimization results * @throws \Exception If optimization fails */ public function optimize_llms_txt(array $website_data, array $options = []): array { // Use shared prompt builder for consistent prompts across all AI providers $prompt_builder = $this->get_prompt_builder(); $prompt = $prompt_builder->build_llms_txt_prompt($website_data, $options, 'openai'); $body = $this->build_chat_request( $prompt, 'You are an expert technical writer specializing in creating llms.txt files for AI assistants. Provide structured, comprehensive content in JSON format.', $this->get_recommended_tokens('llms_txt'), 0.4 ); $response = $this->make_request('chat/completions', $body); return $this->parse_llms_txt_response($response); } /** * Parse LLMs.txt optimization response * * @param array $response OpenAI API response * @return array Parsed optimization data * @throws \Exception If parsing fails */ private function parse_llms_txt_response(array $response): array { if (!isset($response['choices'][0]['message']['content'])) { throw new \Exception('Invalid response format from OpenAI'); } $content = trim($response['choices'][0]['message']['content']); $ai_text = $content; // Store the raw AI-generated text (Content Brief pattern) // Extract JSON from response $json_start = strpos($content, '{'); $json_end = strrpos($content, '}'); if (false === $json_start || false === $json_end) { throw new \Exception('No valid JSON found in response'); } $json_content = substr($content, $json_start, $json_end - $json_start + 1); $optimization = json_decode($json_content, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception('Invalid JSON in response: ' . esc_html(json_last_error_msg())); } // Validate and sanitize response return [ 'optimized_data' => [ 'site_name' => sanitize_text_field($optimization['optimized_data']['site_name'] ?? ''), 'project_overview' => sanitize_textarea_field($optimization['optimized_data']['project_overview'] ?? ''), 'key_features' => sanitize_textarea_field($optimization['optimized_data']['key_features'] ?? ''), 'architecture' => sanitize_textarea_field($optimization['optimized_data']['architecture'] ?? ''), 'development_guidelines' => sanitize_textarea_field($optimization['optimized_data']['development_guidelines'] ?? ''), 'ai_context' => sanitize_textarea_field($optimization['optimized_data']['ai_context'] ?? ''), ], 'suggestions' => array_map('sanitize_text_field', $optimization['suggestions'] ?? []), 'tokens_used' => $response['usage']['total_tokens'] ?? 0, '_ai_text' => $ai_text, // Store the raw AI-generated text (Content Brief pattern) ]; } /** * Parse homepage meta optimization response * * @param array $response OpenAI API response * @return array Parsed optimization data * @throws \Exception If parsing fails */ private function parse_homepage_meta_response(array $response): array { if (!isset($response['choices'][0]['message']['content'])) { throw new \Exception('Invalid response format from OpenAI'); } $content = trim($response['choices'][0]['message']['content']); $ai_text = $content; // Store the raw AI-generated text (Content Brief pattern) // Extract JSON from response $json_start = strpos($content, '{'); $json_end = strrpos($content, '}'); if (false === $json_start || false === $json_end) { throw new \Exception('No valid JSON found in response'); } $json_content = substr($content, $json_start, $json_end - $json_start + 1); $optimization = json_decode($json_content, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception('Failed to parse JSON response: ' . esc_html(json_last_error_msg())); } // Validate and sanitize response return [ 'optimized_data' => [ 'title' => sanitize_text_field($optimization['optimized_data']['title'] ?? ''), 'meta_description' => sanitize_text_field($optimization['optimized_data']['meta_description'] ?? ''), ], 'analysis' => sanitize_textarea_field($optimization['analysis'] ?? ''), 'suggestions' => array_map('sanitize_text_field', $optimization['suggestions'] ?? []), 'score' => min(100, max(0, (int) ($optimization['score'] ?? 0))), 'tokens_used' => $response['usage']['total_tokens'] ?? 0, '_ai_text' => $ai_text, // Store the raw AI-generated text (Content Brief pattern) ]; } /** * Parse homepage hero optimization response * * @param array $response OpenAI API response * @return array Parsed optimization data * @throws \Exception If parsing fails */ private function parse_homepage_hero_response(array $response): array { if (!isset($response['choices'][0]['message']['content'])) { throw new \Exception('Invalid response format from OpenAI'); } $content = trim($response['choices'][0]['message']['content']); $ai_text = $content; // Store the raw AI-generated text (Content Brief pattern) // Extract JSON from response $json_start = strpos($content, '{'); $json_end = strrpos($content, '}'); if (false === $json_start || false === $json_end) { throw new \Exception('No valid JSON found in response'); } $json_content = substr($content, $json_start, $json_end - $json_start + 1); $optimization = json_decode($json_content, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception('Failed to parse JSON response: ' . esc_html(json_last_error_msg())); } // Validate and sanitize response return [ 'optimized_data' => [ 'hero_title' => sanitize_text_field($optimization['optimized_data']['hero_title'] ?? ''), 'hero_subtitle' => sanitize_text_field($optimization['optimized_data']['hero_subtitle'] ?? ''), 'hero_cta_text' => sanitize_text_field($optimization['optimized_data']['hero_cta_text'] ?? '') ], 'analysis' => sanitize_textarea_field($optimization['analysis'] ?? ''), 'suggestions' => array_map('sanitize_text_field', $optimization['suggestions'] ?? []), 'score' => min(100, max(0, (int) ($optimization['score'] ?? 0))), 'tokens_used' => $response['usage']['total_tokens'] ?? 0, '_ai_text' => $ai_text, // Store the raw AI-generated text (Content Brief pattern) ]; } }