PluginProbe ʕ •ᴥ•ʔ
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / trunk
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI vtrunk
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
Helpers 11 months ago Integrations 1 week ago RestApi 2 weeks ago abilities 2 weeks ago abstracts 2 weeks ago admin 2 weeks ago blocks 1 year ago elementor 2 years ago export 2 months ago fields 2 weeks ago interfaces 8 years ago libraries 2 years ago log-handlers 1 year ago shortcodes 2 weeks ago stats 5 months ago templates 3 months ago traits 2 weeks ago class-everest-forms.php 1 week ago class-evf-addon-upsell.php 2 weeks ago class-evf-ajax.php 2 weeks ago class-evf-autoloader.php 7 years ago class-evf-background-process-import-entries.php 1 year ago class-evf-background-updater.php 7 years ago class-evf-cache-helper.php 2 months ago class-evf-cron.php 2 years ago class-evf-deprecated-action-hooks.php 6 years ago class-evf-deprecated-filter-hooks.php 5 years ago class-evf-email-entries-report.php 3 months ago class-evf-emails.php 2 weeks ago class-evf-fields.php 2 weeks ago class-evf-form-handler.php 2 weeks ago class-evf-form-task.php 2 weeks ago class-evf-forms-features.php 2 weeks ago class-evf-frontend-scripts.php 2 weeks ago class-evf-install.php 2 months ago class-evf-integrations.php 3 months ago class-evf-log-levels.php 8 years ago class-evf-logger.php 5 years ago class-evf-post-types.php 1 year ago class-evf-privacy.php 6 years ago class-evf-report-cron.php 2 months ago class-evf-reporting.php 2 months ago class-evf-session-handler.php 7 years ago class-evf-shortcodes.php 1 year ago class-evf-smart-tags.php 9 months ago class-evf-template-loader.php 1 year ago class-evf-validation.php 6 years ago evf-conditional-functions.php 6 years ago evf-core-functions.php 2 weeks ago evf-deprecated-functions.php 6 years ago evf-entry-functions.php 4 months 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
595 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'] = array(
36 'everest_forms_view_form',
37 'everest_forms_create_forms',
38 );
39 }
40
41 if ( ! empty( $id ) ) {
42 if ( ! empty( $args['cap'] ) ) {
43 $caps = (array) $args['cap'];
44 $has_cap = false;
45 foreach ( $caps as $cap ) {
46 if ( current_user_can( $cap, $id ) ) {
47 $has_cap = true;
48 break;
49 }
50 }
51 if ( ! $has_cap ) {
52 return false;
53 }
54 }
55
56 $the_post = get_post( absint( $id ) );
57
58 if ( $the_post && 'everest_form' === $the_post->post_type ) {
59 $forms = empty( $args['content_only'] ) ? $the_post : evf_decode( $the_post->post_content );
60 }
61 } else {
62 // No ID provided, get multiple forms.
63 $args = wp_parse_args(
64 $args,
65 array(
66 'order' => 'DESC',
67 )
68 );
69
70 $forms = $this->get_multiple( $args );
71 }
72
73 if ( empty( $forms ) ) {
74 return false;
75 }
76
77 return $forms;
78 }
79
80 /**
81 * Fetch multiple forms.
82 *
83 * @since 1.7.0
84 *
85 * @param array $args Additional arguments array.
86 * @param bool $content_only True to return post content only.
87 *
88 * @return array
89 */
90 public function get_multiple( $args = array(), $content_only = false ) {
91 $forms = array();
92 $user_id = get_current_user_id();
93 $args = apply_filters( 'everest_forms_get_multiple_forms_args', $args, $content_only );
94
95 // No ID provided, get multiple forms.
96 $defaults = array(
97 'orderby' => 'id',
98 'order' => 'ASC',
99 'no_found_rows' => true,
100 'nopaging' => true,
101 'status' => 'publish',
102 'post_status' => 'publish',
103 'numberposts' => -1,
104 );
105
106 $args = wp_parse_args( $args, $defaults );
107
108 $args['post_type'] = 'everest_form';
109
110 // Can user interact, lets check the view capabilities?
111 if ( current_user_can( 'everest_forms_view_forms' ) && ! current_user_can( 'everest_forms_view_others_forms' ) ) {
112 $args['author'] = $user_id;
113 }
114
115 if ( ! current_user_can( 'everest_forms_view_forms' ) && current_user_can( 'everest_forms_view_others_forms' ) ) {
116 $args['author__not_in'] = $user_id;
117 }
118
119 if ( ! current_user_can( 'everest_forms_view_forms' ) && ! current_user_can( 'everest_forms_view_others_forms' ) ) {
120 if ( isset( $args['cap'] ) && ( 'everest_forms_view_conversational_forms' !== $args['cap'] && 'everest_forms_pro_view_landing_page' !== $args['cap'] ) ) {
121 $args['post__in'] = array( 0 );
122 }
123 }
124
125 // For cache lets unset the cap args.
126 unset( $args['cap'] );
127
128 // Fetch posts.
129 $forms = get_posts( $args );
130
131 if ( $content_only ) {
132 $forms = array_map( array( $this, 'prepare_post_content' ), $forms );
133 }
134
135 return $forms;
136 }
137
138 /**
139 * Prepares post content.
140 *
141 * @param object $post Post object.
142 */
143 public function prepare_post_content( $post ) {
144 return ! empty( $post->post_content ) ? evf_decode( $post->post_content ) : false;
145 }
146
147 /**
148 * Delete forms.
149 *
150 * @since 1.0.0
151 * @param array $ids Form IDs.
152 * @return boolean
153 */
154 public function delete( $ids = array() ) {
155 if ( ! is_array( $ids ) ) {
156 $ids = array( $ids );
157 }
158
159 $ids = array_map( 'absint', $ids );
160
161 foreach ( $ids as $id ) {
162
163 // Check for permissions.
164 if ( ! current_user_can( 'everest_forms_delete', $id ) ) {
165 return false;
166 }
167
168 $form = wp_delete_post( $id, true );
169
170 if ( ! $form ) {
171 return false;
172 }
173 }
174
175 do_action( 'everest_forms_delete_form', $ids );
176
177 return true;
178 }
179
180 /**
181 * Create new form.
182 *
183 * @since 1.0.0
184 * @param string $title Form title.
185 * @param string $template Form template.
186 * @param array $args Form Arguments.
187 * @param array $data Additional data.
188 * @return int|bool Form ID on successful creation else false.
189 */
190 public function create( $title = '', $template = 'blank', $args = array(), $data = array() ) {
191 if ( empty( $title ) || ! current_user_can( 'everest_forms_create_forms' ) ) {
192 return false;
193 }
194
195 $args = apply_filters( 'everest_forms_create_form_args', $args, $data );
196 $form_style = array();
197 $style_needed = false;
198 $form_content = array(
199 'form_field_id' => '1',
200 'settings' => array(
201 'form_title' => sanitize_text_field( $title ),
202 'form_desc' => '',
203 ),
204 );
205
206 // Prevent content filters from corrupting JSON in post_content.
207 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
208 if ( $has_kses ) {
209 kses_remove_filters();
210 }
211 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
212 if ( $has_targeted_link_rel_filters ) {
213 wp_remove_targeted_link_rel_filters();
214 }
215
216 // Create a form.
217 $form_id = wp_insert_post(
218 array(
219 'post_title' => esc_html( $title ),
220 'post_status' => 'publish',
221 'post_type' => 'everest_form',
222 'post_content' => '{}',
223 )
224 );
225
226 $templates = EVF_Admin_Form_Templates::get_template_data();
227 $templates = is_array( $templates ) ? $templates[0]->templates : array();
228 if ( ! empty( $templates ) ) {
229 foreach ( $templates as $template_data ) {
230 if ( $template_data->slug === $template && 'blank' !== $template_data->slug ) {
231 $form_content = json_decode( base64_decode( $template_data->settings ), true );
232 if ( isset( $template_data->styles ) ) {
233 $style_needed = true;
234 $form_style[ $form_id ] = json_decode( base64_decode( $template_data->styles ), true );
235 }
236 }
237 }
238 }
239
240 if ( $form_id ) {
241 $form_content['id'] = $form_id;
242 $form_content['settings']['form_title'] = $title;
243 $form_content['imported_form_templates'] = $template;
244 // Mark as a fresh builder form (not yet saved in the editor). Cleared on first builder save.
245 // Add-ons may read this from encoded post_content to skip legacy setup UI for brand-new forms.
246 $form_content['is_new_form'] = true;
247
248 $form_data = wp_parse_args(
249 $args,
250 array(
251 'ID' => $form_id,
252 'post_title' => esc_html( $title ),
253 'post_content' => evf_encode( array_merge( array( 'id' => $form_id ), $form_content ) ),
254 )
255 );
256
257 wp_update_post( $form_data );
258
259 if ( ! empty( $form_style ) ) {
260 update_option( 'everest_forms_styles', $form_style );
261 }
262 }
263
264 // Restore removed content filters.
265 if ( $has_kses ) {
266 kses_init_filters();
267 }
268 if ( $has_targeted_link_rel_filters ) {
269 wp_init_targeted_link_rel_filters();
270 }
271
272 do_action( 'everest_forms_create_form', $form_id, $form_data, $data, $style_needed );
273
274 return $form_id;
275 }
276
277 /**
278 * Updates form
279 *
280 * @since 1.0.0
281 *
282 * @param string|int $form_id Form ID.
283 * @param array $data Data retrieved from $_POST and processed.
284 * @param array $args Empty by default, may have custom data not intended to be saved.
285 *
286 * @return mixed
287 * @internal param string $title
288 */
289 public function update( $form_id = '', $data = array(), $args = array() ) {
290 if ( empty( $data ) ) {
291 return false;
292 }
293
294 if ( empty( $form_id ) ) {
295 $form_id = $data['form_id'];
296 }
297
298 if ( ! isset( $args['cap'] ) ) {
299 $args['cap'] = 'everest_forms_edit_form';
300 }
301
302 // Check for permissions.
303 if ( ! empty( $args['cap'] ) && ! current_user_can( $args['cap'], $form_id ) ) {
304 return false;
305 }
306
307 $data = wp_unslash( $data );
308
309 if ( ! empty( $data['settings']['form_title'] ) ) {
310 $title = $data['settings']['form_title'];
311 } else {
312 $title = get_the_title( $form_id );
313 }
314
315 if ( ! empty( $data['settings']['form_desc'] ) ) {
316 $desc = $data['settings']['form_desc'];
317 } else {
318 $desc = '';
319 }
320
321 $data['form_field_id'] = ! empty( $data['form_field_id'] ) ? absint( $data['form_field_id'] ) : '0';
322
323 // This filter can destroy the JSON when messing with HTML.
324 remove_filter( 'content_save_pre', 'balanceTags', 50 );
325
326 // Don't allow tags for users who do not have appropriate cap.
327 if ( ! current_user_can( 'unfiltered_html' ) ) {
328 $data = map_deep( $data, 'wp_strip_all_tags' );
329 }
330
331 // Prevent content filters from corrupting JSON in post_content.
332 $has_kses = ( false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' ) );
333 if ( $has_kses ) {
334 kses_remove_filters();
335 }
336 $has_targeted_link_rel_filters = ( false !== has_filter( 'content_save_pre', 'wp_targeted_link_rel' ) );
337 if ( $has_targeted_link_rel_filters ) {
338 wp_remove_targeted_link_rel_filters();
339 }
340
341 $evf_form_data = evf()->form->get( $form_id, array( 'content_only' => true ) );
342
343 if ( isset( $evf_form_data['meta']['entry_columns'] ) && ! isset( $data['meta']['entry_columns'] ) ) {
344 $data['meta']['entry_columns'] = $evf_form_data['meta']['entry_columns'];
345 }
346
347 $form = array(
348 'ID' => $form_id,
349 'post_title' => esc_html( $title ),
350 'post_excerpt' => $desc,
351 'post_content' => evf_encode( $data ),
352 );
353 $form = apply_filters( 'everest_forms_save_form_args', $form, $data, $args );
354 $form_id = wp_update_post( $form );
355
356 // Import form styles if present.
357 $style_needed = false;
358 if ( ! empty( $data['form_styles'] ) ) {
359 $style_needed = true;
360 $form_styles = get_option( 'everest_forms_styles', array() );
361 $form_styles[ $form_id ] = evf_decode( $data['form_styles'] );
362
363 // Update forms styles.
364 update_option( 'everest_forms_styles', $form_styles );
365 }
366
367 // Restore removed content filters.
368 if ( $has_kses ) {
369 kses_init_filters();
370 }
371 if ( $has_targeted_link_rel_filters ) {
372 wp_init_targeted_link_rel_filters();
373 }
374
375 do_action( 'everest_forms_save_form', $form_id, $form, array(), $style_needed );
376
377 return $form_id;
378 }
379
380 /**
381 * Duplicate forms.
382 *
383 * @since 1.0.0
384 *
385 * @param array $ids Form IDs to duplicate.
386 *
387 * @return boolean
388 */
389 public function duplicate( $ids = array() ) {
390 // Check for permissions.
391 if ( ! current_user_can( 'everest_forms_create_forms' ) ) {
392 return false;
393 }
394
395 if ( ! is_array( $ids ) ) {
396 $ids = array( $ids );
397 }
398
399 $ids = array_map( 'absint', $ids );
400
401 foreach ( $ids as $id ) {
402
403 // Get original entry.
404 $form = get_post( $id );
405
406 if ( ! current_user_can( 'everest_forms_view_form', $id ) ) {
407 return false;
408 }
409
410 // Confirm form exists.
411 if ( ! $form || empty( $form ) ) {
412 return false;
413 }
414
415 // Get the form data.
416 $new_form_data = evf_decode( $form->post_content );
417
418 // Get the form styles.
419 $form_styles = get_option( 'everest_forms_styles', array() );
420 if ( ! empty( $form_styles[ $id ] ) ) {
421 $new_form_data['form_styles'] = wp_json_encode( $form_styles[ $id ] );
422 }
423
424 // Remove form ID from title if present.
425 $new_form_data['settings']['form_title'] = str_replace( '(ID #' . absint( $id ) . ')', '', $new_form_data['settings']['form_title'] );
426
427 // Create the duplicate form.
428 $new_form = array(
429 'post_author' => $form->post_author,
430 'post_content' => evf_encode( $new_form_data ),
431 'post_excerpt' => $form->post_excerpt,
432 'post_status' => $form->post_status,
433 'post_title' => $new_form_data['settings']['form_title'],
434 'post_type' => $form->post_type,
435 );
436 $new_form_id = wp_insert_post( $new_form );
437
438 if ( ! $new_form_id || is_wp_error( $new_form_id ) ) {
439 return false;
440 }
441
442 // Set new form name.
443 $new_form_data['settings']['form_title'] .= ' (ID #' . absint( $new_form_id ) . ')';
444
445 // Set new form ID.
446 $new_form_data['id'] = absint( $new_form_id );
447 // Duplicates are new posts; treat like template creation until the builder saves once.
448 $new_form_data['is_new_form'] = true;
449
450 // Update new duplicate form.
451 $new_form_id = $this->update( $new_form_id, $new_form_data );
452
453 if ( ! $new_form_id || is_wp_error( $new_form_id ) ) {
454 return false;
455 }
456
457 $form_styles = get_option( 'everest_forms_styles', array() );
458 if ( isset( $form_styles[ $id ] ) ) {
459 $form_styles[ $new_form_id ] = $form_styles[ $id ];
460 update_option( 'everest_forms_styles', $form_styles );
461 }
462
463 return $new_form_id;
464 }
465
466 return true;
467 }
468
469 /**
470 * Get private meta information for a form.
471 *
472 * @since 1.1.0
473 *
474 * @param int $form_id Form ID.
475 * @param string $field Field.
476 * @param array $args Additional arguments.
477 *
478 * @return false|array
479 */
480 public function get_meta( $form_id, $field = '', $args = array() ) {
481 if ( empty( $form_id ) ) {
482 return false;
483 }
484
485 if ( isset( $args['cap'] ) ) {
486 $defaults['cap'] = $args['cap'];
487 }
488
489 $data = $this->get(
490 $form_id,
491 array(
492 'content_only' => true,
493 )
494 );
495
496 if ( isset( $data['meta'] ) ) {
497 if ( empty( $field ) ) {
498 return $data['meta'];
499 } elseif ( isset( $data['meta'][ $field ] ) ) {
500 return $data['meta'][ $field ];
501 }
502 }
503
504 return false;
505 }
506
507 /**
508 * Get the next available field ID and increment by one.
509 *
510 * @since 1.0.0
511 * @param int $form_id Form ID.
512 * @return mixed int or false
513 */
514 public function field_unique_key( $form_id ) {
515 if ( ! current_user_can( 'everest_forms_edit_form', $form_id ) ) {
516 return false;
517 }
518
519 if ( empty( $form_id ) ) {
520 return false;
521 }
522
523 $form = $this->get(
524 $form_id,
525 array(
526 'content_only' => true,
527 )
528 );
529
530 if ( ! empty( $form['form_field_id'] ) ) {
531 $form_field_id = absint( $form['form_field_id'] );
532 $form['form_field_id'] ++;
533 } else {
534 $form_field_id = '0';
535 $form['form_field_id'] = '1';
536 }
537
538 $this->update( $form_id, $form );
539
540 $field_id = evf_get_random_string() . '-' . $form_field_id;
541
542 return $field_id;
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 ID.
552 * @param array $args Additional arguments.
553 *
554 * @return array|bool
555 */
556 public function get_field( $form_id, $field_id = '', $args = array() ) {
557 if ( empty( $form_id ) ) {
558 return false;
559 }
560
561 if ( isset( $args['cap'] ) ) {
562 $defaults['cap'] = $args['cap'];
563 }
564
565 $data = $this->get(
566 $form_id,
567 array(
568 'content_only' => true,
569 )
570 );
571
572 return isset( $data['form_fields'][ $field_id ] ) ? $data['form_fields'][ $field_id ] : false;
573 }
574
575 /**
576 * Get private meta information for a form field.
577 *
578 * @since 1.0.0
579 *
580 * @param int $form_id Form ID.
581 * @param string $field_id Field.
582 * @param array $args Additional arguments.
583 *
584 * @return bool
585 */
586 public function get_field_meta( $form_id, $field_id = '', $args = array() ) {
587 $field = $this->get_field( $form_id, $field_id, $args );
588 if ( ! $field ) {
589 return false;
590 }
591
592 return isset( $field['meta'] ) ? $field['meta'] : false;
593 }
594 }
595