| @@ -88,18 +88,10 @@ | ||
| 88 | 88 | // $this->get( $this->endpoint . '/calculate-credit', [ $this, 'calculate_credit' ] ); |
| 89 | 89 | $this->post($this->endpoint . '/modify-content', [$this, 'modify_content']); |
| 90 | 90 | $this->post($this->endpoint . '/ai-update', [$this, 'ai_update']); |
| 91 | 91 | $this->post($this->endpoint . '/ai-update-preview', [$this, 'ai_update_preview']); |
| 92 | - $this->post($this->endpoint . '/generate-tagline', [$this, 'generate_tagline']); | |
| 93 | - $this->get($this->endpoint . '/chatbot-conversation', [$this, 'get_chatbot_conversation'], [ | |
| 94 | - 'chat' => [ | |
| 95 | - 'required' => true, | |
| 96 | - 'sanitize_callback' => 'sanitize_text_field', | |
| 97 | - 'validate_callback' => function($param, $request, $key) { | |
| 98 | - return is_string($param) && strlen($param) > 0 && strlen($param) <= 128 && preg_match('/^[A-Za-z0-9\-_]+$/', $param); | |
| 99 | - }, | |
| 100 | - ], | |
| 101 | - ]); | |
| 92 | + $this->post($this->endpoint . '/generate-logo', [$this, 'generate_logo']); | |
| 93 | + $this->post($this->endpoint . '/generate-meta', [$this, 'generate_meta']); | |
| 102 | 94 | $this->get($this->endpoint . '/attachments', [$this, 'get_attachments'], [ |
| 103 | 95 | 'type' => [ |
| 104 | 96 | 'default' => 'pack', |
| 105 | 97 | 'required' => false, |
| @@ -188,9 +180,9 @@ | ||
| 188 | 180 | |
| 189 | 181 | |
| 190 | 182 | // return $response; |
| 191 | 183 | if (is_wp_error($response)) { |
| 192 | - return $this->error('request_failed', __('Request failed.', 'templately'), 'calculate_credit', 500, ['error_detail' => $response->get_error_message()]); | |
| 184 | + return $this->error('request_failed', __('Request failed.', 'templately'), 'calculate_credit', 500, $response); | |
| 193 | 185 | } |
| 194 | 186 | |
| 195 | 187 | $body = wp_remote_retrieve_body($response); |
| 196 | 188 | $data = json_decode($body, true); |
| @@ -224,9 +216,9 @@ | ||
| 224 | 216 | |
| 225 | 217 | $preview_pages = $this->get_param('preview_pages', [], null); |
| 226 | 218 | $image_replace = $this->get_param('imageReplace', [], null); |
| 227 | 219 | $platform = $this->get_param('platform'); |
| 228 | - $language = $this->get_param('language', null); | |
| 220 | + $language = $this->get_param('language', 'English'); | |
| 229 | 221 | |
| 230 | 222 | // ai content fields |
| 231 | 223 | $name = $this->get_param('name'); |
| 232 | 224 | $category = $this->get_param('category'); |
| @@ -273,17 +265,8 @@ | ||
| 273 | 265 | 'preview_pages' => $preview_pages, |
| 274 | 266 | 'language' => $language, |
| 275 | 267 | 'callback' => defined('TEMPLATELY_CALLBACK') ? TEMPLATELY_CALLBACK . '/wp-json/templately/v1/ai-content/ai-update' : rest_url('templately/v1/ai-content/ai-update'), |
| 276 | 268 | ]; |
| 277 | - /** | |
| 278 | - * Filter body data before making API request to modify-content endpoint | |
| 279 | - * | |
| 280 | - * @since 3.5.0 | |
| 281 | - * @param array $body_data The request body data | |
| 282 | - * @param WP_REST_Request $request The REST request object | |
| 283 | - */ | |
| 284 | - $body_data = apply_filters( 'templately_ai_modify_content_body_data', $body_data, $this->request ); | |
| 285 | - | |
| 286 | 269 | $response = Helper::make_api_post_request('v2/ai/modify-content/pack', $body_data, $extra_headers, 15 * MINUTE_IN_SECONDS); |
| 287 | 270 | |
| 288 | 271 | // set_transient( '__templately_ai_process_id', $response, 60 * 60 * 24 * 30 ); |
| 289 | 272 | // } |
| @@ -296,9 +279,9 @@ | ||
| 296 | 279 | |
| 297 | 280 | // return $response; |
| 298 | 281 | if (is_wp_error($response)) { |
| 299 | 282 | error_log(print_r($response, true)); |
| 300 | - return $this->error('request_failed', __('Request failed.', 'templately'), 'modify_content', 500, ['error_data' => $response->get_error_data()]); | |
| 283 | + return $this->error('request_failed', __('Request failed.', 'templately'), 'modify_content', 500, $response->get_error_data()); | |
| 301 | 284 | } |
| 302 | 285 | |
| 303 | 286 | $body = wp_remote_retrieve_body($response); |
| 304 | 287 | $data = json_decode($body, true); |
| @@ -303,9 +286,9 @@ | ||
| 303 | 286 | $body = wp_remote_retrieve_body($response); |
| 304 | 287 | $data = json_decode($body, true); |
| 305 | 288 | // error status is ok, if status is error then return as is |
| 306 | 289 | if (! is_array($data) || ! isset($data['status'])) { |
| 307 | - return $this->error('invalid_response', __('Invalid response.', 'templately'), 'modify_content', 500, ['data' => $data]); | |
| 290 | + return $this->error('invalid_response', __('Invalid response.', 'templately'), 'modify_content', 500, $data); | |
| 308 | 291 | } |
| 309 | 292 | |
| 310 | 293 | // "{"status":"success","message":"The content is being generated in the queue","process_id":"01JRQQD39GNWTNF18EWF8YH0BG-271838-pack-408"}" |
| 311 | 294 | if (isset($data['status']) && $data['status'] === 'success' && isset($data['process_id'])) { |
| @@ -334,27 +317,26 @@ | ||
| 334 | 317 | |
| 335 | 318 | $user = $this->utils('options')->get('user'); |
| 336 | 319 | |
| 337 | 320 | $ai_process_data[$process_id] = [ |
| 338 | - 'name' => $name, | |
| 339 | - 'category' => $category, | |
| 340 | - 'description' => $description, | |
| 341 | - 'email' => $email, | |
| 342 | - 'contactNumber' => $contactNumber, | |
| 343 | - 'businessAddress' => $businessAddress, | |
| 344 | - 'openingHour' => $openingHour, | |
| 345 | - 'process_id' => $process_id, | |
| 346 | - 'pack_id' => $pack_id, | |
| 347 | - 'ai_page_ids' => $ai_page_ids, | |
| 348 | - 'ai_preview_ids' => $preview_pages, | |
| 349 | - 'content_ids' => $content_ids, | |
| 350 | - 'platform' => $platform, | |
| 351 | - 'api_key' => $this->api_key, | |
| 352 | - 'user_id' => isset($user['id']) ? $user['id'] : null, | |
| 353 | - 'session_id' => $session_id, | |
| 354 | - 'imageReplace' => $image_replace, | |
| 355 | - 'language' => $language, | |
| 356 | - 'requested_platform' => $requested_platform, | |
| 321 | + 'name' => $name, | |
| 322 | + 'category' => $category, | |
| 323 | + 'description' => $description, | |
| 324 | + 'email' => $email, | |
| 325 | + 'contactNumber' => $contactNumber, | |
| 326 | + 'businessAddress' => $businessAddress, | |
| 327 | + 'openingHour' => $openingHour, | |
| 328 | + 'process_id' => $process_id, | |
| 329 | + 'pack_id' => $pack_id, | |
| 330 | + 'ai_page_ids' => $ai_page_ids, | |
| 331 | + 'ai_preview_ids' => $preview_pages, | |
| 332 | + 'content_ids' => $content_ids, | |
| 333 | + 'platform' => $platform, | |
| 334 | + 'api_key' => $this->api_key, | |
| 335 | + 'user_id' => isset($user['id']) ? $user['id'] : null, | |
| 336 | + 'session_id' => $session_id, // Store session_id for coordination | |
| 337 | + 'imageReplace' => $image_replace, // Store session_id for coordination | |
| 338 | + 'language' => $language, | |
| 357 | 339 | ]; |
| 358 | 340 | |
| 359 | 341 | // Update using API key-based storage with automatic count-based cleanup |
| 360 | 342 | AIUtils::update_ai_process_data($ai_process_data); |
| @@ -511,20 +493,17 @@ | ||
| 511 | 493 | ), |
| 512 | 494 | ]; |
| 513 | 495 | } |
| 514 | 496 | |
| 515 | - | |
| 516 | - | |
| 517 | 497 | /** |
| 518 | 498 | * Get attachments from API endpoint |
| 519 | 499 | * |
| 520 | - * @return array|\WP_Error | |
| 500 | + * @return array | |
| 521 | 501 | */ |
| 522 | 502 | public function get_attachments() { |
| 523 | 503 | // Get parameters from request |
| 524 | - $type = $this->get_param('type', 'pack'); | |
| 525 | - $id = $this->get_param('pack_id'); | |
| 526 | - $requested_platform = $this->get_param('requested_platform', 'templately'); | |
| 504 | + $type = $this->get_param('type', 'pack'); | |
| 505 | + $id = $this->get_param('pack_id'); | |
| 527 | 506 | |
| 528 | 507 | // Require ID parameter - return error if not provided |
| 529 | 508 | if (empty($id)) { |
| 530 | 509 | return $this->error('missing_id', __('Pack ID or ID parameter is required.', 'templately'), 'get_attachments', 400); |
| @@ -535,16 +514,15 @@ | ||
| 535 | 514 | $api_endpoint = "get-xml-attachment/{$type}/{$id}"; |
| 536 | 515 | |
| 537 | 516 | // Make API call |
| 538 | 517 | $extra_headers = [ |
| 539 | - 'Accept' => 'application/xml, text/xml', | |
| 540 | - 'x-templately-requested-platform' => $requested_platform, | |
| 518 | + 'Accept' => 'application/xml, text/xml', | |
| 541 | 519 | ]; |
| 542 | 520 | $response = Helper::make_api_get_request("v2/$api_endpoint", [], $extra_headers, 30); |
| 543 | 521 | |
| 544 | 522 | // Check for HTTP errors |
| 545 | 523 | if (is_wp_error($response)) { |
| 546 | - return $this->error('api_request_failed', __('Failed to fetch attachments from API.', 'templately'), 'get_attachments', 500, ['error_detail' => $response->get_error_message()]); | |
| 524 | + return $this->error('api_request_failed', __('Failed to fetch attachments from API.', 'templately'), 'get_attachments', 500, $response->get_error_message()); | |
| 547 | 525 | } |
| 548 | 526 | |
| 549 | 527 | $response_code = wp_remote_retrieve_response_code($response); |
| 550 | 528 | $xml_content = wp_remote_retrieve_body($response); |
| @@ -567,9 +545,9 @@ | ||
| 567 | 545 | // Parse the XML content from API response |
| 568 | 546 | $parsed_data = $this->parse_xml_content($xml_content); |
| 569 | 547 | |
| 570 | 548 | if (is_wp_error($parsed_data)) { |
| 571 | - return $this->error('xml_parse_error', __('Failed to parse XML content.', 'templately'), 'get_attachments', 500, ['error_detail' => $parsed_data->get_error_message()]); | |
| 549 | + return $this->error('xml_parse_error', __('Failed to parse XML content.', 'templately'), 'get_attachments', 500, $parsed_data->get_error_message()); | |
| 572 | 550 | } |
| 573 | 551 | |
| 574 | 552 | // Extract attachments from parsed data |
| 575 | 553 | $attachments = $this->extract_attachments_from_parsed_data($parsed_data); |
| @@ -580,9 +558,9 @@ | ||
| 580 | 558 | 'message' => sprintf(__('Found %d attachments.', 'templately'), count($attachments)), |
| 581 | 559 | ]; |
| 582 | 560 | |
| 583 | 561 | } catch (Exception $e) { |
| 584 | - return $this->error('exception', __('An unexpected error occurred while fetching attachments.', 'templately'), 'get_attachments', 500, ['error_detail' => $e->getMessage()]); | |
| 562 | + return $this->error('exception', __('An unexpected error occurred while fetching attachments.', 'templately'), 'get_attachments', 500, $e->getMessage()); | |
| 585 | 563 | } |
| 586 | 564 | } |
| 587 | 565 | |
| 588 | 566 | |
| @@ -590,9 +568,9 @@ | ||
| 590 | 568 | /** |
| 591 | 569 | * Parse XML content string using WXR Parser |
| 592 | 570 | * |
| 593 | 571 | * @param string $xml_content XML content string |
| 594 | - * @return array|\WP_Error Parsed data or error | |
| 572 | + * @return array|WP_Error Parsed data or error | |
| 595 | 573 | */ |
| 596 | 574 | private function parse_xml_content($xml_content) { |
| 597 | 575 | // Ensure WordPress filesystem functions are available |
| 598 | 576 | if (!function_exists('wp_tempnam')) { |
| @@ -846,33 +824,44 @@ | ||
| 846 | 824 | 'total_pages' => $total_results > 0 ? ceil($total_results / $per_page_count) : 0, |
| 847 | 825 | ]); |
| 848 | 826 | } |
| 849 | 827 | |
| 850 | - | |
| 851 | - | |
| 852 | 828 | /** |
| 853 | - * Generate tagline using AI | |
| 829 | + * Generate logo using AI | |
| 854 | 830 | * |
| 855 | 831 | * @return array|WP_Error |
| 856 | 832 | */ |
| 857 | - public function generate_tagline() { | |
| 833 | + public function generate_logo() { | |
| 858 | 834 | // Get parameters |
| 859 | - $prompt = $this->get_param('prompt'); | |
| 835 | + $business_name = $this->get_param('business_name', ''); | |
| 836 | + $description = $this->get_param('description'); | |
| 837 | + $logo_type = $this->get_param('logo_type'); | |
| 860 | 838 | $requested_platform = $this->get_param('requested_platform', 'templately'); |
| 861 | 839 | |
| 862 | 840 | // Validate required parameters |
| 863 | - if (empty($prompt)) { | |
| 841 | + if (empty($description)) { | |
| 864 | 842 | return $this->error( |
| 865 | - 'missing_prompt', | |
| 866 | - __('Prompt is required for tagline generation.', 'templately'), | |
| 867 | - 'generate_tagline', | |
| 843 | + 'missing_description', | |
| 844 | + __('Description is required for logo generation.', 'templately'), | |
| 845 | + 'generate_logo', | |
| 868 | 846 | 400 |
| 869 | 847 | ); |
| 870 | 848 | } |
| 871 | 849 | |
| 850 | + if (empty($logo_type)) { | |
| 851 | + return $this->error( | |
| 852 | + 'missing_logo_type', | |
| 853 | + __('Logo type is required.', 'templately'), | |
| 854 | + 'generate_logo', | |
| 855 | + 400 | |
| 856 | + ); | |
| 857 | + } | |
| 858 | + | |
| 872 | 859 | // Prepare request body |
| 873 | 860 | $body_data = [ |
| 874 | - 'prompt' => $prompt, | |
| 861 | + 'business_name' => $business_name, | |
| 862 | + 'description' => $description, | |
| 863 | + 'logo_type' => $logo_type, | |
| 875 | 864 | ]; |
| 876 | 865 | |
| 877 | 866 | // Make API request |
| 878 | 867 | $extra_headers = [ |
| @@ -879,18 +868,18 @@ | ||
| 879 | 868 | 'Content-Type' => 'application/json', |
| 880 | 869 | 'x-templately-requested-platform' => $requested_platform, |
| 881 | 870 | ]; |
| 882 | 871 | |
| 883 | - $response = Helper::make_api_post_request('v2/generate-tagline', $body_data, $extra_headers, 30); | |
| 872 | + $response = Helper::make_api_post_request('v2/generate-logo', $body_data, $extra_headers, 60); | |
| 884 | 873 | |
| 885 | 874 | // Handle API response errors |
| 886 | 875 | if (is_wp_error($response)) { |
| 887 | 876 | return $this->error( |
| 888 | 877 | 'api_request_failed', |
| 889 | - __('Failed to generate tagline.', 'templately'), | |
| 890 | - 'generate_tagline', | |
| 878 | + __('Something went wrong. Please try again or contact support.', 'templately'), | |
| 879 | + 'generate_logo', | |
| 891 | 880 | 500, |
| 892 | - ['error_detail' => $response->get_error_message()] | |
| 881 | + $response->get_error_message() | |
| 893 | 882 | ); |
| 894 | 883 | } |
| 895 | 884 | |
| 896 | 885 | $response_code = wp_remote_retrieve_response_code($response); |
| @@ -905,9 +894,9 @@ | ||
| 905 | 894 | $error_message = isset($data['message']) ? $data['message'] : __('Something went wrong. Please try again or contact support.', 'templately'); |
| 906 | 895 | return $this->error( |
| 907 | 896 | 'api_response_error', |
| 908 | 897 | $error_message, |
| 909 | - 'generate_tagline', | |
| 898 | + 'generate_logo', | |
| 910 | 899 | $response_code |
| 911 | 900 | ); |
| 912 | 901 | } |
| 913 | 902 | |
| @@ -914,9 +903,9 @@ | ||
| 914 | 903 | // Otherwise, return generic error |
| 915 | 904 | return $this->error( |
| 916 | 905 | 'api_response_error', |
| 917 | 906 | __('Something went wrong. Please try again or contact support.', 'templately'), |
| 918 | - 'generate_tagline', | |
| 907 | + 'generate_logo', | |
| 919 | 908 | $response_code |
| 920 | 909 | ); |
| 921 | 910 | } |
| 922 | 911 | |
| @@ -925,9 +914,9 @@ | ||
| 925 | 914 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 926 | 915 | return $this->error( |
| 927 | 916 | 'invalid_response', |
| 928 | 917 | __('Invalid response from API.', 'templately'), |
| 929 | - 'generate_tagline', | |
| 918 | + 'generate_logo', | |
| 930 | 919 | 500 |
| 931 | 920 | ); |
| 932 | 921 | } |
| 933 | 922 | |
| @@ -935,54 +924,150 @@ | ||
| 935 | 924 | if (!isset($data['status'])) { |
| 936 | 925 | return $this->error( |
| 937 | 926 | 'api_response_error', |
| 938 | 927 | __('API returned an unexpected response.', 'templately'), |
| 939 | - 'generate_tagline', | |
| 928 | + 'generate_logo', | |
| 940 | 929 | 500 |
| 941 | 930 | ); |
| 942 | 931 | } |
| 943 | 932 | |
| 944 | - // Return the response as-is | |
| 933 | + // Extract image_base64 from nested data field if present | |
| 934 | + $image_base64 = null; | |
| 935 | + if (isset($data['data']['image_base64'])) { | |
| 936 | + $image_base64 = $data['data']['image_base64']; | |
| 937 | + } elseif (isset($data['image_base64'])) { | |
| 938 | + // Fallback for legacy response format | |
| 939 | + $image_base64 = $data['image_base64']; | |
| 940 | + } | |
| 941 | + | |
| 942 | + // Upload base64 image to media library if provided | |
| 943 | + if (!empty($image_base64)) { | |
| 944 | + $upload_result = Utils::upload_logo_base64($image_base64); | |
| 945 | + | |
| 946 | + if (is_array($upload_result) && isset($upload_result['error'])) { | |
| 947 | + // Log error but don't fail the entire response | |
| 948 | + error_log('Logo upload error: ' . $upload_result['error']); | |
| 949 | + } else if (is_array($upload_result) && isset($upload_result['id'])) { | |
| 950 | + // Return clean response with only essential uploaded logo information | |
| 951 | + // Overwrite the data field to remove base64 image data and return only ID and URL | |
| 952 | + return [ | |
| 953 | + 'status' => 'success', | |
| 954 | + 'data' => [ | |
| 955 | + 'id' => $upload_result['id'], | |
| 956 | + 'url' => $upload_result['url'], | |
| 957 | + ], | |
| 958 | + ]; | |
| 959 | + } | |
| 960 | + } | |
| 961 | + | |
| 962 | + // Return the original response if no logo was uploaded | |
| 945 | 963 | return $data; |
| 946 | 964 | } |
| 947 | 965 | |
| 948 | 966 | /** |
| 949 | - * Fetch a chatbot conversation by ID from the external Templately chatbot API. | |
| 967 | + * Generate meta (title or tagline) using AI | |
| 950 | 968 | * |
| 951 | - * Used to resume a conversation that began on templately.dev when the user | |
| 952 | - * is redirected into the plugin with ?process=ai&chat={uuid}. | |
| 953 | - * | |
| 954 | - * @return array|\WP_Error Pass-through of the external response { status, data } or WP_Error. | |
| 969 | + * @return array|WP_Error | |
| 955 | 970 | */ |
| 956 | - public function get_chatbot_conversation() { | |
| 957 | - $chat = $this->get_param('chat'); | |
| 971 | + public function generate_meta() { | |
| 972 | + // Get parameters | |
| 973 | + $field = $this->get_param('field'); | |
| 974 | + $text = $this->get_param('text'); | |
| 975 | + $requested_platform = $this->get_param('requested_platform', 'templately'); | |
| 958 | 976 | |
| 959 | - if (empty($chat)) { | |
| 960 | - return $this->error('invalid_chat_id', __('Invalid conversation ID.', 'templately'), 'ai-content/chatbot-conversation', 400); | |
| 977 | + // Validate required parameters | |
| 978 | + if (empty($field)) { | |
| 979 | + return $this->error( | |
| 980 | + 'missing_field', | |
| 981 | + __('Field is required.', 'templately'), | |
| 982 | + 'generate_meta', | |
| 983 | + 400 | |
| 984 | + ); | |
| 961 | 985 | } |
| 962 | 986 | |
| 987 | + if (empty($text)) { | |
| 988 | + return $this->error( | |
| 989 | + 'missing_text', | |
| 990 | + __('Text is required for meta generation.', 'templately'), | |
| 991 | + 'generate_meta', | |
| 992 | + 400 | |
| 993 | + ); | |
| 994 | + } | |
| 995 | + | |
| 996 | + // Prepare request body | |
| 997 | + $body_data = [ | |
| 998 | + 'field' => $field, | |
| 999 | + 'text' => $text, | |
| 1000 | + ]; | |
| 1001 | + | |
| 1002 | + // Make API request | |
| 963 | 1003 | $extra_headers = [ |
| 964 | - 'Accept' => 'application/json', | |
| 1004 | + 'Content-Type' => 'application/json', | |
| 1005 | + 'x-templately-requested-platform' => $requested_platform, | |
| 965 | 1006 | ]; |
| 966 | - $response = Helper::make_api_get_request("v2/chatbot/conversation/{$chat}", [], $extra_headers, 30); | |
| 967 | 1007 | |
| 1008 | + $response = Helper::make_api_post_request('v2/generate-meta', $body_data, $extra_headers, 30); | |
| 1009 | + | |
| 1010 | + // Handle API response errors | |
| 968 | 1011 | if (is_wp_error($response)) { |
| 969 | - return $this->error('request_failed', __('Failed to fetch conversation.', 'templately'), 'ai-content/chatbot-conversation', 500, ['error_detail' => $response->get_error_message()]); | |
| 1012 | + return $this->error( | |
| 1013 | + 'api_request_failed', | |
| 1014 | + __('Failed to generate meta.', 'templately'), | |
| 1015 | + 'generate_meta', | |
| 1016 | + 500, | |
| 1017 | + $response->get_error_message() | |
| 1018 | + ); | |
| 970 | 1019 | } |
| 971 | 1020 | |
| 972 | 1021 | $response_code = wp_remote_retrieve_response_code($response); |
| 973 | - $body = wp_remote_retrieve_body($response); | |
| 974 | - $data = json_decode($body, true); | |
| 1022 | + $response_body = wp_remote_retrieve_body($response); | |
| 975 | 1023 | |
| 976 | 1024 | if ($response_code !== 200) { |
| 977 | - $message = (is_array($data) && !empty($data['message'])) ? $data['message'] : sprintf(__('API returned HTTP %d error.', 'templately'), $response_code); | |
| 978 | - return $this->error('api_http_error', $message, 'ai-content/chatbot-conversation', $response_code); | |
| 1025 | + // Try to parse the response body as JSON to get specific error details | |
| 1026 | + $data = json_decode($response_body, true); | |
| 1027 | + | |
| 1028 | + // If valid JSON, extract error message and return with proper status code | |
| 1029 | + if (json_last_error() === JSON_ERROR_NONE && is_array($data)) { | |
| 1030 | + $error_message = isset($data['message']) ? $data['message'] : __('Something went wrong. Please try again or contact support.', 'templately'); | |
| 1031 | + return $this->error( | |
| 1032 | + 'api_response_error', | |
| 1033 | + $error_message, | |
| 1034 | + 'generate_meta', | |
| 1035 | + $response_code | |
| 1036 | + ); | |
| 1037 | + } | |
| 1038 | + | |
| 1039 | + // Otherwise, return generic error | |
| 1040 | + return $this->error( | |
| 1041 | + 'api_response_error', | |
| 1042 | + __('Something went wrong. Please try again or contact support.', 'templately'), | |
| 1043 | + 'generate_meta', | |
| 1044 | + $response_code | |
| 1045 | + ); | |
| 979 | 1046 | } |
| 980 | 1047 | |
| 981 | - if (!is_array($data) || !isset($data['status'])) { | |
| 982 | - return $this->error('invalid_response', __('Invalid response.', 'templately'), 'ai-content/chatbot-conversation', 500); | |
| 1048 | + // Parse and validate response | |
| 1049 | + $data = json_decode($response_body, true); | |
| 1050 | + if (json_last_error() !== JSON_ERROR_NONE) { | |
| 1051 | + return $this->error( | |
| 1052 | + 'invalid_response', | |
| 1053 | + __('Invalid response from API.', 'templately'), | |
| 1054 | + 'generate_meta', | |
| 1055 | + 500 | |
| 1056 | + ); | |
| 983 | 1057 | } |
| 984 | 1058 | |
| 1059 | + // Check if the response has the expected structure | |
| 1060 | + if (!isset($data['status'])) { | |
| 1061 | + return $this->error( | |
| 1062 | + 'api_response_error', | |
| 1063 | + __('API returned an unexpected response.', 'templately'), | |
| 1064 | + 'generate_meta', | |
| 1065 | + 500 | |
| 1066 | + ); | |
| 1067 | + } | |
| 1068 | + | |
| 1069 | + // Return the response as-is | |
| 985 | 1070 | return $data; |
| 986 | 1071 | } |
| 987 | 1072 | |
| 988 | 1073 | /** |