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