| @@ -1,156 +1,206 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | - | |
| 4 | -namespace Jet_Form_Builder\Request; | |
| 5 | - | |
| 6 | -use Jet_Form_Builder\Actions\Events\Bad_Request\Bad_Request_Event; | |
| 7 | -use Jet_Form_Builder\Blocks\Block_Helper; | |
| 8 | -use Jet_Form_Builder\Classes\Resources\Media_Block_Value; | |
| 9 | -use Jet_Form_Builder\Exceptions\Action_Exception; | |
| 10 | -use Jet_Form_Builder\Exceptions\Request_Exception; | |
| 11 | -use JFB_Modules\Security\Exceptions\Spam_Exception; | |
| 12 | -use Jet_Form_Builder\Request\Exceptions\Sanitize_Value_Exception; | |
| 13 | -use JFB_Modules\Logger; | |
| 14 | - | |
| 15 | -// If this file is called directly, abort. | |
| 16 | -if ( ! defined( 'WPINC' ) ) { | |
| 17 | - die; | |
| 18 | -} | |
| 19 | - | |
| 20 | -class Request_Handler { | |
| 21 | - | |
| 22 | - // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore | |
| 23 | - public $_fields = array(); | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * Get submitted form data | |
| 27 | - * | |
| 28 | - * @throws Action_Exception|Spam_Exception|Request_Exception | |
| 29 | - */ | |
| 30 | - public function set_form_data() { | |
| 31 | - $this->_fields = Block_Helper::get_blocks_by_post( | |
| 32 | - jet_fb_handler()->get_form_id() | |
| 33 | - ); | |
| 34 | - | |
| 35 | - do_action( 'jet-form-builder/request' ); | |
| 36 | - } | |
| 37 | - | |
| 38 | - | |
| 39 | - /** | |
| 40 | - * @deprecated 3.1.0 | |
| 41 | - * | |
| 42 | - * @param string|array $name 'field_name'|'repeater_name.field_name'|['repeater_name', 'field_name'] | |
| 43 | - */ | |
| 44 | - public function exclude( $name ) { | |
| 45 | - jet_fb_context()->make_secure( $name ); | |
| 46 | - } | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * @deprecated 3.1.0 | |
| 50 | - * | |
| 51 | - * @return array | |
| 52 | - */ | |
| 53 | - public function get_types(): array { | |
| 54 | - return iterator_to_array( jet_fb_context()->iterate_fields_types() ); | |
| 55 | - } | |
| 56 | - | |
| 57 | - /** | |
| 58 | - * @deprecated 3.1.0 | |
| 59 | - * | |
| 60 | - * @return array | |
| 61 | - */ | |
| 62 | - public function get_attrs(): array { | |
| 63 | - return iterator_to_array( jet_fb_context()->iterate_fields_settings() ); | |
| 64 | - } | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * @deprecated 3.1.0 | |
| 68 | - * | |
| 69 | - * @param string|array $name 'field_name'|'repeater_name.0.field_name'|['repeater_name', 0, 'field_name'] | |
| 70 | - * @param string $field_type | |
| 71 | - * | |
| 72 | - * @return bool | |
| 73 | - */ | |
| 74 | - public function is_type( $name, string $field_type ): bool { | |
| 75 | - return jet_fb_context()->get_field_type( $name ) === $field_type; | |
| 76 | - } | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * @deprecated 3.1.0 | |
| 80 | - * @param $name | |
| 81 | - * | |
| 82 | - * @return string | |
| 83 | - */ | |
| 84 | - public function get_type( $name ): string { | |
| 85 | - return jet_fb_context()->get_field_type( $name ); | |
| 86 | - } | |
| 87 | - | |
| 88 | - /** | |
| 89 | - * @deprecated 3.1.0 | |
| 90 | - * | |
| 91 | - * @return array | |
| 92 | - */ | |
| 93 | - public function get_request(): array { | |
| 94 | - return jet_fb_context()->get_request(); | |
| 95 | - } | |
| 96 | - | |
| 97 | - /** | |
| 98 | - * @deprecated 3.1.0 | |
| 99 | - * | |
| 100 | - * @return array | |
| 101 | - */ | |
| 102 | - public function get_files(): array { | |
| 103 | - return array_merge( | |
| 104 | - jet_fb_context()->resolve_files(), | |
| 105 | - jet_fb_context()->get_files() | |
| 106 | - ); | |
| 107 | - } | |
| 108 | - | |
| 109 | - /** | |
| 110 | - * @deprecated 3.1.0 | |
| 111 | - * @param string $name | |
| 112 | - * | |
| 113 | - * @return false|Media_Block_Value | |
| 114 | - */ | |
| 115 | - public function get_file( string $name ) { | |
| 116 | - return jet_fb_context()->get_file( $name ); | |
| 117 | - } | |
| 118 | - | |
| 119 | - /** | |
| 120 | - * @deprecated 3.1.0 | |
| 121 | - * | |
| 122 | - * @param $field_name | |
| 123 | - * @param $attr_name | |
| 124 | - * @param false $if_not_exist | |
| 125 | - * | |
| 126 | - * @return mixed | |
| 127 | - */ | |
| 128 | - public function get_attr( | |
| 129 | - $field_name, | |
| 130 | - $attr_name, | |
| 131 | - $if_not_exist = false | |
| 132 | - ) { | |
| 133 | - $attr = jet_fb_context()->get_setting( $attr_name, $field_name ); | |
| 134 | - | |
| 135 | - return false === $attr ? $if_not_exist : $attr; | |
| 136 | - } | |
| 137 | - | |
| 138 | - | |
| 139 | - /** | |
| 140 | - * @param $field_name | |
| 141 | - * @param string $attr_name | |
| 142 | - * @param bool $if_empty | |
| 143 | - * | |
| 144 | - * @return array | |
| 145 | - * @deprecated since 2.0.0 | |
| 146 | - * Use jfb_request_handler()->get_attr instead | |
| 147 | - */ | |
| 148 | - public function get_field_attrs_by_name( | |
| 149 | - $field_name, | |
| 150 | - $attr_name = '', | |
| 151 | - $if_empty = false | |
| 152 | - ) { | |
| 153 | - return $this->get_attr( $field_name, $attr_name, $if_empty ); | |
| 154 | - } | |
| 155 | - | |
| 156 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | + | |
| 4 | +namespace Jet_Form_Builder\Request; | |
| 5 | + | |
| 6 | +use Jet_Form_Builder\Actions\Events\Bad_Request\Bad_Request_Event; | |
| 7 | +use Jet_Form_Builder\Blocks\Block_Helper; | |
| 8 | +use Jet_Form_Builder\Blocks\Modules\Fields_Errors\Error_Handler; | |
| 9 | +use Jet_Form_Builder\Classes\Resources\Media_Block_Value; | |
| 10 | +use Jet_Form_Builder\Classes\Security\Wp_Nonce_Tools; | |
| 11 | +use Jet_Form_Builder\Classes\Resources\File; | |
| 12 | +use Jet_Form_Builder\Classes\Resources\File_Collection; | |
| 13 | +use Jet_Form_Builder\Classes\Resources\Sanitize_File_Exception; | |
| 14 | +use Jet_Form_Builder\Classes\Tools; | |
| 15 | +use Jet_Form_Builder\Exceptions\Action_Exception; | |
| 16 | +use Jet_Form_Builder\Exceptions\Request_Exception; | |
| 17 | +use Jet_Form_Builder\Live_Form; | |
| 18 | + | |
| 19 | +class Request_Handler { | |
| 20 | + | |
| 21 | + public $_fields = array(); | |
| 22 | + private $request_types = array(); | |
| 23 | + private $request_attrs = array(); | |
| 24 | + private $raw_request = array(); | |
| 25 | + private $files = array(); | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Get submitted form data | |
| 29 | + * | |
| 30 | + * @throws Action_Exception|Request_Exception | |
| 31 | + */ | |
| 32 | + public function set_form_data() { | |
| 33 | + $this->set_raw_request( $this->get_raw_request() ); | |
| 34 | + $this->set_raw_files( Request_Tools::get_files( $_FILES ) ); | |
| 35 | + | |
| 36 | + $context = ( new Parser_Context() ) | |
| 37 | + ->set_files_context( $this->files ) | |
| 38 | + ->set_request_context( $this->raw_request ); | |
| 39 | + | |
| 40 | + $request = apply_filters( | |
| 41 | + 'jet-form-builder/form-handler/form-data', | |
| 42 | + Parser_Manager::instance()->get_values_fields( $this->_fields, $context ), | |
| 43 | + $this | |
| 44 | + ); | |
| 45 | + | |
| 46 | + jet_fb_action_handler()->add_request( $request ); | |
| 47 | + | |
| 48 | + if ( Error_Handler::instance()->empty_errors() ) { | |
| 49 | + return; | |
| 50 | + } | |
| 51 | + | |
| 52 | + jet_fb_events()->execute( Bad_Request_Event::class ); | |
| 53 | + | |
| 54 | + throw new Request_Exception( | |
| 55 | + 'validation_failed', | |
| 56 | + Error_Handler::instance()->errors(), | |
| 57 | + $request | |
| 58 | + ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + private function get_raw_request(): array { | |
| 62 | + $this->_fields = Block_Helper::get_blocks_by_post( | |
| 63 | + jet_fb_handler()->get_form_id() | |
| 64 | + ); | |
| 65 | + | |
| 66 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 67 | + $values = Tools::sanitize_recursive( wp_unslash( $_POST ) ); | |
| 68 | + | |
| 69 | + Live_Form::instance() | |
| 70 | + ->set_form_id( jet_fb_handler()->get_form_id() ) | |
| 71 | + ->set_specific_data_for_render(); | |
| 72 | + | |
| 73 | + return apply_filters( 'jet-form-builder/request-handler/request', $values ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + public function exclude( string $field_name ): Request_Handler { | |
| 77 | + $attrs = $this->get_attrs_by_name( $field_name ); | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * @see \Jet_Form_Builder\Actions\Methods\Form_Record\Controller::generate_request | |
| 81 | + */ | |
| 82 | + $attrs['field_type'] = 'password'; | |
| 83 | + | |
| 84 | + return $this->set_request_attrs( | |
| 85 | + array( | |
| 86 | + $field_name => $attrs | |
| 87 | + ) | |
| 88 | + ); | |
| 89 | + } | |
| 90 | + | |
| 91 | + public function set_request_type( array $types ): Request_Handler { | |
| 92 | + $this->request_types = array_merge( $this->request_types, $types ); | |
| 93 | + | |
| 94 | + return $this; | |
| 95 | + } | |
| 96 | + | |
| 97 | + public function set_request_attrs( array $attrs ): Request_Handler { | |
| 98 | + $this->request_attrs = array_merge( $this->request_attrs, $attrs ); | |
| 99 | + | |
| 100 | + return $this; | |
| 101 | + } | |
| 102 | + | |
| 103 | + public function get_types(): array { | |
| 104 | + return $this->request_types; | |
| 105 | + } | |
| 106 | + | |
| 107 | + public function get_attrs(): array { | |
| 108 | + return $this->request_attrs; | |
| 109 | + } | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * @param string $name | |
| 113 | + * @param string $field_type | |
| 114 | + * | |
| 115 | + * @return bool | |
| 116 | + */ | |
| 117 | + public function is_type( string $name, string $field_type ): bool { | |
| 118 | + return $this->get_type( $name ) === $field_type; | |
| 119 | + } | |
| 120 | + | |
| 121 | + public function get_type( string $name ): string { | |
| 122 | + return $this->request_types[ $name ] ?? ''; | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * @param string $name | |
| 127 | + * | |
| 128 | + * @return array | |
| 129 | + */ | |
| 130 | + public function get_attrs_by_name( string $name ): array { | |
| 131 | + return $this->request_attrs[ $name ] ?? array(); | |
| 132 | + } | |
| 133 | + | |
| 134 | + public function set_raw_request( array $request ): Request_Handler { | |
| 135 | + $this->raw_request = $request; | |
| 136 | + | |
| 137 | + return $this; | |
| 138 | + } | |
| 139 | + | |
| 140 | + public function set_raw_files( array $files ): Request_Handler { | |
| 141 | + $this->files = $files; | |
| 142 | + | |
| 143 | + return $this; | |
| 144 | + } | |
| 145 | + | |
| 146 | + /** | |
| 147 | + * @return array | |
| 148 | + */ | |
| 149 | + public function get_request(): array { | |
| 150 | + return $this->raw_request; | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * @return File[]|File_Collection[]|array[] | |
| 155 | + */ | |
| 156 | + public function get_files(): array { | |
| 157 | + return $this->files; | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * @param string $name | |
| 162 | + * | |
| 163 | + * @return false|Media_Block_Value | |
| 164 | + */ | |
| 165 | + public function get_file( string $name ) { | |
| 166 | + return $this->files[ $name ] ?? false; | |
| 167 | + } | |
| 168 | + | |
| 169 | + public function update_file( string $name, $file ): Request_Handler { | |
| 170 | + $this->files[ $name ] = $file; | |
| 171 | + | |
| 172 | + return $this; | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * @param $field_name | |
| 177 | + * @param $attr_name | |
| 178 | + * @param false $if_not_exist | |
| 179 | + * | |
| 180 | + * @return mixed | |
| 181 | + */ | |
| 182 | + public function get_attr( | |
| 183 | + $field_name, $attr_name, $if_not_exist = false | |
| 184 | + ) { | |
| 185 | + $attrs = $this->get_attrs_by_name( $field_name ); | |
| 186 | + | |
| 187 | + return $attrs[ $attr_name ] ?? $if_not_exist; | |
| 188 | + } | |
| 189 | + | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * @param $field_name | |
| 193 | + * @param string $attr_name | |
| 194 | + * @param bool $if_empty | |
| 195 | + * | |
| 196 | + * @return array | |
| 197 | + * @deprecated since 2.0.0 | |
| 198 | + * Use jfb_request_handler()->get_attr instead | |
| 199 | + */ | |
| 200 | + public function get_field_attrs_by_name( | |
| 201 | + $field_name, $attr_name = '', $if_empty = false | |
| 202 | + ) { | |
| 203 | + return $this->get_attr( $field_name, $attr_name, $if_empty ); | |
| 204 | + } | |
| 205 | + | |
| 206 | +} | |