PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.01.02
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.01.02
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmForm.php

FrmForm.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 4.01.02, at classes/models/FrmForm.php

934 lines 25.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmForm {
7
8 /**
9 * @return int|boolean id on success or false on failure
10 */
11 public static function create( $values ) {
12 global $wpdb;
13
14 $new_values = array(
15 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ),
16 'name' => $values['name'],
17 'description' => $values['description'],
18 'status' => isset( $values['status'] ) ? $values['status'] : 'published',
19 'logged_in' => isset( $values['logged_in'] ) ? $values['logged_in'] : 0,
20 'is_template' => isset( $values['is_template'] ) ? (int) $values['is_template'] : 0,
21 'parent_form_id' => isset( $values['parent_form_id'] ) ? absint( $values['parent_form_id'] ) : 0,
22 'editable' => isset( $values['editable'] ) ? (int) $values['editable'] : 0,
23 'created_at' => isset( $values['created_at'] ) ? $values['created_at'] : current_time( 'mysql', 1 ),
24 );
25
26 $options = isset( $values['options'] ) ? (array) $values['options'] : array();
27 FrmFormsHelper::fill_form_options( $options, $values );
28
29 $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
30 $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
31 $options['submit_html'] = isset( $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
32
33 $options = apply_filters( 'frm_form_options_before_update', $options, $values );
34 $new_values['options'] = serialize( $options );
35
36 //if(isset($values['id']) && is_numeric($values['id']))
37 // $new_values['id'] = $values['id'];
38
39 $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values );
40
41 $id = $wpdb->insert_id;
42
43 // Clear form caching
44 self::clear_form_cache();
45
46 return $id;
47 }
48
49 /**
50 * @return int|boolean ID on success or false on failure
51 */
52 public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) {
53 global $wpdb;
54
55 $values = self::getOne( $id, $blog_id );
56 if ( ! $values ) {
57 return false;
58 }
59
60 $new_key = $copy_keys ? $values->form_key : '';
61
62 $new_values = array(
63 'form_key' => FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_forms', 'form_key' ),
64 'name' => $values->name,
65 'description' => $values->description,
66 'status' => $values->status ? $values->status : 'published',
67 'logged_in' => $values->logged_in ? $values->logged_in : 0,
68 'editable' => $values->editable ? $values->editable : 0,
69 'created_at' => current_time( 'mysql', 1 ),
70 'is_template' => $template ? 1 : 0,
71 );
72
73 if ( $blog_id ) {
74 $new_values['status'] = 'published';
75 $new_options = maybe_unserialize( $values->options );
76 $new_options['email_to'] = get_option( 'admin_email' );
77 $new_options['copy'] = false;
78 $new_values['options'] = $new_options;
79 } else {
80 $new_values['options'] = $values->options;
81 }
82
83 if ( is_array( $new_values['options'] ) ) {
84 $new_values['options'] = serialize( $new_values['options'] );
85 }
86
87 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values );
88
89 if ( $query_results ) {
90 // Clear form caching
91 self::clear_form_cache();
92
93 $form_id = $wpdb->insert_id;
94 FrmField::duplicate( $id, $form_id, $copy_keys, $blog_id );
95
96 // update form settings after fields are created
97 do_action( 'frm_after_duplicate_form', $form_id, $new_values, array( 'old_id' => $id ) );
98
99 return $form_id;
100 }
101
102 return false;
103 }
104
105 public static function after_duplicate( $form_id, $values ) {
106 $new_opts = maybe_unserialize( $values['options'] );
107 $values['options'] = $new_opts;
108
109 if ( isset( $new_opts['success_msg'] ) ) {
110 $new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids( $new_opts['success_msg'] );
111 }
112
113 $new_opts = apply_filters( 'frm_after_duplicate_form_values', $new_opts, $form_id );
114
115 if ( $new_opts != $values['options'] ) {
116 global $wpdb;
117 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) );
118 }
119 }
120
121 /**
122 * @return int|boolean
123 */
124 public static function update( $id, $values, $create_link = false ) {
125 global $wpdb;
126
127 if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) {
128 $values['status'] = 'published';
129 }
130
131 if ( isset( $values['form_key'] ) ) {
132 $values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id );
133 }
134
135 $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' );
136
137 $new_values = self::set_update_options( array(), $values );
138
139 foreach ( $values as $value_key => $value ) {
140 if ( $value_key && in_array( $value_key, $form_fields ) ) {
141 $new_values[ $value_key ] = $value;
142 }
143 }
144
145 if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) {
146 $new_values['status'] = $values['new_status'];
147 }
148
149 if ( ! empty( $new_values ) ) {
150 $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) );
151 if ( $query_results ) {
152 self::clear_form_cache();
153 }
154 } else {
155 $query_results = true;
156 }
157 unset( $new_values );
158
159 $values = self::update_fields( $id, $values );
160
161 do_action( 'frm_update_form', $id, $values );
162 do_action( 'frm_update_form_' . $id, $values );
163
164 return $query_results;
165 }
166
167 /**
168 * @return array
169 */
170 public static function set_update_options( $new_values, $values ) {
171 if ( ! isset( $values['options'] ) ) {
172 return $new_values;
173 }
174
175 $options = isset( $values['options'] ) ? (array) $values['options'] : array();
176 FrmFormsHelper::fill_form_options( $options, $values );
177
178 $options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0;
179 $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
180 $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
181 $options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
182
183 $options = apply_filters( 'frm_form_options_before_update', $options, $values );
184 $new_values['options'] = serialize( $options );
185
186 return $new_values;
187 }
188
189 /**
190 * @return array
191 */
192 public static function update_fields( $id, $values ) {
193
194 if ( ! isset( $values['item_meta'] ) && ! isset( $values['field_options'] ) ) {
195 return $values;
196 }
197
198 $all_fields = FrmField::get_all_for_form( $id );
199 if ( empty( $all_fields ) ) {
200 return $values;
201 }
202
203 if ( ! isset( $values['item_meta'] ) ) {
204 $values['item_meta'] = array();
205 }
206
207 $field_array = array();
208 $existing_keys = array_keys( $values['item_meta'] );
209 foreach ( $all_fields as $fid ) {
210 if ( ! in_array( $fid->id, $existing_keys ) && ( isset( $values['frm_fields_submitted'] ) && in_array( $fid->id, $values['frm_fields_submitted'] ) ) || isset( $values['options'] ) ) {
211 $values['item_meta'][ $fid->id ] = '';
212 }
213 $field_array[ $fid->id ] = $fid;
214 }
215 unset( $all_fields );
216
217 foreach ( $values['item_meta'] as $field_id => $default_value ) {
218 if ( isset( $field_array[ $field_id ] ) ) {
219 $field = $field_array[ $field_id ];
220 } else {
221 $field = FrmField::getOne( $field_id );
222 }
223
224 if ( ! $field ) {
225 continue;
226 }
227
228 $is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) );
229 if ( $is_settings_page ) {
230 self::get_settings_page_html( $values, $field );
231
232 if ( ! defined( 'WP_IMPORTING' ) ) {
233 continue;
234 }
235 }
236
237 //updating the form
238 $update_options = FrmFieldsHelper::get_default_field_options_from_field( $field );
239 unset( $update_options['custom_html'] ); // don't check for POST html
240 $update_options = apply_filters( 'frm_field_options_to_update', $update_options );
241
242 foreach ( $update_options as $opt => $default ) {
243 $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default;
244 self::sanitize_field_opt( $opt, $field->field_options[ $opt ] );
245 }
246
247 $field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values );
248
249 $new_field = array(
250 'field_options' => $field->field_options,
251 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? maybe_serialize( $values[ 'default_value_' . $field_id ] ) : '',
252 );
253
254 self::prepare_field_update_values( $field, $values, $new_field );
255
256 FrmField::update( $field_id, $new_field );
257
258 FrmField::delete_form_transient( $field->form_id );
259 }
260 self::clear_form_cache();
261
262 return $values;
263 }
264
265 private static function sanitize_field_opt( $opt, &$value ) {
266 if ( is_string( $value ) ) {
267 if ( $opt === 'calc' ) {
268 $value = strip_tags( $value );
269 } else {
270 $value = FrmAppHelper::kses( $value, 'all' );
271 }
272 $value = trim( $value );
273 }
274 }
275
276 /**
277 * Updating the settings page
278 */
279 private static function get_settings_page_html( $values, &$field ) {
280 if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) {
281 $prev_opts = array();
282 $fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type );
283
284 $field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ? $values['field_options'][ 'custom_html_' . $field->id ] : $fallback_html;
285 } elseif ( $field->type == 'hidden' || $field->type == 'user_id' ) {
286 $prev_opts = $field->field_options;
287 }
288
289 if ( isset( $prev_opts ) ) {
290 $field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values );
291 if ( $prev_opts != $field->field_options ) {
292 FrmField::update( $field->id, array( 'field_options' => $field->field_options ) );
293 }
294 }
295 }
296
297 private static function prepare_field_update_values( $field, $values, &$new_field ) {
298 $field_cols = array(
299 'field_order' => 0,
300 'field_key' => '',
301 'required' => false,
302 'type' => '',
303 'description' => '',
304 'options' => '',
305 'name' => '',
306 );
307 foreach ( $field_cols as $col => $default ) {
308 $default = ( $default === '' ) ? $field->{$col} : $default;
309
310 $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default;
311 }
312
313 // Don't save the template option.
314 if ( is_array( $new_field['options'] ) && isset( $new_field['options']['000'] ) ) {
315 unset( $new_field['options']['000'] );
316 }
317 }
318
319 /**
320 * Get a list of all form settings that should be translated
321 * on a multilingual site.
322 *
323 * @since 3.06.01
324 * @param object $form - The form object
325 */
326 public static function translatable_strings( $form ) {
327 $strings = array(
328 'name',
329 'description',
330 'submit_value',
331 'submit_msg',
332 'success_msg',
333 );
334
335 return apply_filters( 'frm_form_strings', $strings, $form );
336 }
337
338 /**
339 * @param string $status
340 *
341 * @return int|boolean
342 */
343 public static function set_status( $id, $status ) {
344 if ( 'trash' == $status ) {
345 return self::trash( $id );
346 }
347
348 $statuses = array( 'published', 'draft', 'trash' );
349 if ( ! in_array( $status, $statuses ) ) {
350 return false;
351 }
352
353 global $wpdb;
354
355 if ( is_array( $id ) ) {
356 $where = array(
357 'id' => $id,
358 'parent_form_id' => $id,
359 'or' => 1,
360 );
361 FrmDb::get_where_clause_and_values( $where );
362 array_unshift( $where['values'], $status );
363
364 $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // WPCS: unprepared SQL ok.
365 } else {
366 $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) );
367 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) );
368 }
369
370 if ( $query_results ) {
371 self::clear_form_cache();
372 }
373
374 return $query_results;
375 }
376
377 /**
378 * @return int|boolean
379 */
380 public static function trash( $id ) {
381 if ( ! EMPTY_TRASH_DAYS ) {
382 return self::destroy( $id );
383 }
384
385 $form = self::getOne( $id );
386 if ( ! $form ) {
387 return false;
388 }
389
390 $options = $form->options;
391 $options['trash_time'] = time();
392
393 global $wpdb;
394 $query_results = $wpdb->update(
395 $wpdb->prefix . 'frm_forms',
396 array(
397 'status' => 'trash',
398 'options' => serialize( $options ),
399 ),
400 array(
401 'id' => $id,
402 )
403 );
404
405 $wpdb->update(
406 $wpdb->prefix . 'frm_forms',
407 array(
408 'status' => 'trash',
409 'options' => serialize( $options ),
410 ),
411 array(
412 'parent_form_id' => $id,
413 )
414 );
415
416 if ( $query_results ) {
417 self::clear_form_cache();
418 }
419
420 return $query_results;
421 }
422
423 /**
424 * @return int|boolean
425 */
426 public static function destroy( $id ) {
427 global $wpdb;
428
429 $form = self::getOne( $id );
430 if ( ! $form ) {
431 return false;
432 }
433 $id = $form->id;
434
435 // Disconnect the entries from this form
436 $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) );
437 foreach ( $entries as $entry_id ) {
438 FrmEntry::destroy( $entry_id );
439 unset( $entry_id );
440 }
441
442 // Disconnect the fields from this form
443 $wpdb->query( $wpdb->prepare( 'DELETE fi FROM ' . $wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $id, $id ) );
444
445 $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) );
446 if ( $query_results ) {
447 // Delete all form actions linked to this form
448 $action_control = FrmFormActionsController::get_form_actions( 'email' );
449 $action_control->destroy( $id, 'all' );
450
451 // Clear form caching
452 self::clear_form_cache();
453
454 do_action( 'frm_destroy_form', $id );
455 do_action( 'frm_destroy_form_' . $id );
456 }
457
458 return $query_results;
459 }
460
461 /**
462 * Delete trashed forms based on how long they have been trashed
463 *
464 * @return int The number of forms deleted
465 */
466 public static function scheduled_delete( $delete_timestamp = '' ) {
467 global $wpdb;
468
469 $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' );
470
471 if ( ! $trash_forms ) {
472 return;
473 }
474
475 if ( empty( $delete_timestamp ) ) {
476 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
477 }
478
479 $count = 0;
480 foreach ( $trash_forms as $form ) {
481 $form->options = maybe_unserialize( $form->options );
482 if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) {
483 self::destroy( $form->id );
484 $count ++;
485 }
486
487 unset( $form );
488 }
489
490 return $count;
491 }
492
493 /**
494 * @return string form name
495 */
496 public static function getName( $id ) {
497 $form = FrmDb::check_cache( $id, 'frm_form' );
498 if ( $form ) {
499 $r = stripslashes( $form->name );
500
501 return $r;
502 }
503
504 $query_key = is_numeric( $id ) ? 'id' : 'form_key';
505 $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' );
506 $r = stripslashes( $r );
507
508 return $r;
509 }
510
511 /**
512 * @since 3.0
513 *
514 * @param string $key
515 *
516 * @return int form id
517 */
518 public static function get_id_by_key( $key ) {
519 return (int) FrmDb::get_var( 'frm_forms', array( 'form_key' => sanitize_title( $key ) ) );
520 }
521
522 /**
523 * @since 3.0
524 *
525 * @param int $id
526 *
527 * @return string form key
528 */
529 public static function get_key_by_id( $id ) {
530 $id = (int) $id;
531 $cache = FrmDb::check_cache( $id, 'frm_form' );
532 if ( $cache ) {
533 return $cache->form_key;
534 }
535
536 $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' );
537
538 return $key;
539 }
540
541 /**
542 * If $form is numeric, get the form object
543 *
544 * @param object|int $form
545 *
546 * @since 2.0.9
547 */
548 public static function maybe_get_form( &$form ) {
549 if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) {
550 $form = self::getOne( $form );
551 }
552 }
553
554 /**
555 * @return object form
556 */
557 public static function getOne( $id, $blog_id = false ) {
558 global $wpdb;
559
560 if ( $blog_id && is_multisite() ) {
561 global $wpmuBaseTablePrefix;
562 $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id );
563
564 $table_name = $prefix . 'frm_forms';
565 } else {
566 $table_name = $wpdb->prefix . 'frm_forms';
567 $cache = wp_cache_get( $id, 'frm_form' );
568 if ( $cache ) {
569 if ( isset( $cache->options ) ) {
570 $cache->options = maybe_unserialize( $cache->options );
571 }
572
573 return wp_unslash( $cache );
574 }
575 }
576
577 if ( is_numeric( $id ) ) {
578 $where = array( 'id' => $id );
579 } else {
580 $where = array( 'form_key' => $id );
581 }
582
583 $results = FrmDb::get_row( $table_name, $where );
584
585 if ( isset( $results->options ) ) {
586 FrmDb::set_cache( $results->id, $results, 'frm_form' );
587 $results->options = maybe_unserialize( $results->options );
588 }
589
590 return wp_unslash( $results );
591 }
592
593 /**
594 * @return object|array of objects
595 */
596 public static function getAll( $where = array(), $order_by = '', $limit = '' ) {
597 if ( is_array( $where ) && ! empty( $where ) ) {
598 if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
599 // don't get trashed templates
600 $where['status'] = array( null, '', 'published' );
601 }
602
603 $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) );
604 } else {
605 global $wpdb;
606
607 // the query has already been prepared if this is not an array
608 $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit );
609 $results = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok.
610 }
611
612 if ( $results ) {
613 foreach ( $results as $result ) {
614 FrmDb::set_cache( $result->id, $result, 'frm_form' );
615 $result->options = maybe_unserialize( $result->options );
616 }
617 }
618
619 if ( $limit == ' LIMIT 1' || $limit == 1 ) {
620 // return the first form object if we are only getting one form
621 $results = reset( $results );
622 }
623
624 return wp_unslash( $results );
625 }
626
627 /**
628 * Get all published forms
629 *
630 * @since 2.0
631 * @return array of forms
632 */
633 public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) {
634 $query['is_template'] = 0;
635 $query['status'] = array( null, '', 'published' );
636 if ( $inc_children == 'exclude' ) {
637 $query['parent_form_id'] = array( null, 0 );
638 }
639
640 $forms = self::getAll( $query, 'name', $limit );
641
642 return $forms;
643 }
644
645 /**
646 * @return object count of forms
647 */
648 public static function get_count() {
649 global $wpdb;
650
651 $cache_key = 'frm_form_counts';
652
653 $counts = wp_cache_get( $cache_key, 'frm_form' );
654 if ( false !== $counts ) {
655 return $counts;
656 }
657
658 $results = (array) FrmDb::get_results(
659 'frm_forms',
660 array(
661 'or' => 1,
662 'parent_form_id' => null,
663 'parent_form_id <' => 0,
664 ),
665 'status, is_template'
666 );
667
668 $statuses = array( 'published', 'draft', 'template', 'trash' );
669 $counts = array_fill_keys( $statuses, 0 );
670
671 foreach ( $results as $row ) {
672 if ( 'trash' != $row->status ) {
673 if ( $row->is_template ) {
674 $counts['template'] ++;
675 } else {
676 $counts['published'] ++;
677 }
678 } else {
679 $counts['trash'] ++;
680 }
681
682 if ( 'draft' == $row->status ) {
683 $counts['draft'] ++;
684 }
685
686 unset( $row );
687 }
688
689 $counts = (object) $counts;
690 FrmDb::set_cache( $cache_key, $counts, 'frm_form' );
691
692 return $counts;
693 }
694
695 /**
696 * Clear form caching
697 * Called when a form is created, updated, duplicated, or deleted
698 * or when the form status is changed
699 *
700 * @since 2.0.4
701 */
702 public static function clear_form_cache() {
703 FrmDb::cache_delete_group( 'frm_form' );
704 }
705
706 /**
707 * @return array of errors
708 */
709 public static function validate( $values ) {
710 $errors = array();
711
712 return apply_filters( 'frm_validate_form', $errors, $values );
713 }
714
715 public static function get_params( $form = null ) {
716 global $frm_vars;
717
718 if ( ! $form ) {
719 $form = self::getAll( array(), 'name', 1 );
720 } else {
721 self::maybe_get_form( $form );
722 }
723
724 if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) {
725 return $frm_vars['form_params'][ $form->id ];
726 }
727
728 $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // WPCS: CSRF ok.
729 $action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form );
730
731 $default_values = array(
732 'id' => '',
733 'form_name' => '',
734 'paged' => 1,
735 'form' => $form->id,
736 'form_id' => $form->id,
737 'field_id' => '',
738 'search' => '',
739 'sort' => '',
740 'sdir' => '',
741 'action' => $action,
742 );
743
744 $values = array();
745 $values['posted_form_id'] = FrmAppHelper::get_param( 'form_id', '', 'get', 'absint' );
746 if ( ! $values['posted_form_id'] ) {
747 $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
748 }
749
750 if ( $form->id == $values['posted_form_id'] ) {
751 //if there are two forms on the same page, make sure not to submit both
752 foreach ( $default_values as $var => $default ) {
753 if ( $var == 'action' ) {
754 $values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' );
755 } else {
756 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
757 }
758 unset( $var, $default );
759 }
760 } else {
761 foreach ( $default_values as $var => $default ) {
762 $values[ $var ] = $default;
763 unset( $var, $default );
764 }
765 }
766
767 if ( in_array( $values['action'], array( 'create', 'update' ) ) &&
768 ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // WPCS: CSRF ok.
769 ) {
770 $values['action'] = 'new';
771 }
772
773 return $values;
774 }
775
776 public static function list_page_params() {
777 $values = array();
778 $defaults = array(
779 'template' => 0,
780 'id' => '',
781 'paged' => 1,
782 'form' => '',
783 'search' => '',
784 'sort' => '',
785 'sdir' => '',
786 );
787 foreach ( $defaults as $var => $default ) {
788 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
789 }
790
791 return $values;
792 }
793
794 public static function get_admin_params( $form = null ) {
795 $form_id = $form;
796 if ( $form === null ) {
797 $form_id = self::get_current_form_id();
798 } elseif ( $form && is_object( $form ) ) {
799 $form_id = $form->id;
800 }
801
802 $values = array();
803 $defaults = array(
804 'id' => '',
805 'form_name' => '',
806 'paged' => 1,
807 'form' => $form_id,
808 'field_id' => '',
809 'search' => '',
810 'sort' => '',
811 'sdir' => '',
812 'fid' => '',
813 'keep_post' => '',
814 );
815 foreach ( $defaults as $var => $default ) {
816 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
817 }
818
819 return $values;
820 }
821
822 public static function get_current_form_id( $default_form = 'none' ) {
823 if ( 'first' == $default_form ) {
824 $form = self::get_current_form();
825 } else {
826 $form = self::maybe_get_current_form();
827 }
828 $form_id = $form ? $form->id : 0;
829
830 return $form_id;
831 }
832
833 public static function maybe_get_current_form( $form_id = 0 ) {
834 global $frm_vars;
835
836 if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) {
837 return $frm_vars['current_form'];
838 }
839
840 $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' );
841 if ( $form_id ) {
842 $form_id = self::set_current_form( $form_id );
843 }
844
845 return $form_id;
846 }
847
848 public static function get_current_form( $form_id = 0 ) {
849 $form = self::maybe_get_current_form( $form_id );
850 if ( is_numeric( $form ) ) {
851 $form = self::set_current_form( $form );
852 }
853
854 return $form;
855 }
856
857 public static function set_current_form( $form_id ) {
858 global $frm_vars;
859
860 $query = array();
861 if ( $form_id ) {
862 $query['id'] = $form_id;
863 }
864
865 $frm_vars['current_form'] = self::get_published_forms( $query, 1 );
866
867 return $frm_vars['current_form'];
868 }
869
870 public static function is_form_loaded( $form, $this_load, $global_load ) {
871 global $frm_vars;
872 $small_form = new stdClass();
873 foreach ( array( 'id', 'form_key', 'name' ) as $var ) {
874 $small_form->{$var} = $form->{$var};
875 unset( $var );
876 }
877
878 $frm_vars['forms_loaded'][] = $small_form;
879
880 if ( $this_load && empty( $global_load ) ) {
881 $global_load = true;
882 $frm_vars['load_css'] = true;
883 }
884
885 return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load );
886 }
887
888 public static function show_submit( $form ) {
889 $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() );
890 $show = apply_filters( 'frm_show_submit_button', $show, $form );
891
892 return $show;
893 }
894
895 /**
896 * @since 2.3
897 */
898 public static function get_option( $atts ) {
899 $form = $atts['form'];
900
901 return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $atts['default'];
902 }
903
904 /**
905 * Get the link to edit this form.
906 *
907 * @since 4.0
908 * @param int $form_id The id of the form.
909 */
910 public static function get_edit_link( $form_id ) {
911 return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id );
912 }
913
914 /**
915 * @deprecated 3.0
916 * @codeCoverageIgnore
917 *
918 * @param string $key
919 *
920 * @return int form id
921 */
922 public static function getIdByKey( $key ) {
923 return FrmFormDeprecated::getIdByKey( $key );
924 }
925
926 /**
927 * @deprecated 3.0
928 * @codeCoverageIgnore
929 */
930 public static function getKeyById( $id ) {
931 return FrmFormDeprecated::getKeyById( $id );
932 }
933 }
934