| 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 get_common_args(): array { |
| 27 |
return array( |
| 28 |
'prompt' => array( |
| 29 |
'type' => 'string', |
| 30 |
'required' => true, |
| 31 |
'validate_callback' => array( $this, 'validate_prompt' ), |
| 32 |
), |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param mixed $value |
| 38 |
* |
| 39 |
* @return bool|\WP_Error |
| 40 |
*/ |
| 41 |
public function validate_prompt( $value ) { |
| 42 |
if ( mb_strlen( (string) $value, 'UTF-8' ) > Generate_Form_Action::MAX_PROMPT_LENGTH ) { |
| 43 |
return new \WP_Error( |
| 44 |
'rest_invalid_param', |
| 45 |
sprintf( |
| 46 |
/* translators: %d: maximum allowed prompt length */ |
| 47 |
__( 'Prompt is too long. Maximum length is %d characters.', 'jet-form-builder' ), |
| 48 |
Generate_Form_Action::MAX_PROMPT_LENGTH |
| 49 |
), |
| 50 |
array( 'status' => 400 ) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
public function run_callback( \WP_REST_Request $request ) { |
| 58 |
$action = new Generate_Form_Action(); |
| 59 |
$action->set_prompt( |
| 60 |
$request->get_param( 'prompt' ) |
| 61 |
); |
| 62 |
$action->set_license( $this->get_license() ); |
| 63 |
|
| 64 |
try { |
| 65 |
$response = $action->send_request(); |
| 66 |
} catch ( Gateway_Exception $exception ) { |
| 67 |
return new \WP_REST_Response( |
| 68 |
array( |
| 69 |
'message' => $exception->getMessage(), |
| 70 |
'data' => $exception->get_additional(), |
| 71 |
), |
| 72 |
400 |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
if ( empty( $response['success'] ) || empty( $response['data']['completion'] ) ) { |
| 77 |
return new \WP_REST_Response( |
| 78 |
array( |
| 79 |
'message' => $response['data'] ?? __( 'Something went wrong', 'jet-form-builder' ), |
| 80 |
'data' => $response, |
| 81 |
), |
| 82 |
400 |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
return new \WP_REST_Response( |
| 87 |
array( |
| 88 |
'form' => $response['data']['completion'], |
| 89 |
'usage' => $response['data']['usage'], |
| 90 |
'limit' => $response['data']['limit'], |
| 91 |
) |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
private function get_license(): string { |
| 96 |
$server_ip = Http_Tools::get_server_ip_address(); |
| 97 |
|
| 98 |
// Define local IP address ranges |
| 99 |
$local_ip_ranges = array( |
| 100 |
'127.', // Loopback |
| 101 |
'192.168.', // Private network |
| 102 |
'10.', // Private network |
| 103 |
); |
| 104 |
|
| 105 |
// Check if the server IP is within any of the local ranges |
| 106 |
foreach ( $local_ip_ranges as $range ) { |
| 107 |
if ( 0 === strpos( $server_ip, $range ) ) { |
| 108 |
return md5( $server_ip . site_url() ); // It's a local IP address |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Add unique site suffix from LOGGED_IN_SALT |
| 114 |
* |
| 115 |
* @see https://github.com/Crocoblock/issues-tracker/issues/14825 |
| 116 |
*/ |
| 117 |
if ( defined( 'LOGGED_IN_SALT' ) ) { |
| 118 |
$server_ip .= LOGGED_IN_SALT; |
| 119 |
} |
| 120 |
|
| 121 |
return md5( $server_ip ); // It's not a local IP address |
| 122 |
} |
| 123 |
} |
| 124 |
|