PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 2.0.3
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v2.0.3
3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.10 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.6.1 1.6.7 1.7.0 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.2 1.7.2.1 1.7.2.2 1.7.3 1.7.4 1.7.5 1.7.5.1 1.7.5.2 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.8.0 1.8.0.1 1.8.1 1.8.2 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.0.1 1.9.1 1.9.2 1.9.3 1.9.4 1.9.4.1 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.0.1 2.0.1 2.0.2 2.0.3 2.0.3.1 2.0.4 2.0.4.1 2.0.5 2.0.6 2.0.7 2.0.8 2.0.8.1 2.0.9 3.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.3.1 3.0.4 3.0.4.1 3.0.4.2 3.0.5 3.0.5.1 3.0.5.2 3.0.6 3.0.6.1 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.0.9.1 3.0.9.2 3.0.9.3 3.0.9.4 3.0.9.5 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.3.0 3.4.0 3.4.1 3.4.2 3.4.2.1 3.4.3 3.4.4 3.4.5 trunk 1.0 1.0.1 1.0.2 1.0.3
everest-forms / includes / class-evf-form-handler.php
everest-forms / includes Last commit date
abstracts 2 years ago admin 2 years ago elementor 4 years ago export 3 years ago fields 2 years ago interfaces 8 years ago libraries 2 years ago log-handlers 4 years ago shortcodes 2 years ago stats 3 years ago templates 5 years ago class-everest-forms.php 2 years ago class-evf-ajax.php 2 years ago class-evf-autoloader.php 7 years ago class-evf-background-updater.php 7 years ago class-evf-cache-helper.php 6 years ago class-evf-cron.php 3 years ago class-evf-deprecated-action-hooks.php 6 years ago class-evf-deprecated-filter-hooks.php 5 years ago class-evf-emails.php 2 years ago class-evf-fields.php 2 years ago class-evf-form-block.php 4 years ago class-evf-form-handler.php 3 years ago class-evf-form-task.php 2 years ago class-evf-forms-features.php 2 years ago class-evf-frontend-scripts.php 2 years ago class-evf-install.php 2 years ago class-evf-integrations.php 7 years ago class-evf-log-levels.php 8 years ago class-evf-logger.php 5 years ago class-evf-post-types.php 5 years ago class-evf-privacy.php 6 years ago class-evf-session-handler.php 7 years ago class-evf-shortcodes.php 4 years ago class-evf-smart-tags.php 2 years ago class-evf-template-loader.php 2 years ago class-evf-validation.php 6 years ago evf-conditional-functions.php 6 years ago evf-core-functions.php 2 years ago evf-deprecated-functions.php 6 years ago evf-entry-functions.php 3 years ago evf-formatting-functions.php 4 years ago evf-notice-functions.php 4 years ago evf-template-functions.php 4 years ago evf-template-hooks.php 7 years ago evf-update-functions.php 5 years ago
class-evf-form-handler.php
565 lines
1 <?php
2 /**
3 * Form handler.
4 *
5 * Contains a bunch of helper methods as well.
6 *
7 * @package EverestForms
8 * @since 1.0.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Form Handler class.
15 */
16 class EVF_Form_Handler {
17
18 /**
19 * Fetches forms
20 *
21 * @since 1.0.0
22 * @param mixed $id Form ID.
23 * @param array $args Form Arguments.
24 * @return array|bool|null|WP_Post Form object.
25 */
26 public function get( $id = '', $args = array() ) {
27 $forms = array();
28 $args = apply_filters( 'everest_forms_get_form_args', $args );
29
30 if ( false === $id ) {
31 return false;
32 }
33
34 if ( ! isset( $args['cap'] ) && ( is_admin() && ! wp_doing_ajax() ) ) {
35 $args['cap'] = 'everest_forms_view_form';
36 }
37
38 if ( ! empty( $id ) ) {
39 if ( ! empty( $args['cap'] ) && ! current_user_can( $args['cap'], $id ) ) {
40 return false;
41 }
42
43 $the_post = get_post( absint( $id ) );
44
45 if ( $the_post && 'everest_form' === $the_post->post_type ) {
46 $forms = empty( $args['content_only'] ) ? $the_post : evf_decode( $the_post->post_content );
47 }
48 } else {
49 // No ID provided, get multiple forms.
50 $args = wp_parse_args(
51 $args,
52 array(
53 'order' => 'DESC',
54 )
55 );
56
57 $forms = $this->get_multiple( $args );
58 }
59
60 if ( empty( $forms ) ) {
61 return false;
62 }
63
64 return $forms;
65 }
66
67 /**
68 * Fetch multiple forms.
69 *
70 * @since 1.7.0
71 *
72 * @param array $args Additional arguments array.
73 * @param bool $content_only True to return post content only.
74 *
75 * @return array
76 */
77 public function get_multiple( $args = array(), $content_only = false ) {
78 $forms = array();
79 $user_id = get_current_user_id();
80 $args = apply_filters( 'everest_forms_get_multiple_forms_args', $args, $content_only );
81
82 // No ID provided, get multiple forms.
83 $defaults = array(
84 'orderby' => 'id',
85 'order' => 'ASC',
86 'no_found_rows' => true,
87 'nopaging' => true,
88 'status' => 'publish',
89 'post_status' => 'publish',
90 'numberposts' => -1,
91 );
92
93 $args = wp_parse_args( $args, $defaults );
94
95 $args['post_type'] = 'everest_form';
96
97 // Can user interact, lets check the view capabilities?
98 if ( current_user_can( 'everest_forms_view_forms' ) && ! current_user_can( 'everest_forms_view_others_forms' ) ) {
99 $args['author'] = $user_id;
100 }
101
102 if ( ! current_user_can( 'everest_forms_view_forms' ) && current_user_can( 'everest_forms_view_others_forms' ) ) {
103 $args['author__not_in'] = $user_id;
104 }
105
106 if ( ! current_user_can( 'everest_forms_view_forms' ) && ! current_user_can( 'everest_forms_view_others_forms' ) ) {
107 if ( isset( $args['cap'] ) && 'everest_forms_view_conversational_forms' !== $args['cap'] ) {
108 $args['post__in'] = array( 0 );
109 }
110 }
111
112 // For cache lets unset the cap args.
113 unset( $args['cap'] );
114
115 // Fetch posts.
116 $forms = get_posts( $args );
117
118 if ( $content_only ) {
119 $forms = array_map( array( $this, 'prepare_post_content' ), $forms );
120 }
121
122 return $forms;
123 }
124
125 /**
126 * Prepares post content.
127 *
128 * @param object $post Post object.
129 */
130 public function prepare_post_content( $post ) {
131 return ! empty( $post->post_content ) ? evf_decode( $post->post_content ) : false;
132 }
133
134 /**
135 * Delete forms.
136 *
137 * @since 1.0.0
138 * @param array $ids Form IDs.
139 * @return boolean
140 */
141 public function delete( $ids = array() ) {
142 if ( ! is_array( $ids ) ) {
143 $ids = array( $ids );
144 }
145
146 $ids = array_map( 'absint', $ids );
147
148 foreach ( $ids as $id ) {
149
150 // Check for permissions.
151 if ( ! current_user_can( 'everest_forms_delete', $id ) ) {
152 return false;
153 }
154
155 $form = wp_delete_post( $id, true );
156
157 if ( ! $form ) {
158 return false;
159 }
160 }
161
162 do_action( 'everest_forms_delete_form', $ids );
163
164 return true;
165 }
166
167 /**
168 * Create new form.
169 *
170 * @since 1.0.0
171 * @param string $title Form title.
172 * @param string $template Form template.
173 * @param array $args Form Arguments.
174 * @param array $data Additional data.
175 * @return int|bool Form ID on successful creation else false.
176 */
177 public function create( $title = '', $template = 'blank', $args = array(), $data = array() ) {
178 if ( empty( $title ) || ! current_user_can( 'everest_forms_create_forms' ) ) {
179 return false;
180 }
181
182 $args = apply_filters( 'everest_forms_create_form_args', $args, $data );
183 $form_style = array();
184 $style_needed = false;
185 $form_content = array(
186 'form_field_id' => '1',
187 'settings' => array(
188 'form_title' => sanitize_text_field( $title ),
189 'form_desc' => '',
190 ),
191 );
192
193 // Prevent content filters from corrupting JSON in post_content.
194 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
195 if ( $has_kses ) {
196 kses_remove_filters();
197 }
198 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
199 if ( $has_targeted_link_rel_filters ) {
200 wp_remove_targeted_link_rel_filters();
201 }
202
203 // Create a form.
204 $form_id = wp_insert_post(
205 array(
206 'post_title' => esc_html( $title ),
207 'post_status' => 'publish',
208 'post_type' => 'everest_form',
209 'post_content' => '{}',
210 )
211 );
212
213 $templates = EVF_Admin_Form_Templates::get_template_data();
214 $templates = is_array( $templates ) ? $templates : array();
215 if ( ! empty( $templates ) ) {
216 foreach ( $templates as $template_data ) {
217 if ( $template_data->slug === $template && 'blank' !== $template_data->slug ) {
218 $form_content = json_decode( base64_decode( $template_data->settings ), true );
219 if ( isset( $template_data->styles ) ) {
220 $style_needed = true;
221 $form_style[ $form_id ] = json_decode( base64_decode( $template_data->styles ), true );
222 }
223 }
224 }
225 }
226
227 if ( $form_id ) {
228 $form_content['id'] = $form_id;
229 $form_content['settings']['form_title'] = $title;
230 $form_content['imported_form_templates'] = $template;
231
232 $form_data = wp_parse_args(
233 $args,
234 array(
235 'ID' => $form_id,
236 'post_title' => esc_html( $title ),
237 'post_content' => evf_encode( array_merge( array( 'id' => $form_id ), $form_content ) ),
238 )
239 );
240
241 wp_update_post( $form_data );
242
243 if ( ! empty( $form_style ) ) {
244 update_option( 'everest_forms_styles', $form_style );
245 }
246 }
247
248 // Restore removed content filters.
249 if ( $has_kses ) {
250 kses_init_filters();
251 }
252 if ( $has_targeted_link_rel_filters ) {
253 wp_init_targeted_link_rel_filters();
254 }
255
256 do_action( 'everest_forms_create_form', $form_id, $form_data, $data, $style_needed );
257
258 return $form_id;
259 }
260
261 /**
262 * Updates form
263 *
264 * @since 1.0.0
265 *
266 * @param string|int $form_id Form ID.
267 * @param array $data Data retrieved from $_POST and processed.
268 * @param array $args Empty by default, may have custom data not intended to be saved.
269 *
270 * @return mixed
271 * @internal param string $title
272 */
273 public function update( $form_id = '', $data = array(), $args = array() ) {
274 if ( empty( $data ) ) {
275 return false;
276 }
277
278 if ( empty( $form_id ) ) {
279 $form_id = $data['form_id'];
280 }
281
282 if ( ! isset( $args['cap'] ) ) {
283 $args['cap'] = 'everest_forms_edit_form';
284 }
285
286 // Check for permissions.
287 if ( ! empty( $args['cap'] ) && ! current_user_can( $args['cap'], $form_id ) ) {
288 return false;
289 }
290
291 $data = wp_unslash( $data );
292
293 if ( ! empty( $data['settings']['form_title'] ) ) {
294 $title = $data['settings']['form_title'];
295 } else {
296 $title = get_the_title( $form_id );
297 }
298
299 if ( ! empty( $data['settings']['form_desc'] ) ) {
300 $desc = $data['settings']['form_desc'];
301 } else {
302 $desc = '';
303 }
304
305 $data['form_field_id'] = ! empty( $data['form_field_id'] ) ? absint( $data['form_field_id'] ) : '0';
306
307 // This filter can destroy the JSON when messing with HTML.
308 remove_filter( 'content_save_pre', 'balanceTags', 50 );
309
310 // Don't allow tags for users who do not have appropriate cap.
311 if ( ! current_user_can( 'unfiltered_html' ) ) {
312 $data = map_deep( $data, 'wp_strip_all_tags' );
313 }
314
315 // Prevent content filters from corrupting JSON in post_content.
316 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
317 if ( $has_kses ) {
318 kses_remove_filters();
319 }
320 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
321 if ( $has_targeted_link_rel_filters ) {
322 wp_remove_targeted_link_rel_filters();
323 }
324
325 $form = array(
326 'ID' => $form_id,
327 'post_title' => esc_html( $title ),
328 'post_excerpt' => $desc,
329 'post_content' => evf_encode( $data ),
330 );
331 $form = apply_filters( 'everest_forms_save_form_args', $form, $data, $args );
332 $form_id = wp_update_post( $form );
333
334 // Import form styles if present.
335 $style_needed = false;
336 if ( ! empty( $data['form_styles'] ) ) {
337 $style_needed = true;
338 $form_styles = get_option( 'everest_forms_styles', array() );
339 $form_styles[ $form_id ] = evf_decode( $data['form_styles'] );
340
341 // Update forms styles.
342 update_option( 'everest_forms_styles', $form_styles );
343 }
344
345 // Restore removed content filters.
346 if ( $has_kses ) {
347 kses_init_filters();
348 }
349 if ( $has_targeted_link_rel_filters ) {
350 wp_init_targeted_link_rel_filters();
351 }
352
353 do_action( 'everest_forms_save_form', $form_id, $form, array(), $style_needed );
354
355 return $form_id;
356 }
357
358 /**
359 * Duplicate forms.
360 *
361 * @since 1.0.0
362 *
363 * @param array $ids Form IDs to duplicate.
364 *
365 * @return boolean
366 */
367 public function duplicate( $ids = array() ) {
368 // Check for permissions.
369 if ( ! current_user_can( 'everest_forms_create_forms' ) ) {
370 return false;
371 }
372
373 if ( ! is_array( $ids ) ) {
374 $ids = array( $ids );
375 }
376
377 $ids = array_map( 'absint', $ids );
378
379 foreach ( $ids as $id ) {
380
381 // Get original entry.
382 $form = get_post( $id );
383
384 if ( ! current_user_can( 'everest_forms_view_form', $id ) ) {
385 return false;
386 }
387
388 // Confirm form exists.
389 if ( ! $form || empty( $form ) ) {
390 return false;
391 }
392
393 // Get the form data.
394 $new_form_data = evf_decode( $form->post_content );
395
396 // Get the form styles.
397 $form_styles = get_option( 'everest_forms_styles', array() );
398 if ( ! empty( $form_styles[ $id ] ) ) {
399 $new_form_data['form_styles'] = wp_json_encode( $form_styles[ $id ] );
400 }
401
402 // Remove form ID from title if present.
403 $new_form_data['settings']['form_title'] = str_replace( '(ID #' . absint( $id ) . ')', '', $new_form_data['settings']['form_title'] );
404
405 // Create the duplicate form.
406 $new_form = array(
407 'post_author' => $form->post_author,
408 'post_content' => evf_encode( $new_form_data ),
409 'post_excerpt' => $form->post_excerpt,
410 'post_status' => $form->post_status,
411 'post_title' => $new_form_data['settings']['form_title'],
412 'post_type' => $form->post_type,
413 );
414 $new_form_id = wp_insert_post( $new_form );
415
416 if ( ! $new_form_id || is_wp_error( $new_form_id ) ) {
417 return false;
418 }
419
420 // Set new form name.
421 $new_form_data['settings']['form_title'] .= ' (ID #' . absint( $new_form_id ) . ')';
422
423 // Set new form ID.
424 $new_form_data['id'] = absint( $new_form_id );
425
426 // Update new duplicate form.
427 $new_form_id = $this->update( $new_form_id, $new_form_data );
428
429 if ( ! $new_form_id || is_wp_error( $new_form_id ) ) {
430 return false;
431 }
432
433 return $new_form_id;
434 }
435
436 return true;
437 }
438
439 /**
440 * Get private meta information for a form.
441 *
442 * @since 1.1.0
443 *
444 * @param int $form_id Form ID.
445 * @param string $field Field.
446 * @param array $args Additional arguments.
447 *
448 * @return false|array
449 */
450 public function get_meta( $form_id, $field = '', $args = array() ) {
451 if ( empty( $form_id ) ) {
452 return false;
453 }
454
455 if ( isset( $args['cap'] ) ) {
456 $defaults['cap'] = $args['cap'];
457 }
458
459 $data = $this->get(
460 $form_id,
461 array(
462 'content_only' => true,
463 )
464 );
465
466 if ( isset( $data['meta'] ) ) {
467 if ( empty( $field ) ) {
468 return $data['meta'];
469 } elseif ( isset( $data['meta'][ $field ] ) ) {
470 return $data['meta'][ $field ];
471 }
472 }
473
474 return false;
475 }
476
477 /**
478 * Get the next available field ID and increment by one.
479 *
480 * @since 1.0.0
481 * @param int $form_id Form ID.
482 * @return mixed int or false
483 */
484 public function field_unique_key( $form_id ) {
485 if ( ! current_user_can( 'everest_forms_edit_form', $form_id ) ) {
486 return false;
487 }
488
489 if ( empty( $form_id ) ) {
490 return false;
491 }
492
493 $form = $this->get(
494 $form_id,
495 array(
496 'content_only' => true,
497 )
498 );
499
500 if ( ! empty( $form['form_field_id'] ) ) {
501 $form_field_id = absint( $form['form_field_id'] );
502 $form['form_field_id'] ++;
503 } else {
504 $form_field_id = '0';
505 $form['form_field_id'] = '1';
506 }
507
508 $this->update( $form_id, $form );
509
510 $field_id = evf_get_random_string() . '-' . $form_field_id;
511
512 return $field_id;
513 }
514
515 /**
516 * Get private meta information for a form field.
517 *
518 * @since 1.0.0
519 *
520 * @param int $form_id Form ID.
521 * @param string $field_id Field ID.
522 * @param array $args Additional arguments.
523 *
524 * @return array|bool
525 */
526 public function get_field( $form_id, $field_id = '', $args = array() ) {
527 if ( empty( $form_id ) ) {
528 return false;
529 }
530
531 if ( isset( $args['cap'] ) ) {
532 $defaults['cap'] = $args['cap'];
533 }
534
535 $data = $this->get(
536 $form_id,
537 array(
538 'content_only' => true,
539 )
540 );
541
542 return isset( $data['form_fields'][ $field_id ] ) ? $data['form_fields'][ $field_id ] : false;
543 }
544
545 /**
546 * Get private meta information for a form field.
547 *
548 * @since 1.0.0
549 *
550 * @param int $form_id Form ID.
551 * @param string $field_id Field.
552 * @param array $args Additional arguments.
553 *
554 * @return bool
555 */
556 public function get_field_meta( $form_id, $field_id = '', $args = array() ) {
557 $field = $this->get_field( $form_id, $field_id, $args );
558 if ( ! $field ) {
559 return false;
560 }
561
562 return isset( $field['meta'] ) ? $field['meta'] : false;
563 }
564 }
565