| @@ -15,9 +15,8 @@ | ||
| 15 | 15 | use WP_REST_Request; |
| 16 | 16 | use WP_Error; |
| 17 | 17 | use Templately\Core\Importer\Utils\Utils; |
| 18 | 18 | use Templately\Core\Importer\Utils\AIUtils; |
| 19 | -use Templately\Core\Importer\Utils\SignatureVerifier; | |
| 20 | 19 | use Templately\Core\Importer\Parsers\WXR_Parser; |
| 21 | 20 | |
| 22 | 21 | class AIContent extends API { |
| 23 | 22 | private $endpoint = 'ai-content'; |
| @@ -42,32 +41,12 @@ | ||
| 42 | 41 | $process_id = $this->get_param('process_id'); |
| 43 | 42 | |
| 44 | 43 | $_route = $request->get_route(); |
| 45 | 44 | if ('/templately/v1/ai-content/ai-update' === $_route || '/templately/v1/ai-content/ai-update-preview' === $_route) { |
| 46 | - Helper::log( [ | |
| 47 | - 'headers' => $request->get_headers(), | |
| 48 | - 'body' => $request->get_params(), | |
| 49 | - ], 'ai_update_request' ); | |
| 50 | - | |
| 51 | 45 | if (empty($process_id)) { |
| 52 | 46 | return $this->error('invalid_id', __('Invalid ID.', 'templately'), 'calculate_credit', 400); |
| 53 | 47 | } |
| 54 | 48 | |
| 55 | - $header_api_key = sanitize_text_field($request->get_header('x_templately_apikey')); | |
| 56 | - if (empty($header_api_key)) { | |
| 57 | - $header_api_key = sanitize_text_field($request->get_header('X-Templately-Apikey')); | |
| 58 | - } | |
| 59 | - | |
| 60 | - // Validate API key from header against database | |
| 61 | - if (empty($header_api_key)) { | |
| 62 | - return $this->error('missing_api_key', __('Missing API key in header.', 'templately'), 'ai-content/permission', 403); | |
| 63 | - } | |
| 64 | - | |
| 65 | - $is_valid_key = $this->validate_api_key_in_db($header_api_key); | |
| 66 | - if (!$is_valid_key) { | |
| 67 | - return $this->error('invalid_api_key', __('Invalid API key provided in header.', 'templately'), 'ai-content/permission', 403); | |
| 68 | - } | |
| 69 | - | |
| 70 | 49 | // Check AI process data using API key-based storage |
| 71 | 50 | $ai_process_data = AIUtils::get_ai_process_data(); |
| 72 | 51 | if (is_array($ai_process_data) && !empty($ai_process_data[$process_id])) { |
| 73 | 52 | return true; |
| @@ -79,9 +58,10 @@ | ||
| 79 | 58 | // // Allow access to attachments endpoint |
| 80 | 59 | // if ('/templately/v1/ai-content/attachments' === $_route) { |
| 81 | 60 | // return true; |
| 82 | 61 | // } |
| 83 | - return parent::_permission_check($request); | |
| 62 | + | |
| 63 | + return parent::permission_check($request); | |
| 84 | 64 | } |
| 85 | 65 | |
| 86 | 66 | |
| 87 | 67 | public function register_routes() { |
| @@ -88,9 +68,10 @@ | ||
| 88 | 68 | // $this->get( $this->endpoint . '/calculate-credit', [ $this, 'calculate_credit' ] ); |
| 89 | 69 | $this->post($this->endpoint . '/modify-content', [$this, 'modify_content']); |
| 90 | 70 | $this->post($this->endpoint . '/ai-update', [$this, 'ai_update']); |
| 91 | 71 | $this->post($this->endpoint . '/ai-update-preview', [$this, 'ai_update_preview']); |
| 92 | - $this->post($this->endpoint . '/generate-tagline', [$this, 'generate_tagline']); | |
| 72 | + $this->post($this->endpoint . '/generate-logo', [$this, 'generate_logo']); | |
| 73 | + $this->post($this->endpoint . '/generate-meta', [$this, 'generate_meta']); | |
| 93 | 74 | $this->get($this->endpoint . '/attachments', [$this, 'get_attachments'], [ |
| 94 | 75 | 'type' => [ |
| 95 | 76 | 'default' => 'pack', |
| 96 | 77 | 'required' => false, |
| @@ -179,9 +160,9 @@ | ||
| 179 | 160 | |
| 180 | 161 | |
| 181 | 162 | // return $response; |
| 182 | 163 | if (is_wp_error($response)) { |
| 183 | - return $this->error('request_failed', __('Request failed.', 'templately'), 'calculate_credit', 500, ['error_detail' => $response->get_error_message()]); | |
| 164 | + return $this->error('request_failed', __('Request failed.', 'templately'), 'calculate_credit', 500, $response); | |
| 184 | 165 | } |
| 185 | 166 | |
| 186 | 167 | $body = wp_remote_retrieve_body($response); |
| 187 | 168 | $data = json_decode($body, true); |
| @@ -203,21 +184,12 @@ | ||
| 203 | 184 | $isBusinessNichesNew = $this->get_param('isBusinessNichesNew', false); |
| 204 | 185 | $ai_page_ids = $this->get_param('ai_page_ids', [], null); |
| 205 | 186 | $content_ids = $this->get_param('content_ids', [], null); |
| 206 | 187 | $session_id = $this->get_param('session_id'); // Add session_id parameter |
| 207 | - | |
| 208 | - // Security: Sanitize session_id if provided | |
| 209 | - if (!empty($session_id)) { | |
| 210 | - $session_id = AIUtils::sanitize_path_component($session_id, 'session_id'); | |
| 211 | - if (is_wp_error($session_id)) { | |
| 212 | - return $session_id; | |
| 213 | - } | |
| 214 | - } | |
| 215 | - | |
| 216 | 188 | $preview_pages = $this->get_param('preview_pages', [], null); |
| 217 | 189 | $image_replace = $this->get_param('imageReplace', [], null); |
| 218 | 190 | $platform = $this->get_param('platform'); |
| 219 | - $language = $this->get_param('language', null); | |
| 191 | + $language = $this->get_param('language', 'English'); | |
| 220 | 192 | |
| 221 | 193 | // ai content fields |
| 222 | 194 | $name = $this->get_param('name'); |
| 223 | 195 | $category = $this->get_param('category'); |
| @@ -264,17 +236,8 @@ | ||
| 264 | 236 | 'preview_pages' => $preview_pages, |
| 265 | 237 | 'language' => $language, |
| 266 | 238 | 'callback' => defined('TEMPLATELY_CALLBACK') ? TEMPLATELY_CALLBACK . '/wp-json/templately/v1/ai-content/ai-update' : rest_url('templately/v1/ai-content/ai-update'), |
| 267 | 239 | ]; |
| 268 | - /** | |
| 269 | - * Filter body data before making API request to modify-content endpoint | |
| 270 | - * | |
| 271 | - * @since 3.5.0 | |
| 272 | - * @param array $body_data The request body data | |
| 273 | - * @param WP_REST_Request $request The REST request object | |
| 274 | - */ | |
| 275 | - $body_data = apply_filters( 'templately_ai_modify_content_body_data', $body_data, $this->request ); | |
| 276 | - | |
| 277 | 240 | $response = Helper::make_api_post_request('v2/ai/modify-content/pack', $body_data, $extra_headers, 15 * MINUTE_IN_SECONDS); |
| 278 | 241 | |
| 279 | 242 | // set_transient( '__templately_ai_process_id', $response, 60 * 60 * 24 * 30 ); |
| 280 | 243 | // } |
| @@ -287,9 +250,9 @@ | ||
| 287 | 250 | |
| 288 | 251 | // return $response; |
| 289 | 252 | if (is_wp_error($response)) { |
| 290 | 253 | error_log(print_r($response, true)); |
| 291 | - return $this->error('request_failed', __('Request failed.', 'templately'), 'modify_content', 500, ['error_data' => $response->get_error_data()]); | |
| 254 | + return $this->error('request_failed', __('Request failed.', 'templately'), 'modify_content', 500, $response->get_error_data()); | |
| 292 | 255 | } |
| 293 | 256 | |
| 294 | 257 | $body = wp_remote_retrieve_body($response); |
| 295 | 258 | $data = json_decode($body, true); |
| @@ -294,9 +257,9 @@ | ||
| 294 | 257 | $body = wp_remote_retrieve_body($response); |
| 295 | 258 | $data = json_decode($body, true); |
| 296 | 259 | // error status is ok, if status is error then return as is |
| 297 | 260 | if (! is_array($data) || ! isset($data['status'])) { |
| 298 | - return $this->error('invalid_response', __('Invalid response.', 'templately'), 'modify_content', 500, ['data' => $data]); | |
| 261 | + return $this->error('invalid_response', __('Invalid response.', 'templately'), 'modify_content', 500, $data); | |
| 299 | 262 | } |
| 300 | 263 | |
| 301 | 264 | // "{"status":"success","message":"The content is being generated in the queue","process_id":"01JRQQD39GNWTNF18EWF8YH0BG-271838-pack-408"}" |
| 302 | 265 | if (isset($data['status']) && $data['status'] === 'success' && isset($data['process_id'])) { |
| @@ -325,27 +288,26 @@ | ||
| 325 | 288 | |
| 326 | 289 | $user = $this->utils('options')->get('user'); |
| 327 | 290 | |
| 328 | 291 | $ai_process_data[$process_id] = [ |
| 329 | - 'name' => $name, | |
| 330 | - 'category' => $category, | |
| 331 | - 'description' => $description, | |
| 332 | - 'email' => $email, | |
| 333 | - 'contactNumber' => $contactNumber, | |
| 334 | - 'businessAddress' => $businessAddress, | |
| 335 | - 'openingHour' => $openingHour, | |
| 336 | - 'process_id' => $process_id, | |
| 337 | - 'pack_id' => $pack_id, | |
| 338 | - 'ai_page_ids' => $ai_page_ids, | |
| 339 | - 'ai_preview_ids' => $preview_pages, | |
| 340 | - 'content_ids' => $content_ids, | |
| 341 | - 'platform' => $platform, | |
| 342 | - 'api_key' => $this->api_key, | |
| 343 | - 'user_id' => isset($user['id']) ? $user['id'] : null, | |
| 344 | - 'session_id' => $session_id, | |
| 345 | - 'imageReplace' => $image_replace, | |
| 346 | - 'language' => $language, | |
| 347 | - 'requested_platform' => $requested_platform, | |
| 292 | + 'name' => $name, | |
| 293 | + 'category' => $category, | |
| 294 | + 'description' => $description, | |
| 295 | + 'email' => $email, | |
| 296 | + 'contactNumber' => $contactNumber, | |
| 297 | + 'businessAddress' => $businessAddress, | |
| 298 | + 'openingHour' => $openingHour, | |
| 299 | + 'process_id' => $process_id, | |
| 300 | + 'pack_id' => $pack_id, | |
| 301 | + 'ai_page_ids' => $ai_page_ids, | |
| 302 | + 'ai_preview_ids' => $preview_pages, | |
| 303 | + 'content_ids' => $content_ids, | |
| 304 | + 'platform' => $platform, | |
| 305 | + 'api_key' => $this->api_key, | |
| 306 | + 'user_id' => isset($user['id']) ? $user['id'] : null, | |
| 307 | + 'session_id' => $session_id, // Store session_id for coordination | |
| 308 | + 'imageReplace' => $image_replace, // Store session_id for coordination | |
| 309 | + 'language' => $language, | |
| 348 | 310 | ]; |
| 349 | 311 | |
| 350 | 312 | // Update using API key-based storage with automatic count-based cleanup |
| 351 | 313 | AIUtils::update_ai_process_data($ai_process_data); |
| @@ -502,20 +464,17 @@ | ||
| 502 | 464 | ), |
| 503 | 465 | ]; |
| 504 | 466 | } |
| 505 | 467 | |
| 506 | - | |
| 507 | - | |
| 508 | 468 | /** |
| 509 | 469 | * Get attachments from API endpoint |
| 510 | 470 | * |
| 511 | - * @return array|\WP_Error | |
| 471 | + * @return array | |
| 512 | 472 | */ |
| 513 | 473 | public function get_attachments() { |
| 514 | 474 | // Get parameters from request |
| 515 | - $type = $this->get_param('type', 'pack'); | |
| 516 | - $id = $this->get_param('pack_id'); | |
| 517 | - $requested_platform = $this->get_param('requested_platform', 'templately'); | |
| 475 | + $type = $this->get_param('type', 'pack'); | |
| 476 | + $id = $this->get_param('pack_id'); | |
| 518 | 477 | |
| 519 | 478 | // Require ID parameter - return error if not provided |
| 520 | 479 | if (empty($id)) { |
| 521 | 480 | return $this->error('missing_id', __('Pack ID or ID parameter is required.', 'templately'), 'get_attachments', 400); |
| @@ -526,16 +485,15 @@ | ||
| 526 | 485 | $api_endpoint = "get-xml-attachment/{$type}/{$id}"; |
| 527 | 486 | |
| 528 | 487 | // Make API call |
| 529 | 488 | $extra_headers = [ |
| 530 | - 'Accept' => 'application/xml, text/xml', | |
| 531 | - 'x-templately-requested-platform' => $requested_platform, | |
| 489 | + 'Accept' => 'application/xml, text/xml', | |
| 532 | 490 | ]; |
| 533 | 491 | $response = Helper::make_api_get_request("v2/$api_endpoint", [], $extra_headers, 30); |
| 534 | 492 | |
| 535 | 493 | // Check for HTTP errors |
| 536 | 494 | if (is_wp_error($response)) { |
| 537 | - return $this->error('api_request_failed', __('Failed to fetch attachments from API.', 'templately'), 'get_attachments', 500, ['error_detail' => $response->get_error_message()]); | |
| 495 | + return $this->error('api_request_failed', __('Failed to fetch attachments from API.', 'templately'), 'get_attachments', 500, $response->get_error_message()); | |
| 538 | 496 | } |
| 539 | 497 | |
| 540 | 498 | $response_code = wp_remote_retrieve_response_code($response); |
| 541 | 499 | $xml_content = wp_remote_retrieve_body($response); |
| @@ -558,9 +516,9 @@ | ||
| 558 | 516 | // Parse the XML content from API response |
| 559 | 517 | $parsed_data = $this->parse_xml_content($xml_content); |
| 560 | 518 | |
| 561 | 519 | if (is_wp_error($parsed_data)) { |
| 562 | - return $this->error('xml_parse_error', __('Failed to parse XML content.', 'templately'), 'get_attachments', 500, ['error_detail' => $parsed_data->get_error_message()]); | |
| 520 | + return $this->error('xml_parse_error', __('Failed to parse XML content.', 'templately'), 'get_attachments', 500, $parsed_data->get_error_message()); | |
| 563 | 521 | } |
| 564 | 522 | |
| 565 | 523 | // Extract attachments from parsed data |
| 566 | 524 | $attachments = $this->extract_attachments_from_parsed_data($parsed_data); |
| @@ -571,9 +529,9 @@ | ||
| 571 | 529 | 'message' => sprintf(__('Found %d attachments.', 'templately'), count($attachments)), |
| 572 | 530 | ]; |
| 573 | 531 | |
| 574 | 532 | } catch (Exception $e) { |
| 575 | - return $this->error('exception', __('An unexpected error occurred while fetching attachments.', 'templately'), 'get_attachments', 500, ['error_detail' => $e->getMessage()]); | |
| 533 | + return $this->error('exception', __('An unexpected error occurred while fetching attachments.', 'templately'), 'get_attachments', 500, $e->getMessage()); | |
| 576 | 534 | } |
| 577 | 535 | } |
| 578 | 536 | |
| 579 | 537 | |
| @@ -581,9 +539,9 @@ | ||
| 581 | 539 | /** |
| 582 | 540 | * Parse XML content string using WXR Parser |
| 583 | 541 | * |
| 584 | 542 | * @param string $xml_content XML content string |
| 585 | - * @return array|\WP_Error Parsed data or error | |
| 543 | + * @return array|WP_Error Parsed data or error | |
| 586 | 544 | */ |
| 587 | 545 | private function parse_xml_content($xml_content) { |
| 588 | 546 | // Ensure WordPress filesystem functions are available |
| 589 | 547 | if (!function_exists('wp_tempnam')) { |
| @@ -837,33 +795,44 @@ | ||
| 837 | 795 | 'total_pages' => $total_results > 0 ? ceil($total_results / $per_page_count) : 0, |
| 838 | 796 | ]); |
| 839 | 797 | } |
| 840 | 798 | |
| 841 | - | |
| 842 | - | |
| 843 | 799 | /** |
| 844 | - * Generate tagline using AI | |
| 800 | + * Generate logo using AI | |
| 845 | 801 | * |
| 846 | 802 | * @return array|WP_Error |
| 847 | 803 | */ |
| 848 | - public function generate_tagline() { | |
| 804 | + public function generate_logo() { | |
| 849 | 805 | // Get parameters |
| 850 | - $prompt = $this->get_param('prompt'); | |
| 806 | + $business_name = $this->get_param('business_name', ''); | |
| 807 | + $description = $this->get_param('description'); | |
| 808 | + $logo_type = $this->get_param('logo_type'); | |
| 851 | 809 | $requested_platform = $this->get_param('requested_platform', 'templately'); |
| 852 | 810 | |
| 853 | 811 | // Validate required parameters |
| 854 | - if (empty($prompt)) { | |
| 812 | + if (empty($description)) { | |
| 855 | 813 | return $this->error( |
| 856 | - 'missing_prompt', | |
| 857 | - __('Prompt is required for tagline generation.', 'templately'), | |
| 858 | - 'generate_tagline', | |
| 814 | + 'missing_description', | |
| 815 | + __('Description is required for logo generation.', 'templately'), | |
| 816 | + 'generate_logo', | |
| 859 | 817 | 400 |
| 860 | 818 | ); |
| 861 | 819 | } |
| 862 | 820 | |
| 821 | + if (empty($logo_type)) { | |
| 822 | + return $this->error( | |
| 823 | + 'missing_logo_type', | |
| 824 | + __('Logo type is required.', 'templately'), | |
| 825 | + 'generate_logo', | |
| 826 | + 400 | |
| 827 | + ); | |
| 828 | + } | |
| 829 | + | |
| 863 | 830 | // Prepare request body |
| 864 | 831 | $body_data = [ |
| 865 | - 'prompt' => $prompt, | |
| 832 | + 'business_name' => $business_name, | |
| 833 | + 'description' => $description, | |
| 834 | + 'logo_type' => $logo_type, | |
| 866 | 835 | ]; |
| 867 | 836 | |
| 868 | 837 | // Make API request |
| 869 | 838 | $extra_headers = [ |
| @@ -870,18 +839,18 @@ | ||
| 870 | 839 | 'Content-Type' => 'application/json', |
| 871 | 840 | 'x-templately-requested-platform' => $requested_platform, |
| 872 | 841 | ]; |
| 873 | 842 | |
| 874 | - $response = Helper::make_api_post_request('v2/generate-tagline', $body_data, $extra_headers, 30); | |
| 843 | + $response = Helper::make_api_post_request('v2/generate-logo', $body_data, $extra_headers, 60); | |
| 875 | 844 | |
| 876 | 845 | // Handle API response errors |
| 877 | 846 | if (is_wp_error($response)) { |
| 878 | 847 | return $this->error( |
| 879 | 848 | 'api_request_failed', |
| 880 | - __('Failed to generate tagline.', 'templately'), | |
| 881 | - 'generate_tagline', | |
| 849 | + __('Something went wrong. Please try again or contact support.', 'templately'), | |
| 850 | + 'generate_logo', | |
| 882 | 851 | 500, |
| 883 | - ['error_detail' => $response->get_error_message()] | |
| 852 | + $response->get_error_message() | |
| 884 | 853 | ); |
| 885 | 854 | } |
| 886 | 855 | |
| 887 | 856 | $response_code = wp_remote_retrieve_response_code($response); |
| @@ -896,9 +865,9 @@ | ||
| 896 | 865 | $error_message = isset($data['message']) ? $data['message'] : __('Something went wrong. Please try again or contact support.', 'templately'); |
| 897 | 866 | return $this->error( |
| 898 | 867 | 'api_response_error', |
| 899 | 868 | $error_message, |
| 900 | - 'generate_tagline', | |
| 869 | + 'generate_logo', | |
| 901 | 870 | $response_code |
| 902 | 871 | ); |
| 903 | 872 | } |
| 904 | 873 | |
| @@ -905,9 +874,9 @@ | ||
| 905 | 874 | // Otherwise, return generic error |
| 906 | 875 | return $this->error( |
| 907 | 876 | 'api_response_error', |
| 908 | 877 | __('Something went wrong. Please try again or contact support.', 'templately'), |
| 909 | - 'generate_tagline', | |
| 878 | + 'generate_logo', | |
| 910 | 879 | $response_code |
| 911 | 880 | ); |
| 912 | 881 | } |
| 913 | 882 | |
| @@ -916,9 +885,9 @@ | ||
| 916 | 885 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 917 | 886 | return $this->error( |
| 918 | 887 | 'invalid_response', |
| 919 | 888 | __('Invalid response from API.', 'templately'), |
| 920 | - 'generate_tagline', | |
| 889 | + 'generate_logo', | |
| 921 | 890 | 500 |
| 922 | 891 | ); |
| 923 | 892 | } |
| 924 | 893 | |
| @@ -926,52 +895,149 @@ | ||
| 926 | 895 | if (!isset($data['status'])) { |
| 927 | 896 | return $this->error( |
| 928 | 897 | 'api_response_error', |
| 929 | 898 | __('API returned an unexpected response.', 'templately'), |
| 930 | - 'generate_tagline', | |
| 899 | + 'generate_logo', | |
| 931 | 900 | 500 |
| 932 | 901 | ); |
| 933 | 902 | } |
| 934 | 903 | |
| 935 | - // Return the response as-is | |
| 904 | + // Extract image_base64 from nested data field if present | |
| 905 | + $image_base64 = null; | |
| 906 | + if (isset($data['data']['image_base64'])) { | |
| 907 | + $image_base64 = $data['data']['image_base64']; | |
| 908 | + } elseif (isset($data['image_base64'])) { | |
| 909 | + // Fallback for legacy response format | |
| 910 | + $image_base64 = $data['image_base64']; | |
| 911 | + } | |
| 912 | + | |
| 913 | + // Upload base64 image to media library if provided | |
| 914 | + if (!empty($image_base64)) { | |
| 915 | + $upload_result = Utils::upload_logo_base64($image_base64); | |
| 916 | + | |
| 917 | + if (is_array($upload_result) && isset($upload_result['error'])) { | |
| 918 | + // Log error but don't fail the entire response | |
| 919 | + error_log('Logo upload error: ' . $upload_result['error']); | |
| 920 | + } else if (is_array($upload_result) && isset($upload_result['id'])) { | |
| 921 | + // Return clean response with only essential uploaded logo information | |
| 922 | + // Overwrite the data field to remove base64 image data and return only ID and URL | |
| 923 | + return [ | |
| 924 | + 'status' => 'success', | |
| 925 | + 'data' => [ | |
| 926 | + 'id' => $upload_result['id'], | |
| 927 | + 'url' => $upload_result['url'], | |
| 928 | + ], | |
| 929 | + ]; | |
| 930 | + } | |
| 931 | + } | |
| 932 | + | |
| 933 | + // Return the original response if no logo was uploaded | |
| 936 | 934 | return $data; |
| 937 | 935 | } |
| 938 | 936 | |
| 939 | 937 | /** |
| 940 | - * Validate API key against database | |
| 941 | - * Checks if the provided API key exists for any user on the current site | |
| 942 | - * Handles both single-site and multisite WordPress installations | |
| 938 | + * Generate meta (title or tagline) using AI | |
| 943 | 939 | * |
| 944 | - * @param string $api_key The API key to validate | |
| 945 | - * @return bool True if valid, false otherwise | |
| 940 | + * @return array|WP_Error | |
| 946 | 941 | */ |
| 947 | - private function validate_api_key_in_db($api_key) { | |
| 948 | - global $wpdb; | |
| 942 | + public function generate_meta() { | |
| 943 | + // Get parameters | |
| 944 | + $field = $this->get_param('field'); | |
| 945 | + $text = $this->get_param('text'); | |
| 946 | + $requested_platform = $this->get_param('requested_platform', 'templately'); | |
| 949 | 947 | |
| 950 | - $api_key = sanitize_text_field($api_key); | |
| 948 | + // Validate required parameters | |
| 949 | + if (empty($field)) { | |
| 950 | + return $this->error( | |
| 951 | + 'missing_field', | |
| 952 | + __('Field is required.', 'templately'), | |
| 953 | + 'generate_meta', | |
| 954 | + 400 | |
| 955 | + ); | |
| 956 | + } | |
| 951 | 957 | |
| 952 | - if (empty($api_key)) { | |
| 953 | - return false; | |
| 958 | + if (empty($text)) { | |
| 959 | + return $this->error( | |
| 960 | + 'missing_text', | |
| 961 | + __('Text is required for meta generation.', 'templately'), | |
| 962 | + 'generate_meta', | |
| 963 | + 400 | |
| 964 | + ); | |
| 954 | 965 | } |
| 955 | 966 | |
| 956 | - $meta_key = '_templately_api_key'; | |
| 967 | + // Prepare request body | |
| 968 | + $body_data = [ | |
| 969 | + 'field' => $field, | |
| 970 | + 'text' => $text, | |
| 971 | + ]; | |
| 957 | 972 | |
| 958 | - // Handle multisite: key will have site prefix in multisite | |
| 959 | - if (is_multisite()) { | |
| 960 | - // get_user_option() uses the format: {$wpdb->base_prefix}{$blog_id}_{$meta_key} | |
| 961 | - // For current blog, we need to check with the current blog prefix | |
| 962 | - $blog_id = get_current_blog_id(); | |
| 963 | - $meta_key = $wpdb->get_blog_prefix($blog_id) . $meta_key; | |
| 973 | + // Make API request | |
| 974 | + $extra_headers = [ | |
| 975 | + 'Content-Type' => 'application/json', | |
| 976 | + 'x-templately-requested-platform' => $requested_platform, | |
| 977 | + ]; | |
| 978 | + | |
| 979 | + $response = Helper::make_api_post_request('v2/generate-meta', $body_data, $extra_headers, 30); | |
| 980 | + | |
| 981 | + // Handle API response errors | |
| 982 | + if (is_wp_error($response)) { | |
| 983 | + return $this->error( | |
| 984 | + 'api_request_failed', | |
| 985 | + __('Failed to generate meta.', 'templately'), | |
| 986 | + 'generate_meta', | |
| 987 | + 500, | |
| 988 | + $response->get_error_message() | |
| 989 | + ); | |
| 964 | 990 | } |
| 965 | 991 | |
| 966 | - // Query to check if this API key exists for any user | |
| 967 | - $query = $wpdb->prepare( | |
| 968 | - "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1", | |
| 969 | - $meta_key, | |
| 970 | - $api_key | |
| 971 | - ); | |
| 992 | + $response_code = wp_remote_retrieve_response_code($response); | |
| 993 | + $response_body = wp_remote_retrieve_body($response); | |
| 972 | 994 | |
| 973 | - $user_id = $wpdb->get_var($query); | |
| 995 | + if ($response_code !== 200) { | |
| 996 | + // Try to parse the response body as JSON to get specific error details | |
| 997 | + $data = json_decode($response_body, true); | |
| 974 | 998 | |
| 975 | - return !empty($user_id); | |
| 999 | + // If valid JSON, extract error message and return with proper status code | |
| 1000 | + if (json_last_error() === JSON_ERROR_NONE && is_array($data)) { | |
| 1001 | + $error_message = isset($data['message']) ? $data['message'] : __('Something went wrong. Please try again or contact support.', 'templately'); | |
| 1002 | + return $this->error( | |
| 1003 | + 'api_response_error', | |
| 1004 | + $error_message, | |
| 1005 | + 'generate_meta', | |
| 1006 | + $response_code | |
| 1007 | + ); | |
| 1008 | + } | |
| 1009 | + | |
| 1010 | + // Otherwise, return generic error | |
| 1011 | + return $this->error( | |
| 1012 | + 'api_response_error', | |
| 1013 | + __('Something went wrong. Please try again or contact support.', 'templately'), | |
| 1014 | + 'generate_meta', | |
| 1015 | + $response_code | |
| 1016 | + ); | |
| 1017 | + } | |
| 1018 | + | |
| 1019 | + // Parse and validate response | |
| 1020 | + $data = json_decode($response_body, true); | |
| 1021 | + if (json_last_error() !== JSON_ERROR_NONE) { | |
| 1022 | + return $this->error( | |
| 1023 | + 'invalid_response', | |
| 1024 | + __('Invalid response from API.', 'templately'), | |
| 1025 | + 'generate_meta', | |
| 1026 | + 500 | |
| 1027 | + ); | |
| 1028 | + } | |
| 1029 | + | |
| 1030 | + // Check if the response has the expected structure | |
| 1031 | + if (!isset($data['status'])) { | |
| 1032 | + return $this->error( | |
| 1033 | + 'api_response_error', | |
| 1034 | + __('API returned an unexpected response.', 'templately'), | |
| 1035 | + 'generate_meta', | |
| 1036 | + 500 | |
| 1037 | + ); | |
| 1038 | + } | |
| 1039 | + | |
| 1040 | + // Return the response as-is | |
| 1041 | + return $data; | |
| 976 | 1042 | } |
| 977 | 1043 | } |