actions
5 years ago
admin
5 years ago
blocks
5 years ago
classes
5 years ago
compatibility
5 years ago
exceptions
5 years ago
gateways
5 years ago
generators
5 years ago
integrations
5 years ago
autoloader.php
5 years ago
file-upload.php
5 years ago
form-handler.php
5 years ago
form-manager.php
5 years ago
form-messages-builder.php
5 years ago
form-messages-manager.php
5 years ago
form-preset.php
5 years ago
live-form.php
5 years ago
plugin.php
5 years ago
post-type.php
5 years ago
request-handler.php
5 years ago
request-handler.php
257 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Classes\Tools; |
| 8 | use Jet_Form_Builder\Exceptions\Request_Exception; |
| 9 | |
| 10 | class Request_Handler { |
| 11 | public $request; |
| 12 | public $errors; |
| 13 | |
| 14 | private $repeaters = array(); |
| 15 | |
| 16 | const REPEATERS_SETTINGS = '__repeaters_settings'; |
| 17 | |
| 18 | public function __construct( $request ) { |
| 19 | $this->request = $request; |
| 20 | } |
| 21 | |
| 22 | private function merge_with_base_request( $data ) { |
| 23 | foreach ( $this->request as $name => $field ) { |
| 24 | $data[ '__' . $name ] = $field; |
| 25 | } |
| 26 | $data[ self::REPEATERS_SETTINGS ] = $this->repeaters; |
| 27 | |
| 28 | return $data; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Get form values from request |
| 34 | * |
| 35 | * @return [type] [description] |
| 36 | */ |
| 37 | public function get_values_from_request() { |
| 38 | |
| 39 | if ( $this->request['is_ajax'] ) { |
| 40 | |
| 41 | $prepared = array(); |
| 42 | $raw = ! empty( $_REQUEST['values'] ) ? Tools::maybe_recursive_sanitize( $_REQUEST['values'] ) : array(); |
| 43 | |
| 44 | if ( empty( $raw ) ) { |
| 45 | return $prepared; |
| 46 | } |
| 47 | |
| 48 | foreach ( $raw as $data ) { |
| 49 | |
| 50 | $name = $data['name']; |
| 51 | $value = $data['value']; |
| 52 | |
| 53 | if ( preg_match( '/\[\d\]\[/', $name ) ) { |
| 54 | |
| 55 | $name_parts = explode( '[', $name ); |
| 56 | |
| 57 | $name = $name_parts[0]; |
| 58 | $index = absint( rtrim( $name_parts[1], ']' ) ); |
| 59 | $key = rtrim( $name_parts[2], ']' ); |
| 60 | |
| 61 | if ( empty( $prepared[ $name ] ) ) { |
| 62 | $prepared[ $name ] = array(); |
| 63 | } |
| 64 | |
| 65 | if ( empty( $prepared[ $name ][ $index ] ) ) { |
| 66 | $prepared[ $name ][ $index ] = array(); |
| 67 | } |
| 68 | |
| 69 | if ( isset( $name_parts[3] ) ) { |
| 70 | |
| 71 | if ( empty( $prepared[ $name ][ $index ][ $key ] ) ) { |
| 72 | $prepared[ $name ][ $index ][ $key ] = array(); |
| 73 | } |
| 74 | |
| 75 | $prepared[ $name ][ $index ][ $key ][] = $value; |
| 76 | |
| 77 | } else { |
| 78 | $prepared[ $name ][ $index ][ $key ] = $value; |
| 79 | } |
| 80 | |
| 81 | } elseif ( false !== strpos( $name, '[]' ) ) { |
| 82 | |
| 83 | $name = str_replace( '[]', '', $name ); |
| 84 | |
| 85 | if ( empty( $prepared[ $name ] ) ) { |
| 86 | $prepared[ $name ] = array(); |
| 87 | } |
| 88 | |
| 89 | $prepared[ $name ][] = $value; |
| 90 | |
| 91 | } else { |
| 92 | $prepared[ $name ] = $value; |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | |
| 97 | return $prepared; |
| 98 | |
| 99 | } else { |
| 100 | return $_REQUEST; |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Get submitted form data |
| 108 | * |
| 109 | * @return array [type] [description] |
| 110 | * @throws Request_Exception |
| 111 | */ |
| 112 | public function get_form_data() { |
| 113 | |
| 114 | $fields = Plugin::instance()->form->get_only_form_fields( $this->request['form_id'] ); |
| 115 | $data = array(); |
| 116 | $errors = array(); |
| 117 | $invalid_email = true; |
| 118 | $request = $this->get_values_from_request(); |
| 119 | |
| 120 | $skip_fields = array( 'submit-field', 'form-break-field', 'heading-field', 'group-break-field' ); |
| 121 | |
| 122 | foreach ( $fields as $field ) { |
| 123 | $settings = $field['attrs']; |
| 124 | $required = ! empty( $settings['required'] ) ? $settings['required'] : ''; |
| 125 | $name = isset( $settings['name'] ) ? $settings['name'] : 'field_name'; |
| 126 | $value = isset( $request[ $name ] ) ? $request[ $name ] : ''; |
| 127 | |
| 128 | $type = Plugin::instance()->form->field_name( $field['blockName'] ); |
| 129 | |
| 130 | |
| 131 | if ( in_array( $type, $skip_fields ) ) { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if ( ! $this->is_field_visible( $settings ) ) { |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | $is_repeater = false; |
| 140 | |
| 141 | if ( in_array( $type, array( 'date-field', 'datetime-field' ) ) && ! empty( $settings['is_timestamp'] ) ) { |
| 142 | $value = strtotime( $value ); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | if ( 'repeater-field' === $type ) { |
| 147 | $is_repeater = true; |
| 148 | $in_repeater = true; |
| 149 | $current_repeater = $name; |
| 150 | $this->repeaters[ $settings['name'] ] = $settings; |
| 151 | } else { |
| 152 | $in_repeater = false; |
| 153 | $current_repeater = null; |
| 154 | } |
| 155 | |
| 156 | if ( ! $is_repeater && $in_repeater ) { |
| 157 | if ( 'media' === $settings['type'] && ! empty( $data[ $current_repeater ] ) ) { |
| 158 | foreach ( $data[ $current_repeater ] as $index => $row ) { |
| 159 | if ( ! empty( $row[ $name ] ) ) { |
| 160 | |
| 161 | $value = json_decode( wp_unslash( $row[ $name ] ), true ); |
| 162 | |
| 163 | if ( ! empty( $settings['insert_attachment'] ) && ! empty( $settings['value_format'] ) && 'id' === $settings['value_format'] ) { |
| 164 | if ( ! is_array( $value ) ) { |
| 165 | $value = ! empty( $value ) ? absint( $value ) : null; |
| 166 | } else { |
| 167 | $value = implode( ',', $value ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | $row[ $name ] = $value; |
| 172 | $data[ $current_repeater ][ $index ] = $row; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | if ( 'media-field' === $type ) { |
| 180 | $value = json_decode( wp_unslash( $value ), true ); |
| 181 | |
| 182 | if ( ! empty( $settings['insert_attachment'] ) && ( |
| 183 | ( ! empty( $settings['value_format'] ) && 'id' === $settings['value_format'] ) |
| 184 | || ! isset( $settings['value_format'] ) |
| 185 | ) ) { |
| 186 | if ( ! is_array( $value ) ) { |
| 187 | $value = absint( $value ); |
| 188 | } else { |
| 189 | $value = implode( ',', $value ); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if ( 'wysiwyg-field' === $type ) { |
| 195 | $required = false; |
| 196 | $value = Tools::sanitize_wysiwyg( $value ); |
| 197 | } |
| 198 | |
| 199 | if ( isset( $settings['field_type'] ) && 'text-field' === $type && 'email' === $settings['field_type'] && ! is_email( $value ) ) { |
| 200 | throw new Request_Exception( 'invalid_email' ); |
| 201 | } |
| 202 | |
| 203 | /*if ( is_array( $value ) && ! $is_repeater ) { |
| 204 | $value = implode( ', ', $value ); |
| 205 | }*/ |
| 206 | |
| 207 | if ( $required && empty( $value ) ) { |
| 208 | throw new Request_Exception( 'empty_field' ); |
| 209 | } |
| 210 | |
| 211 | $data[ $name ] = $value; |
| 212 | |
| 213 | } |
| 214 | |
| 215 | if ( ! Plugin::instance()->captcha->verify( $this->request['form_id'], $this->request['is_ajax'] ) ) { |
| 216 | throw new Request_Exception( 'captcha_failed' ); |
| 217 | } |
| 218 | |
| 219 | $data = apply_filters( 'jet-form-builder/form-handler/form-data', $data, $this->request['form_id'], $fields ); |
| 220 | |
| 221 | return $this->merge_with_base_request( $data ); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * Returns true if field is visible |
| 227 | * |
| 228 | * @param array $field [description] |
| 229 | * |
| 230 | * @return boolean [description] |
| 231 | */ |
| 232 | public function is_field_visible( $field = array() ) { |
| 233 | |
| 234 | // For backward compatibility and hidden fields |
| 235 | if ( empty( $field['visibility'] ) ) { |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | // If is visible for all - show field |
| 240 | if ( 'all' === $field['visibility'] ) { |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | // If is visible for logged in users and user is logged in - show field |
| 245 | if ( 'logged_id' === $field['visibility'] && is_user_logged_in() ) { |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | // If is visible for not logged in users and user is not logged in - show field |
| 250 | if ( 'not_logged_in' === $field['visibility'] && ! is_user_logged_in() ) { |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | return false; |
| 255 | |
| 256 | } |
| 257 | } |