| @@ -88,9 +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']); | |
| 92 | + $this->post($this->endpoint . '/generate-logo', [$this, 'generate_logo']); | |
| 93 | + $this->post($this->endpoint . '/generate-meta', [$this, 'generate_meta']); | |
| 93 | 94 | $this->get($this->endpoint . '/attachments', [$this, 'get_attachments'], [ |
| 94 | 95 | 'type' => [ |
| 95 | 96 | 'default' => 'pack', |
| 96 | 97 | 'required' => false, |
| @@ -179,9 +180,9 @@ | ||
| 179 | 180 | |
| 180 | 181 | |
| 181 | 182 | // return $response; |
| 182 | 183 | if (is_wp_error($response)) { |
| 183 | - 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); | |
| 184 | 185 | } |
| 185 | 186 | |
| 186 | 187 | $body = wp_remote_retrieve_body($response); |
| 187 | 188 | $data = json_decode($body, true); |
| @@ -215,9 +216,9 @@ | ||
| 215 | 216 | |
| 216 | 217 | $preview_pages = $this->get_param('preview_pages', [], null); |
| 217 | 218 | $image_replace = $this->get_param('imageReplace', [], null); |
| 218 | 219 | $platform = $this->get_param('platform'); |
| 219 | - $language = $this->get_param('language', null); | |
| 220 | + $language = $this->get_param('language', 'English'); | |
| 220 | 221 | |
| 221 | 222 | // ai content fields |
| 222 | 223 | $name = $this->get_param('name'); |
| 223 | 224 | $category = $this->get_param('category'); |
| @@ -264,17 +265,8 @@ | ||
| 264 | 265 | 'preview_pages' => $preview_pages, |
| 265 | 266 | 'language' => $language, |
| 266 | 267 | 'callback' => defined('TEMPLATELY_CALLBACK') ? TEMPLATELY_CALLBACK . '/wp-json/templately/v1/ai-content/ai-update' : rest_url('templately/v1/ai-content/ai-update'), |
| 267 | 268 | ]; |
| 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 | 269 | $response = Helper::make_api_post_request('v2/ai/modify-content/pack', $body_data, $extra_headers, 15 * MINUTE_IN_SECONDS); |
| 278 | 270 | |
| 279 | 271 | // set_transient( '__templately_ai_process_id', $response, 60 * 60 * 24 * 30 ); |
| 280 | 272 | // } |
| @@ -287,9 +279,9 @@ | ||
| 287 | 279 | |
| 288 | 280 | // return $response; |
| 289 | 281 | if (is_wp_error($response)) { |
| 290 | 282 | error_log(print_r($response, true)); |
| 291 | - 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()); | |
| 292 | 284 | } |
| 293 | 285 | |
| 294 | 286 | $body = wp_remote_retrieve_body($response); |
| 295 | 287 | $data = json_decode($body, true); |
| @@ -294,9 +286,9 @@ | ||
| 294 | 286 | $body = wp_remote_retrieve_body($response); |
| 295 | 287 | $data = json_decode($body, true); |
| 296 | 288 | // error status is ok, if status is error then return as is |
| 297 | 289 | if (! is_array($data) || ! isset($data['status'])) { |
| 298 | - 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); | |
| 299 | 291 | } |
| 300 | 292 | |
| 301 | 293 | // "{"status":"success","message":"The content is being generated in the queue","process_id":"01JRQQD39GNWTNF18EWF8YH0BG-271838-pack-408"}" |
| 302 | 294 | if (isset($data['status']) && $data['status'] === 'success' && isset($data['process_id'])) { |
| @@ -325,27 +317,26 @@ | ||
| 325 | 317 | |
| 326 | 318 | $user = $this->utils('options')->get('user'); |
| 327 | 319 | |
| 328 | 320 | $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, | |
| 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, | |
| 348 | 339 | ]; |
| 349 | 340 | |
| 350 | 341 | // Update using API key-based storage with automatic count-based cleanup |
| 351 | 342 | AIUtils::update_ai_process_data($ai_process_data); |
| @@ -502,20 +493,17 @@ | ||
| 502 | 493 | ), |
| 503 | 494 | ]; |
| 504 | 495 | } |
| 505 | 496 | |
| 506 | - | |
| 507 | - | |
| 508 | 497 | /** |
| 509 | 498 | * Get attachments from API endpoint |
| 510 | 499 | * |
| 511 | - * @return array|\WP_Error | |
| 500 | + * @return array | |
| 512 | 501 | */ |
| 513 | 502 | public function get_attachments() { |
| 514 | 503 | // 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'); | |
| 504 | + $type = $this->get_param('type', 'pack'); | |
| 505 | + $id = $this->get_param('pack_id'); | |
| 518 | 506 | |
| 519 | 507 | // Require ID parameter - return error if not provided |
| 520 | 508 | if (empty($id)) { |
| 521 | 509 | return $this->error('missing_id', __('Pack ID or ID parameter is required.', 'templately'), 'get_attachments', 400); |
| @@ -526,16 +514,15 @@ | ||
| 526 | 514 | $api_endpoint = "get-xml-attachment/{$type}/{$id}"; |
| 527 | 515 | |
| 528 | 516 | // Make API call |
| 529 | 517 | $extra_headers = [ |
| 530 | - 'Accept' => 'application/xml, text/xml', | |
| 531 | - 'x-templately-requested-platform' => $requested_platform, | |
| 518 | + 'Accept' => 'application/xml, text/xml', | |
| 532 | 519 | ]; |
| 533 | 520 | $response = Helper::make_api_get_request("v2/$api_endpoint", [], $extra_headers, 30); |
| 534 | 521 | |
| 535 | 522 | // Check for HTTP errors |
| 536 | 523 | 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()]); | |
| 524 | + return $this->error('api_request_failed', __('Failed to fetch attachments from API.', 'templately'), 'get_attachments', 500, $response->get_error_message()); | |
| 538 | 525 | } |
| 539 | 526 | |
| 540 | 527 | $response_code = wp_remote_retrieve_response_code($response); |
| 541 | 528 | $xml_content = wp_remote_retrieve_body($response); |
| @@ -558,9 +545,9 @@ | ||
| 558 | 545 | // Parse the XML content from API response |
| 559 | 546 | $parsed_data = $this->parse_xml_content($xml_content); |
| 560 | 547 | |
| 561 | 548 | 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()]); | |
| 549 | + return $this->error('xml_parse_error', __('Failed to parse XML content.', 'templately'), 'get_attachments', 500, $parsed_data->get_error_message()); | |
| 563 | 550 | } |
| 564 | 551 | |
| 565 | 552 | // Extract attachments from parsed data |
| 566 | 553 | $attachments = $this->extract_attachments_from_parsed_data($parsed_data); |
| @@ -571,9 +558,9 @@ | ||
| 571 | 558 | 'message' => sprintf(__('Found %d attachments.', 'templately'), count($attachments)), |
| 572 | 559 | ]; |
| 573 | 560 | |
| 574 | 561 | } catch (Exception $e) { |
| 575 | - 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()); | |
| 576 | 563 | } |
| 577 | 564 | } |
| 578 | 565 | |
| 579 | 566 | |
| @@ -581,9 +568,9 @@ | ||
| 581 | 568 | /** |
| 582 | 569 | * Parse XML content string using WXR Parser |
| 583 | 570 | * |
| 584 | 571 | * @param string $xml_content XML content string |
| 585 | - * @return array|\WP_Error Parsed data or error | |
| 572 | + * @return array|WP_Error Parsed data or error | |
| 586 | 573 | */ |
| 587 | 574 | private function parse_xml_content($xml_content) { |
| 588 | 575 | // Ensure WordPress filesystem functions are available |
| 589 | 576 | if (!function_exists('wp_tempnam')) { |
| @@ -837,33 +824,180 @@ | ||
| 837 | 824 | 'total_pages' => $total_results > 0 ? ceil($total_results / $per_page_count) : 0, |
| 838 | 825 | ]); |
| 839 | 826 | } |
| 840 | 827 | |
| 828 | + /** | |
| 829 | + * Generate logo using AI | |
| 830 | + * | |
| 831 | + * @return array|WP_Error | |
| 832 | + */ | |
| 833 | + public function generate_logo() { | |
| 834 | + // Get parameters | |
| 835 | + $business_name = $this->get_param('business_name', ''); | |
| 836 | + $description = $this->get_param('description'); | |
| 837 | + $logo_type = $this->get_param('logo_type'); | |
| 838 | + $requested_platform = $this->get_param('requested_platform', 'templately'); | |
| 841 | 839 | |
| 840 | + // Validate required parameters | |
| 841 | + if (empty($description)) { | |
| 842 | + return $this->error( | |
| 843 | + 'missing_description', | |
| 844 | + __('Description is required for logo generation.', 'templately'), | |
| 845 | + 'generate_logo', | |
| 846 | + 400 | |
| 847 | + ); | |
| 848 | + } | |
| 842 | 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 | + | |
| 859 | + // Prepare request body | |
| 860 | + $body_data = [ | |
| 861 | + 'business_name' => $business_name, | |
| 862 | + 'description' => $description, | |
| 863 | + 'logo_type' => $logo_type, | |
| 864 | + ]; | |
| 865 | + | |
| 866 | + // Make API request | |
| 867 | + $extra_headers = [ | |
| 868 | + 'Content-Type' => 'application/json', | |
| 869 | + 'x-templately-requested-platform' => $requested_platform, | |
| 870 | + ]; | |
| 871 | + | |
| 872 | + $response = Helper::make_api_post_request('v2/generate-logo', $body_data, $extra_headers, 60); | |
| 873 | + | |
| 874 | + // Handle API response errors | |
| 875 | + if (is_wp_error($response)) { | |
| 876 | + return $this->error( | |
| 877 | + 'api_request_failed', | |
| 878 | + __('Something went wrong. Please try again or contact support.', 'templately'), | |
| 879 | + 'generate_logo', | |
| 880 | + 500, | |
| 881 | + $response->get_error_message() | |
| 882 | + ); | |
| 883 | + } | |
| 884 | + | |
| 885 | + $response_code = wp_remote_retrieve_response_code($response); | |
| 886 | + $response_body = wp_remote_retrieve_body($response); | |
| 887 | + | |
| 888 | + if ($response_code !== 200) { | |
| 889 | + // Try to parse the response body as JSON to get specific error details | |
| 890 | + $data = json_decode($response_body, true); | |
| 891 | + | |
| 892 | + // If valid JSON, extract error message and return with proper status code | |
| 893 | + if (json_last_error() === JSON_ERROR_NONE && is_array($data)) { | |
| 894 | + $error_message = isset($data['message']) ? $data['message'] : __('Something went wrong. Please try again or contact support.', 'templately'); | |
| 895 | + return $this->error( | |
| 896 | + 'api_response_error', | |
| 897 | + $error_message, | |
| 898 | + 'generate_logo', | |
| 899 | + $response_code | |
| 900 | + ); | |
| 901 | + } | |
| 902 | + | |
| 903 | + // Otherwise, return generic error | |
| 904 | + return $this->error( | |
| 905 | + 'api_response_error', | |
| 906 | + __('Something went wrong. Please try again or contact support.', 'templately'), | |
| 907 | + 'generate_logo', | |
| 908 | + $response_code | |
| 909 | + ); | |
| 910 | + } | |
| 911 | + | |
| 912 | + // Parse and validate response | |
| 913 | + $data = json_decode($response_body, true); | |
| 914 | + if (json_last_error() !== JSON_ERROR_NONE) { | |
| 915 | + return $this->error( | |
| 916 | + 'invalid_response', | |
| 917 | + __('Invalid response from API.', 'templately'), | |
| 918 | + 'generate_logo', | |
| 919 | + 500 | |
| 920 | + ); | |
| 921 | + } | |
| 922 | + | |
| 923 | + // Check if the response has the expected structure | |
| 924 | + if (!isset($data['status'])) { | |
| 925 | + return $this->error( | |
| 926 | + 'api_response_error', | |
| 927 | + __('API returned an unexpected response.', 'templately'), | |
| 928 | + 'generate_logo', | |
| 929 | + 500 | |
| 930 | + ); | |
| 931 | + } | |
| 932 | + | |
| 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 | |
| 963 | + return $data; | |
| 964 | + } | |
| 965 | + | |
| 843 | 966 | /** |
| 844 | - * Generate tagline using AI | |
| 967 | + * Generate meta (title or tagline) using AI | |
| 845 | 968 | * |
| 846 | 969 | * @return array|WP_Error |
| 847 | 970 | */ |
| 848 | - public function generate_tagline() { | |
| 971 | + public function generate_meta() { | |
| 849 | 972 | // Get parameters |
| 850 | - $prompt = $this->get_param('prompt'); | |
| 973 | + $field = $this->get_param('field'); | |
| 974 | + $text = $this->get_param('text'); | |
| 851 | 975 | $requested_platform = $this->get_param('requested_platform', 'templately'); |
| 852 | 976 | |
| 853 | 977 | // Validate required parameters |
| 854 | - if (empty($prompt)) { | |
| 978 | + if (empty($field)) { | |
| 855 | 979 | return $this->error( |
| 856 | - 'missing_prompt', | |
| 857 | - __('Prompt is required for tagline generation.', 'templately'), | |
| 858 | - 'generate_tagline', | |
| 980 | + 'missing_field', | |
| 981 | + __('Field is required.', 'templately'), | |
| 982 | + 'generate_meta', | |
| 859 | 983 | 400 |
| 860 | 984 | ); |
| 861 | 985 | } |
| 862 | 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 | + | |
| 863 | 996 | // Prepare request body |
| 864 | 997 | $body_data = [ |
| 865 | - 'prompt' => $prompt, | |
| 998 | + 'field' => $field, | |
| 999 | + 'text' => $text, | |
| 866 | 1000 | ]; |
| 867 | 1001 | |
| 868 | 1002 | // Make API request |
| 869 | 1003 | $extra_headers = [ |
| @@ -870,18 +1004,18 @@ | ||
| 870 | 1004 | 'Content-Type' => 'application/json', |
| 871 | 1005 | 'x-templately-requested-platform' => $requested_platform, |
| 872 | 1006 | ]; |
| 873 | 1007 | |
| 874 | - $response = Helper::make_api_post_request('v2/generate-tagline', $body_data, $extra_headers, 30); | |
| 1008 | + $response = Helper::make_api_post_request('v2/generate-meta', $body_data, $extra_headers, 30); | |
| 875 | 1009 | |
| 876 | 1010 | // Handle API response errors |
| 877 | 1011 | if (is_wp_error($response)) { |
| 878 | 1012 | return $this->error( |
| 879 | 1013 | 'api_request_failed', |
| 880 | - __('Failed to generate tagline.', 'templately'), | |
| 881 | - 'generate_tagline', | |
| 1014 | + __('Failed to generate meta.', 'templately'), | |
| 1015 | + 'generate_meta', | |
| 882 | 1016 | 500, |
| 883 | - ['error_detail' => $response->get_error_message()] | |
| 1017 | + $response->get_error_message() | |
| 884 | 1018 | ); |
| 885 | 1019 | } |
| 886 | 1020 | |
| 887 | 1021 | $response_code = wp_remote_retrieve_response_code($response); |
| @@ -896,9 +1030,9 @@ | ||
| 896 | 1030 | $error_message = isset($data['message']) ? $data['message'] : __('Something went wrong. Please try again or contact support.', 'templately'); |
| 897 | 1031 | return $this->error( |
| 898 | 1032 | 'api_response_error', |
| 899 | 1033 | $error_message, |
| 900 | - 'generate_tagline', | |
| 1034 | + 'generate_meta', | |
| 901 | 1035 | $response_code |
| 902 | 1036 | ); |
| 903 | 1037 | } |
| 904 | 1038 | |
| @@ -905,9 +1039,9 @@ | ||
| 905 | 1039 | // Otherwise, return generic error |
| 906 | 1040 | return $this->error( |
| 907 | 1041 | 'api_response_error', |
| 908 | 1042 | __('Something went wrong. Please try again or contact support.', 'templately'), |
| 909 | - 'generate_tagline', | |
| 1043 | + 'generate_meta', | |
| 910 | 1044 | $response_code |
| 911 | 1045 | ); |
| 912 | 1046 | } |
| 913 | 1047 | |
| @@ -916,9 +1050,9 @@ | ||
| 916 | 1050 | if (json_last_error() !== JSON_ERROR_NONE) { |
| 917 | 1051 | return $this->error( |
| 918 | 1052 | 'invalid_response', |
| 919 | 1053 | __('Invalid response from API.', 'templately'), |
| 920 | - 'generate_tagline', | |
| 1054 | + 'generate_meta', | |
| 921 | 1055 | 500 |
| 922 | 1056 | ); |
| 923 | 1057 | } |
| 924 | 1058 | |
| @@ -926,9 +1060,9 @@ | ||
| 926 | 1060 | if (!isset($data['status'])) { |
| 927 | 1061 | return $this->error( |
| 928 | 1062 | 'api_response_error', |
| 929 | 1063 | __('API returned an unexpected response.', 'templately'), |
| 930 | - 'generate_tagline', | |
| 1064 | + 'generate_meta', | |
| 931 | 1065 | 500 |
| 932 | 1066 | ); |
| 933 | 1067 | } |
| 934 | 1068 | |