| @@ -1,156 +1,210 @@ | ||
| 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\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 | +} | |