PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.01.02
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.01.02
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmForm.php +36 -259 6.34.01.02 View file →
@@ -5,16 +5,13 @@
5 5
6 6 class FrmForm {
7 7
8 8 /**
9 - * @param array $values
10 - * @return int|bool id on success or false on failure.
9 + * @return int|boolean id on success or false on failure
11 10 */
12 11 public static function create( $values ) {
13 12 global $wpdb;
14 13
15 - $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) );
16 -
17 14 $new_values = array(
18 15 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ),
19 16 'name' => $values['name'],
20 17 'description' => $values['description'],
@@ -32,21 +29,14 @@
32 29 $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
33 30 $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
34 31 $options['submit_html'] = isset( $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
35 32
36 - /**
37 - * Allows modifying form options before updating or creating.
38 - *
39 - * @since 5.4 Add the third param.
40 - *
41 - * @param array $options Form options.
42 - * @param array $values Form data.
43 - * @param bool $update Is form updating or creating. It's `true` if is updating.
44 - */
45 - $options = apply_filters( 'frm_form_options_before_update', $options, $values, false );
46 - $options = self::maybe_filter_form_options( $options );
33 + $options = apply_filters( 'frm_form_options_before_update', $options, $values );
47 34 $new_values['options'] = serialize( $options );
48 35
36 + //if(isset($values['id']) && is_numeric($values['id']))
37 + // $new_values['id'] = $values['id'];
38 +
49 39 $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values );
50 40
51 41 $id = $wpdb->insert_id;
52 42
@@ -56,21 +46,8 @@
56 46 return $id;
57 47 }
58 48
59 49 /**
60 - * @since 5.0.08
61 - *
62 - * @param array $options
63 - * @return array
64 - */
65 - private static function maybe_filter_form_options( $options ) {
66 - if ( ! FrmAppHelper::allow_unfiltered_html() && ! empty( $options['submit_html'] ) ) {
67 - $options['submit_html'] = FrmAppHelper::kses_submit_button( $options['submit_html'] );
68 - }
69 - return FrmAppHelper::maybe_filter_array( $options, array( 'submit_value', 'success_msg', 'before_html', 'after_html' ) );
70 - }
71 -
72 - /**
73 50 * @return int|boolean ID on success or false on failure
74 51 */
75 52 public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) {
76 53 global $wpdb;
@@ -94,10 +71,9 @@
94 71 );
95 72
96 73 if ( $blog_id ) {
97 74 $new_values['status'] = 'published';
98 - $new_options = $values->options;
99 - FrmAppHelper::unserialize_or_decode( $new_options );
75 + $new_options = maybe_unserialize( $values->options );
100 76 $new_options['email_to'] = get_option( 'admin_email' );
101 77 $new_options['copy'] = false;
102 78 $new_values['options'] = $new_options;
103 79 } else {
@@ -126,10 +102,9 @@
126 102 return false;
127 103 }
128 104
129 105 public static function after_duplicate( $form_id, $values ) {
130 - $new_opts = $values['options'];
131 - FrmAppHelper::unserialize_or_decode( $new_opts );
106 + $new_opts = maybe_unserialize( $values['options'] );
132 107 $values['options'] = $new_opts;
133 108
134 109 if ( isset( $new_opts['success_msg'] ) ) {
135 110 $new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids( $new_opts['success_msg'] );
@@ -140,93 +115,16 @@
140 115 if ( $new_opts != $values['options'] ) {
141 116 global $wpdb;
142 117 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) );
143 118 }
144 -
145 - self::switch_field_ids_in_fields( $form_id );
146 119 }
147 120
148 121 /**
149 - * Switches field ID in fields.
150 - *
151 - * @since 5.3
152 - *
153 - * @param int $form_id Form ID.
154 - */
155 - private static function switch_field_ids_in_fields( $form_id ) {
156 - global $wpdb;
157 -
158 - // Keys of fields that you want to check to replace field ID.
159 - $keys = array( 'default_value', 'field_options' );
160 - $sql_cols = 'fi.id';
161 - foreach ( $keys as $key ) {
162 - $sql_cols .= ( ',fi.' . $key );
163 - }
164 -
165 - $fields = FrmDb::get_results(
166 - "{$wpdb->prefix}frm_fields AS fi LEFT OUTER JOIN {$wpdb->prefix}frm_forms AS fr ON fi.form_id = fr.id",
167 - array(
168 - 'or' => 1,
169 - 'fi.form_id' => $form_id,
170 - 'fr.parent_form_id' => $form_id,
171 - ),
172 - $sql_cols
173 - );
174 -
175 - if ( ! $fields || ! is_array( $fields ) ) {
176 - return;
177 - }
178 -
179 - foreach ( $fields as $field ) {
180 - self::switch_field_ids_in_field( (array) $field );
181 - }
182 - }
183 -
184 - /**
185 - * Switches field ID in a field.
186 - *
187 - * @since 5.3
188 - *
189 - * @param array $field Field array.
190 - */
191 - private static function switch_field_ids_in_field( $field ) {
192 - $new_values = array();
193 - foreach ( $field as $key => $value ) {
194 - if ( 'id' === $key || ! $value ) {
195 - continue;
196 - }
197 -
198 - if ( ! is_string( $value ) && ! is_array( $value ) ) {
199 - continue;
200 - }
201 -
202 - if ( 'field_options' === $key ) {
203 - // Need to loop through field_options to prevent breaking serialized string when length changed.
204 - FrmAppHelper::unserialize_or_decode( $value );
205 - $new_val = FrmFieldsHelper::switch_field_ids( $value );
206 - $new_val = serialize( $new_val );
207 - } else {
208 - $new_val = FrmFieldsHelper::switch_field_ids( $value );
209 - }
210 -
211 - if ( $new_val !== $value ) {
212 - $new_values[ $key ] = $new_val;
213 - }
214 - }
215 -
216 - if ( ! empty( $new_values ) ) {
217 - FrmField::update( $field['id'], $new_values );
218 - }
219 - }
220 -
221 - /**
222 122 * @return int|boolean
223 123 */
224 124 public static function update( $id, $values, $create_link = false ) {
225 125 global $wpdb;
226 126
227 - $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) );
228 -
229 127 if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) {
230 128 $values['status'] = 'published';
231 129 }
232 130
@@ -235,9 +133,9 @@
235 133 }
236 134
237 135 $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' );
238 136
239 - $new_values = self::set_update_options( array(), $values, array( 'form_id' => $id ) );
137 + $new_values = self::set_update_options( array(), $values );
240 138
241 139 foreach ( $values as $value_key => $value ) {
242 140 if ( $value_key && in_array( $value_key, $form_fields ) ) {
243 141 $new_values[ $value_key ] = $value;
@@ -266,14 +164,11 @@
266 164 return $query_results;
267 165 }
268 166
269 167 /**
270 - * @param array $new_values
271 - * @param array $values
272 - * @param array $args
273 168 * @return array
274 169 */
275 - public static function set_update_options( $new_values, $values, $args = array() ) {
170 + public static function set_update_options( $new_values, $values ) {
276 171 if ( ! isset( $values['options'] ) ) {
277 172 return $new_values;
278 173 }
279 174
@@ -284,19 +179,9 @@
284 179 $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
285 180 $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
286 181 $options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
287 182
288 - /**
289 - * Allows modifying form options before updating or creating.
290 - *
291 - * @since 5.4 Added the third param.
292 - *
293 - * @param array $options Form options.
294 - * @param array $values Form data.
295 - * @param bool $update Is form updating or creating. It's `true` if is updating.
296 - */
297 - $options = apply_filters( 'frm_form_options_before_update', $options, $values, true );
298 - $options = self::maybe_filter_form_options( $options );
183 + $options = apply_filters( 'frm_form_options_before_update', $options, $values );
299 184 $new_values['options'] = serialize( $options );
300 185
301 186 return $new_values;
302 187 }
@@ -362,21 +247,11 @@
362 247 $field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values );
363 248
364 249 $new_field = array(
365 250 'field_options' => $field->field_options,
366 - 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? FrmAppHelper::maybe_json_encode( $values[ 'default_value_' . $field_id ] ) : '',
251 + 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? maybe_serialize( $values[ 'default_value_' . $field_id ] ) : '',
367 252 );
368 253
369 - if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) {
370 - foreach ( $values['field_options'][ 'options_' . $field_id ] as $option_key => $option ) {
371 - if ( is_array( $option ) ) {
372 - foreach ( $option as $key => $item ) {
373 - $values['field_options'][ 'options_' . $field_id ][ $option_key ][ $key ] = FrmAppHelper::kses( $item, 'all' );
374 - }
375 - }
376 - }
377 - }
378 -
379 254 self::prepare_field_update_values( $field, $values, $new_field );
380 255
381 256 FrmField::update( $field_id, $new_field );
382 257
@@ -386,76 +261,20 @@
386 261
387 262 return $values;
388 263 }
389 264
390 - /**
391 - * @param string $opt
392 - * @param mixed $value
393 - * @return void
394 - */
395 265 private static function sanitize_field_opt( $opt, &$value ) {
396 - if ( ! is_string( $value ) ) {
397 - return;
266 + if ( is_string( $value ) ) {
267 + if ( $opt === 'calc' ) {
268 + $value = strip_tags( $value );
269 + } else {
270 + $value = FrmAppHelper::kses( $value, 'all' );
271 + }
272 + $value = trim( $value );
398 273 }
399 -
400 - /**
401 - * Allow the option to turn off sanitization for a field. This way a custom rule can be used instead.
402 - * Make sure to add custom sanitization using the frm_update_field_options filter as the data will no longer be sanitized.
403 - *
404 - * @since 6.0
405 - *
406 - * @param bool $should_sanitize
407 - * @param string $opt
408 - */
409 - $should_sanitize = apply_filters( 'frm_should_sanitize_field_opt_string', true, $opt );
410 -
411 - if ( ! $should_sanitize ) {
412 - return;
413 - }
414 -
415 - if ( $opt === 'calc' ) {
416 - $value = self::sanitize_calc( $value );
417 - } else {
418 - $value = FrmAppHelper::kses( $value, 'all' );
419 - }
420 -
421 - $value = trim( $value );
422 274 }
423 275
424 276 /**
425 - * @param string $value
426 - * @return string
427 - */
428 - private static function sanitize_calc( $value ) {
429 - if ( false !== strpos( $value, '<' ) ) {
430 - $value = self::normalize_calc_spaces( $value );
431 - }
432 - $allow = array( '<= ', ' >=' ); // Allow <= and >=
433 - $temp = array( '< = ', ' > =' );
434 - $value = str_replace( $allow, $temp, $value );
435 - $value = strip_tags( $value );
436 - $value = str_replace( $temp, $allow, $value );
437 - return $value;
438 - }
439 -
440 - /**
441 - * Format a comparison like 5<10 to 5 < 10. Also works on 5< 10, 5 <10, 5<=10 variations.
442 - * This is to avoid an issue with unspaced calculations being recognized as HTML that gets removed when strip_tags is called.
443 - *
444 - * @param string $calc
445 - * @return string
446 - */
447 - private static function normalize_calc_spaces( $calc ) {
448 - // Check for a pattern with 5 parts
449 - // $1 \d the first comparison digit.
450 - // $2 a space (optional).
451 - // $3 an equals sign (optional) that follows the < operator for <= comparisons.
452 - // $4 another space (optional).
453 - // $5 \d the second comparison digit.
454 - return preg_replace( '/(\d)( ){0,1}<(=){0,1}( ){0,1}(\d)/', '$1 <$3 $5', $calc );
455 - }
456 -
457 - /**
458 277 * Updating the settings page
459 278 */
460 279 private static function get_settings_page_html( $values, &$field ) {
461 280 if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) {
@@ -510,12 +329,8 @@
510 329 'description',
511 330 'submit_value',
512 331 'submit_msg',
513 332 'success_msg',
514 - 'invalid_msg',
515 - 'failed_msg',
516 - 'login_msg',
517 - 'admin_permission',
518 333 );
519 334
520 335 return apply_filters( 'frm_form_strings', $strings, $form );
521 336 }
@@ -545,9 +360,9 @@
545 360 );
546 361 FrmDb::get_where_clause_and_values( $where );
547 362 array_unshift( $where['values'], $status );
548 363
549 - $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
364 + $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // WPCS: unprepared SQL ok.
550 365 } else {
551 366 $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) );
552 367 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) );
553 368 }
@@ -653,9 +468,9 @@
653 468
654 469 $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' );
655 470
656 471 if ( ! $trash_forms ) {
657 - return 0;
472 + return;
658 473 }
659 474
660 475 if ( empty( $delete_timestamp ) ) {
661 476 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
@@ -662,9 +477,9 @@
662 477 }
663 478
664 479 $count = 0;
665 480 foreach ( $trash_forms as $form ) {
666 - FrmAppHelper::unserialize_or_decode( $form->options );
481 + $form->options = maybe_unserialize( $form->options );
667 482 if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) {
668 483 self::destroy( $form->id );
669 484 $count ++;
670 485 }
@@ -751,12 +566,12 @@
751 566 $table_name = $wpdb->prefix . 'frm_forms';
752 567 $cache = wp_cache_get( $id, 'frm_form' );
753 568 if ( $cache ) {
754 569 if ( isset( $cache->options ) ) {
755 - FrmAppHelper::unserialize_or_decode( $cache->options );
570 + $cache->options = maybe_unserialize( $cache->options );
756 571 }
757 572
758 - return apply_filters( 'frm_form_object', wp_unslash( $cache ) );
573 + return wp_unslash( $cache );
759 574 }
760 575 }
761 576
762 577 if ( is_numeric( $id ) ) {
@@ -768,12 +583,12 @@
768 583 $results = FrmDb::get_row( $table_name, $where );
769 584
770 585 if ( isset( $results->options ) ) {
771 586 FrmDb::set_cache( $results->id, $results, 'frm_form' );
772 - FrmAppHelper::unserialize_or_decode( $results->options );
587 + $results->options = maybe_unserialize( $results->options );
773 588 }
774 589
775 - return apply_filters( 'frm_form_object', wp_unslash( $results ) );
590 + return wp_unslash( $results );
776 591 }
777 592
778 593 /**
779 594 * @return object|array of objects
@@ -790,15 +605,15 @@
790 605 global $wpdb;
791 606
792 607 // the query has already been prepared if this is not an array
793 608 $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit );
794 - $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
609 + $results = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok.
795 610 }
796 611
797 612 if ( $results ) {
798 613 foreach ( $results as $result ) {
799 614 FrmDb::set_cache( $result->id, $result, 'frm_form' );
800 - FrmAppHelper::unserialize_or_decode( $result->options );
615 + $result->options = maybe_unserialize( $result->options );
801 616 }
802 617 }
803 618
804 619 if ( $limit == ' LIMIT 1' || $limit == 1 ) {
@@ -812,13 +627,9 @@
812 627 /**
813 628 * Get all published forms
814 629 *
815 630 * @since 2.0
816 - *
817 - * @param array $query
818 - * @param int $limit
819 - * @param string $inc_children
820 - * @return array|object of forms A single form object would be passed if $limit was set to 1.
631 + * @return array of forms
821 632 */
822 633 public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) {
823 634 $query['is_template'] = 0;
824 635 $query['status'] = array( null, '', 'published' );
@@ -913,9 +724,9 @@
913 724 if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) {
914 725 return $frm_vars['form_params'][ $form->id ];
915 726 }
916 727
917 - $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // phpcs:ignore WordPress.Security.NonceVerification.Missing
728 + $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // WPCS: CSRF ok.
918 729 $action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form );
919 730
920 731 $default_values = array(
921 732 'id' => '',
@@ -953,9 +764,9 @@
953 764 }
954 765 }
955 766
956 767 if ( in_array( $values['action'], array( 'create', 'update' ) ) &&
957 - ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
768 + ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // WPCS: CSRF ok.
958 769 ) {
959 770 $values['action'] = 'new';
960 771 }
961 772
@@ -1073,25 +884,8 @@
1073 884
1074 885 return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load );
1075 886 }
1076 887
1077 - /**
1078 - * @since 4.06.03
1079 - *
1080 - * @param object $form
1081 - *
1082 - * @return bool
1083 - */
1084 - public static function &is_visible_to_user( $form ) {
1085 - if ( $form->logged_in && isset( $form->options['logged_in_role'] ) ) {
1086 - $visible = FrmAppHelper::user_has_permission( $form->options['logged_in_role'] );
1087 - } else {
1088 - $visible = true;
1089 - }
1090 -
1091 - return $visible;
1092 - }
1093 -
1094 888 public static function show_submit( $form ) {
1095 889 $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() );
1096 890 $show = apply_filters( 'frm_show_submit_button', $show, $form );
1097 891
@@ -1102,11 +896,10 @@
1102 896 * @since 2.3
1103 897 */
1104 898 public static function get_option( $atts ) {
1105 899 $form = $atts['form'];
1106 - $default = isset( $atts['default'] ) ? $atts['default'] : '';
1107 900
1108 - return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $default;
901 + return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $atts['default'];
1109 902 }
1110 903
1111 904 /**
1112 905 * Get the link to edit this form.
@@ -1118,39 +911,23 @@
1118 911 return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id );
1119 912 }
1120 913
1121 914 /**
1122 - * Check if the "Submit this form with AJAX" setting is toggled on.
1123 - *
1124 - * @since 6.2
1125 - *
1126 - * @param stdClass $form
1127 - * @return bool
1128 - */
1129 - public static function is_ajax_on( $form ) {
1130 - return ! empty( $form->options['ajax_submit'] );
1131 - }
1132 -
1133 - /**
1134 - * @deprecated 2.03.05 This is still referenced in a few add ons (API, locations).
915 + * @deprecated 3.0
1135 916 * @codeCoverageIgnore
1136 917 *
1137 918 * @param string $key
919 + *
1138 920 * @return int form id
1139 921 */
1140 922 public static function getIdByKey( $key ) {
1141 - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_id_by_key' );
1142 - return self::get_id_by_key( $key );
923 + return FrmFormDeprecated::getIdByKey( $key );
1143 924 }
1144 925
1145 926 /**
1146 - * @deprecated 2.03.05 This is still referenced in the API add on as of v1.13.
927 + * @deprecated 3.0
1147 928 * @codeCoverageIgnore
1148 - *
1149 - * @param string|int $id
1150 - * @return string
1151 929 */
1152 930 public static function getKeyById( $id ) {
1153 - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_key_by_id' );
1154 - return self::get_key_by_id( $id );
931 + return FrmFormDeprecated::getKeyById( $id );
1155 932 }
1156 933 }