| @@ -7,12 +7,23 @@ | ||
| 7 | 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | 8 | die; |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | +use Jet_Form_Builder\Exceptions\Gateway_Exception; | |
| 11 | 12 | use JFB_Modules\Gateways\Base_Gateway_Action; |
| 12 | 13 | |
| 13 | 14 | class Generate_Form_Action extends Base_Gateway_Action { |
| 14 | 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 | + | |
| 15 | 26 | protected $method = \WP_REST_Server::READABLE; |
| 16 | 27 | |
| 17 | 28 | protected $license = ''; |
| 18 | 29 | protected $prompt = ''; |
| @@ -21,12 +32,33 @@ | ||
| 21 | 32 | return 'https://account.crocoblock.com/'; |
| 22 | 33 | } |
| 23 | 34 | |
| 24 | 35 | public function action_query_args(): array { |
| 36 | + $this->assert_prompt_length(); | |
| 37 | + | |
| 25 | 38 | return array( |
| 26 | 39 | 'ai_api' => 'jfb', |
| 27 | 40 | 'license' => $this->get_licence(), |
| 28 | 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 | + ) | |
| 29 | 61 | ); |
| 30 | 62 | } |
| 31 | 63 | |
| 32 | 64 | /** |