PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.9.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.9.0
2.12.8 2.12.7 2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 All 98 releases
sureforms / inc / ai-form-builder / ai-form-builder.php

ai-form-builder.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 2.9.0, at inc/ai-form-builder/ai-form-builder.php

124 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SureForms - AI Form Builder.
4 *
5 * @package sureforms
6 * @since 0.0.8
7 */
8
9 namespace SRFM\Inc\AI_Form_Builder;
10
11 use SRFM\Inc\Traits\Get_Instance;
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * SureForms AI Form Builder Class.
20 */
21 class AI_Form_Builder {
22 use Get_Instance;
23
24 /**
25 * Fetches ai data from the middleware server
26 *
27 * @param \WP_REST_Request $request request object.
28 * @since 0.0.8
29 * @return void
30 */
31 public function generate_ai_form( $request ) {
32
33 // Get the params.
34 $params = $request->get_params();
35
36 // If the message array doesn't exist, abandon ship.
37 if ( empty( $params['message_array'] ) || ! is_array( $params['message_array'] ) ) {
38 wp_send_json_error( [ 'message' => __( 'The message array was not supplied', 'sureforms' ) ] );
39 }
40
41 // Set the token count to 0, and create messages array.
42 $messages = [];
43
44 // Start with the last message - going upwards until the token count hits 2000.
45 foreach ( array_reverse( $params['message_array'] ) as $current_message ) {
46 // If the message content doesn't exist, skip it.
47 if ( empty( $current_message['content'] ) ) {
48 continue;
49 }
50
51 // Add the message to the start of the messages to send to the SCS Middleware.
52 array_unshift( $messages, $current_message );
53 }
54
55 // Get the response from the endpoint.
56 $response = AI_Helper::get_chat_completions_response(
57 apply_filters(
58 'srfm_ai_form_generator_body',
59 [
60 'query' => $messages[0]['content'],
61 ],
62 $params
63 )
64 );
65
66 // check if response is an array if not then send error.
67 if ( ! is_array( $response ) ) {
68 wp_send_json_error( [ 'message' => __( 'The SureForms AI Middleware encountered an error.', 'sureforms' ) ] );
69 }
70
71 if ( ! empty( $response['error'] ) ) {
72 // If the response has an error, handle it and report it back.
73 // We sanitize before returning so OpenAI / middleware infra details
74 // (URLs, request IDs, model names, account IDs) do not leak to the
75 // client; the raw message is preserved in the debug log via
76 // AI_Helper::sanitize_ai_error_message() when WP_DEBUG[_LOG] is on.
77 $raw = '';
78 if ( is_array( $response['error'] ) && ! empty( $response['error']['message'] ) ) {
79 // If any error message received from OpenAI.
80 $raw = $response['error']['message'];
81 } elseif ( is_string( $response['error'] ) ) {
82 // If any error message received from the middleware server.
83 $raw = $response['error'];
84 }
85 $message = AI_Helper::sanitize_ai_error_message( $raw, 'generate/form' );
86 if ( '' === $message ) {
87 $message = __( 'The SureForms AI Middleware encountered an error.', 'sureforms' );
88 }
89 wp_send_json_error( [ 'message' => $message ] );
90 }
91
92 // Validate the expected form structure piece by piece so we can return specific errors.
93 if ( empty( $response['form'] ) || ! is_array( $response['form'] ) ) {
94 wp_send_json_error(
95 [
96 'message' => __( 'The AI did not return a form. Please refine your prompt and try again.', 'sureforms' ),
97 ]
98 );
99 }
100
101 if ( empty( $response['form']['formTitle'] ) ) {
102 wp_send_json_error(
103 [
104 'message' => __( 'The AI response is missing a form title. Please try again.', 'sureforms' ),
105 ]
106 );
107 }
108
109 if (
110 empty( $response['form']['formFields'] ) ||
111 ! is_array( $response['form']['formFields'] )
112 ) {
113 wp_send_json_error(
114 [
115 'message' => __( 'The AI was unable to generate form fields. Please try again.', 'sureforms' ),
116 ]
117 );
118 }
119
120 wp_send_json_success( $response );
121 }
122
123 }
124