PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.7.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.7.1
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-helper.php

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

286 lines 9.3 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 - Helper.
4 *
5 * This file contains the helper functions of SureForms AI Form Builder.
6 * Helpers are functions that are used throughout the library.
7 *
8 * @package sureforms
9 * @since 0.0.8
10 */
11
12 namespace SRFM\Inc\AI_Form_Builder;
13
14 use SRFM\Inc\Traits\Get_Instance;
15 use SRFM_Pro\Admin\Licensing;
16
17 // Exit if accessed directly.
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * The Helper Class.
24 */
25 class AI_Helper {
26 use Get_Instance;
27
28 /**
29 * Get the SureForms AI Response from the SureForms Credit Server.
30 *
31 * @param array<mixed> $body The data to be passed as the request body, if any.
32 * @param array<mixed> $extra_args Extra arguments to be passed to the request, if any.
33 * @since 0.0.8
34 * @return array<array<array<array<mixed>>>|string>|mixed The SureForms AI Response.
35 */
36 public static function get_chat_completions_response( $body = [], $extra_args = [] ) {
37 // Set the API URL.
38 $api_url = SRFM_AI_MIDDLEWARE . 'generate/form';
39
40 $api_args = [
41 'headers' => [
42 'X-Token' => base64_encode( self::get_user_token() ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- This is not for obfuscation.
43 'Content-Type' => 'application/json',
44 'Referer' => site_url(),
45 ],
46 'timeout' => 90, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- 90 seconds is required sometime for open ai responses
47 ];
48
49 // If the data array was passed, add it to the args.
50 if ( ! empty( $body ) && is_array( $body ) ) {
51 $api_args['body'] = wp_json_encode( $body );
52 }
53
54 // If there are any extra arguments, then we can overwrite the required arguments.
55 if ( ! empty( $extra_args ) && is_array( $extra_args ) ) {
56 $api_args = array_merge( $api_args, $extra_args );
57 }
58
59 // Get the response from the endpoint.
60 $response = wp_remote_post( $api_url, $api_args );
61
62 // If the response was an error, or not a 200 status code, then abandon ship.
63 if ( is_wp_error( $response ) || empty( $response['response'] ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
64 return self::get_error_message( $response );
65 }
66
67 // Get the response body.
68 $response_body = wp_remote_retrieve_body( $response );
69
70 // If the response body is not a JSON, then abandon ship.
71 if ( empty( $response_body ) || ! json_decode( $response_body ) ) {
72 return [
73 'error' => __( 'The SureForms AI Middleware encountered an error.', 'sureforms' ),
74 ];
75 }
76
77 // Return the response body.
78 return json_decode( $response_body, true );
79 }
80
81 /**
82 * Get the SureForms Token from the SureForms AI Settings.
83 *
84 * @since 0.0.8
85 * @return array<mixed>|void The SureForms Token.
86 */
87 public static function get_current_usage_details() {
88 $current_usage_details = [];
89
90 // Get the response from the endpoint.
91 $response = self::get_usage_response();
92
93 // check if response is an array if not then send error.
94 if ( ! is_array( $response ) ) {
95 wp_send_json_error( [ 'message' => __( 'Unable to get usage response.', 'sureforms' ) ] );
96 }
97
98 // If the response is not an error, then use it - else create an error response array.
99 if ( empty( $response['error'] ) && is_array( $response ) ) {
100 $current_usage_details = $response;
101 if ( empty( $current_usage_details['status'] ) ) {
102 $current_usage_details['status'] = 'ok';
103 }
104 } else {
105 $current_usage_details['status'] = 'error';
106 if ( ! empty( $response['error'] ) ) {
107 $current_usage_details['error'] = $response['error'];
108 }
109 }
110
111 return $current_usage_details;
112 }
113
114 /**
115 * Get a response from the SureForms API server.
116 *
117 * @since 0.0.8
118 * @return array<mixed>|mixed The SureForms API Response.
119 */
120 public static function get_usage_response() {
121 // Set the API URL.
122 $api_url = SRFM_AI_MIDDLEWARE . 'usage';
123
124 // Get the response from the endpoint.
125 $response = wp_remote_post(
126 $api_url,
127 [
128 'headers' => [
129 'X-Token' => base64_encode( self::get_user_token() ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- This is not for obfuscation.
130 'Content-Type' => 'application/json',
131 'Referer' => site_url(),
132 ],
133 'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- 30 seconds is required sometime for the SureForms API response
134 ]
135 );
136
137 // If the response was an error, or not a 200 status code, then abandon ship.
138 if ( is_wp_error( $response ) || empty( $response['response'] ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
139 return self::get_error_message( $response );
140 }
141
142 // Get the response body.
143 $response_body = wp_remote_retrieve_body( $response );
144
145 // If the response body is not a JSON, then abandon ship.
146 if ( empty( $response_body ) || ! json_decode( $response_body ) ) {
147 return [
148 'error' => __( 'The SureForms API server encountered an error.', 'sureforms' ),
149 ];
150 }
151
152 // Return the response body.
153 return json_decode( $response_body, true );
154 }
155
156 /**
157 * Get the Error Message.
158 *
159 * @param array<string,mixed>|array<int|string,mixed>|\WP_Error $response The response from the SureForms API server.
160 * @since 0.0.10
161 * @return array<string, mixed> The Error Message.
162 */
163 public static function get_error_message( $response ) {
164 $errors = $response->errors ?? [];
165
166 if ( empty( $errors )
167 && is_array( $response ) && isset( $response['body'] ) && is_string( $response['body'] )
168 ) {
169 $errors = json_decode( $response['body'], true );
170 $error_key = is_array( $errors ) && isset( $errors['code'] ) ? $errors['code'] : '';
171 } else {
172 $error_key = array_key_first( $errors );
173 if ( empty( $errors[ $error_key ] ) ) {
174 $message = __( 'An unknown error occurred.', 'sureforms' );
175 }
176 }
177
178 // Error Codes with Messages.
179 switch ( $error_key ) {
180 case 'http_request_failed':
181 $title = __( 'HTTP Request Failed', 'sureforms' );
182 $message = __( 'Unable to connect to SureForms API. Please check your connection.', 'sureforms' );
183 break;
184 case 'license_verification_failed':
185 $title = __( 'License Verification Failed', 'sureforms' );
186 $message = __( 'Unable to verify license. Please check your license key.', 'sureforms' );
187 break;
188 case 'user_verification_failed':
189 $title = __( 'User Verification Failed', 'sureforms' );
190 $message = __( 'An error occurred while trying to verify your email. Please check your email you have used to log in or sign up on billing.sureforms.com.', 'sureforms' );
191 break;
192 case 'referer_mismatch':
193 $title = __( 'Referer Mismatch', 'sureforms' );
194 $message = __( 'Unable to verify referer. Please check your referer.', 'sureforms' );
195 break;
196 case 'invalid_token':
197 $title = __( 'Invalid Website URL', 'sureforms' );
198 $message = __( 'AI Form Builder does not work on localhost. Please try on a live website.', 'sureforms' );
199 break;
200 case 'domain_verification_failed':
201 $title = __( 'Domain Verification Failed', 'sureforms' );
202 $message = __( 'Domain Verification Failed on current site. Please try again on another website.', 'sureforms' );
203 break;
204 default:
205 $title = __( 'Unknown Error', 'sureforms' );
206 $message = __( 'An unknown error occurred.', 'sureforms' );
207 }
208
209 return [
210 'code' => $error_key,
211 'title' => $title,
212 'message' => $message,
213 ];
214 }
215
216 /**
217 * Check if the SureForms Pro license is active.
218 *
219 * @since 0.0.10
220 * @return bool|string True if the SureForms Pro license is active, false otherwise.
221 */
222 public static function is_pro_license_active() {
223 $licensing = self::get_licensing_instance();
224 if ( ! $licensing || ! method_exists( $licensing, 'is_license_active' )
225 ) {
226 return '';
227 }
228 // Check if the SureForms Pro license is active.
229 return $licensing->is_license_active();
230 }
231
232 /**
233 * Get the User Token.
234 *
235 * @since 0.0.8
236 * @return string The User Token.
237 */
238 private static function get_user_token() {
239 // if the license is active then use the license key as the token.
240 if ( defined( 'SRFM_PRO_VER' ) ) {
241 $license_key = self::get_license_key();
242 if ( ! empty( $license_key ) ) {
243 return $license_key;
244 }
245 }
246
247 $user_email = get_option( 'srfm_ai_auth_user_email' );
248
249 // if the license is not active then use the user email/site url as the token.
250 return ! empty( $user_email ) && is_array( $user_email ) ? $user_email['user_email'] : site_url();
251 }
252
253 /**
254 * Get the Licensing Instance.
255 *
256 * @since 0.0.10
257 * @return object|null The Licensing Instance.
258 */
259 private static function get_licensing_instance() {
260 if ( ! class_exists( 'SRFM_Pro\Admin\Licensing' ) ) {
261 return null;
262 }
263 return Licensing::get_instance();
264 }
265
266 /**
267 * Get the SureForms Pro License Key.
268 *
269 * @since 0.0.10
270 * @return string The SureForms Pro License Key.
271 */
272 private static function get_license_key() {
273 $licensing = self::get_licensing_instance();
274 if ( ! $licensing ||
275 ! method_exists( $licensing, 'licensing_setup' ) || ! method_exists( $licensing->licensing_setup(), 'settings' ) ) {
276 return '';
277 }
278 // Check if the SureForms Pro license is active.
279 $is_license_active = self::is_pro_license_active();
280 // If the license is active, get the license key.
281 $license_setup = $licensing->licensing_setup();
282 return ! empty( $is_license_active ) && is_object( $license_setup ) && method_exists( $license_setup, 'settings' ) ? $license_setup->settings()->license_key : '';
283 }
284
285 }
286