PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.02.04
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.02.04
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.02.04, at classes/models/FrmForm.php

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