PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0.08
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0.08
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 +86 -367 6.255.0.08 View file →
@@ -6,9 +6,9 @@
6 6 class FrmForm {
7 7
8 8 /**
9 9 * @param array $values
10 - * @return bool|int id on success or false on failure.
10 + * @return int|boolean id on success or false on failure
11 11 */
12 12 public static function create( $values ) {
13 13 global $wpdb;
14 14
@@ -17,33 +17,24 @@
17 17 $new_values = array(
18 18 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ),
19 19 'name' => $values['name'],
20 20 'description' => $values['description'],
21 - 'status' => $values['status'] ?? 'published',
22 - 'logged_in' => $values['logged_in'] ?? 0,
21 + 'status' => isset( $values['status'] ) ? $values['status'] : 'published',
22 + 'logged_in' => isset( $values['logged_in'] ) ? $values['logged_in'] : 0,
23 23 'is_template' => isset( $values['is_template'] ) ? (int) $values['is_template'] : 0,
24 24 'parent_form_id' => isset( $values['parent_form_id'] ) ? absint( $values['parent_form_id'] ) : 0,
25 25 'editable' => isset( $values['editable'] ) ? (int) $values['editable'] : 0,
26 - 'created_at' => $values['created_at'] ?? current_time( 'mysql', 1 ),
26 + 'created_at' => isset( $values['created_at'] ) ? $values['created_at'] : current_time( 'mysql', 1 ),
27 27 );
28 28
29 29 $options = isset( $values['options'] ) ? (array) $values['options'] : array();
30 30 FrmFormsHelper::fill_form_options( $options, $values );
31 31
32 - $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' );
33 - $options['after_html'] = $values['options']['after_html'] ?? FrmFormsHelper::get_default_html( 'after' );
34 - $options['submit_html'] = $values['options']['submit_html'] ?? FrmFormsHelper::get_default_html( 'submit' );
32 + $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
33 + $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
34 + $options['submit_html'] = isset( $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
35 35
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 );
36 + $options = apply_filters( 'frm_form_options_before_update', $options, $values );
46 37 $options = self::maybe_filter_form_options( $options );
47 38 $new_values['options'] = serialize( $options );
48 39
49 40 $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values );
@@ -62,16 +53,13 @@
62 53 * @param array $options
63 54 * @return array
64 55 */
65 56 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' ) );
57 + return FrmAppHelper::maybe_filter_array( $options, array( 'submit_value', 'success_msg', 'before_html', 'after_html', 'submit_html' ) );
70 58 }
71 59
72 60 /**
73 - * @return bool|int ID on success or false on failure
61 + * @return int|boolean ID on success or false on failure
74 62 */
75 63 public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) {
76 64 global $wpdb;
77 65
@@ -93,10 +81,10 @@
93 81 'is_template' => $template ? 1 : 0,
94 82 );
95 83
96 84 if ( $blog_id ) {
97 - $new_values['status'] = 'published';
98 - $new_options = $values->options;
85 + $new_values['status'] = 'published';
86 + $new_options = $values->options;
99 87 FrmAppHelper::unserialize_or_decode( $new_options );
100 88 $new_options['email_to'] = get_option( 'admin_email' );
101 89 $new_options['copy'] = false;
102 90 $new_values['options'] = $new_options;
@@ -126,9 +114,9 @@
126 114 return false;
127 115 }
128 116
129 117 public static function after_duplicate( $form_id, $values ) {
130 - $new_opts = $values['options'];
118 + $new_opts = $values['options'];
131 119 FrmAppHelper::unserialize_or_decode( $new_opts );
132 120 $values['options'] = $new_opts;
133 121
134 122 if ( isset( $new_opts['success_msg'] ) ) {
@@ -140,88 +128,13 @@
140 128 if ( $new_opts != $values['options'] ) {
141 129 global $wpdb;
142 130 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) );
143 131 }
144 -
145 - self::switch_field_ids_in_fields( $form_id );
146 132 }
147 133
148 134 /**
149 - * Switches field ID in fields.
150 - *
151 - * @since 5.3
152 - *
153 - * @param int $form_id Form ID.
135 + * @return int|boolean
154 136 */
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 - }//end foreach
215 -
216 - if ( ! empty( $new_values ) ) {
217 - FrmField::update( $field['id'], $new_values );
218 - }
219 - }
220 -
221 - /**
222 - * @return bool|int
223 - */
224 137 public static function update( $id, $values, $create_link = false ) {
225 138 global $wpdb;
226 139
227 140 $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) );
@@ -235,9 +148,9 @@
235 148 }
236 149
237 150 $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' );
238 151
239 - $new_values = self::set_update_options( array(), $values, array( 'form_id' => $id ) );
152 + $new_values = self::set_update_options( array(), $values );
240 153
241 154 foreach ( $values as $value_key => $value ) {
242 155 if ( $value_key && in_array( $value_key, $form_fields ) ) {
243 156 $new_values[ $value_key ] = $value;
@@ -243,9 +156,9 @@
243 156 $new_values[ $value_key ] = $value;
244 157 }
245 158 }
246 159
247 - if ( ! empty( $values['new_status'] ) ) {
160 + if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) {
248 161 $new_values['status'] = $values['new_status'];
249 162 }
250 163
251 164 if ( ! empty( $new_values ) ) {
@@ -266,36 +179,24 @@
266 179 return $query_results;
267 180 }
268 181
269 182 /**
270 - * @param array $new_values
271 - * @param array $values
272 - * @param array $args
273 183 * @return array
274 184 */
275 - public static function set_update_options( $new_values, $values, $args = array() ) {
185 + public static function set_update_options( $new_values, $values ) {
276 186 if ( ! isset( $values['options'] ) ) {
277 187 return $new_values;
278 188 }
279 189
280 - $options = ! empty( $values['options'] ) ? (array) $values['options'] : array();
190 + $options = isset( $values['options'] ) ? (array) $values['options'] : array();
281 191 FrmFormsHelper::fill_form_options( $options, $values );
282 192
283 - $options['custom_style'] = $values['options']['custom_style'] ?? 0;
284 - $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' );
285 - $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' );
193 + $options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0;
194 + $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
195 + $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
196 + $options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
287 197
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 );
198 + $options = apply_filters( 'frm_form_options_before_update', $options, $values );
298 199 $options = self::maybe_filter_form_options( $options );
299 200 $new_values['options'] = serialize( $options );
300 201
301 202 return $new_values;
@@ -348,20 +249,15 @@
348 249 continue;
349 250 }
350 251 }
351 252
352 - // Updating the form.
253 + //updating the form
353 254 $update_options = FrmFieldsHelper::get_default_field_options_from_field( $field );
354 - // Don't check for POST html.
355 - unset( $update_options['custom_html'] );
255 + unset( $update_options['custom_html'] ); // don't check for POST html
356 256 $update_options = apply_filters( 'frm_field_options_to_update', $update_options );
357 257
358 - if ( ! is_array( $field->field_options ) ) {
359 - $field->field_options = array();
360 - }
361 -
362 258 foreach ( $update_options as $opt => $default ) {
363 - $field->field_options[ $opt ] = $values['field_options'][ $opt . '_' . $field_id ] ?? $default;
259 + $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default;
364 260 self::sanitize_field_opt( $opt, $field->field_options[ $opt ] );
365 261 }
366 262
367 263 $field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values );
@@ -370,87 +266,28 @@
370 266 'field_options' => $field->field_options,
371 267 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? FrmAppHelper::maybe_json_encode( $values[ 'default_value_' . $field_id ] ) : '',
372 268 );
373 269
374 - if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) {
375 - foreach ( $values['field_options'][ 'options_' . $field_id ] as $option_key => $option ) {
376 - if ( is_array( $option ) ) {
377 - foreach ( $option as $key => $item ) {
378 - $values['field_options'][ 'options_' . $field_id ][ $option_key ][ $key ] = FrmAppHelper::kses( $item, 'all' );
379 - }
380 - }
381 - }
382 - }
383 -
384 270 self::prepare_field_update_values( $field, $values, $new_field );
385 - self::maybe_update_max_option( $field, $values, $new_field );
386 271
387 272 FrmField::update( $field_id, $new_field );
388 273
389 274 FrmField::delete_form_transient( $field->form_id );
390 - }//end foreach
275 + }
391 276 self::clear_form_cache();
392 277
393 278 return $values;
394 279 }
395 280
396 - /**
397 - * Resets the 'max' option of a field when changing paragraph field type to other field types like text, email etc.
398 - *
399 - * @since 6.7
400 - *
401 - * @param array $field
402 - * @param array $values
403 - * @param array $new_field
404 - * @return void
405 - */
406 - private static function maybe_update_max_option( $field, $values, &$new_field ) {
407 - if ( $field->type === 'textarea' &&
408 - ! empty( $values['field_options'][ 'type_' . $field->id ] ) &&
409 - in_array( $values['field_options'][ 'type_' . $field->id ], array( 'text', 'email', 'url', 'password', 'phone' ), true ) ) {
410 -
411 - $new_field['field_options']['max'] = '';
412 -
413 - /**
414 - * Update posted field setting so that new 'max' option is displayed after form is saved and page reloads.
415 - * FrmFieldsHelper::fill_default_field_opts populates field options by calling self::get_posted_field_setting.
416 - */
417 - $_POST['field_options'][ 'max_' . $field->id ] = '';
418 - }
419 - }
420 -
421 - /**
422 - * @param string $opt
423 - * @param mixed $value
424 - * @return void
425 - */
426 281 private static function sanitize_field_opt( $opt, &$value ) {
427 - if ( ! is_string( $value ) ) {
428 - return;
282 + if ( is_string( $value ) ) {
283 + if ( $opt === 'calc' ) {
284 + $value = self::sanitize_calc( $value );
285 + } else {
286 + $value = FrmAppHelper::kses( $value, 'all' );
287 + }
288 + $value = trim( $value );
429 289 }
430 -
431 - /**
432 - * Allow the option to turn off sanitization for a field. This way a custom rule can be used instead.
433 - * Make sure to add custom sanitization using the frm_update_field_options filter as the data will no longer be sanitized.
434 - *
435 - * @since 6.0
436 - *
437 - * @param bool $should_sanitize
438 - * @param string $opt
439 - */
440 - $should_sanitize = apply_filters( 'frm_should_sanitize_field_opt_string', true, $opt );
441 -
442 - if ( ! $should_sanitize ) {
443 - return;
444 - }
445 -
446 - if ( $opt === 'calc' ) {
447 - $value = self::sanitize_calc( $value );
448 - } else {
449 - $value = FrmAppHelper::kses( $value, 'all' );
450 - }
451 -
452 - $value = trim( $value );
453 290 }
454 291
455 292 /**
456 293 * @param string $value
@@ -459,10 +296,9 @@
459 296 private static function sanitize_calc( $value ) {
460 297 if ( false !== strpos( $value, '<' ) ) {
461 298 $value = self::normalize_calc_spaces( $value );
462 299 }
463 - // Allow <= and >=.
464 - $allow = array( '<= ', ' >=' );
300 + $allow = array( '<= ', ' >=' ); // Allow <= and >=
465 301 $temp = array( '< = ', ' > =' );
466 302 $value = str_replace( $allow, $temp, $value );
467 303 $value = strip_tags( $value );
468 304 $value = str_replace( $temp, $allow, $value );
@@ -477,16 +313,14 @@
477 313 * @return string
478 314 */
479 315 private static function normalize_calc_spaces( $calc ) {
480 316 // Check for a pattern with 5 parts
481 - // $1 \d|\] the first comparison digit or the end of a comparison shortcode.
317 + // $1 \d the first comparison digit.
482 318 // $2 a space (optional).
483 319 // $3 an equals sign (optional) that follows the < operator for <= comparisons.
484 320 // $4 another space (optional).
485 - // $5 \d|\[ the second comparison digit or the start of a comparison shortcode.
486 - $calc = preg_replace( '/(\d|\])( ){0,1}<(=){0,1}( ){0,1}(\d|\[)/', '$1 <$3 $5', $calc );
487 -
488 - return $calc;
321 + // $5 \d the second comparison digit.
322 + return preg_replace( '/(\d)( ){0,1}<(=){0,1}( ){0,1}(\d)/', '$1 <$3 $5', $calc );
489 323 }
490 324
491 325 /**
492 326 * Updating the settings page
@@ -493,12 +327,12 @@
493 327 */
494 328 private static function get_settings_page_html( $values, &$field ) {
495 329 if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) {
496 330 $prev_opts = array();
497 - $fallback_html = $field->field_options['custom_html'] ?? FrmFieldsHelper::get_default_html( $field->type );
331 + $fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type );
498 332
499 - $field->field_options['custom_html'] = $values['field_options'][ 'custom_html_' . $field->id ] ?? $fallback_html;
500 - } elseif ( $field->type === 'hidden' || $field->type === 'user_id' ) {
333 + $field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ? $values['field_options'][ 'custom_html_' . $field->id ] : $fallback_html;
334 + } elseif ( $field->type == 'hidden' || $field->type == 'user_id' ) {
501 335 $prev_opts = $field->field_options;
502 336 }
503 337
504 338 if ( isset( $prev_opts ) ) {
@@ -519,14 +353,11 @@
519 353 'options' => '',
520 354 'name' => '',
521 355 );
522 356 foreach ( $field_cols as $col => $default ) {
523 - $default = $default === '' ? $field->{$col} : $default;
524 - $new_field[ $col ] = $values['field_options'][ $col . '_' . $field->id ] ?? $default;
525 - }
357 + $default = ( $default === '' ) ? $field->{$col} : $default;
526 358
527 - if ( $field->type === 'submit' && isset( $new_field['field_order'] ) && (int) $new_field['field_order'] === FrmSubmitHelper::DEFAULT_ORDER ) {
528 - $new_field['field_order'] = $field->field_order;
359 + $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default;
529 360 }
530 361
531 362 // Don't save the template option.
532 363 if ( is_array( $new_field['options'] ) && isset( $new_field['options']['000'] ) ) {
@@ -538,9 +369,9 @@
538 369 * Get a list of all form settings that should be translated
539 370 * on a multilingual site.
540 371 *
541 372 * @since 3.06.01
542 - * @param object $form The form object.
373 + * @param object $form - The form object
543 374 */
544 375 public static function translatable_strings( $form ) {
545 376 $strings = array(
546 377 'name',
@@ -547,12 +378,8 @@
547 378 'description',
548 379 'submit_value',
549 380 'submit_msg',
550 381 'success_msg',
551 - 'invalid_msg',
552 - 'failed_msg',
553 - 'login_msg',
554 - 'admin_permission',
555 382 );
556 383
557 384 return apply_filters( 'frm_form_strings', $strings, $form );
558 385 }
@@ -557,12 +384,11 @@
557 384 return apply_filters( 'frm_form_strings', $strings, $form );
558 385 }
559 386
560 387 /**
561 - * @param int $id
562 388 * @param string $status
563 389 *
564 - * @return bool|int
390 + * @return int|boolean
565 391 */
566 392 public static function set_status( $id, $status ) {
567 393 if ( 'trash' == $status ) {
568 394 return self::trash( $id );
@@ -568,9 +394,9 @@
568 394 return self::trash( $id );
569 395 }
570 396
571 397 $statuses = array( 'published', 'draft', 'trash' );
572 - if ( ! in_array( $status, $statuses, true ) ) {
398 + if ( ! in_array( $status, $statuses ) ) {
573 399 return false;
574 400 }
575 401
576 402 global $wpdb;
@@ -583,9 +409,9 @@
583 409 );
584 410 FrmDb::get_where_clause_and_values( $where );
585 411 array_unshift( $where['values'], $status );
586 412
587 - $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
413 + $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // WPCS: unprepared SQL ok.
588 414 } else {
589 415 $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) );
590 416 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) );
591 417 }
@@ -597,9 +423,9 @@
597 423 return $query_results;
598 424 }
599 425
600 426 /**
601 - * @return bool|int
427 + * @return int|boolean
602 428 */
603 429 public static function trash( $id ) {
604 430 if ( ! EMPTY_TRASH_DAYS ) {
605 431 return self::destroy( $id );
@@ -609,12 +435,8 @@
609 435 if ( ! $form ) {
610 436 return false;
611 437 }
612 438
613 - if ( $form->status === 'trash' ) {
614 - return false;
615 - }
616 -
617 439 $options = $form->options;
618 440 $options['trash_time'] = time();
619 441
620 442 global $wpdb;
@@ -647,9 +469,9 @@
647 469 return $query_results;
648 470 }
649 471
650 472 /**
651 - * @return bool|int
473 + * @return int|boolean
652 474 */
653 475 public static function destroy( $id ) {
654 476 global $wpdb;
655 477
@@ -671,11 +493,8 @@
671 493
672 494 $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) );
673 495 if ( $query_results ) {
674 496 // Delete all form actions linked to this form
675 - /**
676 - * @var FrmFormAction
677 - */
678 497 $action_control = FrmFormActionsController::get_form_actions( 'email' );
679 498 $action_control->destroy( $id, 'all' );
680 499
681 500 // Clear form caching
@@ -695,12 +514,12 @@
695 514 */
696 515 public static function scheduled_delete( $delete_timestamp = '' ) {
697 516 global $wpdb;
698 517
699 - $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' );
518 + $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' );
700 519
701 520 if ( ! $trash_forms ) {
702 - return 0;
521 + return;
703 522 }
704 523
705 524 if ( empty( $delete_timestamp ) ) {
706 525 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
@@ -710,11 +529,9 @@
710 529 foreach ( $trash_forms as $form ) {
711 530 FrmAppHelper::unserialize_or_decode( $form->options );
712 531 if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) {
713 532 self::destroy( $form->id );
714 - if ( empty( $form->parent_form_id ) ) {
715 - ++$count;
716 - }
533 + $count ++;
717 534 }
718 535
719 536 unset( $form );
720 537 }
@@ -734,12 +551,10 @@
734 551 }
735 552
736 553 $query_key = is_numeric( $id ) ? 'id' : 'form_key';
737 554 $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' );
555 + $r = stripslashes( $r );
738 556
739 - // An empty form name can result in a null value.
740 - $r = is_null( $r ) ? '' : stripslashes( $r );
741 -
742 557 return $r;
743 558 }
744 559
745 560 /**
@@ -774,10 +589,11 @@
774 589
775 590 /**
776 591 * If $form is numeric, get the form object
777 592 *
593 + * @param object|int $form
594 + *
778 595 * @since 2.0.9
779 - * @param int|object $form
780 596 */
781 597 public static function maybe_get_form( &$form ) {
782 598 if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) {
783 599 $form = self::getOne( $form );
@@ -784,11 +600,9 @@
784 600 }
785 601 }
786 602
787 603 /**
788 - * @param int|string $id
789 - * @param false|int $blog_id
790 - * @return stdClass|null
604 + * @return object form
791 605 */
792 606 public static function getOne( $id, $blog_id = false ) {
793 607 global $wpdb;
794 608
@@ -803,9 +617,10 @@
803 617 if ( $cache ) {
804 618 if ( isset( $cache->options ) ) {
805 619 FrmAppHelper::unserialize_or_decode( $cache->options );
806 620 }
807 - return self::prepare_form_row_data( $cache );
621 +
622 + return wp_unslash( $cache );
808 623 }
809 624 }
810 625
811 626 if ( is_numeric( $id ) ) {
@@ -820,43 +635,17 @@
820 635 FrmDb::set_cache( $results->id, $results, 'frm_form' );
821 636 FrmAppHelper::unserialize_or_decode( $results->options );
822 637 }
823 638
824 - return self::prepare_form_row_data( $results );
639 + return apply_filters( 'frm_form_object', wp_unslash( $results ) );
825 640 }
826 641
827 642 /**
828 - * Make sure that if $row is an object, that $row->options is an array and not a string.
829 - *
830 - * @since 6.8.3
831 - *
832 - * @param stdClass|null $row The database row for a target form.
833 - * @return stdClass|null
643 + * @return object|array of objects
834 644 */
835 - private static function prepare_form_row_data( $row ) {
836 - $row = wp_unslash( $row );
837 - if ( ! is_object( $row ) ) {
838 - return $row;
839 - }
840 -
841 - if ( ! is_array( $row->options ) ) {
842 - $row->options = FrmFormsHelper::get_default_opts();
843 - }
844 -
845 - /**
846 - * @since 4.03.02
847 - *
848 - * @param stdClass $row
849 - */
850 - return apply_filters( 'frm_form_object', $row );
851 - }
852 -
853 - /**
854 - * @return array|object of objects
855 - */
856 645 public static function getAll( $where = array(), $order_by = '', $limit = '' ) {
857 646 if ( is_array( $where ) && ! empty( $where ) ) {
858 - if ( ! empty( $where['is_template'] ) && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
647 + if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
859 648 // don't get trashed templates
860 649 $where['status'] = array( null, '', 'published' );
861 650 }
862 651
@@ -863,11 +652,11 @@
863 652 $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) );
864 653 } else {
865 654 global $wpdb;
866 655
867 - // The query has already been prepared if this is not an array.
656 + // the query has already been prepared if this is not an array
868 657 $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit );
869 - $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
658 + $results = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok.
870 659 }
871 660
872 661 if ( $results ) {
873 662 foreach ( $results as $result ) {
@@ -875,9 +664,9 @@
875 664 FrmAppHelper::unserialize_or_decode( $result->options );
876 665 }
877 666 }
878 667
879 - if ( $limit === ' LIMIT 1' || $limit == 1 ) {
668 + if ( $limit == ' LIMIT 1' || $limit == 1 ) {
880 669 // return the first form object if we are only getting one form
881 670 $results = reset( $results );
882 671 }
883 672
@@ -887,18 +676,14 @@
887 676 /**
888 677 * Get all published forms
889 678 *
890 679 * @since 2.0
891 - *
892 - * @param array $query
893 - * @param int $limit
894 - * @param string $inc_children
895 - * @return array|object of forms A single form object would be passed if $limit was set to 1.
680 + * @return array of forms
896 681 */
897 682 public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) {
898 683 $query['is_template'] = 0;
899 684 $query['status'] = array( null, '', 'published' );
900 - if ( $inc_children === 'exclude' ) {
685 + if ( $inc_children == 'exclude' ) {
901 686 $query['parent_form_id'] = array( null, 0 );
902 687 }
903 688
904 689 $forms = self::getAll( $query, 'name', $limit );
@@ -934,18 +719,18 @@
934 719
935 720 foreach ( $results as $row ) {
936 721 if ( 'trash' != $row->status ) {
937 722 if ( $row->is_template ) {
938 - ++$counts['template'];
723 + $counts['template'] ++;
939 724 } else {
940 - ++$counts['published'];
725 + $counts['published'] ++;
941 726 }
942 727 } else {
943 - ++$counts['trash'];
728 + $counts['trash'] ++;
944 729 }
945 730
946 731 if ( 'draft' == $row->status ) {
947 - ++$counts['draft'];
732 + $counts['draft'] ++;
948 733 }
949 734
950 735 unset( $row );
951 736 }
@@ -988,9 +773,9 @@
988 773 if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) {
989 774 return $frm_vars['form_params'][ $form->id ];
990 775 }
991 776
992 - $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // phpcs:ignore WordPress.Security.NonceVerification.Missing
777 + $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // WPCS: CSRF ok.
993 778 $action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form );
994 779
995 780 $default_values = array(
996 781 'id' => '',
@@ -1011,11 +796,11 @@
1011 796 $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
1012 797 }
1013 798
1014 799 if ( $form->id == $values['posted_form_id'] ) {
1015 - // If there are two forms on the same page, make sure not to submit both.
800 + //if there are two forms on the same page, make sure not to submit both
1016 801 foreach ( $default_values as $var => $default ) {
1017 - if ( $var === 'action' ) {
802 + if ( $var == 'action' ) {
1018 803 $values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' );
1019 804 } else {
1020 805 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
1021 806 }
@@ -1028,9 +813,9 @@
1028 813 }
1029 814 }
1030 815
1031 816 if ( in_array( $values['action'], array( 'create', 'update' ) ) &&
1032 - ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
817 + ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // WPCS: CSRF ok.
1033 818 ) {
1034 819 $values['action'] = 'new';
1035 820 }
1036 821
@@ -1083,9 +868,9 @@
1083 868 return $values;
1084 869 }
1085 870
1086 871 public static function get_current_form_id( $default_form = 'none' ) {
1087 - if ( 'first' === $default_form ) {
872 + if ( 'first' == $default_form ) {
1088 873 $form = self::get_current_form();
1089 874 } else {
1090 875 $form = self::maybe_get_current_form();
1091 876 }
@@ -1096,9 +881,9 @@
1096 881
1097 882 public static function maybe_get_current_form( $form_id = 0 ) {
1098 883 global $frm_vars;
1099 884
1100 - if ( ! empty( $frm_vars['current_form'] ) && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) {
885 + if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) {
1101 886 return $frm_vars['current_form'];
1102 887 }
1103 888
1104 889 $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' );
@@ -1145,9 +930,9 @@
1145 930 $global_load = true;
1146 931 $frm_vars['load_css'] = true;
1147 932 }
1148 933
1149 - return empty( $frm_vars['css_loaded'] ) && $global_load;
934 + return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load );
1150 935 }
1151 936
1152 937 /**
1153 938 * @since 4.06.03
@@ -1162,21 +947,13 @@
1162 947 } else {
1163 948 $visible = true;
1164 949 }
1165 950
1166 - /**
1167 - * @since 6.25
1168 - *
1169 - * @param bool $visible
1170 - * @param object $form
1171 - */
1172 - $visible = (bool) apply_filters( 'frm_form_is_visible', $visible, $form );
1173 -
1174 951 return $visible;
1175 952 }
1176 953
1177 954 public static function show_submit( $form ) {
1178 - $show = ( ! $form->is_template && $form->status === 'published' && ! FrmAppHelper::is_admin() );
955 + $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() );
1179 956 $show = apply_filters( 'frm_show_submit_button', $show, $form );
1180 957
1181 958 return $show;
1182 959 }
@@ -1184,12 +961,12 @@
1184 961 /**
1185 962 * @since 2.3
1186 963 */
1187 964 public static function get_option( $atts ) {
1188 - $form = $atts['form'];
1189 - $default = $atts['default'] ?? '';
965 + $form = $atts['form'];
966 + $default = isset( $atts['default'] ) ? $atts['default'] : '';
1190 967
1191 - return $form->options[ $atts['option'] ] ?? $default;
968 + return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $default;
1192 969 }
1193 970
1194 971 /**
1195 972 * Get the link to edit this form.
@@ -1201,81 +978,23 @@
1201 978 return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id );
1202 979 }
1203 980
1204 981 /**
1205 - * Check if the "Submit this form with AJAX" setting is toggled on.
1206 - *
1207 - * @since 6.2
1208 - *
1209 - * @param stdClass $form
1210 - * @return bool
1211 - */
1212 - public static function is_ajax_on( $form ) {
1213 - return ! empty( $form->options['ajax_submit'] );
1214 - }
1215 -
1216 - /**
1217 - * Get the latest form available.
1218 - *
1219 - * @since 6.8
1220 - * @return object
1221 - */
1222 - public static function get_latest_form() {
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 self::getAll( $args, 'created_at desc', 1 );
1235 - }
1236 -
1237 - /**
1238 - * Count and return total forms.
1239 - *
1240 - * @since 6.8
1241 - * @return int
1242 - */
1243 - public static function get_forms_count() {
1244 -
1245 - $args = array(
1246 - array(
1247 - 'or' => 1,
1248 - 'parent_form_id' => null,
1249 - 'parent_form_id <' => 1,
1250 - ),
1251 - 'is_template' => 0,
1252 - 'status !' => 'trash',
1253 - );
1254 -
1255 - return FrmDb::get_count( 'frm_forms', $args );
1256 - }
1257 -
1258 - /**
1259 - * @deprecated 2.03.05 This is still referenced in a few add ons (API, locations).
982 + * @deprecated 3.0
1260 983 * @codeCoverageIgnore
1261 984 *
1262 985 * @param string $key
986 + *
1263 987 * @return int form id
1264 988 */
1265 989 public static function getIdByKey( $key ) {
1266 - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_id_by_key' );
1267 - return self::get_id_by_key( $key );
990 + return FrmFormDeprecated::getIdByKey( $key );
1268 991 }
1269 992
1270 993 /**
1271 - * @deprecated 2.03.05 This is still referenced in the API add on as of v1.13.
994 + * @deprecated 3.0
1272 995 * @codeCoverageIgnore
1273 - *
1274 - * @param int|string $id
1275 - * @return string
1276 996 */
1277 997 public static function getKeyById( $id ) {
1278 - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_key_by_id' );
1279 - return self::get_key_by_id( $id );
998 + return FrmFormDeprecated::getKeyById( $id );
1280 999 }
1281 1000 }