PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.8
JetFormBuilder — Dynamic Blocks Form Builder v3.0.8
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / actions / methods / form-record / controller.php
jetformbuilder / includes / actions / methods / form-record Last commit date
admin 3 years ago constraints 3 years ago models 3 years ago query-views 3 years ago rest-endpoints 3 years ago controller.php 3 years ago records-rest-controller.php 3 years ago tools.php 3 years ago
controller.php
313 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Actions\Methods\Form_Record;
5
6 use Jet_Form_Builder\Actions\Methods\Form_Record\Models;
7 use Jet_Form_Builder\Actions\Methods\Form_Record\Query_Views\Record_Fields_View;
8 use Jet_Form_Builder\Actions\Types\Base;
9 use Jet_Form_Builder\Blocks\Block_Helper;
10 use Jet_Form_Builder\Classes\Http\Http_Tools;
11 use Jet_Form_Builder\Classes\Tools;
12 use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception;
13 use Jet_Form_Builder\Dev_Mode\Logger;
14 use Jet_Form_Builder\Exceptions\Gateway_Exception;
15 use Jet_Form_Builder\Live_Form;
16
17 class Controller {
18
19 protected $settings = array(
20 'save_user_data' => false,
21 'save_empty_fields' => false,
22 'save_errors' => false,
23 );
24 protected $columns = array();
25 protected $record_id;
26 protected $is_new = true;
27
28 public function get_record_id(): int {
29 return (int) $this->record_id;
30 }
31
32 public function set_record_id( $record_id ): Controller {
33 $this->record_id = (int) $record_id;
34 $this->is_new = false;
35
36 return $this;
37 }
38
39 /**
40 * @return $this
41 * @throws Sql_Exception
42 */
43 public function save() {
44 /**
45 * Saving general information about the request from the form
46 * in `*_jet_fb_records`
47 * by \Jet_Form_Builder\Actions\Methods\Form_Record\Record_Model
48 */
49 $this->record_id = $this->save_record();
50
51 /**
52 * Saving each field as a separate record
53 * in `*_jet_fb_records_fields`
54 * by \Jet_Form_Builder\Actions\Methods\Form_Record\Record_Field_Model
55 */
56 $this->save_fields();
57
58 /**
59 * We save information about completed actions
60 * in `*_jet_fb_records_actions`
61 * by \Jet_Form_Builder\Actions\Methods\Form_Record\Record_Action_Result_Model
62 */
63 $this->save_actions();
64
65 /**
66 * We save the errors that the Logger collected.
67 * in `*_jet_fb_records_errors`
68 * by \Jet_Form_Builder\Actions\Methods\Form_Record\Record_Error_Model
69 */
70 $this->save_errors();
71
72 return $this;
73 }
74
75 /**
76 * @return int
77 * @throws Sql_Exception
78 */
79 public function save_record(): int {
80 $args = jet_form_builder()->form_handler->get_response_args();
81 $columns = array(
82 'form_id' => jet_fb_handler()->get_form_id(),
83 'referrer' => jet_fb_handler()->refer,
84 'submit_type' => jet_form_builder()->form_handler->is_ajax() ? 'ajax' : 'reload',
85 'user_id' => get_current_user_id(),
86 'from_content_id' => Live_Form::instance()->post->ID ?? 0,
87 'from_content_type' => Live_Form::instance()->post->post_type, /* it can be replaced by CCT slug */
88 'status' => $args['status'] ?? '',
89 );
90
91 $this->set_columns( $columns );
92 $this->maybe_set_user_data();
93
94 return ( new Models\Record_Model() )->insert( $this->columns );
95 }
96
97 /**
98 * @return int[]
99 * @throws Sql_Exception
100 */
101 public function save_actions(): array {
102 list( $success, $skipped, $failed ) = $this->get_chunked_actions();
103
104 $actions = array_merge(
105 $this->get_prepared_actions( $success, 'success' ),
106 $this->get_prepared_actions( $skipped, 'skipped' ),
107 $this->get_prepared_actions( $failed, 'failed' )
108 );
109
110 return ( new Models\Record_Action_Result_Model() )->insert_many( $actions );
111 }
112
113 public function get_chunked_actions(): array {
114 list( $passed, $skipped ) = array(
115 jet_fb_action_handler()->get_passed_actions(),
116 jet_fb_action_handler()->get_skipped_actions(),
117 );
118
119 $passed_actions = array();
120 $skipped_actions = array();
121 $with_errors = array();
122
123 foreach ( jet_fb_action_handler()->get_all() as $index => $action ) {
124 if ( jet_fb_action_handler()->get_position() === $index ) {
125 continue;
126 }
127 if ( in_array( $index, $passed, true ) ) {
128 $passed_actions[] = $action;
129 } elseif ( in_array( $index, $skipped, true ) ) {
130 $skipped_actions[] = $action;
131 } else {
132 $with_errors[] = $action;
133 }
134 }
135
136 return array(
137 $passed_actions,
138 $skipped_actions,
139 $with_errors,
140 );
141 }
142
143 /**
144 * @return int[]
145 * @throws Sql_Exception
146 */
147 public function save_fields(): array {
148 $fields = $this->get_prepared_fields();
149
150 return ( new Models\Record_Field_Model() )->insert_many( $fields );
151 }
152
153 /**
154 * @return int[]
155 * @throws Sql_Exception
156 */
157 public function save_errors(): array {
158 $errors = $this->get_prepared_errors();
159
160 return ( new Models\Record_Error_Model() )->insert_many( $errors );
161 }
162
163 private function get_prepared_errors(): array {
164 $errors = array();
165
166 if ( empty( $this->settings['save_errors'] ) ) {
167 return array();
168 }
169
170 foreach ( Logger::instance()->get_logs() as $instance => $logs ) {
171 foreach ( $logs as $log ) {
172 $errors[] = array(
173 'record_id' => $this->record_id,
174 'name' => $instance,
175 'message' => $log['message'],
176 'file' => $log['file'],
177 'line' => $log['line'],
178 'data' => Tools::encode_json( $log['data'] ),
179 'trace_string' => $log['trace_string'],
180 );
181 }
182 }
183
184 return $errors;
185 }
186
187 /**
188 * @since 2.1.6 https://github.com/Crocoblock/issues-tracker/issues/1436
189 * @since 2.0.0 Introduced
190 *
191 * @return array
192 */
193 private function get_prepared_fields(): array {
194 $fields = array();
195
196 foreach ( $this->generate_request() as list( $field_name, $value, $type, $attrs ) ) {
197 $fields[] = array(
198 'record_id' => $this->record_id,
199 'field_name' => $field_name,
200 'field_type' => empty( $type ) ? 'computed' : $type,
201 'field_value' => $value,
202 'field_attrs' => Tools::encode_json( $attrs ),
203 );
204 }
205
206 return $fields;
207 }
208
209 private function generate_request(): \Generator {
210 $core_fields = jet_form_builder()->form_handler->hidden_request_fields();
211 $saved = array();
212
213 if ( ! $this->is_new ) {
214 $saved = Record_Fields_View::get_request_list( $this->record_id );
215 }
216
217 foreach ( jet_fb_action_handler()->request_data as $field_name => $value ) {
218 if (
219 isset( $core_fields[ $field_name ] ) ||
220 ( empty( $this->settings['save_empty_fields'] ) && empty( $value ) ) ||
221 array_key_exists( $field_name, $saved )
222 ) {
223 continue;
224 }
225
226 $current_attrs = jet_fb_request_handler()->get_attrs_by_name( $field_name );
227
228 if ( 'password' === ( $current_attrs['field_type'] ?? '' ) ) {
229 continue;
230 }
231
232 $type = jet_fb_request_handler()->get_type( $field_name );
233 $attrs_to_save = $this->get_attrs_by_field_type( $type, $current_attrs );
234
235 if ( ! is_scalar( $value ) ) {
236 $value = Tools::encode_json( $value );
237
238 $attrs_to_save['is_encoded'] = true;
239 }
240
241 yield array( $field_name, $value, $type, $attrs_to_save );
242 }
243 }
244
245 private function get_attrs_by_field_type( string $type, array $block ) {
246 $list = array( 'label' );
247
248 switch ( $type ) {
249 case 'text-field':
250 $list[] = 'field_type';
251 break;
252 }
253 $attrs = Block_Helper::get_attrs_from_block( $block, $list );
254
255 return apply_filters( 'jet-form-builder/on-save-record/field-attributes', $attrs, $type, $block );
256 }
257
258 private function get_prepared_actions( $source, $status ): array {
259 $actions = array();
260
261 /**
262 * @var $source Base[]
263 */
264 foreach ( $source as $action ) {
265 if ( ! $action->get_executed_events() ) {
266 continue;
267 }
268 foreach ( $action->get_executed_events() as $on_event ) {
269 $actions[] = array(
270 'record_id' => $this->record_id,
271 'action_slug' => $action->get_id(),
272 'action_id' => $action->_id,
273 'on_event' => $on_event,
274 'status' => $status,
275 );
276 }
277 }
278
279 return $actions;
280 }
281
282 public function maybe_set_user_data(): Controller {
283 if ( empty( $this->settings['save_user_data'] ) ) {
284 return $this;
285 }
286
287 $this->columns['user_agent'] = Http_Tools::get_user_agent();
288 $this->columns['ip_address'] = Http_Tools::get_ip_address();
289
290 return $this;
291 }
292
293 public function set_setting( string $key, $value ) {
294 $this->settings[ $key ] = $value;
295
296 return $this;
297 }
298
299 public function set_settings( array $source ) {
300 $this->settings = array_merge( $this->settings, $source );
301
302 return $this;
303 }
304
305
306 public function set_columns( array $columns ) {
307 $this->columns = array_merge( $this->columns, $columns );
308
309 return $this;
310 }
311
312 }
313