| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Ai\External_Api; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
use Jet_Form_Builder\Exceptions\Gateway_Exception; |
| 12 |
use JFB_Modules\Gateways\Base_Gateway_Action; |
| 13 |
|
| 14 |
class Generate_Form_Action extends Base_Gateway_Action { |
| 15 |
|
| 16 |
/** |
| 17 |
* Maximum allowed length (in characters) of the `prompt` value. |
| 18 |
* |
| 19 |
* The prompt is sent as a GET query argument, so it must stay well |
| 20 |
* under common web server/proxy URL length limits. |
| 21 |
* |
| 22 |
* @see https://github.com/Crocoblock/issues-tracker/issues/14825 |
| 23 |
*/ |
| 24 |
const MAX_PROMPT_LENGTH = 4000; |
| 25 |
|
| 26 |
protected $method = \WP_REST_Server::READABLE; |
| 27 |
|
| 28 |
protected $license = ''; |
| 29 |
protected $prompt = ''; |
| 30 |
|
| 31 |
public function base_url(): string { |
| 32 |
return 'https://account.crocoblock.com/'; |
| 33 |
} |
| 34 |
|
| 35 |
public function action_query_args(): array { |
| 36 |
$this->assert_prompt_length(); |
| 37 |
|
| 38 |
return array( |
| 39 |
'ai_api' => 'jfb', |
| 40 |
'license' => $this->get_licence(), |
| 41 |
'prompt' => $this->prompt, |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @throws Gateway_Exception |
| 47 |
*/ |
| 48 |
protected function assert_prompt_length() { |
| 49 |
if ( mb_strlen( $this->prompt, 'UTF-8' ) <= self::MAX_PROMPT_LENGTH ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
throw new Gateway_Exception( |
| 54 |
esc_html( |
| 55 |
sprintf( |
| 56 |
/* translators: %d: maximum allowed prompt length */ |
| 57 |
__( 'Prompt is too long. Maximum length is %d characters.', 'jet-form-builder' ), |
| 58 |
self::MAX_PROMPT_LENGTH |
| 59 |
) |
| 60 |
) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function get_licence(): string { |
| 68 |
if ( defined( 'JFB_AI_LICENSE' ) ) { |
| 69 |
return JFB_AI_LICENSE; |
| 70 |
} |
| 71 |
|
| 72 |
return $this->license; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @param string $license |
| 77 |
*/ |
| 78 |
public function set_license( string $license ) { |
| 79 |
$this->license = $license; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param string $prompt |
| 84 |
*/ |
| 85 |
public function set_prompt( string $prompt ) { |
| 86 |
$this->prompt = $prompt; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public function get_prompt(): string { |
| 93 |
return $this->prompt; |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|