PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.21
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.21
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
← All changes | classes/models/FrmForm.php +160 -49 6.36.21 View file →
@@ -6,9 +6,9 @@
6 6 class FrmForm {
7 7
8 8 /**
9 9 * @param array $values
10 - * @return int|bool id on success or false on failure.
10 + * @return bool|int id on success or false on failure.
11 11 */
12 12 public static function create( $values ) {
13 13 global $wpdb;
14 14
@@ -69,9 +69,9 @@
69 69 return FrmAppHelper::maybe_filter_array( $options, array( 'submit_value', 'success_msg', 'before_html', 'after_html' ) );
70 70 }
71 71
72 72 /**
73 - * @return int|boolean ID on success or false on failure
73 + * @return bool|int ID on success or false on failure
74 74 */
75 75 public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) {
76 76 global $wpdb;
77 77
@@ -93,10 +93,10 @@
93 93 'is_template' => $template ? 1 : 0,
94 94 );
95 95
96 96 if ( $blog_id ) {
97 - $new_values['status'] = 'published';
98 - $new_options = $values->options;
97 + $new_values['status'] = 'published';
98 + $new_options = $values->options;
99 99 FrmAppHelper::unserialize_or_decode( $new_options );
100 100 $new_options['email_to'] = get_option( 'admin_email' );
101 101 $new_options['copy'] = false;
102 102 $new_values['options'] = $new_options;
@@ -126,9 +126,9 @@
126 126 return false;
127 127 }
128 128
129 129 public static function after_duplicate( $form_id, $values ) {
130 - $new_opts = $values['options'];
130 + $new_opts = $values['options'];
131 131 FrmAppHelper::unserialize_or_decode( $new_opts );
132 132 $values['options'] = $new_opts;
133 133
134 134 if ( isset( $new_opts['success_msg'] ) ) {
@@ -158,9 +158,9 @@
158 158 // Keys of fields that you want to check to replace field ID.
159 159 $keys = array( 'default_value', 'field_options' );
160 160 $sql_cols = 'fi.id';
161 161 foreach ( $keys as $key ) {
162 - $sql_cols .= ( ',fi.' . $key );
162 + $sql_cols .= ',fi.' . $key;
163 163 }
164 164
165 165 $fields = FrmDb::get_results(
166 166 "{$wpdb->prefix}frm_fields AS fi LEFT OUTER JOIN {$wpdb->prefix}frm_forms AS fr ON fi.form_id = fr.id",
@@ -210,9 +210,9 @@
210 210
211 211 if ( $new_val !== $value ) {
212 212 $new_values[ $key ] = $new_val;
213 213 }
214 - }
214 + }//end foreach
215 215
216 216 if ( ! empty( $new_values ) ) {
217 217 FrmField::update( $field['id'], $new_values );
218 218 }
@@ -218,9 +218,9 @@
218 218 }
219 219 }
220 220
221 221 /**
222 - * @return int|boolean
222 + * @return bool|int
223 223 */
224 224 public static function update( $id, $values, $create_link = false ) {
225 225 global $wpdb;
226 226
@@ -243,9 +243,9 @@
243 243 $new_values[ $value_key ] = $value;
244 244 }
245 245 }
246 246
247 - if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) {
247 + if ( ! empty( $values['new_status'] ) ) {
248 248 $new_values['status'] = $values['new_status'];
249 249 }
250 250
251 251 if ( ! empty( $new_values ) ) {
@@ -276,15 +276,15 @@
276 276 if ( ! isset( $values['options'] ) ) {
277 277 return $new_values;
278 278 }
279 279
280 - $options = isset( $values['options'] ) ? (array) $values['options'] : array();
280 + $options = ! empty( $values['options'] ) ? (array) $values['options'] : array();
281 281 FrmFormsHelper::fill_form_options( $options, $values );
282 282
283 283 $options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0;
284 284 $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
285 285 $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
286 - $options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
286 + $options['submit_html'] = isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
287 287
288 288 /**
289 289 * Allows modifying form options before updating or creating.
290 290 *
@@ -348,11 +348,12 @@
348 348 continue;
349 349 }
350 350 }
351 351
352 - //updating the form
352 + // Updating the form.
353 353 $update_options = FrmFieldsHelper::get_default_field_options_from_field( $field );
354 - unset( $update_options['custom_html'] ); // don't check for POST html
354 + // Don't check for POST html.
355 + unset( $update_options['custom_html'] );
355 356 $update_options = apply_filters( 'frm_field_options_to_update', $update_options );
356 357
357 358 foreach ( $update_options as $opt => $default ) {
358 359 $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default;
@@ -376,13 +377,14 @@
376 377 }
377 378 }
378 379
379 380 self::prepare_field_update_values( $field, $values, $new_field );
381 + self::maybe_update_max_option( $field, $values, $new_field );
380 382
381 383 FrmField::update( $field_id, $new_field );
382 384
383 385 FrmField::delete_form_transient( $field->form_id );
384 - }
386 + }//end foreach
385 387 self::clear_form_cache();
386 388
387 389 return $values;
388 390 }
@@ -387,8 +389,33 @@
387 389 return $values;
388 390 }
389 391
390 392 /**
393 + * Resets the 'max' option of a field when changing paragraph field type to other field types like text, email etc.
394 + *
395 + * @since 6.7
396 + *
397 + * @param array $field
398 + * @param array $values
399 + * @param array $new_field
400 + * @return void
401 + */
402 + private static function maybe_update_max_option( $field, $values, &$new_field ) {
403 + if ( $field->type === 'textarea' &&
404 + ! empty( $values['field_options'][ 'type_' . $field->id ] ) &&
405 + in_array( $values['field_options'][ 'type_' . $field->id ], array( 'text', 'email', 'url', 'password', 'phone' ), true ) ) {
406 +
407 + $new_field['field_options']['max'] = '';
408 +
409 + /**
410 + * Update posted field setting so that new 'max' option is displayed after form is saved and page reloads.
411 + * FrmFieldsHelper::fill_default_field_opts populates field options by calling self::get_posted_field_setting.
412 + */
413 + $_POST['field_options'][ 'max_' . $field->id ] = '';
414 + }
415 + }
416 +
417 + /**
391 418 * @param string $opt
392 419 * @param mixed $value
393 420 * @return void
394 421 */
@@ -428,9 +455,10 @@
428 455 private static function sanitize_calc( $value ) {
429 456 if ( false !== strpos( $value, '<' ) ) {
430 457 $value = self::normalize_calc_spaces( $value );
431 458 }
432 - $allow = array( '<= ', ' >=' ); // Allow <= and >=
459 + // Allow <= and >=.
460 + $allow = array( '<= ', ' >=' );
433 461 $temp = array( '< = ', ' > =' );
434 462 $value = str_replace( $allow, $temp, $value );
435 463 $value = strip_tags( $value );
436 464 $value = str_replace( $temp, $allow, $value );
@@ -462,9 +490,9 @@
462 490 $prev_opts = array();
463 491 $fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type );
464 492
465 493 $field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ? $values['field_options'][ 'custom_html_' . $field->id ] : $fallback_html;
466 - } elseif ( $field->type == 'hidden' || $field->type == 'user_id' ) {
494 + } elseif ( $field->type === 'hidden' || $field->type === 'user_id' ) {
467 495 $prev_opts = $field->field_options;
468 496 }
469 497
470 498 if ( isset( $prev_opts ) ) {
@@ -485,11 +513,14 @@
485 513 'options' => '',
486 514 'name' => '',
487 515 );
488 516 foreach ( $field_cols as $col => $default ) {
489 - $default = ( $default === '' ) ? $field->{$col} : $default;
517 + $default = $default === '' ? $field->{$col} : $default;
518 + $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default;
519 + }
490 520
491 - $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default;
521 + if ( $field->type === 'submit' && isset( $new_field['field_order'] ) && (int) $new_field['field_order'] === FrmSubmitHelper::DEFAULT_ORDER ) {
522 + $new_field['field_order'] = $field->field_order;
492 523 }
493 524
494 525 // Don't save the template option.
495 526 if ( is_array( $new_field['options'] ) && isset( $new_field['options']['000'] ) ) {
@@ -501,9 +532,9 @@
501 532 * Get a list of all form settings that should be translated
502 533 * on a multilingual site.
503 534 *
504 535 * @since 3.06.01
505 - * @param object $form - The form object
536 + * @param object $form The form object.
506 537 */
507 538 public static function translatable_strings( $form ) {
508 539 $strings = array(
509 540 'name',
@@ -520,11 +551,12 @@
520 551 return apply_filters( 'frm_form_strings', $strings, $form );
521 552 }
522 553
523 554 /**
555 + * @param int $id
524 556 * @param string $status
525 557 *
526 - * @return int|boolean
558 + * @return bool|int
527 559 */
528 560 public static function set_status( $id, $status ) {
529 561 if ( 'trash' == $status ) {
530 562 return self::trash( $id );
@@ -530,9 +562,9 @@
530 562 return self::trash( $id );
531 563 }
532 564
533 565 $statuses = array( 'published', 'draft', 'trash' );
534 - if ( ! in_array( $status, $statuses ) ) {
566 + if ( ! in_array( $status, $statuses, true ) ) {
535 567 return false;
536 568 }
537 569
538 570 global $wpdb;
@@ -559,9 +591,9 @@
559 591 return $query_results;
560 592 }
561 593
562 594 /**
563 - * @return int|boolean
595 + * @return bool|int
564 596 */
565 597 public static function trash( $id ) {
566 598 if ( ! EMPTY_TRASH_DAYS ) {
567 599 return self::destroy( $id );
@@ -571,8 +603,12 @@
571 603 if ( ! $form ) {
572 604 return false;
573 605 }
574 606
607 + if ( $form->status === 'trash' ) {
608 + return false;
609 + }
610 +
575 611 $options = $form->options;
576 612 $options['trash_time'] = time();
577 613
578 614 global $wpdb;
@@ -605,9 +641,9 @@
605 641 return $query_results;
606 642 }
607 643
608 644 /**
609 - * @return int|boolean
645 + * @return bool|int
610 646 */
611 647 public static function destroy( $id ) {
612 648 global $wpdb;
613 649
@@ -629,8 +665,11 @@
629 665
630 666 $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) );
631 667 if ( $query_results ) {
632 668 // Delete all form actions linked to this form
669 + /**
670 + * @var FrmFormAction
671 + */
633 672 $action_control = FrmFormActionsController::get_form_actions( 'email' );
634 673 $action_control->destroy( $id, 'all' );
635 674
636 675 // Clear form caching
@@ -650,9 +689,9 @@
650 689 */
651 690 public static function scheduled_delete( $delete_timestamp = '' ) {
652 691 global $wpdb;
653 692
654 - $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' );
693 + $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' );
655 694
656 695 if ( ! $trash_forms ) {
657 696 return 0;
658 697 }
@@ -665,9 +704,11 @@
665 704 foreach ( $trash_forms as $form ) {
666 705 FrmAppHelper::unserialize_or_decode( $form->options );
667 706 if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) {
668 707 self::destroy( $form->id );
669 - $count ++;
708 + if ( empty( $form->parent_form_id ) ) {
709 + ++$count;
710 + }
670 711 }
671 712
672 713 unset( $form );
673 714 }
@@ -687,10 +728,12 @@
687 728 }
688 729
689 730 $query_key = is_numeric( $id ) ? 'id' : 'form_key';
690 731 $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' );
691 - $r = stripslashes( $r );
692 732
733 + // An empty form name can result in a null value.
734 + $r = is_null( $r ) ? '' : stripslashes( $r );
735 +
693 736 return $r;
694 737 }
695 738
696 739 /**
@@ -725,11 +768,10 @@
725 768
726 769 /**
727 770 * If $form is numeric, get the form object
728 771 *
729 - * @param object|int $form
730 - *
731 772 * @since 2.0.9
773 + * @param int|object $form
732 774 */
733 775 public static function maybe_get_form( &$form ) {
734 776 if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) {
735 777 $form = self::getOne( $form );
@@ -736,9 +778,11 @@
736 778 }
737 779 }
738 780
739 781 /**
740 - * @return object form
782 + * @param int|string $id
783 + * @param false|int $blog_id
784 + * @return stdClass|null
741 785 */
742 786 public static function getOne( $id, $blog_id = false ) {
743 787 global $wpdb;
744 788
@@ -753,10 +797,9 @@
753 797 if ( $cache ) {
754 798 if ( isset( $cache->options ) ) {
755 799 FrmAppHelper::unserialize_or_decode( $cache->options );
756 800 }
757 -
758 - return apply_filters( 'frm_form_object', wp_unslash( $cache ) );
801 + return self::prepare_form_row_data( $cache );
759 802 }
760 803 }
761 804
762 805 if ( is_numeric( $id ) ) {
@@ -771,17 +814,43 @@
771 814 FrmDb::set_cache( $results->id, $results, 'frm_form' );
772 815 FrmAppHelper::unserialize_or_decode( $results->options );
773 816 }
774 817
775 - return apply_filters( 'frm_form_object', wp_unslash( $results ) );
818 + return self::prepare_form_row_data( $results );
776 819 }
777 820
778 821 /**
779 - * @return object|array of objects
822 + * Make sure that if $row is an object, that $row->options is an array and not a string.
823 + *
824 + * @since 6.8.3
825 + *
826 + * @param stdClass|null $row The database row for a target form.
827 + * @return stdClass|null
780 828 */
829 + private static function prepare_form_row_data( $row ) {
830 + $row = wp_unslash( $row );
831 + if ( ! is_object( $row ) ) {
832 + return $row;
833 + }
834 +
835 + if ( ! is_array( $row->options ) ) {
836 + $row->options = FrmFormsHelper::get_default_opts();
837 + }
838 +
839 + /**
840 + * @since 4.03.02
841 + *
842 + * @param stdClass $row
843 + */
844 + return apply_filters( 'frm_form_object', $row );
845 + }
846 +
847 + /**
848 + * @return array|object of objects
849 + */
781 850 public static function getAll( $where = array(), $order_by = '', $limit = '' ) {
782 851 if ( is_array( $where ) && ! empty( $where ) ) {
783 - if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
852 + if ( ! empty( $where['is_template'] ) && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
784 853 // don't get trashed templates
785 854 $where['status'] = array( null, '', 'published' );
786 855 }
787 856
@@ -788,9 +857,9 @@
788 857 $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) );
789 858 } else {
790 859 global $wpdb;
791 860
792 - // the query has already been prepared if this is not an array
861 + // The query has already been prepared if this is not an array.
793 862 $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit );
794 863 $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
795 864 }
796 865
@@ -800,9 +869,9 @@
800 869 FrmAppHelper::unserialize_or_decode( $result->options );
801 870 }
802 871 }
803 872
804 - if ( $limit == ' LIMIT 1' || $limit == 1 ) {
873 + if ( $limit === ' LIMIT 1' || $limit == 1 ) {
805 874 // return the first form object if we are only getting one form
806 875 $results = reset( $results );
807 876 }
808 877
@@ -821,9 +890,9 @@
821 890 */
822 891 public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) {
823 892 $query['is_template'] = 0;
824 893 $query['status'] = array( null, '', 'published' );
825 - if ( $inc_children == 'exclude' ) {
894 + if ( $inc_children === 'exclude' ) {
826 895 $query['parent_form_id'] = array( null, 0 );
827 896 }
828 897
829 898 $forms = self::getAll( $query, 'name', $limit );
@@ -859,18 +928,18 @@
859 928
860 929 foreach ( $results as $row ) {
861 930 if ( 'trash' != $row->status ) {
862 931 if ( $row->is_template ) {
863 - $counts['template'] ++;
932 + ++$counts['template'];
864 933 } else {
865 - $counts['published'] ++;
934 + ++$counts['published'];
866 935 }
867 936 } else {
868 - $counts['trash'] ++;
937 + ++$counts['trash'];
869 938 }
870 939
871 940 if ( 'draft' == $row->status ) {
872 - $counts['draft'] ++;
941 + ++$counts['draft'];
873 942 }
874 943
875 944 unset( $row );
876 945 }
@@ -936,11 +1005,11 @@
936 1005 $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
937 1006 }
938 1007
939 1008 if ( $form->id == $values['posted_form_id'] ) {
940 - //if there are two forms on the same page, make sure not to submit both
1009 + // If there are two forms on the same page, make sure not to submit both.
941 1010 foreach ( $default_values as $var => $default ) {
942 - if ( $var == 'action' ) {
1011 + if ( $var === 'action' ) {
943 1012 $values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' );
944 1013 } else {
945 1014 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
946 1015 }
@@ -1008,9 +1077,9 @@
1008 1077 return $values;
1009 1078 }
1010 1079
1011 1080 public static function get_current_form_id( $default_form = 'none' ) {
1012 - if ( 'first' == $default_form ) {
1081 + if ( 'first' === $default_form ) {
1013 1082 $form = self::get_current_form();
1014 1083 } else {
1015 1084 $form = self::maybe_get_current_form();
1016 1085 }
@@ -1021,9 +1090,9 @@
1021 1090
1022 1091 public static function maybe_get_current_form( $form_id = 0 ) {
1023 1092 global $frm_vars;
1024 1093
1025 - if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) {
1094 + if ( ! empty( $frm_vars['current_form'] ) && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) {
1026 1095 return $frm_vars['current_form'];
1027 1096 }
1028 1097
1029 1098 $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' );
@@ -1070,9 +1139,9 @@
1070 1139 $global_load = true;
1071 1140 $frm_vars['load_css'] = true;
1072 1141 }
1073 1142
1074 - return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load );
1143 + return empty( $frm_vars['css_loaded'] ) && $global_load;
1075 1144 }
1076 1145
1077 1146 /**
1078 1147 * @since 4.06.03
@@ -1091,9 +1160,9 @@
1091 1160 return $visible;
1092 1161 }
1093 1162
1094 1163 public static function show_submit( $form ) {
1095 - $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() );
1164 + $show = ( ! $form->is_template && $form->status === 'published' && ! FrmAppHelper::is_admin() );
1096 1165 $show = apply_filters( 'frm_show_submit_button', $show, $form );
1097 1166
1098 1167 return $show;
1099 1168 }
@@ -1101,9 +1170,9 @@
1101 1170 /**
1102 1171 * @since 2.3
1103 1172 */
1104 1173 public static function get_option( $atts ) {
1105 - $form = $atts['form'];
1174 + $form = $atts['form'];
1106 1175 $default = isset( $atts['default'] ) ? $atts['default'] : '';
1107 1176
1108 1177 return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $default;
1109 1178 }
@@ -1130,8 +1199,50 @@
1130 1199 return ! empty( $form->options['ajax_submit'] );
1131 1200 }
1132 1201
1133 1202 /**
1203 + * Get the latest form available.
1204 + *
1205 + * @since 6.8
1206 + * @return object
1207 + */
1208 + public static function get_latest_form() {
1209 +
1210 + $args = array(
1211 + array(
1212 + 'or' => 1,
1213 + 'parent_form_id' => null,
1214 + 'parent_form_id <' => 1,
1215 + ),
1216 + 'is_template' => 0,
1217 + 'status !' => 'trash',
1218 + );
1219 +
1220 + return self::getAll( $args, 'created_at desc', 1 );
1221 + }
1222 +
1223 + /**
1224 + * Count and return total forms.
1225 + *
1226 + * @since 6.8
1227 + * @return int
1228 + */
1229 + public static function get_forms_count() {
1230 +
1231 + $args = array(
1232 + array(
1233 + 'or' => 1,
1234 + 'parent_form_id' => null,
1235 + 'parent_form_id <' => 1,
1236 + ),
1237 + 'is_template' => 0,
1238 + 'status !' => 'trash',
1239 + );
1240 +
1241 + return FrmDb::get_count( 'frm_forms', $args );
1242 + }
1243 +
1244 + /**
1134 1245 * @deprecated 2.03.05 This is still referenced in a few add ons (API, locations).
1135 1246 * @codeCoverageIgnore
1136 1247 *
1137 1248 * @param string $key
@@ -1145,9 +1256,9 @@
1145 1256 /**
1146 1257 * @deprecated 2.03.05 This is still referenced in the API add on as of v1.13.
1147 1258 * @codeCoverageIgnore
1148 1259 *
1149 - * @param string|int $id
1260 + * @param int|string $id
1150 1261 * @return string
1151 1262 */
1152 1263 public static function getKeyById( $id ) {
1153 1264 _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_key_by_id' );