PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.3.1
JetFormBuilder — Dynamic Blocks Form Builder v3.3.1
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 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.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / ai / rest-api / endpoints / generate-form-endpoint.php
jetformbuilder / modules / ai / rest-api / endpoints Last commit date
generate-form-endpoint.php 2 years ago
generate-form-endpoint.php
66 lines
1 <?php
2
3
4 namespace JFB_Modules\Ai\Rest_Api\Endpoints;
5
6 // If this file is called directly, abort.
7 if ( ! defined( 'WPINC' ) ) {
8 die;
9 }
10
11 use Jet_Form_Builder\Classes\Http\Http_Tools;
12 use Jet_Form_Builder\Exceptions\Gateway_Exception;
13 use JFB_Components\Rest_Api\Rest_Api_Endpoint_Base;
14 use JFB_Modules\Ai\External_Api\Generate_Form_Action;
15
16 class Generate_Form_Endpoint extends Rest_Api_Endpoint_Base {
17
18 public static function get_rest_base() {
19 return 'ai/generate';
20 }
21
22 public static function get_methods() {
23 return \WP_REST_Server::CREATABLE;
24 }
25
26 public function run_callback( \WP_REST_Request $request ) {
27 $action = new Generate_Form_Action();
28 $action->set_prompt(
29 $request->get_param( 'prompt' )
30 );
31 $action->set_license(
32 md5( Http_Tools::get_server_ip_address() )
33 );
34
35 try {
36 $response = $action->send_request();
37 } catch ( Gateway_Exception $exception ) {
38 return new \WP_REST_Response(
39 array(
40 'message' => $exception->getMessage(),
41 'data' => $exception->get_additional(),
42 ),
43 400
44 );
45 }
46
47 if ( empty( $response['success'] ) || empty( $response['data']['completion'] ) ) {
48 return new \WP_REST_Response(
49 array(
50 'message' => $response['data'] ?? __( 'Something went wrong', 'jet-form-builder' ),
51 'data' => $response,
52 ),
53 400
54 );
55 }
56
57 return new \WP_REST_Response(
58 array(
59 'form' => $response['data']['completion'],
60 'usage' => $response['data']['usage'],
61 'limit' => $response['data']['limit'],
62 )
63 );
64 }
65 }
66