PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.6
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.6
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
← All changes | includes/API/AIContent.php +39 -274 3.6.53.4.6 View file →
@@ -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,18 +68,8 @@
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']);
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 - ]);
102 72 $this->get($this->endpoint . '/attachments', [$this, 'get_attachments'], [
103 73 'type' => [
104 74 'default' => 'pack',
105 75 'required' => false,
@@ -188,9 +158,9 @@
188 158
189 159
190 160 // return $response;
191 161 if (is_wp_error($response)) {
192 - return $this->error('request_failed', __('Request failed.', 'templately'), 'calculate_credit', 500, ['error_detail' => $response->get_error_message()]);
162 + return $this->error('request_failed', __('Request failed.', 'templately'), 'calculate_credit', 500, $response);
193 163 }
194 164
195 165 $body = wp_remote_retrieve_body($response);
196 166 $data = json_decode($body, true);
@@ -212,31 +182,20 @@
212 182 $isBusinessNichesNew = $this->get_param('isBusinessNichesNew', false);
213 183 $ai_page_ids = $this->get_param('ai_page_ids', [], null);
214 184 $content_ids = $this->get_param('content_ids', [], null);
215 185 $session_id = $this->get_param('session_id'); // Add session_id parameter
216 -
217 - // Security: Sanitize session_id if provided
218 - if (!empty($session_id)) {
219 - $session_id = AIUtils::sanitize_path_component($session_id, 'session_id');
220 - if (is_wp_error($session_id)) {
221 - return $session_id;
222 - }
223 - }
224 -
225 186 $preview_pages = $this->get_param('preview_pages', [], null);
226 187 $image_replace = $this->get_param('imageReplace', [], null);
227 188 $platform = $this->get_param('platform');
228 - $language = $this->get_param('language', null);
229 189
230 190 // ai content fields
231 - $name = $this->get_param('name');
232 - $category = $this->get_param('category');
233 - $description = $this->get_param('description');
234 - $email = $this->get_param('email');
235 - $contactNumber = $this->get_param('contactNumber');
236 - $businessAddress = $this->get_param('businessAddress');
237 - $openingHour = $this->get_param('openingHour');
238 - $requested_platform = $this->get_param('requested_platform', 'templately');
191 + $name = $this->get_param('name');
192 + $category = $this->get_param('category');
193 + $description = $this->get_param('description');
194 + $email = $this->get_param('email');
195 + $contactNumber = $this->get_param('contactNumber');
196 + $businessAddress = $this->get_param('businessAddress');
197 + $openingHour = $this->get_param('openingHour');
239 198
240 199 if (empty($pack_id)) {
241 200 return $this->error('invalid_id', __('Invalid ID.', 'templately'), 'modify_content', 400);
242 201 }
@@ -254,11 +213,10 @@
254 213 // $response = get_transient( '__templately_ai_process_id' );
255 214
256 215 // if(empty($response)) {
257 216 $extra_headers = [
258 - 'Accept' => 'application/json',
259 - 'x-templately-session-id' => $session_id,
260 - 'x-templately-requested-platform' => $requested_platform,
217 + 'Accept' => 'application/json',
218 + 'x-templately-session-id' => $session_id,
261 219 ];
262 220 $body_data = [
263 221 'business_name' => $name,
264 222 'business_niches' => $category,
@@ -270,20 +228,10 @@
270 228 'pack_id' => $pack_id,
271 229 'content_ids' => $content_ids,
272 230 'platform' => $platform,
273 231 'preview_pages' => $preview_pages,
274 - 'language' => $language,
275 232 'callback' => defined('TEMPLATELY_CALLBACK') ? TEMPLATELY_CALLBACK . '/wp-json/templately/v1/ai-content/ai-update' : rest_url('templately/v1/ai-content/ai-update'),
276 233 ];
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 234 $response = Helper::make_api_post_request('v2/ai/modify-content/pack', $body_data, $extra_headers, 15 * MINUTE_IN_SECONDS);
287 235
288 236 // set_transient( '__templately_ai_process_id', $response, 60 * 60 * 24 * 30 );
289 237 // }
@@ -296,9 +244,9 @@
296 244
297 245 // return $response;
298 246 if (is_wp_error($response)) {
299 247 error_log(print_r($response, true));
300 - return $this->error('request_failed', __('Request failed.', 'templately'), 'modify_content', 500, ['error_data' => $response->get_error_data()]);
248 + return $this->error('request_failed', __('Request failed.', 'templately'), 'modify_content', 500, $response->additional_data);
301 249 }
302 250
303 251 $body = wp_remote_retrieve_body($response);
304 252 $data = json_decode($body, true);
@@ -303,9 +251,9 @@
303 251 $body = wp_remote_retrieve_body($response);
304 252 $data = json_decode($body, true);
305 253 // error status is ok, if status is error then return as is
306 254 if (! is_array($data) || ! isset($data['status'])) {
307 - return $this->error('invalid_response', __('Invalid response.', 'templately'), 'modify_content', 500, ['data' => $data]);
255 + return $this->error('invalid_response', __('Invalid response.', 'templately'), 'modify_content', 500, $data);
308 256 }
309 257
310 258 // "{"status":"success","message":"The content is being generated in the queue","process_id":"01JRQQD39GNWTNF18EWF8YH0BG-271838-pack-408"}"
311 259 if (isset($data['status']) && $data['status'] === 'success' && isset($data['process_id'])) {
@@ -334,27 +282,25 @@
334 282
335 283 $user = $this->utils('options')->get('user');
336 284
337 285 $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,
286 + 'name' => $name,
287 + 'category' => $category,
288 + 'description' => $description,
289 + 'email' => $email,
290 + 'contactNumber' => $contactNumber,
291 + 'businessAddress' => $businessAddress,
292 + 'openingHour' => $openingHour,
293 + 'process_id' => $process_id,
294 + 'pack_id' => $pack_id,
295 + 'ai_page_ids' => $ai_page_ids,
296 + 'ai_preview_ids' => $preview_pages,
297 + 'content_ids' => $content_ids,
298 + 'platform' => $platform,
299 + 'api_key' => $this->api_key,
300 + 'user_id' => isset($user['id']) ? $user['id'] : null,
301 + 'session_id' => $session_id, // Store session_id for coordination
302 + 'imageReplace' => $image_replace, // Store session_id for coordination
357 303 ];
358 304
359 305 // Update using API key-based storage with automatic count-based cleanup
360 306 AIUtils::update_ai_process_data($ai_process_data);
@@ -511,20 +457,17 @@
511 457 ),
512 458 ];
513 459 }
514 460
515 -
516 -
517 461 /**
518 462 * Get attachments from API endpoint
519 463 *
520 - * @return array|\WP_Error
464 + * @return array
521 465 */
522 466 public function get_attachments() {
523 467 // 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');
468 + $type = $this->get_param('type', 'pack');
469 + $id = $this->get_param('pack_id');
527 470
528 471 // Require ID parameter - return error if not provided
529 472 if (empty($id)) {
530 473 return $this->error('missing_id', __('Pack ID or ID parameter is required.', 'templately'), 'get_attachments', 400);
@@ -535,16 +478,15 @@
535 478 $api_endpoint = "get-xml-attachment/{$type}/{$id}";
536 479
537 480 // Make API call
538 481 $extra_headers = [
539 - 'Accept' => 'application/xml, text/xml',
540 - 'x-templately-requested-platform' => $requested_platform,
482 + 'Accept' => 'application/xml, text/xml',
541 483 ];
542 484 $response = Helper::make_api_get_request("v2/$api_endpoint", [], $extra_headers, 30);
543 485
544 486 // Check for HTTP errors
545 487 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()]);
488 + return $this->error('api_request_failed', __('Failed to fetch attachments from API.', 'templately'), 'get_attachments', 500, $response->get_error_message());
547 489 }
548 490
549 491 $response_code = wp_remote_retrieve_response_code($response);
550 492 $xml_content = wp_remote_retrieve_body($response);
@@ -567,9 +509,9 @@
567 509 // Parse the XML content from API response
568 510 $parsed_data = $this->parse_xml_content($xml_content);
569 511
570 512 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()]);
513 + return $this->error('xml_parse_error', __('Failed to parse XML content.', 'templately'), 'get_attachments', 500, $parsed_data->get_error_message());
572 514 }
573 515
574 516 // Extract attachments from parsed data
575 517 $attachments = $this->extract_attachments_from_parsed_data($parsed_data);
@@ -580,9 +522,9 @@
580 522 'message' => sprintf(__('Found %d attachments.', 'templately'), count($attachments)),
581 523 ];
582 524
583 525 } catch (Exception $e) {
584 - return $this->error('exception', __('An unexpected error occurred while fetching attachments.', 'templately'), 'get_attachments', 500, ['error_detail' => $e->getMessage()]);
526 + return $this->error('exception', __('An unexpected error occurred while fetching attachments.', 'templately'), 'get_attachments', 500, $e->getMessage());
585 527 }
586 528 }
587 529
588 530
@@ -590,9 +532,9 @@
590 532 /**
591 533 * Parse XML content string using WXR Parser
592 534 *
593 535 * @param string $xml_content XML content string
594 - * @return array|\WP_Error Parsed data or error
536 + * @return array|WP_Error Parsed data or error
595 537 */
596 538 private function parse_xml_content($xml_content) {
597 539 // Ensure WordPress filesystem functions are available
598 540 if (!function_exists('wp_tempnam')) {
@@ -844,183 +786,6 @@
844 786 'page' => $current_page,
845 787 'per_page' => $per_page_count,
846 788 'total_pages' => $total_results > 0 ? ceil($total_results / $per_page_count) : 0,
847 789 ]);
848 - }
849 -
850 -
851 -
852 - /**
853 - * Generate tagline using AI
854 - *
855 - * @return array|WP_Error
856 - */
857 - public function generate_tagline() {
858 - // Get parameters
859 - $prompt = $this->get_param('prompt');
860 - $requested_platform = $this->get_param('requested_platform', 'templately');
861 -
862 - // Validate required parameters
863 - if (empty($prompt)) {
864 - return $this->error(
865 - 'missing_prompt',
866 - __('Prompt is required for tagline generation.', 'templately'),
867 - 'generate_tagline',
868 - 400
869 - );
870 - }
871 -
872 - // Prepare request body
873 - $body_data = [
874 - 'prompt' => $prompt,
875 - ];
876 -
877 - // Make API request
878 - $extra_headers = [
879 - 'Content-Type' => 'application/json',
880 - 'x-templately-requested-platform' => $requested_platform,
881 - ];
882 -
883 - $response = Helper::make_api_post_request('v2/generate-tagline', $body_data, $extra_headers, 30);
884 -
885 - // Handle API response errors
886 - if (is_wp_error($response)) {
887 - return $this->error(
888 - 'api_request_failed',
889 - __('Failed to generate tagline.', 'templately'),
890 - 'generate_tagline',
891 - 500,
892 - ['error_detail' => $response->get_error_message()]
893 - );
894 - }
895 -
896 - $response_code = wp_remote_retrieve_response_code($response);
897 - $response_body = wp_remote_retrieve_body($response);
898 -
899 - if ($response_code !== 200) {
900 - // Try to parse the response body as JSON to get specific error details
901 - $data = json_decode($response_body, true);
902 -
903 - // If valid JSON, extract error message and return with proper status code
904 - if (json_last_error() === JSON_ERROR_NONE && is_array($data)) {
905 - $error_message = isset($data['message']) ? $data['message'] : __('Something went wrong. Please try again or contact support.', 'templately');
906 - return $this->error(
907 - 'api_response_error',
908 - $error_message,
909 - 'generate_tagline',
910 - $response_code
911 - );
912 - }
913 -
914 - // Otherwise, return generic error
915 - return $this->error(
916 - 'api_response_error',
917 - __('Something went wrong. Please try again or contact support.', 'templately'),
918 - 'generate_tagline',
919 - $response_code
920 - );
921 - }
922 -
923 - // Parse and validate response
924 - $data = json_decode($response_body, true);
925 - if (json_last_error() !== JSON_ERROR_NONE) {
926 - return $this->error(
927 - 'invalid_response',
928 - __('Invalid response from API.', 'templately'),
929 - 'generate_tagline',
930 - 500
931 - );
932 - }
933 -
934 - // Check if the response has the expected structure
935 - if (!isset($data['status'])) {
936 - return $this->error(
937 - 'api_response_error',
938 - __('API returned an unexpected response.', 'templately'),
939 - 'generate_tagline',
940 - 500
941 - );
942 - }
943 -
944 - // Return the response as-is
945 - return $data;
946 - }
947 -
948 - /**
949 - * Fetch a chatbot conversation by ID from the external Templately chatbot API.
950 - *
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.
955 - */
956 - public function get_chatbot_conversation() {
957 - $chat = $this->get_param('chat');
958 -
959 - if (empty($chat)) {
960 - return $this->error('invalid_chat_id', __('Invalid conversation ID.', 'templately'), 'ai-content/chatbot-conversation', 400);
961 - }
962 -
963 - $extra_headers = [
964 - 'Accept' => 'application/json',
965 - ];
966 - $response = Helper::make_api_get_request("v2/chatbot/conversation/{$chat}", [], $extra_headers, 30);
967 -
968 - 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()]);
970 - }
971 -
972 - $response_code = wp_remote_retrieve_response_code($response);
973 - $body = wp_remote_retrieve_body($response);
974 - $data = json_decode($body, true);
975 -
976 - 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);
979 - }
980 -
981 - if (!is_array($data) || !isset($data['status'])) {
982 - return $this->error('invalid_response', __('Invalid response.', 'templately'), 'ai-content/chatbot-conversation', 500);
983 - }
984 -
985 - return $data;
986 - }
987 -
988 - /**
989 - * Validate API key against database
990 - * Checks if the provided API key exists for any user on the current site
991 - * Handles both single-site and multisite WordPress installations
992 - *
993 - * @param string $api_key The API key to validate
994 - * @return bool True if valid, false otherwise
995 - */
996 - private function validate_api_key_in_db($api_key) {
997 - global $wpdb;
998 -
999 - $api_key = sanitize_text_field($api_key);
1000 -
1001 - if (empty($api_key)) {
1002 - return false;
1003 - }
1004 -
1005 - $meta_key = '_templately_api_key';
1006 -
1007 - // Handle multisite: key will have site prefix in multisite
1008 - if (is_multisite()) {
1009 - // get_user_option() uses the format: {$wpdb->base_prefix}{$blog_id}_{$meta_key}
1010 - // For current blog, we need to check with the current blog prefix
1011 - $blog_id = get_current_blog_id();
1012 - $meta_key = $wpdb->get_blog_prefix($blog_id) . $meta_key;
1013 - }
1014 -
1015 - // Query to check if this API key exists for any user
1016 - $query = $wpdb->prepare(
1017 - "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s LIMIT 1",
1018 - $meta_key,
1019 - $api_key
1020 - );
1021 -
1022 - $user_id = $wpdb->get_var($query);
1023 -
1024 - return !empty($user_id);
1025 790 }
1026 791 }