exceptions
4 years ago
fields
4 years ago
field-data-parser.php
4 years ago
form-request-router.php
4 years ago
parser-manager.php
4 years ago
request-handler.php
4 years ago
request-router.php
4 years ago
request-handler.php
211 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Request; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | use Jet_Form_Builder\Blocks\Modules\Fields_Errors\Error_Handler; |
| 8 | use Jet_Form_Builder\Classes\Tools; |
| 9 | use Jet_Form_Builder\Exceptions\Request_Exception; |
| 10 | use Jet_Form_Builder\Live_Form; |
| 11 | |
| 12 | class Request_Handler { |
| 13 | |
| 14 | public $_fields = array(); |
| 15 | private $request_types = array(); |
| 16 | private $request_attrs = array(); |
| 17 | private $raw_request = array(); |
| 18 | |
| 19 | const WP_NONCE_KEY = '_wpnonce'; |
| 20 | |
| 21 | public function set_request_type( array $types ): Request_Handler { |
| 22 | $this->request_types = array_merge( $this->request_types, $types ); |
| 23 | |
| 24 | return $this; |
| 25 | } |
| 26 | |
| 27 | public function set_request_attrs( array $attrs ): Request_Handler { |
| 28 | $this->request_attrs = array_merge( $this->request_attrs, $attrs ); |
| 29 | |
| 30 | return $this; |
| 31 | } |
| 32 | |
| 33 | public function get_types(): array { |
| 34 | return $this->request_types; |
| 35 | } |
| 36 | |
| 37 | public function get_attrs(): array { |
| 38 | return $this->request_attrs; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param string $name |
| 43 | * @param string $field_type |
| 44 | * |
| 45 | * @return bool |
| 46 | */ |
| 47 | public function is_type( string $name, string $field_type ): bool { |
| 48 | return ( ( $this->request_types[ $name ] ?? false ) === $field_type ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param string $name |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | public function get_attrs_by_name( string $name ): array { |
| 57 | return $this->request_attrs[ $name ] ?? array(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get form values from request |
| 62 | * |
| 63 | * @return [type] [description] |
| 64 | */ |
| 65 | public function get_values_from_request() { |
| 66 | // phpcs:disable WordPress.Security |
| 67 | |
| 68 | if ( ! jet_fb_handler()->is_ajax() ) { |
| 69 | return $_POST; |
| 70 | } |
| 71 | |
| 72 | $prepared = array(); |
| 73 | $raw = ! empty( $_POST['values'] ) ? Tools::sanitize_recursive( wp_unslash( $_POST['values'] ) ) : array(); |
| 74 | |
| 75 | // phpcs:enable WordPress.Security |
| 76 | |
| 77 | if ( empty( $raw ) ) { |
| 78 | return $prepared; |
| 79 | } |
| 80 | |
| 81 | foreach ( $raw as $data ) { |
| 82 | |
| 83 | $name = $data['name']; |
| 84 | $value = $data['value']; |
| 85 | |
| 86 | if ( preg_match( '/\[\d\]\[/', $name ) ) { |
| 87 | |
| 88 | $name_parts = explode( '[', $name ); |
| 89 | |
| 90 | $name = $name_parts[0]; |
| 91 | $index = absint( rtrim( $name_parts[1], ']' ) ); |
| 92 | $key = rtrim( $name_parts[2], ']' ); |
| 93 | |
| 94 | if ( empty( $prepared[ $name ] ) ) { |
| 95 | $prepared[ $name ] = array(); |
| 96 | } |
| 97 | |
| 98 | if ( empty( $prepared[ $name ][ $index ] ) ) { |
| 99 | $prepared[ $name ][ $index ] = array(); |
| 100 | } |
| 101 | |
| 102 | if ( isset( $name_parts[3] ) ) { |
| 103 | |
| 104 | if ( empty( $prepared[ $name ][ $index ][ $key ] ) ) { |
| 105 | $prepared[ $name ][ $index ][ $key ] = array(); |
| 106 | } |
| 107 | |
| 108 | $prepared[ $name ][ $index ][ $key ][] = $value; |
| 109 | |
| 110 | } else { |
| 111 | $prepared[ $name ][ $index ][ $key ] = $value; |
| 112 | } |
| 113 | } elseif ( false !== strpos( $name, '[]' ) ) { |
| 114 | |
| 115 | $name = str_replace( '[]', '', $name ); |
| 116 | |
| 117 | if ( empty( $prepared[ $name ] ) ) { |
| 118 | $prepared[ $name ] = array(); |
| 119 | } |
| 120 | |
| 121 | $prepared[ $name ][] = $value; |
| 122 | |
| 123 | } else { |
| 124 | $prepared[ $name ] = $value; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return $prepared; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @throws Request_Exception |
| 133 | */ |
| 134 | private function get_raw_request(): array { |
| 135 | $this->_fields = Block_Helper::get_blocks_by_post( |
| 136 | jet_fb_handler()->get_form_id() |
| 137 | ); |
| 138 | |
| 139 | $values = $this->get_values_from_request(); |
| 140 | $nonce = $values[ self::WP_NONCE_KEY ] ?? ''; |
| 141 | |
| 142 | Live_Form::instance() |
| 143 | ->set_form_id( jet_fb_handler()->get_form_id() ) |
| 144 | ->set_specific_data_for_render(); |
| 145 | |
| 146 | if ( 'render' === Live_Form::instance()->spec_data->load_nonce |
| 147 | && ! wp_verify_nonce( $nonce, Live_Form::instance()->get_nonce_id() ) |
| 148 | ) { |
| 149 | throw ( new Request_Exception( 'Invalid nonce.' ) )->dynamic_error(); |
| 150 | } |
| 151 | |
| 152 | return $values; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get submitted form data |
| 157 | * |
| 158 | * @return array [type] [description] |
| 159 | * @throws Request_Exception |
| 160 | */ |
| 161 | public function get_form_data(): array { |
| 162 | $this->raw_request = $this->get_raw_request(); |
| 163 | $request = Parser_Manager::instance()->get_values_fields( $this->_fields, $this->raw_request ); |
| 164 | |
| 165 | if ( ! Error_Handler::instance()->empty_errors() ) { |
| 166 | throw new Request_Exception( |
| 167 | 'validation_failed', |
| 168 | Error_Handler::instance()->errors(), |
| 169 | $request |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | return apply_filters( 'jet-form-builder/form-handler/form-data', $request, $this ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @return array |
| 178 | */ |
| 179 | public function get_request(): array { |
| 180 | return $this->raw_request; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @param $field_name |
| 185 | * @param $attr_name |
| 186 | * @param false $if_not_exist |
| 187 | * |
| 188 | * @return mixed |
| 189 | */ |
| 190 | public function get_attr( $field_name, $attr_name, $if_not_exist = false ) { |
| 191 | $attrs = $this->get_attrs_by_name( $field_name ); |
| 192 | |
| 193 | return $attrs[ $attr_name ] ?? $if_not_exist; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /** |
| 198 | * @deprecated since 2.0.0 |
| 199 | * Use jfb_request_handler()->get_single_attr instead |
| 200 | * |
| 201 | * @param $field_name |
| 202 | * @param string $attr_name |
| 203 | * |
| 204 | * @return array |
| 205 | */ |
| 206 | public function get_field_attrs_by_name( $field_name, $attr_name = '', $if_empty = false ) { |
| 207 | return $this->get_attr( $field_name, $attr_name, $if_empty ); |
| 208 | } |
| 209 | |
| 210 | } |
| 211 |