PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.10
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.10
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 0.0.10, at inc/ai-form-builder/ai-helper.php

289 lines 9.4 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' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- 30 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 /**
116 * Get a response from the SureForms API server.
117 *
118 * @since 0.0.8
119 * @return array<mixed>|mixed The SureForms API Response.
120 */
121 public static function get_usage_response() {
122 // Set the API URL.
123 $api_url = SRFM_AI_MIDDLEWARE . 'usage';
124
125 // Get the response from the endpoint.
126 $response = wp_remote_post(
127 $api_url,
128 [
129 'headers' => [
130 'X-Token' => base64_encode( self::get_user_token() ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- This is not for obfuscation.
131 'Content-Type' => 'application/json',
132 'Referer' => site_url(),
133 ],
134 'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- 30 seconds is required sometime for the SureForms API response
135 ]
136 );
137
138 // If the response was an error, or not a 200 status code, then abandon ship.
139 if ( is_wp_error( $response ) || empty( $response['response'] ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
140 return self::get_error_message( $response );
141 }
142
143 // Get the response body.
144 $response_body = wp_remote_retrieve_body( $response );
145
146 // If the response body is not a JSON, then abandon ship.
147 if ( empty( $response_body ) || ! json_decode( $response_body ) ) {
148 return [
149 'error' => __( 'The SureForms API server encountered an error.', 'sureforms' ),
150 ];
151 }
152
153 // Return the response body.
154 return json_decode( $response_body, true );
155 }
156
157 /**
158 * Get the User Token.
159 *
160 * @since 0.0.8
161 * @return string The User Token.
162 */
163 private static function get_user_token() {
164 // if the license is active then use the license key as the token.
165 if ( defined( 'SRFM_PRO_VER' ) ) {
166 $license_key = self::get_license_key();
167 if ( ! empty( $license_key ) ) {
168 return $license_key;
169 }
170 }
171
172 $user_email = get_option( 'srfm_ai_auth_user_email' );
173
174 // if the license is not active then use the user email/site url as the token.
175 return ! empty( $user_email ) && is_array( $user_email ) ? $user_email['user_email'] : site_url();
176 }
177
178 /**
179 * Get the Error Message.
180 *
181 * @param array<string,mixed>|array<int|string,mixed>|\WP_Error $response The response from the SureForms API server.
182 * @since 0.0.10
183 * @return array<string, mixed> The Error Message.
184 */
185 public static function get_error_message( $response ) {
186 $errors = isset( $response->errors ) ? $response->errors : [];
187
188 if ( empty( $errors )
189 && is_array( $response ) && isset( $response['body'] ) && is_string( $response['body'] )
190 ) {
191 $errors = json_decode( $response['body'], true );
192 $error_key = is_array( $errors ) && isset( $errors['code'] ) ? $errors['code'] : '';
193 } else {
194 $error_key = array_key_first( $errors );
195 if ( empty( $errors[ $error_key ] ) ) {
196 $message = __( 'An unknown error occurred.', 'sureforms' );
197 }
198 }
199
200 // Error Codes with Messages.
201 switch ( $error_key ) {
202 case 'http_request_failed':
203 $title = __( 'HTTP Request Failed', 'sureforms' );
204 $message = __( 'An error occurred while trying to connect to the SureForms API server. Please check your connection', 'sureforms' );
205 break;
206 case 'license_verification_failed':
207 $title = __( 'License Verification Failed', 'sureforms' );
208 $message = __( 'An error occurred while trying to verify your license. Please check your license key', 'sureforms' );
209 break;
210 case 'user_verification_failed':
211 $title = __( 'User Verification Failed', 'sureforms' );
212 $message = __( 'An error occurred while trying to verify your email. Please check your email you have used to log in/ sign up on billing.sureforms.com.', 'sureforms' );
213 break;
214 case 'referer_mismatch':
215 $title = __( 'Referer Mismatch', 'sureforms' );
216 $message = __( 'An error occurred while trying to verify your referer. Please check your referer.', 'sureforms' );
217 break;
218 case 'invalid_token':
219 $title = __( 'Invalid Website URL', 'sureforms' );
220 $message = __( 'AI Form Builder does not work on localhost. Please try on a live website.', 'sureforms' );
221 break;
222 case 'domain_verification_failed':
223 $title = __( 'Domain Verification Failed', 'sureforms' );
224 $message = __( 'Domain Verification Failed on current site. Please try again on any another website.', 'sureforms' );
225 break;
226 default:
227 $title = __( 'Unknown Error', 'sureforms' );
228 $message = __( 'An unknown error occurred.', 'sureforms' );
229 }
230
231 return [
232 'code' => $error_key,
233 'title' => $title,
234 'message' => $message,
235 ];
236
237 }
238
239 /**
240 * Get the Licensing Instance.
241 *
242 * @since 0.0.10
243 * @return object|null The Licensing Instance.
244 */
245 private static function get_licensing_instance() {
246 if ( ! class_exists( 'SRFM_Pro\Admin\Licensing' ) ) {
247 return null;
248 }
249 return Licensing::get_instance();
250 }
251
252 /**
253 * Get the SureForms Pro License Key.
254 *
255 * @since 0.0.10
256 * @return string The SureForms Pro License Key.
257 */
258 private static function get_license_key() {
259 $licensing = self::get_licensing_instance();
260 if ( ! $licensing ||
261 ! method_exists( $licensing, 'licensing_setup' ) || ! method_exists( $licensing->licensing_setup(), 'settings' ) ) {
262 return '';
263 }
264 // Check if the SureForms Pro license is active.
265 $is_license_active = self::is_pro_license_active();
266 // If the license is active, get the license key.
267 $license_setup = $licensing->licensing_setup();
268 $license_key = ( ! empty( $is_license_active ) && is_object( $license_setup ) && method_exists( $license_setup, 'settings' ) ) ? $license_setup->settings()->license_key : '';
269 return $license_key;
270 }
271
272 /**
273 * Check if the SureForms Pro license is active.
274 *
275 * @since 0.0.10
276 * @return bool|string True if the SureForms Pro license is active, false otherwise.
277 */
278 public static function is_pro_license_active() {
279 $licensing = self::get_licensing_instance();
280 if ( ! $licensing || ! method_exists( $licensing, 'is_license_active' )
281 ) {
282 return '';
283 }
284 // Check if the SureForms Pro license is active.
285 return $licensing->is_license_active();
286 }
287
288 }
289