PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.5.4
JetFormBuilder — Dynamic Blocks Form Builder v3.5.4
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 8 months ago
generate-form-endpoint.php
93 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_Private_Endpoint_Base;
14 use JFB_Modules\Ai\External_Api\Generate_Form_Action;
15
16 class Generate_Form_Endpoint extends Rest_Api_Private_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( $this->get_license() );
32
33 try {
34 $response = $action->send_request();
35 } catch ( Gateway_Exception $exception ) {
36 return new \WP_REST_Response(
37 array(
38 'message' => $exception->getMessage(),
39 'data' => $exception->get_additional(),
40 ),
41 400
42 );
43 }
44
45 if ( empty( $response['success'] ) || empty( $response['data']['completion'] ) ) {
46 return new \WP_REST_Response(
47 array(
48 'message' => $response['data'] ?? __( 'Something went wrong', 'jet-form-builder' ),
49 'data' => $response,
50 ),
51 400
52 );
53 }
54
55 return new \WP_REST_Response(
56 array(
57 'form' => $response['data']['completion'],
58 'usage' => $response['data']['usage'],
59 'limit' => $response['data']['limit'],
60 )
61 );
62 }
63
64 private function get_license(): string {
65 $server_ip = Http_Tools::get_server_ip_address();
66
67 // Define local IP address ranges
68 $local_ip_ranges = array(
69 '127.', // Loopback
70 '192.168.', // Private network
71 '10.', // Private network
72 );
73
74 // Check if the server IP is within any of the local ranges
75 foreach ( $local_ip_ranges as $range ) {
76 if ( 0 === strpos( $server_ip, $range ) ) {
77 return md5( $server_ip . site_url() ); // It's a local IP address
78 }
79 }
80
81 /**
82 * Add unique site suffix from LOGGED_IN_SALT
83 *
84 * @see https://github.com/Crocoblock/issues-tracker/issues/14825
85 */
86 if ( defined( 'LOGGED_IN_SALT' ) ) {
87 $server_ip .= LOGGED_IN_SALT;
88 }
89
90 return md5( $server_ip ); // It's not a local IP address
91 }
92 }
93