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

831 lines 25.6 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'] : 'draft',
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 'default_template' => isset($values['default_template']) ? (int) $values['default_template'] : 0,
24 'created_at' => isset($values['created_at']) ? $values['created_at'] : current_time('mysql', 1),
25 );
26
27 $options = isset( $values['options'] ) ? (array) $values['options'] : array();
28 FrmFormsHelper::fill_form_options( $options, $values );
29
30 $options['before_html'] = isset($values['options']['before_html']) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html('before');
31 $options['after_html'] = isset($values['options']['after_html']) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html('after');
32 $options['submit_html'] = isset($values['options']['submit_html']) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html('submit');
33
34 $options = apply_filters('frm_form_options_before_update', $options, $values);
35 $new_values['options'] = serialize($options);
36
37 //if(isset($values['id']) && is_numeric($values['id']))
38 // $new_values['id'] = $values['id'];
39
40 $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values );
41
42 $id = $wpdb->insert_id;
43
44 // Clear form caching
45 self::clear_form_cache();
46
47 return $id;
48 }
49
50 /**
51 * @return int|boolean ID on success or false on failure
52 */
53 public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) {
54 global $wpdb;
55
56 $values = self::getOne( $id, $blog_id );
57 if ( ! $values ) {
58 return false;
59 }
60
61 $new_key = $copy_keys ? $values->form_key : '';
62
63 $new_values = array(
64 'form_key' => FrmAppHelper::get_unique_key( $new_key, $wpdb->prefix . 'frm_forms', 'form_key' ),
65 'name' => $values->name,
66 'description' => $values->description,
67 'status' => $template ? 'published' : 'draft',
68 'logged_in' => $values->logged_in ? $values->logged_in : 0,
69 'editable' => $values->editable ? $values->editable : 0,
70 'created_at' => current_time('mysql', 1),
71 'is_template' => $template ? 1 : 0,
72 );
73
74 if ( $blog_id ) {
75 $new_values['status'] = 'published';
76 $new_options = maybe_unserialize($values->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 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 /**
191 * @return array
192 */
193 public static function update_fields( $id, $values ) {
194
195 if ( ! isset($values['item_meta']) && ! isset($values['field_options']) ) {
196 return $values;
197 }
198
199 $all_fields = FrmField::get_all_for_form($id);
200 if ( empty($all_fields) ) {
201 return $values;
202 }
203
204 if ( ! isset($values['item_meta']) ) {
205 $values['item_meta'] = array();
206 }
207
208 $field_array = array();
209 $existing_keys = array_keys($values['item_meta']);
210 foreach ( $all_fields as $fid ) {
211 if ( ! in_array($fid->id, $existing_keys) && ( isset($values['frm_fields_submitted']) && in_array($fid->id, $values['frm_fields_submitted']) ) || isset($values['options']) ) {
212 $values['item_meta'][ $fid->id ] = '';
213 }
214 $field_array[ $fid->id ] = $fid;
215 }
216 unset($all_fields);
217
218 foreach ( $values['item_meta'] as $field_id => $default_value ) {
219 if ( isset( $field_array[ $field_id ] ) ) {
220 $field = $field_array[ $field_id ];
221 } else {
222 $field = FrmField::getOne($field_id);
223 }
224
225 if ( ! $field ) {
226 continue;
227 }
228
229 $is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) );
230 if ( $is_settings_page ) {
231 //updating the settings page
232 if ( isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ) {
233 $field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field_id ] ) ? $values['field_options'][ 'custom_html_' . $field_id ] : ( isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type ) );
234 $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values);
235 FrmField::update( $field_id, array( 'field_options' => $field->field_options ) );
236 } else if ( $field->type == 'hidden' || $field->type == 'user_id' ) {
237 $prev_opts = $field->field_options;
238 $field->field_options = apply_filters('frm_update_form_field_options', $field->field_options, $field, $values);
239 if ( $prev_opts != $field->field_options ) {
240 FrmField::update( $field_id, array( 'field_options' => $field->field_options ) );
241 }
242 unset($prev_opts);
243 }
244 }
245
246 if ( $is_settings_page && ! defined( 'WP_IMPORTING' ) ) {
247 continue;
248 }
249
250 //updating the form
251 $update_options = FrmFieldsHelper::get_default_field_opts( $field->type, $field, true );
252 unset( $update_options['custom_html'] ); // don't check for POST html
253 $update_options = apply_filters( 'frm_field_options_to_update', $update_options );
254
255 foreach ( $update_options as $opt => $default ) {
256 $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? trim( sanitize_text_field( $values['field_options'][ $opt . '_' . $field_id ] ) ) : $default;
257 }
258
259 $field->field_options = apply_filters('frm_update_field_options', $field->field_options, $field, $values);
260 $default_value = maybe_serialize( $values['item_meta'][ $field_id ] );
261
262 $new_field = array(
263 'field_options' => $field->field_options,
264 'default_value' => $default_value,
265 );
266
267 self::prepare_field_update_values( $field, $values, $new_field );
268
269 FrmField::update( $field_id, $new_field );
270
271 FrmField::delete_form_transient($field->form_id);
272 }
273 self::clear_form_cache();
274
275 return $values;
276 }
277
278 private static function prepare_field_update_values( $field, $values, &$new_field ) {
279 $field_cols = array(
280 'field_key' => '',
281 'required' => false,
282 'type' => '',
283 'description' => '',
284 'options' => '',
285 );
286 foreach ( $field_cols as $col => $default ) {
287 $default = ( $default === '' ) ? $field->{$col} : $default;
288 $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default;
289 }
290 }
291
292 /**
293 * @param string $status
294 * @return int|boolean
295 */
296 public static function set_status( $id, $status ) {
297 if ( 'trash' == $status ) {
298 return self::trash($id);
299 }
300
301 $statuses = array( 'published', 'draft', 'trash' );
302 if ( ! in_array( $status, $statuses ) ) {
303 return false;
304 }
305
306 global $wpdb;
307
308 if ( is_array($id) ) {
309 $where = array(
310 'id' => $id,
311 'parent_form_id' => $id,
312 'or' => 1,
313 );
314 FrmDb::get_where_clause_and_values( $where );
315 array_unshift( $where['values'], $status );
316
317 $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) );
318 } else {
319 $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) );
320 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) );
321 }
322
323 if ( $query_results ) {
324 self::clear_form_cache();
325 }
326
327 return $query_results;
328 }
329
330 /**
331 * @return int|boolean
332 */
333 public static function trash( $id ) {
334 if ( ! EMPTY_TRASH_DAYS ) {
335 return self::destroy( $id );
336 }
337
338 $form = self::getOne($id);
339 if ( ! $form ) {
340 return false;
341 }
342
343 $options = $form->options;
344 $options['trash_time'] = time();
345
346 global $wpdb;
347 $query_results = $wpdb->update(
348 $wpdb->prefix . 'frm_forms',
349 array(
350 'status' => 'trash',
351 'options' => serialize( $options ),
352 ),
353 array(
354 'id' => $id,
355 )
356 );
357
358 $wpdb->update(
359 $wpdb->prefix . 'frm_forms',
360 array(
361 'status' => 'trash',
362 'options' => serialize( $options ),
363 ),
364 array(
365 'parent_form_id' => $id,
366 )
367 );
368
369 if ( $query_results ) {
370 self::clear_form_cache();
371 }
372
373 return $query_results;
374 }
375
376 /**
377 * @return int|boolean
378 */
379 public static function destroy( $id ) {
380 global $wpdb;
381
382 $form = self::getOne($id);
383 if ( ! $form ) {
384 return false;
385 }
386 $id = $form->id;
387
388 // Disconnect the entries from this form
389 $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) );
390 foreach ( $entries as $entry_id ) {
391 FrmEntry::destroy($entry_id);
392 unset($entry_id);
393 }
394
395 // Disconnect the fields from this form
396 $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 ) );
397
398 $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) );
399 if ( $query_results ) {
400 // Delete all form actions linked to this form
401 $action_control = FrmFormActionsController::get_form_actions( 'email' );
402 $action_control->destroy($id, 'all');
403
404 // Clear form caching
405 self::clear_form_cache();
406
407 do_action( 'frm_destroy_form', $id );
408 do_action( 'frm_destroy_form_' . $id );
409 }
410
411 return $query_results;
412 }
413
414 /**
415 * Delete trashed forms based on how long they have been trashed
416 * @return int The number of forms deleted
417 */
418 public static function scheduled_delete( $delete_timestamp = '' ) {
419 global $wpdb;
420
421 $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' );
422
423 if ( ! $trash_forms ) {
424 return;
425 }
426
427 if ( empty( $delete_timestamp ) ) {
428 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
429 }
430
431 $count = 0;
432 foreach ( $trash_forms as $form ) {
433 $form->options = maybe_unserialize( $form->options );
434 if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) {
435 self::destroy( $form->id );
436 $count++;
437 }
438
439 unset( $form );
440 }
441 return $count;
442 }
443
444 /**
445 * @return string form name
446 */
447 public static function getName( $id ) {
448 $form = FrmDb::check_cache( $id, 'frm_form' );
449 if ( $form ) {
450 $r = stripslashes($form->name);
451 return $r;
452 }
453
454 $query_key = is_numeric( $id ) ? 'id' : 'form_key';
455 $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' );
456 $r = stripslashes($r);
457
458 return $r;
459 }
460
461 /**
462 * @param string $key
463 * @return int form id
464 */
465 public static function getIdByKey( $key ) {
466 return (int) FrmDb::get_var( 'frm_forms', array( 'form_key' => sanitize_title( $key ) ) );
467 }
468
469 /**
470 * @param int $id
471 * @return string form key
472 */
473 public static function getKeyById( $id ) {
474 $id = (int) $id;
475 $cache = FrmDb::check_cache( $id, 'frm_form' );
476 if ( $cache ) {
477 return $cache->form_key;
478 }
479
480 $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' );
481
482 return $key;
483 }
484
485 /**
486 * If $form is numeric, get the form object
487 * @param object|int $form
488 * @since 2.0.9
489 */
490 public static function maybe_get_form( &$form ) {
491 if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) {
492 $form = self::getOne( $form );
493 }
494 }
495
496 /**
497 * @return object form
498 */
499 public static function getOne( $id, $blog_id = false ) {
500 global $wpdb;
501
502 if ( $blog_id && is_multisite() ) {
503 global $wpmuBaseTablePrefix;
504 $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id );
505
506 $table_name = $prefix . 'frm_forms';
507 } else {
508 $table_name = $wpdb->prefix . 'frm_forms';
509 $cache = wp_cache_get($id, 'frm_form');
510 if ( $cache ) {
511 if ( isset($cache->options) ) {
512 $cache->options = maybe_unserialize($cache->options);
513 }
514
515 return stripslashes_deep($cache);
516 }
517 }
518
519 if ( is_numeric($id) ) {
520 $where = array( 'id' => $id );
521 } else {
522 $where = array( 'form_key' => $id );
523 }
524
525 $results = FrmDb::get_row( $table_name, $where );
526
527 if ( isset($results->options) ) {
528 FrmDb::set_cache( $results->id, $results, 'frm_form' );
529 $results->options = maybe_unserialize($results->options);
530 }
531 return stripslashes_deep($results);
532 }
533
534 /**
535 * @return object|array of objects
536 */
537 public static function getAll( $where = array(), $order_by = '', $limit = '' ) {
538 if ( is_array( $where ) && ! empty( $where ) ) {
539 $results = FrmDb::get_results( 'frm_forms', $where, '*', array(
540 'order_by' => $order_by,
541 'limit' => $limit,
542 ) );
543 } else {
544 global $wpdb;
545
546 // the query has already been prepared if this is not an array
547 $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit );
548 $results = $wpdb->get_results( $query );
549 }
550
551 if ( $results ) {
552 foreach ( $results as $result ) {
553 FrmDb::set_cache( $result->id, $result, 'frm_form' );
554 $result->options = maybe_unserialize( $result->options );
555 }
556 }
557
558 if ( $limit == ' LIMIT 1' || $limit == 1 ) {
559 // return the first form object if we are only getting one form
560 $results = reset( $results );
561 }
562
563 return stripslashes_deep($results);
564 }
565
566 /**
567 * Get all published forms
568 * @since 2.0
569 * @return array of forms
570 */
571 public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) {
572 $query['is_template'] = 0;
573 $query['status'] = array( null, '', 'published' );
574 if ( $inc_children == 'exclude' ) {
575 $query['parent_form_id'] = array( null, 0 );
576 }
577
578 $forms = self::getAll( $query, 'name', $limit );
579 return $forms;
580 }
581
582 /**
583 * @return int count of forms
584 */
585 public static function get_count() {
586 global $wpdb;
587
588 $cache_key = 'frm_form_counts';
589
590 $counts = wp_cache_get( $cache_key, 'frm_form' );
591 if ( false !== $counts ) {
592 return $counts;
593 }
594
595 $results = (array) FrmDb::get_results( 'frm_forms', array(
596 'or' => 1,
597 'parent_form_id' => null,
598 'parent_form_id <' => 0,
599 ), 'status, is_template' );
600
601 $statuses = array( 'published', 'draft', 'template', 'trash' );
602 $counts = array_fill_keys( $statuses, 0 );
603
604 foreach ( $results as $row ) {
605 if ( 'trash' != $row->status ) {
606 if ( $row->is_template ) {
607 $counts['template']++;
608 } else {
609 $counts['published']++;
610 }
611 } else {
612 $counts['trash']++;
613 }
614
615 if ( 'draft' == $row->status ) {
616 $counts['draft']++;
617 }
618
619 unset($row);
620 }
621
622 $counts = (object) $counts;
623 FrmDb::set_cache( $cache_key, $counts, 'frm_form' );
624
625 return $counts;
626 }
627
628 /**
629 * Clear form caching
630 * Called when a form is created, updated, duplicated, or deleted
631 * or when the form status is changed
632 *
633 * @since 2.0.4
634 */
635 public static function clear_form_cache() {
636 FrmDb::cache_delete_group( 'frm_form' );
637 }
638
639 /**
640 * @return array of errors
641 */
642 public static function validate( $values ) {
643 $errors = array();
644
645 return apply_filters('frm_validate_form', $errors, $values);
646 }
647
648 public static function get_params( $form = null ) {
649 global $frm_vars;
650
651 if ( ! $form ) {
652 $form = self::getAll( array(), 'name', 1 );
653 } else {
654 self::maybe_get_form( $form );
655 }
656
657 if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) {
658 return $frm_vars['form_params'][ $form->id ];
659 }
660
661 $action_var = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
662 $action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form );
663
664 $default_values = array(
665 'id' => '',
666 'form_name' => '',
667 'paged' => 1,
668 'form' => $form->id,
669 'form_id' => $form->id,
670 'field_id' => '',
671 'search' => '',
672 'sort' => '',
673 'sdir' => '',
674 'action' => $action,
675 );
676
677 $values = array();
678 $values['posted_form_id'] = FrmAppHelper::get_param( 'form_id', '', 'get', 'absint' );
679 if ( ! $values['posted_form_id'] ) {
680 $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
681 }
682
683 if ( $form->id == $values['posted_form_id'] ) {
684 //if there are two forms on the same page, make sure not to submit both
685 foreach ( $default_values as $var => $default ) {
686 if ( $var == 'action' ) {
687 $values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' );
688 } else {
689 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
690 }
691 unset( $var, $default );
692 }
693 } else {
694 foreach ( $default_values as $var => $default ) {
695 $values[ $var ] = $default;
696 unset( $var, $default );
697 }
698 }
699
700 if ( in_array( $values['action'], array( 'create', 'update' ) ) && ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) ) {
701 $values['action'] = 'new';
702 }
703
704 return $values;
705 }
706
707 public static function list_page_params() {
708 $values = array();
709 $defaults = array(
710 'template' => 0,
711 'id' => '',
712 'paged' => 1,
713 'form' => '',
714 'search' => '',
715 'sort' => '',
716 'sdir' => '',
717 );
718 foreach ( $defaults as $var => $default ) {
719 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
720 }
721
722 return $values;
723 }
724
725 public static function get_admin_params( $form = null ) {
726 $form_id = $form;
727 if ( $form === null ) {
728 $form_id = FrmForm::get_current_form_id();
729 } else if ( $form && is_object( $form ) ) {
730 $form_id = $form->id;
731 }
732
733 $values = array();
734 $defaults = array(
735 'id' => '',
736 'form_name' => '',
737 'paged' => 1,
738 'form' => $form_id,
739 'field_id' => '',
740 'search' => '',
741 'sort' => '',
742 'sdir' => '',
743 'fid' => '',
744 'keep_post' => '',
745 );
746 foreach ( $defaults as $var => $default ) {
747 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
748 }
749
750 return $values;
751 }
752
753 public static function get_current_form_id( $default_form = 'none' ) {
754 if ( 'first' == $default_form ) {
755 $form = self::get_current_form();
756 } else {
757 $form = self::maybe_get_current_form();
758 }
759 $form_id = $form ? $form->id : 0;
760
761 return $form_id;
762 }
763
764 public static function maybe_get_current_form( $form_id = 0 ) {
765 global $frm_vars;
766
767 if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) {
768 return $frm_vars['current_form'];
769 }
770
771 $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' );
772 if ( $form_id ) {
773 $form_id = self::set_current_form( $form_id );
774 }
775 return $form_id;
776 }
777
778 public static function get_current_form( $form_id = 0 ) {
779 $form = self::maybe_get_current_form( $form_id );
780 if ( is_numeric( $form ) ) {
781 $form = self::set_current_form( $form );
782 }
783 return $form;
784 }
785
786 public static function set_current_form( $form_id ) {
787 global $frm_vars;
788
789 $query = array();
790 if ( $form_id ) {
791 $query['id'] = $form_id;
792 }
793
794 $frm_vars['current_form'] = self::get_published_forms( $query, 1 );
795
796 return $frm_vars['current_form'];
797 }
798
799 public static function is_form_loaded( $form, $this_load, $global_load ) {
800 global $frm_vars;
801 $small_form = new stdClass();
802 foreach ( array( 'id', 'form_key', 'name' ) as $var ) {
803 $small_form->{$var} = $form->{$var};
804 unset($var);
805 }
806
807 $frm_vars['forms_loaded'][] = $small_form;
808
809 if ( $this_load && empty( $global_load ) ) {
810 $global_load = true;
811 $frm_vars['load_css'] = true;
812 }
813
814 return ( ( ! isset($frm_vars['css_loaded']) || ! $frm_vars['css_loaded'] ) && $global_load );
815 }
816
817 public static function show_submit( $form ) {
818 $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() );
819 $show = apply_filters( 'frm_show_submit_button', $show, $form );
820 return $show;
821 }
822
823 /**
824 * @since 2.3
825 */
826 public static function get_option( $atts ) {
827 $form = $atts['form'];
828 return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $atts['default'];
829 }
830 }
831