PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.16
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.16
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 +146 -44 6.56.16 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,9 +513,9 @@
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;
490 518
491 519 $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default;
492 520 }
493 521
@@ -501,9 +529,9 @@
501 529 * Get a list of all form settings that should be translated
502 530 * on a multilingual site.
503 531 *
504 532 * @since 3.06.01
505 - * @param object $form - The form object
533 + * @param object $form The form object.
506 534 */
507 535 public static function translatable_strings( $form ) {
508 536 $strings = array(
509 537 'name',
@@ -520,11 +548,12 @@
520 548 return apply_filters( 'frm_form_strings', $strings, $form );
521 549 }
522 550
523 551 /**
552 + * @param int $id
524 553 * @param string $status
525 554 *
526 - * @return int|boolean
555 + * @return bool|int
527 556 */
528 557 public static function set_status( $id, $status ) {
529 558 if ( 'trash' == $status ) {
530 559 return self::trash( $id );
@@ -530,9 +559,9 @@
530 559 return self::trash( $id );
531 560 }
532 561
533 562 $statuses = array( 'published', 'draft', 'trash' );
534 - if ( ! in_array( $status, $statuses ) ) {
563 + if ( ! in_array( $status, $statuses, true ) ) {
535 564 return false;
536 565 }
537 566
538 567 global $wpdb;
@@ -559,9 +588,9 @@
559 588 return $query_results;
560 589 }
561 590
562 591 /**
563 - * @return int|boolean
592 + * @return bool|int
564 593 */
565 594 public static function trash( $id ) {
566 595 if ( ! EMPTY_TRASH_DAYS ) {
567 596 return self::destroy( $id );
@@ -605,9 +634,9 @@
605 634 return $query_results;
606 635 }
607 636
608 637 /**
609 - * @return int|boolean
638 + * @return bool|int
610 639 */
611 640 public static function destroy( $id ) {
612 641 global $wpdb;
613 642
@@ -629,8 +658,11 @@
629 658
630 659 $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) );
631 660 if ( $query_results ) {
632 661 // Delete all form actions linked to this form
662 + /**
663 + * @var FrmFormAction
664 + */
633 665 $action_control = FrmFormActionsController::get_form_actions( 'email' );
634 666 $action_control->destroy( $id, 'all' );
635 667
636 668 // Clear form caching
@@ -650,9 +682,9 @@
650 682 */
651 683 public static function scheduled_delete( $delete_timestamp = '' ) {
652 684 global $wpdb;
653 685
654 - $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' );
686 + $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' );
655 687
656 688 if ( ! $trash_forms ) {
657 689 return 0;
658 690 }
@@ -665,9 +697,11 @@
665 697 foreach ( $trash_forms as $form ) {
666 698 FrmAppHelper::unserialize_or_decode( $form->options );
667 699 if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) {
668 700 self::destroy( $form->id );
669 - $count ++;
701 + if ( empty( $form->parent_form_id ) ) {
702 + ++$count;
703 + }
670 704 }
671 705
672 706 unset( $form );
673 707 }
@@ -727,11 +761,10 @@
727 761
728 762 /**
729 763 * If $form is numeric, get the form object
730 764 *
731 - * @param object|int $form
732 - *
733 765 * @since 2.0.9
766 + * @param int|object $form
734 767 */
735 768 public static function maybe_get_form( &$form ) {
736 769 if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) {
737 770 $form = self::getOne( $form );
@@ -738,9 +771,11 @@
738 771 }
739 772 }
740 773
741 774 /**
742 - * @return object form
775 + * @param int|string $id
776 + * @param false|int $blog_id
777 + * @return stdClass|null
743 778 */
744 779 public static function getOne( $id, $blog_id = false ) {
745 780 global $wpdb;
746 781
@@ -755,10 +790,9 @@
755 790 if ( $cache ) {
756 791 if ( isset( $cache->options ) ) {
757 792 FrmAppHelper::unserialize_or_decode( $cache->options );
758 793 }
759 -
760 - return apply_filters( 'frm_form_object', wp_unslash( $cache ) );
794 + return self::prepare_form_row_data( $cache );
761 795 }
762 796 }
763 797
764 798 if ( is_numeric( $id ) ) {
@@ -773,14 +807,40 @@
773 807 FrmDb::set_cache( $results->id, $results, 'frm_form' );
774 808 FrmAppHelper::unserialize_or_decode( $results->options );
775 809 }
776 810
777 - return apply_filters( 'frm_form_object', wp_unslash( $results ) );
811 + return self::prepare_form_row_data( $results );
778 812 }
779 813
780 814 /**
781 - * @return object|array of objects
815 + * Make sure that if $row is an object, that $row->options is an array and not a string.
816 + *
817 + * @since 6.8.3
818 + *
819 + * @param stdClass|null $row The database row for a target form.
820 + * @return stdClass|null
782 821 */
822 + private static function prepare_form_row_data( $row ) {
823 + $row = wp_unslash( $row );
824 + if ( ! is_object( $row ) ) {
825 + return $row;
826 + }
827 +
828 + if ( ! is_array( $row->options ) ) {
829 + $row->options = FrmFormsHelper::get_default_opts();
830 + }
831 +
832 + /**
833 + * @since 4.03.02
834 + *
835 + * @param stdClass $row
836 + */
837 + return apply_filters( 'frm_form_object', $row );
838 + }
839 +
840 + /**
841 + * @return array|object of objects
842 + */
783 843 public static function getAll( $where = array(), $order_by = '', $limit = '' ) {
784 844 if ( is_array( $where ) && ! empty( $where ) ) {
785 845 if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
786 846 // don't get trashed templates
@@ -790,9 +850,9 @@
790 850 $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) );
791 851 } else {
792 852 global $wpdb;
793 853
794 - // the query has already been prepared if this is not an array
854 + // The query has already been prepared if this is not an array.
795 855 $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit );
796 856 $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
797 857 }
798 858
@@ -802,9 +862,9 @@
802 862 FrmAppHelper::unserialize_or_decode( $result->options );
803 863 }
804 864 }
805 865
806 - if ( $limit == ' LIMIT 1' || $limit == 1 ) {
866 + if ( $limit === ' LIMIT 1' || $limit == 1 ) {
807 867 // return the first form object if we are only getting one form
808 868 $results = reset( $results );
809 869 }
810 870
@@ -823,9 +883,9 @@
823 883 */
824 884 public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) {
825 885 $query['is_template'] = 0;
826 886 $query['status'] = array( null, '', 'published' );
827 - if ( $inc_children == 'exclude' ) {
887 + if ( $inc_children === 'exclude' ) {
828 888 $query['parent_form_id'] = array( null, 0 );
829 889 }
830 890
831 891 $forms = self::getAll( $query, 'name', $limit );
@@ -861,18 +921,18 @@
861 921
862 922 foreach ( $results as $row ) {
863 923 if ( 'trash' != $row->status ) {
864 924 if ( $row->is_template ) {
865 - $counts['template'] ++;
925 + ++$counts['template'];
866 926 } else {
867 - $counts['published'] ++;
927 + ++$counts['published'];
868 928 }
869 929 } else {
870 - $counts['trash'] ++;
930 + ++$counts['trash'];
871 931 }
872 932
873 933 if ( 'draft' == $row->status ) {
874 - $counts['draft'] ++;
934 + ++$counts['draft'];
875 935 }
876 936
877 937 unset( $row );
878 938 }
@@ -938,11 +998,11 @@
938 998 $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
939 999 }
940 1000
941 1001 if ( $form->id == $values['posted_form_id'] ) {
942 - //if there are two forms on the same page, make sure not to submit both
1002 + // If there are two forms on the same page, make sure not to submit both.
943 1003 foreach ( $default_values as $var => $default ) {
944 - if ( $var == 'action' ) {
1004 + if ( $var === 'action' ) {
945 1005 $values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' );
946 1006 } else {
947 1007 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
948 1008 }
@@ -1010,9 +1070,9 @@
1010 1070 return $values;
1011 1071 }
1012 1072
1013 1073 public static function get_current_form_id( $default_form = 'none' ) {
1014 - if ( 'first' == $default_form ) {
1074 + if ( 'first' === $default_form ) {
1015 1075 $form = self::get_current_form();
1016 1076 } else {
1017 1077 $form = self::maybe_get_current_form();
1018 1078 }
@@ -1093,9 +1153,9 @@
1093 1153 return $visible;
1094 1154 }
1095 1155
1096 1156 public static function show_submit( $form ) {
1097 - $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() );
1157 + $show = ( ! $form->is_template && $form->status === 'published' && ! FrmAppHelper::is_admin() );
1098 1158 $show = apply_filters( 'frm_show_submit_button', $show, $form );
1099 1159
1100 1160 return $show;
1101 1161 }
@@ -1103,9 +1163,9 @@
1103 1163 /**
1104 1164 * @since 2.3
1105 1165 */
1106 1166 public static function get_option( $atts ) {
1107 - $form = $atts['form'];
1167 + $form = $atts['form'];
1108 1168 $default = isset( $atts['default'] ) ? $atts['default'] : '';
1109 1169
1110 1170 return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $default;
1111 1171 }
@@ -1132,8 +1192,50 @@
1132 1192 return ! empty( $form->options['ajax_submit'] );
1133 1193 }
1134 1194
1135 1195 /**
1196 + * Get the latest form available.
1197 + *
1198 + * @since 6.8
1199 + * @return object
1200 + */
1201 + public static function get_latest_form() {
1202 +
1203 + $args = array(
1204 + array(
1205 + 'or' => 1,
1206 + 'parent_form_id' => null,
1207 + 'parent_form_id <' => 1,
1208 + ),
1209 + 'is_template' => 0,
1210 + 'status !' => 'trash',
1211 + );
1212 +
1213 + return self::getAll( $args, 'created_at desc', 1 );
1214 + }
1215 +
1216 + /**
1217 + * Count and return total forms.
1218 + *
1219 + * @since 6.8
1220 + * @return int
1221 + */
1222 + public static function get_forms_count() {
1223 +
1224 + $args = array(
1225 + array(
1226 + 'or' => 1,
1227 + 'parent_form_id' => null,
1228 + 'parent_form_id <' => 1,
1229 + ),
1230 + 'is_template' => 0,
1231 + 'status !' => 'trash',
1232 + );
1233 +
1234 + return FrmDb::get_count( 'frm_forms', $args );
1235 + }
1236 +
1237 + /**
1136 1238 * @deprecated 2.03.05 This is still referenced in a few add ons (API, locations).
1137 1239 * @codeCoverageIgnore
1138 1240 *
1139 1241 * @param string $key
@@ -1147,9 +1249,9 @@
1147 1249 /**
1148 1250 * @deprecated 2.03.05 This is still referenced in the API add on as of v1.13.
1149 1251 * @codeCoverageIgnore
1150 1252 *
1151 - * @param string|int $id
1253 + * @param int|string $id
1152 1254 * @return string
1153 1255 */
1154 1256 public static function getKeyById( $id ) {
1155 1257 _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_key_by_id' );