base-validation-callback.php
2 years ago
is-user-email-unique.php
2 years ago
is-user-login-unique.php
2 years ago
rest-controller.php
2 years ago
rest-validation-endpoint.php
2 years ago
validation-callbacks.php
2 years ago
rest-validation-endpoint.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Blocks\Ssr_Validation; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | use Jet_Form_Builder\Blocks\Validation; |
| 8 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 9 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 10 | use Jet_Form_Builder\Exceptions\Silence_Exception; |
| 11 | use Jet_Form_Builder\Request\Exceptions\Plain_Value_Exception; |
| 12 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 13 | use JFB_Modules\Block_Parsers\Parser_Context; |
| 14 | use Jet_Form_Builder\Request\Request_Tools; |
| 15 | use JFB_Components\Rest_Api; |
| 16 | |
| 17 | |
| 18 | // If this file is called directly, abort. |
| 19 | if ( ! defined( 'WPINC' ) ) { |
| 20 | die; |
| 21 | } |
| 22 | |
| 23 | class Rest_Validation_Endpoint extends Rest_Api\Rest_Api_Endpoint_Base { |
| 24 | |
| 25 | const FIELD_KEY = '_jfb_validation_path'; |
| 26 | const RULE_INDEX_KEY = '_jfb_validation_rule_index'; |
| 27 | |
| 28 | public static function get_rest_base() { |
| 29 | return 'validate-field'; |
| 30 | } |
| 31 | |
| 32 | public static function get_methods() { |
| 33 | return \WP_REST_Server::CREATABLE; |
| 34 | } |
| 35 | |
| 36 | public function run_callback( \WP_REST_Request $request ) { |
| 37 | $body = $request->get_body_params(); |
| 38 | |
| 39 | try { |
| 40 | $parser = $this->get_parser( $request ); |
| 41 | } catch ( Silence_Exception $exception ) { |
| 42 | return new \WP_REST_Response( |
| 43 | array( |
| 44 | 'message' => __( 'Unresolved parser for field', 'jet-form-builder' ), |
| 45 | ), |
| 46 | 400 |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | $validation = $parser->get_setting( 'validation' ); |
| 51 | |
| 52 | $callback = Array_Tools::get( |
| 53 | $validation, |
| 54 | array( 'rules', $body[ self::RULE_INDEX_KEY ], 'value' ) |
| 55 | ); |
| 56 | |
| 57 | if ( ! $parser->get_value() || ! $callback ) { |
| 58 | return new \WP_REST_Response( |
| 59 | array( |
| 60 | 'message' => __( 'Field value or callback is empty', 'jet-form-builder' ), |
| 61 | ), |
| 62 | 400 |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | $result = Validation::instance()->callbacks->validate( $parser, $callback ); |
| 67 | |
| 68 | return new \WP_REST_Response( |
| 69 | array( |
| 70 | 'result' => $result, |
| 71 | ), |
| 72 | 200 |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param \WP_REST_Request $request |
| 78 | * |
| 79 | * @return Field_Data_Parser |
| 80 | * @throws Plain_Value_Exception |
| 81 | * @throws Repository_Exception |
| 82 | */ |
| 83 | protected function get_parser( \WP_REST_Request $request ): Field_Data_Parser { |
| 84 | $body = $request->get_body_params(); |
| 85 | $path = $body[ self::FIELD_KEY ] ?? false; |
| 86 | $form_id = $body[ jet_fb_handler()->form_key ] ?? false; |
| 87 | |
| 88 | jet_fb_handler()->set_form_id( $form_id ); |
| 89 | |
| 90 | jet_fb_context()->set_request( $body ); |
| 91 | jet_fb_context()->set_files( Request_Tools::get_files( $request->get_file_params() ) ); |
| 92 | |
| 93 | // set fields with request |
| 94 | jet_fb_context()->apply( |
| 95 | Block_Helper::get_blocks_by_post( $form_id ) |
| 96 | ); |
| 97 | |
| 98 | return jet_fb_context()->resolve_parser( $path ); |
| 99 | } |
| 100 | } |
| 101 |