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