← All changes
|
includes/extensions/alt-text-generator/Alt_Text_Generator.php
+26
-22
51.1.76
→
51.1.83
View file →
| @@ -39,9 +39,9 @@ | ||
| 39 | 39 | if (!$button_enabled && !$auto_enabled) { |
| 40 | 40 | return; // Don't initialize if both features are disabled |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - $has_api_key = !empty($options['openai_api_key']); | |
| 43 | + $has_api_key = '' !== \King_Addons\AI_Provider::getApiKey(); | |
| 44 | 44 | |
| 45 | 45 | // Hook for new attachments (only if auto generation is enabled and API key exists). |
| 46 | 46 | if ($auto_enabled && $has_api_key) { |
| 47 | 47 | if(king_addons_freemius()->can_use_premium_code()){ |
| @@ -107,9 +107,9 @@ | ||
| 107 | 107 | ); |
| 108 | 108 | |
| 109 | 109 | // Check if API key exists |
| 110 | 110 | $options = get_option('king_addons_ai_options', []); |
| 111 | - $has_api_key = !empty($options['openai_api_key']); | |
| 111 | + $has_api_key = '' !== \King_Addons\AI_Provider::getApiKey(); | |
| 112 | 112 | |
| 113 | 113 | // Pass data to JavaScript. |
| 114 | 114 | wp_localize_script('king-addons-media-alt-text', 'kingAddonsMediaAltText', array( |
| 115 | 115 | 'ajax_url' => admin_url('admin-ajax.php'), |
| @@ -162,9 +162,9 @@ | ||
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | $alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); |
| 165 | 165 | $options = get_option('king_addons_ai_options', []); |
| 166 | - $has_api_key = !empty($options['openai_api_key']); | |
| 166 | + $has_api_key = '' !== \King_Addons\AI_Provider::getApiKey(); | |
| 167 | 167 | |
| 168 | 168 | echo '<div class="king-addons-alt-text-status" data-attachment-id="' . esc_attr($attachment_id) . '">'; |
| 169 | 169 | if (!empty($alt_text)) { |
| 170 | 170 | echo '<span>' . esc_html($alt_text) . '</span>'; |
| @@ -201,9 +201,9 @@ | ||
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | // Check if API key exists |
| 204 | 204 | $options = get_option('king_addons_ai_options', []); |
| 205 | - if (empty($options['openai_api_key'])) { | |
| 205 | + if ('' === \King_Addons\AI_Provider::getApiKey()) { | |
| 206 | 206 | wp_send_json_error(array( |
| 207 | 207 | 'message' => esc_html__('OpenAI API key is not configured. Please set it in the AI settings.', 'king-addons'), |
| 208 | 208 | 'needs_setup' => true |
| 209 | 209 | ), 400); |
| @@ -248,9 +248,9 @@ | ||
| 248 | 248 | |
| 249 | 249 | /** |
| 250 | 250 | * Adds custom cron intervals. |
| 251 | 251 | * |
| 252 | - * Note: Recommended interval is 60+ seconds to avoid OpenAI API rate limits. | |
| 252 | + * Note: Recommended interval is 60+ seconds to avoid provider API rate limits. | |
| 253 | 253 | * Lower intervals may cause API errors during high usage periods. |
| 254 | 254 | * |
| 255 | 255 | * @param array $schedules Existing cron schedules. |
| 256 | 256 | * @return array Modified schedules. |
| @@ -399,18 +399,22 @@ | ||
| 399 | 399 | // For now, we assume the button won't be shown if alt exists, so this path is mainly for the cron job. |
| 400 | 400 | return $is_ajax ? $existing_alt : true; |
| 401 | 401 | } |
| 402 | 402 | |
| 403 | - // Retrieve OpenAI API key and settings from King Addons AI options. | |
| 403 | + // Retrieve the API key and settings of the selected AI provider. | |
| 404 | 404 | $options = get_option('king_addons_ai_options', []); |
| 405 | - $api_key = $options['openai_api_key'] ?? ''; | |
| 405 | + $api_key = \King_Addons\AI_Provider::getApiKey(); | |
| 406 | 406 | // Use dedicated vision model for image analysis (fallback to text model). |
| 407 | - $model = $options['openai_vision_model'] ?? ($options['openai_model'] ?? 'gpt-4o-mini'); | |
| 407 | + $model = \King_Addons\AI_Provider::getVisionModel(); | |
| 408 | 408 | // Get image detail level from settings (default to 'low') |
| 409 | 409 | $image_detail_level = $options['ai_alt_text_image_detail_level'] ?? 'low'; |
| 410 | 410 | |
| 411 | 411 | if (empty($api_key)) { |
| 412 | - $error_msg = esc_html__('OpenAI API key is missing.', 'king-addons'); | |
| 412 | + $error_msg = sprintf( | |
| 413 | + /* translators: %s: provider name */ | |
| 414 | + esc_html__('%s API key is missing.', 'king-addons'), | |
| 415 | + \King_Addons\AI_Provider::getLabel() | |
| 416 | + ); | |
| 413 | 417 | return $is_ajax ? new \WP_Error('missing_api_key', $error_msg) : $error_msg; |
| 414 | 418 | } |
| 415 | 419 | |
| 416 | 420 | // Get image path instead of URL. |
| @@ -440,10 +444,10 @@ | ||
| 440 | 444 | |
| 441 | 445 | // Create the data URI. |
| 442 | 446 | $image_data_uri = "data:{$mime_type};base64,{$base64_image}"; |
| 443 | 447 | |
| 444 | - // --- OpenAI API Call --- // | |
| 445 | - $api_endpoint = 'https://api.openai.com/v1/chat/completions'; | |
| 448 | + // --- AI provider API call --- // | |
| 449 | + $api_endpoint = \King_Addons\AI_Provider::getChatEndpoint(); | |
| 446 | 450 | |
| 447 | 451 | $options_for_lang = get_option('king_addons_ai_options', []); |
| 448 | 452 | $custom_lang_enabled = !empty($options_for_lang['content_language_custom_enable']); |
| 449 | 453 | $custom_lang = trim($options_for_lang['content_language_custom'] ?? ''); |
| @@ -478,13 +482,10 @@ | ||
| 478 | 482 | 'max_tokens' => 50 // Limit the response length |
| 479 | 483 | ); |
| 480 | 484 | |
| 481 | 485 | $args = array( |
| 482 | - 'headers' => array( | |
| 483 | - 'Authorization' => 'Bearer ' . $api_key, | |
| 484 | - 'Content-Type' => 'application/json', | |
| 485 | - ), | |
| 486 | - 'body' => wp_json_encode($payload), | |
| 486 | + 'headers' => \King_Addons\AI_Provider::getHeaders(), | |
| 487 | + 'body' => wp_json_encode(\King_Addons\AI_Provider::prepareChatPayload($payload)), | |
| 487 | 488 | 'timeout' => 60, // Increased timeout |
| 488 | 489 | 'method' => 'POST', |
| 489 | 490 | 'data_format' => 'body', |
| 490 | 491 | ); |
| @@ -501,15 +502,18 @@ | ||
| 501 | 502 | $response_code = wp_remote_retrieve_response_code($response); |
| 502 | 503 | $response_body = wp_remote_retrieve_body($response); |
| 503 | 504 | $decoded_body = json_decode($response_body, true); |
| 504 | 505 | |
| 505 | - if ($response_code !== 200 || !isset($decoded_body['choices'][0]['message']['content'])) { | |
| 506 | - $api_error_message = isset($decoded_body['error']['message']) ? $decoded_body['error']['message'] : esc_html__('API request failed or returned unexpected data.', 'king-addons'); | |
| 506 | + if ($response_code !== 200 || isset($decoded_body['error'])) { | |
| 507 | + $api_error_message = \King_Addons\AI_Provider::extractErrorMessage($decoded_body, esc_html__('API request failed or returned unexpected data.', 'king-addons')); | |
| 507 | 508 | /* translators: 1: HTTP response code, 2: API error message. */ |
| 508 | 509 | return $is_ajax ? new \WP_Error('api_error', $api_error_message, array('status' => $response_code)) : sprintf(esc_html__('API error (%1$d): %2$s', 'king-addons'), $response_code, $api_error_message); |
| 509 | 510 | } |
| 510 | 511 | |
| 511 | - $generated_alt_text = $decoded_body['choices'][0]['message']['content']; | |
| 512 | + $generated_alt_text = \King_Addons\AI_Provider::extractMessageContent($decoded_body); | |
| 513 | + if (is_wp_error($generated_alt_text)) { | |
| 514 | + return $is_ajax ? $generated_alt_text : $generated_alt_text->get_error_message(); | |
| 515 | + } | |
| 512 | 516 | // --- End API Call --- // |
| 513 | 517 | |
| 514 | 518 | // If the response starts with "I'm sorry" (case-insensitive, allow whitespace before), treat as error and do not save |
| 515 | 519 | if (preg_match('/^\s*I\'m sorry/i', $generated_alt_text)) { |
| @@ -524,18 +528,18 @@ | ||
| 524 | 528 | // Update the image alt text meta data. |
| 525 | 529 | if (!empty($sanitized_alt_text)) { |
| 526 | 530 | if (update_post_meta($attachment_id, '_wp_attachment_image_alt', $sanitized_alt_text)) { |
| 527 | 531 | // Log success for debugging |
| 528 | - error_log("King Addons Alt Text: Successfully generated alt text for attachment {$attachment_id}: {$sanitized_alt_text}"); | |
| 532 | + // error_log("King Addons Alt Text: Successfully generated alt text for attachment {$attachment_id}: {$sanitized_alt_text}"); | |
| 529 | 533 | return $is_ajax ? $sanitized_alt_text : true; |
| 530 | 534 | } else { |
| 531 | 535 | $error_msg = esc_html__('Failed to save the generated alt text.', 'king-addons'); |
| 532 | - error_log("King Addons Alt Text: Failed to save alt text for attachment {$attachment_id}"); | |
| 536 | + // error_log("King Addons Alt Text: Failed to save alt text for attachment {$attachment_id}"); | |
| 533 | 537 | return $is_ajax ? new \WP_Error('update_failed', $error_msg) : $error_msg; |
| 534 | 538 | } |
| 535 | 539 | } else { |
| 536 | 540 | $error_msg = esc_html__('AI returned empty or invalid text.', 'king-addons'); |
| 537 | - error_log("King Addons Alt Text: AI returned empty text for attachment {$attachment_id}"); | |
| 541 | + // error_log("King Addons Alt Text: AI returned empty text for attachment {$attachment_id}"); | |
| 538 | 542 | return $is_ajax ? new \WP_Error('empty_alt_text', $error_msg) : $error_msg; |
| 539 | 543 | } |
| 540 | 544 | |
| 541 | 545 | // Should not be reached in normal flow due to checks above |