PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.5.6.3
JetFormBuilder — Dynamic Blocks Form Builder v3.5.6.3
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / validation / rest-api / rest-validation-endpoint.php
jetformbuilder / modules / validation / rest-api Last commit date
rest-validation-endpoint.php 4 months ago
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