PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.2
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 +38 -228 3.6.23.4.2 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,9 +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 72 $this->get($this->endpoint . '/attachments', [$this, 'get_attachments'], [
94 73 'type' => [
95 74 'default' => 'pack',
96 75 'required' => false,
@@ -179,9 +158,9 @@
179 158
180 159
181 160 // return $response;
182 161 if (is_wp_error($response)) {
183 - 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);
184 163 }
185 164
186 165 $body = wp_remote_retrieve_body($response);
187 166 $data = json_decode($body, true);
@@ -203,31 +182,20 @@
203 182 $isBusinessNichesNew = $this->get_param('isBusinessNichesNew', false);
204 183 $ai_page_ids = $this->get_param('ai_page_ids', [], null);
205 184 $content_ids = $this->get_param('content_ids', [], null);
206 185 $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 186 $preview_pages = $this->get_param('preview_pages', [], null);
217 187 $image_replace = $this->get_param('imageReplace', [], null);
218 188 $platform = $this->get_param('platform');
219 - $language = $this->get_param('language', null);
220 189
221 190 // ai content fields
222 - $name = $this->get_param('name');
223 - $category = $this->get_param('category');
224 - $description = $this->get_param('description');
225 - $email = $this->get_param('email');
226 - $contactNumber = $this->get_param('contactNumber');
227 - $businessAddress = $this->get_param('businessAddress');
228 - $openingHour = $this->get_param('openingHour');
229 - $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');
230 198
231 199 if (empty($pack_id)) {
232 200 return $this->error('invalid_id', __('Invalid ID.', 'templately'), 'modify_content', 400);
233 201 }
@@ -245,11 +213,10 @@
245 213 // $response = get_transient( '__templately_ai_process_id' );
246 214
247 215 // if(empty($response)) {
248 216 $extra_headers = [
249 - 'Accept' => 'application/json',
250 - 'x-templately-session-id' => $session_id,
251 - 'x-templately-requested-platform' => $requested_platform,
217 + 'Accept' => 'application/json',
218 + 'x-templately-session-id' => $session_id,
252 219 ];
253 220 $body_data = [
254 221 'business_name' => $name,
255 222 'business_niches' => $category,
@@ -261,20 +228,10 @@
261 228 'pack_id' => $pack_id,
262 229 'content_ids' => $content_ids,
263 230 'platform' => $platform,
264 231 'preview_pages' => $preview_pages,
265 - 'language' => $language,
266 232 'callback' => defined('TEMPLATELY_CALLBACK') ? TEMPLATELY_CALLBACK . '/wp-json/templately/v1/ai-content/ai-update' : rest_url('templately/v1/ai-content/ai-update'),
267 233 ];
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 234 $response = Helper::make_api_post_request('v2/ai/modify-content/pack', $body_data, $extra_headers, 15 * MINUTE_IN_SECONDS);
278 235
279 236 // set_transient( '__templately_ai_process_id', $response, 60 * 60 * 24 * 30 );
280 237 // }
@@ -287,9 +244,9 @@
287 244
288 245 // return $response;
289 246 if (is_wp_error($response)) {
290 247 error_log(print_r($response, true));
291 - 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);
292 249 }
293 250
294 251 $body = wp_remote_retrieve_body($response);
295 252 $data = json_decode($body, true);
@@ -294,9 +251,9 @@
294 251 $body = wp_remote_retrieve_body($response);
295 252 $data = json_decode($body, true);
296 253 // error status is ok, if status is error then return as is
297 254 if (! is_array($data) || ! isset($data['status'])) {
298 - 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);
299 256 }
300 257
301 258 // "{"status":"success","message":"The content is being generated in the queue","process_id":"01JRQQD39GNWTNF18EWF8YH0BG-271838-pack-408"}"
302 259 if (isset($data['status']) && $data['status'] === 'success' && isset($data['process_id'])) {
@@ -322,30 +279,25 @@
322 279 // }
323 280 // }
324 281 // }
325 282
326 - $user = $this->utils('options')->get('user');
327 -
328 283 $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,
284 + 'name' => $name,
285 + 'category' => $category,
286 + 'description' => $description,
287 + 'email' => $email,
288 + 'contactNumber' => $contactNumber,
289 + 'businessAddress' => $businessAddress,
290 + 'openingHour' => $openingHour,
291 + 'process_id' => $process_id,
292 + 'pack_id' => $pack_id,
293 + 'ai_page_ids' => $ai_page_ids,
294 + 'ai_preview_ids' => $preview_pages,
295 + 'content_ids' => $content_ids,
296 + 'platform' => $platform,
297 + 'api_key' => $this->api_key,
298 + 'session_id' => $session_id, // Store session_id for coordination
299 + 'imageReplace' => $image_replace, // Store session_id for coordination
348 300 ];
349 301
350 302 // Update using API key-based storage with automatic count-based cleanup
351 303 AIUtils::update_ai_process_data($ai_process_data);
@@ -502,20 +454,17 @@
502 454 ),
503 455 ];
504 456 }
505 457
506 -
507 -
508 458 /**
509 459 * Get attachments from API endpoint
510 460 *
511 - * @return array|\WP_Error
461 + * @return array
512 462 */
513 463 public function get_attachments() {
514 464 // 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');
465 + $type = $this->get_param('type', 'pack');
466 + $id = $this->get_param('pack_id');
518 467
519 468 // Require ID parameter - return error if not provided
520 469 if (empty($id)) {
521 470 return $this->error('missing_id', __('Pack ID or ID parameter is required.', 'templately'), 'get_attachments', 400);
@@ -526,16 +475,15 @@
526 475 $api_endpoint = "get-xml-attachment/{$type}/{$id}";
527 476
528 477 // Make API call
529 478 $extra_headers = [
530 - 'Accept' => 'application/xml, text/xml',
531 - 'x-templately-requested-platform' => $requested_platform,
479 + 'Accept' => 'application/xml, text/xml',
532 480 ];
533 481 $response = Helper::make_api_get_request("v2/$api_endpoint", [], $extra_headers, 30);
534 482
535 483 // Check for HTTP errors
536 484 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()]);
485 + return $this->error('api_request_failed', __('Failed to fetch attachments from API.', 'templately'), 'get_attachments', 500, $response->get_error_message());
538 486 }
539 487
540 488 $response_code = wp_remote_retrieve_response_code($response);
541 489 $xml_content = wp_remote_retrieve_body($response);
@@ -558,9 +506,9 @@
558 506 // Parse the XML content from API response
559 507 $parsed_data = $this->parse_xml_content($xml_content);
560 508
561 509 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()]);
510 + return $this->error('xml_parse_error', __('Failed to parse XML content.', 'templately'), 'get_attachments', 500, $parsed_data->get_error_message());
563 511 }
564 512
565 513 // Extract attachments from parsed data
566 514 $attachments = $this->extract_attachments_from_parsed_data($parsed_data);
@@ -571,9 +519,9 @@
571 519 'message' => sprintf(__('Found %d attachments.', 'templately'), count($attachments)),
572 520 ];
573 521
574 522 } catch (Exception $e) {
575 - return $this->error('exception', __('An unexpected error occurred while fetching attachments.', 'templately'), 'get_attachments', 500, ['error_detail' => $e->getMessage()]);
523 + return $this->error('exception', __('An unexpected error occurred while fetching attachments.', 'templately'), 'get_attachments', 500, $e->getMessage());
576 524 }
577 525 }
578 526
579 527
@@ -581,9 +529,9 @@
581 529 /**
582 530 * Parse XML content string using WXR Parser
583 531 *
584 532 * @param string $xml_content XML content string
585 - * @return array|\WP_Error Parsed data or error
533 + * @return array|WP_Error Parsed data or error
586 534 */
587 535 private function parse_xml_content($xml_content) {
588 536 // Ensure WordPress filesystem functions are available
589 537 if (!function_exists('wp_tempnam')) {
@@ -640,9 +588,8 @@
640 588 $attachment = [
641 589 'id' => isset($post['post_id']) ? (int) $post['post_id'] : 0,
642 590 'url' => isset($post['attachment_url']) ? (string) $post['attachment_url'] : '',
643 591 'title' => isset($post['post_title']) ? (string) $post['post_title'] : '',
644 - 'type' => isset($post['attachment_type']) ? (string) $post['attachment_type'] : '',
645 592 ];
646 593
647 594 // Extract metadata including dimensions and medium URL
648 595 $metadata = $this->extract_medium_size_url($post, $attachment['url']);
@@ -835,143 +782,6 @@
835 782 'page' => $current_page,
836 783 'per_page' => $per_page_count,
837 784 'total_pages' => $total_results > 0 ? ceil($total_results / $per_page_count) : 0,
838 785 ]);
839 - }
840 -
841 -
842 -
843 - /**
844 - * Generate tagline using AI
845 - *
846 - * @return array|WP_Error
847 - */
848 - public function generate_tagline() {
849 - // Get parameters
850 - $prompt = $this->get_param('prompt');
851 - $requested_platform = $this->get_param('requested_platform', 'templately');
852 -
853 - // Validate required parameters
854 - if (empty($prompt)) {
855 - return $this->error(
856 - 'missing_prompt',
857 - __('Prompt is required for tagline generation.', 'templately'),
858 - 'generate_tagline',
859 - 400
860 - );
861 - }
862 -
863 - // Prepare request body
864 - $body_data = [
865 - 'prompt' => $prompt,
866 - ];
867 -
868 - // Make API request
869 - $extra_headers = [
870 - 'Content-Type' => 'application/json',
871 - 'x-templately-requested-platform' => $requested_platform,
872 - ];
873 -
874 - $response = Helper::make_api_post_request('v2/generate-tagline', $body_data, $extra_headers, 30);
875 -
876 - // Handle API response errors
877 - if (is_wp_error($response)) {
878 - return $this->error(
879 - 'api_request_failed',
880 - __('Failed to generate tagline.', 'templately'),
881 - 'generate_tagline',
882 - 500,
883 - ['error_detail' => $response->get_error_message()]
884 - );
885 - }
886 -
887 - $response_code = wp_remote_retrieve_response_code($response);
888 - $response_body = wp_remote_retrieve_body($response);
889 -
890 - if ($response_code !== 200) {
891 - // Try to parse the response body as JSON to get specific error details
892 - $data = json_decode($response_body, true);
893 -
894 - // If valid JSON, extract error message and return with proper status code
895 - if (json_last_error() === JSON_ERROR_NONE && is_array($data)) {
896 - $error_message = isset($data['message']) ? $data['message'] : __('Something went wrong. Please try again or contact support.', 'templately');
897 - return $this->error(
898 - 'api_response_error',
899 - $error_message,
900 - 'generate_tagline',
901 - $response_code
902 - );
903 - }
904 -
905 - // Otherwise, return generic error
906 - return $this->error(
907 - 'api_response_error',
908 - __('Something went wrong. Please try again or contact support.', 'templately'),
909 - 'generate_tagline',
910 - $response_code
911 - );
912 - }
913 -
914 - // Parse and validate response
915 - $data = json_decode($response_body, true);
916 - if (json_last_error() !== JSON_ERROR_NONE) {
917 - return $this->error(
918 - 'invalid_response',
919 - __('Invalid response from API.', 'templately'),
920 - 'generate_tagline',
921 - 500
922 - );
923 - }
924 -
925 - // Check if the response has the expected structure
926 - if (!isset($data['status'])) {
927 - return $this->error(
928 - 'api_response_error',
929 - __('API returned an unexpected response.', 'templately'),
930 - 'generate_tagline',
931 - 500
932 - );
933 - }
934 -
935 - // Return the response as-is
936 - return $data;
937 - }
938 -
939 - /**
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
943 - *
944 - * @param string $api_key The API key to validate
945 - * @return bool True if valid, false otherwise
946 - */
947 - private function validate_api_key_in_db($api_key) {
948 - global $wpdb;
949 -
950 - $api_key = sanitize_text_field($api_key);
951 -
952 - if (empty($api_key)) {
953 - return false;
954 - }
955 -
956 - $meta_key = '_templately_api_key';
957 -
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;
964 - }
965 -
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 - );
972 -
973 - $user_id = $wpdb->get_var($query);
974 -
975 - return !empty($user_id);
976 786 }
977 787 }