rest-validation-endpoint.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Validation\Rest_Api; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | use Jet_Form_Builder\Classes\Arrayable\Array_Tools; |
| 8 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 9 | use Jet_Form_Builder\Exceptions\Silence_Exception; |
| 10 | use Jet_Form_Builder\Request\Exceptions\Plain_Value_Exception; |
| 11 | use JFB_Modules\Block_Parsers\Field_Data_Parser; |
| 12 | use Jet_Form_Builder\Request\Request_Tools; |
| 13 | use JFB_Components\Rest_Api; |
| 14 | use JFB_Modules\Validation\Module; |
| 15 | use JFB_Modules\Validation\Handlers\Validation_Handler; |
| 16 | use WP_REST_Request; |
| 17 | use WP_REST_Response; |
| 18 | |
| 19 | |
| 20 | // If this file is called directly, abort. |
| 21 | if ( ! defined( 'WPINC' ) ) { |
| 22 | die; |
| 23 | } |
| 24 | |
| 25 | class Rest_Validation_Endpoint extends Rest_Api\Rest_Api_Endpoint_Base { |
| 26 | |
| 27 | const FIELD_KEY = '_jfb_validation_path'; |
| 28 | const RULE_INDEX_KEY = '_jfb_validation_rule_index'; |
| 29 | const SIGNATURE_KEY = '_jfb_validation_sig'; |
| 30 | |
| 31 | public static function get_rest_base() { |
| 32 | return 'validate-field'; |
| 33 | } |
| 34 | |
| 35 | public static function get_methods() { |
| 36 | return \WP_REST_Server::CREATABLE; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Generate a cryptographic signature for validation request. |
| 41 | * |
| 42 | * @since 3.5.6.2 |
| 43 | * |
| 44 | * @param int $form_id The form ID. |
| 45 | * @param string|array $field_path The field path. |
| 46 | * @param int $rule_index The rule index. |
| 47 | * |
| 48 | * @return string The generated signature. |
| 49 | */ |
| 50 | public static function generate_signature( int $form_id, $field_path, int $rule_index ): string { |
| 51 | $path_string = is_array( $field_path ) ? implode( '.', $field_path ) : (string) $field_path; |
| 52 | $data = $form_id . '|' . $path_string . '|' . $rule_index; |
| 53 | |
| 54 | return wp_hash( $data, 'nonce' ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param \WP_REST_Request $request |
| 59 | * |
| 60 | * @return \WP_REST_Response |
| 61 | */ |
| 62 | public function run_callback( \WP_REST_Request $request ) { |
| 63 | $body = $request->get_body_params(); |
| 64 | |
| 65 | // All security validation is centralized in Validation_Handler::validate() |
| 66 | $result = Validation_Handler::validate( $body ); |
| 67 | |
| 68 | return new WP_REST_Response( $result ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param \WP_REST_Request $request |
| 73 | * |
| 74 | * @return Field_Data_Parser |
| 75 | * @throws Plain_Value_Exception |
| 76 | * @throws Repository_Exception |
| 77 | */ |
| 78 | protected function get_parser( \WP_REST_Request $request ): Field_Data_Parser { |
| 79 | $body = $request->get_body_params(); |
| 80 | |
| 81 | $path = $body[ self::FIELD_KEY ] ?? false; |
| 82 | |
| 83 | $form_id = $body[ jet_fb_handler()->form_key ] ?? false; |
| 84 | |
| 85 | jet_fb_handler()->set_form_id( $form_id ); |
| 86 | |
| 87 | jet_fb_context()->set_request( $body ); |
| 88 | jet_fb_context()->set_files( Request_Tools::get_files( $request->get_file_params() ) ); |
| 89 | |
| 90 | // set fields with request |
| 91 | jet_fb_context()->apply( |
| 92 | Block_Helper::get_blocks_by_post( $form_id ) |
| 93 | ); |
| 94 | |
| 95 | return jet_fb_context()->resolve_parser( $path ); |
| 96 | } |
| 97 | |
| 98 | public function get_parser_public( \WP_REST_Request $request ) { |
| 99 | return $this->get_parser( $request ); |
| 100 | } |
| 101 | } |
| 102 |