PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
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 +424 -180 6.3 → trunk View file →
@@ -6,9 +6,10 @@
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 + *
11 + * @return bool|int id on success or false on failure.
11 12 */
12 13 public static function create( $values ) {
13 14 global $wpdb;
14 15
@@ -15,24 +16,24 @@
15 16 $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) );
16 17
17 18 $new_values = array(
18 19 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ),
19 - 'name' => $values['name'],
20 + 'name' => FrmAppHelper::truncate( $values['name'], 255, 1, '', true ),
20 21 'description' => $values['description'],
21 - 'status' => isset( $values['status'] ) ? $values['status'] : 'published',
22 - 'logged_in' => isset( $values['logged_in'] ) ? $values['logged_in'] : 0,
22 + 'status' => $values['status'] ?? 'published',
23 + 'logged_in' => $values['logged_in'] ?? 0,
23 24 'is_template' => isset( $values['is_template'] ) ? (int) $values['is_template'] : 0,
24 25 'parent_form_id' => isset( $values['parent_form_id'] ) ? absint( $values['parent_form_id'] ) : 0,
25 26 'editable' => isset( $values['editable'] ) ? (int) $values['editable'] : 0,
26 - 'created_at' => isset( $values['created_at'] ) ? $values['created_at'] : current_time( 'mysql', 1 ),
27 + 'created_at' => $values['created_at'] ?? current_time( 'mysql', 1 ),
27 28 );
28 29
29 30 $options = isset( $values['options'] ) ? (array) $values['options'] : array();
30 31 FrmFormsHelper::fill_form_options( $options, $values );
31 32
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' );
33 + $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' );
34 + $options['after_html'] = $values['options']['after_html'] ?? FrmFormsHelper::get_default_html( 'after' );
35 + $options['submit_html'] = $values['options']['submit_html'] ?? FrmFormsHelper::get_default_html( 'submit' );
35 36
36 37 /**
37 38 * Allows modifying form options before updating or creating.
38 39 *
@@ -59,8 +60,9 @@
59 60 /**
60 61 * @since 5.0.08
61 62 *
62 63 * @param array $options
64 + *
63 65 * @return array
64 66 */
65 67 private static function maybe_filter_form_options( $options ) {
66 68 if ( ! FrmAppHelper::allow_unfiltered_html() && ! empty( $options['submit_html'] ) ) {
@@ -69,14 +71,22 @@
69 71 return FrmAppHelper::maybe_filter_array( $options, array( 'submit_value', 'success_msg', 'before_html', 'after_html' ) );
70 72 }
71 73
72 74 /**
73 - * @return int|boolean ID on success or false on failure
75 + * Duplicate a form.
76 + *
77 + * @param int $id Form ID to duplicate.
78 + * @param bool $template Whether the duplicated form is a template.
79 + * @param bool $copy_keys Whether to copy the original form key.
80 + * @param false|int $blog_id Blog ID when duplicating across sites, or false for current site.
81 + *
82 + * @return bool|int ID on success or false on failure
74 83 */
75 84 public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) {
76 85 global $wpdb;
77 86
78 87 $values = self::getOne( $id, $blog_id );
88 +
79 89 if ( ! $values ) {
80 90 return false;
81 91 }
82 92
@@ -93,10 +103,10 @@
93 103 'is_template' => $template ? 1 : 0,
94 104 );
95 105
96 106 if ( $blog_id ) {
97 - $new_values['status'] = 'published';
98 - $new_options = $values->options;
107 + $new_values['status'] = 'published';
108 + $new_options = $values->options;
99 109 FrmAppHelper::unserialize_or_decode( $new_options );
100 110 $new_options['email_to'] = get_option( 'admin_email' );
101 111 $new_options['copy'] = false;
102 112 $new_values['options'] = $new_options;
@@ -116,9 +126,9 @@
116 126
117 127 $form_id = $wpdb->insert_id;
118 128 FrmField::duplicate( $id, $form_id, $copy_keys, $blog_id );
119 129
120 - // update form settings after fields are created
130 + // Update form settings after fields are created
121 131 do_action( 'frm_after_duplicate_form', $form_id, $new_values, array( 'old_id' => $id ) );
122 132
123 133 return $form_id;
124 134 }
@@ -125,10 +135,16 @@
125 135
126 136 return false;
127 137 }
128 138
139 + /**
140 + * @param int $form_id
141 + * @param array $values
142 + *
143 + * @return void
144 + */
129 145 public static function after_duplicate( $form_id, $values ) {
130 - $new_opts = $values['options'];
146 + $new_opts = $values['options'];
131 147 FrmAppHelper::unserialize_or_decode( $new_opts );
132 148 $values['options'] = $new_opts;
133 149
134 150 if ( isset( $new_opts['success_msg'] ) ) {
@@ -136,8 +152,9 @@
136 152 }
137 153
138 154 $new_opts = apply_filters( 'frm_after_duplicate_form_values', $new_opts, $form_id );
139 155
156 + // phpcs:ignore Universal.Operators.StrictComparisons
140 157 if ( $new_opts != $values['options'] ) {
141 158 global $wpdb;
142 159 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) );
143 160 }
@@ -148,19 +165,24 @@
148 165 /**
149 166 * Switches field ID in fields.
150 167 *
151 168 * @since 5.3
169 + * @since 6.35 The description column is checked too, so a field id in a description survives a
170 + * duplicate or import when the field it points at is created afterwards.
152 171 *
153 172 * @param int $form_id Form ID.
173 + *
174 + * @return void
154 175 */
155 176 private static function switch_field_ids_in_fields( $form_id ) {
156 177 global $wpdb;
157 178
158 179 // Keys of fields that you want to check to replace field ID.
159 - $keys = array( 'default_value', 'field_options' );
180 + $keys = array( 'default_value', 'description', 'field_options' );
160 181 $sql_cols = 'fi.id';
182 +
161 183 foreach ( $keys as $key ) {
162 - $sql_cols .= ( ',fi.' . $key );
184 + $sql_cols .= ',fi.' . $key;
163 185 }
164 186
165 187 $fields = FrmDb::get_results(
166 188 "{$wpdb->prefix}frm_fields AS fi LEFT OUTER JOIN {$wpdb->prefix}frm_forms AS fr ON fi.form_id = fr.id",
@@ -186,11 +208,14 @@
186 208 *
187 209 * @since 5.3
188 210 *
189 211 * @param array $field Field array.
212 + *
213 + * @return void
190 214 */
191 215 private static function switch_field_ids_in_field( $field ) {
192 216 $new_values = array();
217 +
193 218 foreach ( $field as $key => $value ) {
194 219 if ( 'id' === $key || ! $value ) {
195 220 continue;
196 221 }
@@ -210,17 +235,23 @@
210 235
211 236 if ( $new_val !== $value ) {
212 237 $new_values[ $key ] = $new_val;
213 238 }
214 - }
239 + }//end foreach
215 240
216 - if ( ! empty( $new_values ) ) {
241 + if ( $new_values ) {
217 242 FrmField::update( $field['id'], $new_values );
218 243 }
219 244 }
220 245
221 246 /**
222 - * @return int|boolean
247 + * Update a form.
248 + *
249 + * @param int $id Form ID.
250 + * @param array $values Form values to update.
251 + * @param bool $create_link Whether this is being called from link creation.
252 + *
253 + * @return bool|int
223 254 */
224 255 public static function update( $id, $values, $create_link = false ) {
225 256 global $wpdb;
226 257
@@ -234,23 +265,23 @@
234 265 $values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id );
235 266 }
236 267
237 268 $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' );
269 + $new_values = self::set_update_options( array(), $values, array( 'form_id' => $id ) );
238 270
239 - $new_values = self::set_update_options( array(), $values, array( 'form_id' => $id ) );
240 -
241 271 foreach ( $values as $value_key => $value ) {
242 - if ( $value_key && in_array( $value_key, $form_fields ) ) {
243 - $new_values[ $value_key ] = $value;
272 + if ( $value_key && in_array( $value_key, $form_fields, true ) ) {
273 + $new_values[ $value_key ] = 'name' === $value_key ? FrmAppHelper::truncate( $value, 255, 1, '', true ) : $value;
244 274 }
245 275 }
246 276
247 - if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) {
277 + if ( ! empty( $values['new_status'] ) ) {
248 278 $new_values['status'] = $values['new_status'];
249 279 }
250 280
251 - if ( ! empty( $new_values ) ) {
281 + if ( $new_values ) {
252 282 $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) );
283 +
253 284 if ( $query_results ) {
254 285 self::clear_form_cache();
255 286 }
256 287 } else {
@@ -269,8 +300,9 @@
269 300 /**
270 301 * @param array $new_values
271 302 * @param array $values
272 303 * @param array $args
304 + *
273 305 * @return array
274 306 */
275 307 public static function set_update_options( $new_values, $values, $args = array() ) {
276 308 if ( ! isset( $values['options'] ) ) {
@@ -276,15 +308,15 @@
276 308 if ( ! isset( $values['options'] ) ) {
277 309 return $new_values;
278 310 }
279 311
280 - $options = isset( $values['options'] ) ? (array) $values['options'] : array();
312 + $options = ! empty( $values['options'] ) ? (array) $values['options'] : array();
281 313 FrmFormsHelper::fill_form_options( $options, $values );
282 314
283 - $options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0;
284 - $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
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' );
315 + $options['custom_style'] = $values['options']['custom_style'] ?? 0;
316 + $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' );
317 + $options['after_html'] = $values['options']['after_html'] ?? FrmFormsHelper::get_default_html( 'after' );
318 + $options['submit_html'] = isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
287 319
288 320 /**
289 321 * Allows modifying form options before updating or creating.
290 322 *
@@ -301,11 +333,14 @@
301 333 return $new_values;
302 334 }
303 335
304 336 /**
337 + * @param int $id Form ID.
338 + * @param array $values Form values array.
339 + *
305 340 * @return array
306 341 */
307 - public static function update_fields( $id, $values ) {
342 + public static function update_fields( $id, $values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
308 343
309 344 if ( ! isset( $values['item_meta'] ) && ! isset( $values['field_options'] ) ) {
310 345 return $values;
311 346 }
@@ -310,9 +345,10 @@
310 345 return $values;
311 346 }
312 347
313 348 $all_fields = FrmField::get_all_for_form( $id );
314 - if ( empty( $all_fields ) ) {
349 +
350 + if ( ! $all_fields ) {
315 351 return $values;
316 352 }
317 353
318 354 if ( ! isset( $values['item_meta'] ) ) {
@@ -320,28 +356,28 @@
320 356 }
321 357
322 358 $field_array = array();
323 359 $existing_keys = array_keys( $values['item_meta'] );
360 +
324 361 foreach ( $all_fields as $fid ) {
362 + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict, SlevomatCodingStandard.Files.LineLength.LineTooLong
325 363 if ( ! in_array( $fid->id, $existing_keys ) && ( isset( $values['frm_fields_submitted'] ) && in_array( $fid->id, $values['frm_fields_submitted'] ) ) || isset( $values['options'] ) ) {
326 364 $values['item_meta'][ $fid->id ] = '';
327 365 }
366 +
328 367 $field_array[ $fid->id ] = $fid;
329 368 }
330 369 unset( $all_fields );
331 370
332 371 foreach ( $values['item_meta'] as $field_id => $default_value ) {
333 - if ( isset( $field_array[ $field_id ] ) ) {
334 - $field = $field_array[ $field_id ];
335 - } else {
336 - $field = FrmField::getOne( $field_id );
337 - }
372 + $field = $field_array[ $field_id ] ?? FrmField::getOne( $field_id );
338 373
339 374 if ( ! $field ) {
340 375 continue;
341 376 }
342 377
343 - $is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) );
378 + $is_settings_page = isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] );
379 +
344 380 if ( $is_settings_page ) {
345 381 self::get_settings_page_html( $values, $field );
346 382
347 383 if ( ! defined( 'WP_IMPORTING' ) ) {
@@ -348,15 +384,20 @@
348 384 continue;
349 385 }
350 386 }
351 387
352 - //updating the form
388 + // Updating the form.
353 389 $update_options = FrmFieldsHelper::get_default_field_options_from_field( $field );
354 - unset( $update_options['custom_html'] ); // don't check for POST html
390 + // Don't check for POST html.
391 + unset( $update_options['custom_html'] );
355 392 $update_options = apply_filters( 'frm_field_options_to_update', $update_options );
356 393
394 + if ( ! is_array( $field->field_options ) ) {
395 + $field->field_options = array();
396 + }
397 +
357 398 foreach ( $update_options as $opt => $default ) {
358 - $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default;
399 + $field->field_options[ $opt ] = $values['field_options'][ $opt . '_' . $field_id ] ?? $default;
359 400 self::sanitize_field_opt( $opt, $field->field_options[ $opt ] );
360 401 }
361 402
362 403 $field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values );
@@ -365,24 +406,27 @@
365 406 'field_options' => $field->field_options,
366 407 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? FrmAppHelper::maybe_json_encode( $values[ 'default_value_' . $field_id ] ) : '',
367 408 );
368 409
369 - if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) {
410 + if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) { // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
370 411 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 - }
412 + if ( ! is_array( $option ) ) {
413 + continue;
375 414 }
415 +
416 + foreach ( $option as $key => $item ) {
417 + $values['field_options'][ 'options_' . $field_id ][ $option_key ][ $key ] = FrmAppHelper::kses( $item, 'all' );
418 + }
376 419 }
377 420 }
378 421
379 422 self::prepare_field_update_values( $field, $values, $new_field );
423 + self::maybe_update_max_option( $field, $values, $new_field );
380 424
381 425 FrmField::update( $field_id, $new_field );
382 426
383 427 FrmField::delete_form_transient( $field->form_id );
384 - }
428 + }//end foreach
385 429 self::clear_form_cache();
386 430
387 431 return $values;
388 432 }
@@ -387,10 +431,38 @@
387 431 return $values;
388 432 }
389 433
390 434 /**
435 + * Resets the 'max' option of a field when changing paragraph field type to other field types like text, email etc.
436 + *
437 + * @since 6.7
438 + *
439 + * @param object $field
440 + * @param array $values
441 + * @param array $new_field
442 + *
443 + * @return void
444 + */
445 + private static function maybe_update_max_option( $field, $values, &$new_field ) {
446 + if ( $field->type !== 'textarea' ||
447 + empty( $values['field_options'][ 'type_' . $field->id ] ) ||
448 + ! in_array( $values['field_options'][ 'type_' . $field->id ], array( 'text', 'email', 'url', 'password', 'phone' ), true ) ) {
449 + return;
450 + }
451 +
452 + $new_field['field_options']['max'] = '';
453 +
454 + /**
455 + * Update posted field setting so that new 'max' option is displayed after form is saved and page reloads.
456 + * FrmFieldsHelper::fill_default_field_opts populates field options by calling self::get_posted_field_setting.
457 + */
458 + $_POST['field_options'][ 'max_' . $field->id ] = '';
459 + }
460 +
461 + /**
391 462 * @param string $opt
392 463 * @param mixed $value
464 + *
393 465 * @return void
394 466 */
395 467 private static function sanitize_field_opt( $opt, &$value ) {
396 468 if ( ! is_string( $value ) ) {
@@ -411,31 +483,27 @@
411 483 if ( ! $should_sanitize ) {
412 484 return;
413 485 }
414 486
415 - if ( $opt === 'calc' ) {
416 - $value = self::sanitize_calc( $value );
417 - } else {
418 - $value = FrmAppHelper::kses( $value, 'all' );
419 - }
420 -
487 + $value = $opt === 'calc' ? self::sanitize_calc( $value ) : FrmAppHelper::kses( $value, 'all' );
421 488 $value = trim( $value );
422 489 }
423 490
424 491 /**
425 492 * @param string $value
493 + *
426 494 * @return string
427 495 */
428 496 private static function sanitize_calc( $value ) {
429 - if ( false !== strpos( $value, '<' ) ) {
497 + if ( str_contains( $value, '<' ) ) {
430 498 $value = self::normalize_calc_spaces( $value );
431 499 }
432 - $allow = array( '<= ', ' >=' ); // Allow <= and >=
500 + // Allow <= and >=.
501 + $allow = array( '<= ', ' >=' );
433 502 $temp = array( '< = ', ' > =' );
434 503 $value = str_replace( $allow, $temp, $value );
435 504 $value = strip_tags( $value );
436 - $value = str_replace( $temp, $allow, $value );
437 - return $value;
505 + return str_replace( $temp, $allow, $value );
438 506 }
439 507
440 508 /**
441 509 * Format a comparison like 5<10 to 5 < 10. Also works on 5< 10, 5 <10, 5<=10 variations.
@@ -441,41 +509,58 @@
441 509 * Format a comparison like 5<10 to 5 < 10. Also works on 5< 10, 5 <10, 5<=10 variations.
442 510 * This is to avoid an issue with unspaced calculations being recognized as HTML that gets removed when strip_tags is called.
443 511 *
444 512 * @param string $calc
513 + *
445 514 * @return string
446 515 */
447 516 private static function normalize_calc_spaces( $calc ) {
448 517 // Check for a pattern with 5 parts
449 - // $1 \d the first comparison digit.
518 + // $1 \d|\] the first comparison digit or the end of a comparison shortcode.
450 519 // $2 a space (optional).
451 520 // $3 an equals sign (optional) that follows the < operator for <= comparisons.
452 521 // $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 );
522 + // $5 \d|\[ the second comparison digit or the start of a comparison shortcode.
523 + return preg_replace( '/(\d|\])( ){0,1}<(=){0,1}( ){0,1}(\d|\[)/', '$1 <$3 $5', $calc );
455 524 }
456 525
457 526 /**
458 527 * Updating the settings page
528 + *
529 + * @param array $values Form values array.
530 + * @param object $field Field object, passed by reference.
531 + *
532 + * @return void
459 533 */
460 534 private static function get_settings_page_html( $values, &$field ) {
461 535 if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) {
462 536 $prev_opts = array();
463 - $fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type );
537 + $fallback_html = $field->field_options['custom_html'] ?? FrmFieldsHelper::get_default_html( $field->type );
464 538
465 - $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' ) {
539 + $field->field_options['custom_html'] = $values['field_options'][ 'custom_html_' . $field->id ] ?? $fallback_html;
540 + } elseif ( $field->type === 'hidden' || $field->type === 'user_id' ) {
467 541 $prev_opts = $field->field_options;
468 542 }
469 543
470 - if ( isset( $prev_opts ) ) {
471 - $field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values );
472 - if ( $prev_opts != $field->field_options ) {
473 - FrmField::update( $field->id, array( 'field_options' => $field->field_options ) );
474 - }
544 + if ( ! isset( $prev_opts ) ) {
545 + return;
475 546 }
547 +
548 + $field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values );
549 +
550 + // phpcs:ignore Universal.Operators.StrictComparisons
551 + if ( $prev_opts != $field->field_options ) {
552 + FrmField::update( $field->id, array( 'field_options' => $field->field_options ) );
553 + }
476 554 }
477 555
556 + /**
557 + * @param object $field
558 + * @param array $values
559 + * @param array $new_field
560 + *
561 + * @return void
562 + */
478 563 private static function prepare_field_update_values( $field, $values, &$new_field ) {
479 564 $field_cols = array(
480 565 'field_order' => 0,
481 566 'field_key' => '',
@@ -484,12 +569,16 @@
484 569 'description' => '',
485 570 'options' => '',
486 571 'name' => '',
487 572 );
573 +
488 574 foreach ( $field_cols as $col => $default ) {
489 - $default = ( $default === '' ) ? $field->{$col} : $default;
575 + $default = $default === '' ? $field->{$col} : $default;
576 + $new_field[ $col ] = $values['field_options'][ $col . '_' . $field->id ] ?? $default;
577 + }
490 578
491 - $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default;
579 + if ( $field->type === 'submit' && isset( $new_field['field_order'] ) && (int) $new_field['field_order'] === FrmSubmitHelper::DEFAULT_ORDER ) {
580 + $new_field['field_order'] = $field->field_order;
492 581 }
493 582
494 583 // Don't save the template option.
495 584 if ( is_array( $new_field['options'] ) && isset( $new_field['options']['000'] ) ) {
@@ -501,9 +590,12 @@
501 590 * Get a list of all form settings that should be translated
502 591 * on a multilingual site.
503 592 *
504 593 * @since 3.06.01
505 - * @param object $form - The form object
594 + *
595 + * @param object $form The form object.
596 + *
597 + * @return array
506 598 */
507 599 public static function translatable_strings( $form ) {
508 600 $strings = array(
509 601 'name',
@@ -520,19 +612,21 @@
520 612 return apply_filters( 'frm_form_strings', $strings, $form );
521 613 }
522 614
523 615 /**
524 - * @param string $status
616 + * @param array|int $id
617 + * @param string $status
525 618 *
526 - * @return int|boolean
619 + * @return bool|int
527 620 */
528 621 public static function set_status( $id, $status ) {
529 - if ( 'trash' == $status ) {
622 + if ( 'trash' === $status ) {
530 623 return self::trash( $id );
531 624 }
532 625
533 626 $statuses = array( 'published', 'draft', 'trash' );
534 - if ( ! in_array( $status, $statuses ) ) {
627 +
628 + if ( ! in_array( $status, $statuses, true ) ) {
535 629 return false;
536 630 }
537 631
538 632 global $wpdb;
@@ -545,9 +639,9 @@
545 639 );
546 640 FrmDb::get_where_clause_and_values( $where );
547 641 array_unshift( $where['values'], $status );
548 642
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
643 + $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong
550 644 } else {
551 645 $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) );
552 646 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) );
553 647 }
@@ -559,9 +653,11 @@
559 653 return $query_results;
560 654 }
561 655
562 656 /**
563 - * @return int|boolean
657 + * @param int|string $id Form ID.
658 + *
659 + * @return bool|int
564 660 */
565 661 public static function trash( $id ) {
566 662 if ( ! EMPTY_TRASH_DAYS ) {
567 663 return self::destroy( $id );
@@ -567,12 +663,17 @@
567 663 return self::destroy( $id );
568 664 }
569 665
570 666 $form = self::getOne( $id );
667 +
571 668 if ( ! $form ) {
572 669 return false;
573 670 }
574 671
672 + if ( $form->status === 'trash' ) {
673 + return false;
674 + }
675 +
575 676 $options = $form->options;
576 677 $options['trash_time'] = time();
577 678
578 679 global $wpdb;
@@ -605,21 +706,26 @@
605 706 return $query_results;
606 707 }
607 708
608 709 /**
609 - * @return int|boolean
710 + * @param int|string $id Form ID.
711 + *
712 + * @return bool|int
610 713 */
611 714 public static function destroy( $id ) {
612 715 global $wpdb;
613 716
614 717 $form = self::getOne( $id );
718 +
615 719 if ( ! $form ) {
616 720 return false;
617 721 }
722 +
618 723 $id = $form->id;
619 724
620 725 // Disconnect the entries from this form
621 - $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) );
726 + $entries = FrmDb::get_col( 'frm_items', array( 'form_id' => $id ) );
727 +
622 728 foreach ( $entries as $entry_id ) {
623 729 FrmEntry::destroy( $entry_id );
624 730 unset( $entry_id );
625 731 }
@@ -624,23 +730,29 @@
624 730 unset( $entry_id );
625 731 }
626 732
627 733 // Disconnect the fields from this form
628 - $wpdb->query( $wpdb->prepare( 'DELETE fi FROM ' . $wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $id, $id ) );
734 + $wpdb->query( $wpdb->prepare( 'DELETE fi FROM ' . $wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $id, $id ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
629 735
630 736 $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) );
631 - if ( $query_results ) {
632 - // Delete all form actions linked to this form
633 - $action_control = FrmFormActionsController::get_form_actions( 'email' );
634 - $action_control->destroy( $id, 'all' );
635 737
636 - // Clear form caching
637 - self::clear_form_cache();
738 + if ( ! $query_results ) {
739 + return $query_results;
740 + }
638 741
639 - do_action( 'frm_destroy_form', $id );
640 - do_action( 'frm_destroy_form_' . $id );
641 - }
742 + // Delete all form actions linked to this form
743 + /**
744 + * @var FrmFormAction
745 + */
746 + $action_control = FrmFormActionsController::get_form_actions( 'email' );
747 + $action_control->destroy( $id, 'all' );
642 748
749 + // Clear form caching
750 + self::clear_form_cache();
751 +
752 + do_action( 'frm_destroy_form', $id );
753 + do_action( 'frm_destroy_form_' . $id );
754 +
643 755 return $query_results;
644 756 }
645 757
646 758 /**
@@ -645,29 +757,34 @@
645 757
646 758 /**
647 759 * Delete trashed forms based on how long they have been trashed
648 760 *
761 + * @param int|string $delete_timestamp Timestamp cutoff for deletion.
762 + *
649 763 * @return int The number of forms deleted
650 764 */
651 765 public static function scheduled_delete( $delete_timestamp = '' ) {
652 - global $wpdb;
766 + $trash_forms = FrmDb::get_results( 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' );
653 767
654 - $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' );
655 -
656 768 if ( ! $trash_forms ) {
657 769 return 0;
658 770 }
659 771
660 - if ( empty( $delete_timestamp ) ) {
772 + if ( ! $delete_timestamp ) {
661 773 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
662 774 }
663 775
664 776 $count = 0;
777 +
665 778 foreach ( $trash_forms as $form ) {
666 779 FrmAppHelper::unserialize_or_decode( $form->options );
780 +
667 781 if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) {
668 782 self::destroy( $form->id );
669 - $count ++;
783 +
784 + if ( empty( $form->parent_form_id ) ) {
785 + ++$count;
786 + }
670 787 }
671 788
672 789 unset( $form );
673 790 }
@@ -675,23 +792,24 @@
675 792 return $count;
676 793 }
677 794
678 795 /**
796 + * @param int|string $id Form ID or key.
797 + *
679 798 * @return string form name
680 799 */
681 800 public static function getName( $id ) {
682 801 $form = FrmDb::check_cache( $id, 'frm_form' );
802 +
683 803 if ( $form ) {
684 - $r = stripslashes( $form->name );
685 -
686 - return $r;
804 + return stripslashes( $form->name );
687 805 }
688 806
689 807 $query_key = is_numeric( $id ) ? 'id' : 'form_key';
690 808 $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' );
691 - $r = stripslashes( $r );
692 809
693 - return $r;
810 + // An empty form name can result in a null value.
811 + return is_null( $r ) ? '' : stripslashes( $r );
694 812 }
695 813
696 814 /**
697 815 * @since 3.0
@@ -706,9 +824,9 @@
706 824
707 825 /**
708 826 * @since 3.0
709 827 *
710 - * @param int $id
828 + * @param int|string $id
711 829 *
712 830 * @return string form key
713 831 */
714 832 public static function get_key_by_id( $id ) {
@@ -713,59 +831,57 @@
713 831 */
714 832 public static function get_key_by_id( $id ) {
715 833 $id = (int) $id;
716 834 $cache = FrmDb::check_cache( $id, 'frm_form' );
835 +
717 836 if ( $cache ) {
718 837 return $cache->form_key;
719 838 }
720 839
721 - $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' );
722 -
723 - return $key;
840 + return FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' );
724 841 }
725 842
726 843 /**
727 844 * If $form is numeric, get the form object
728 845 *
729 - * @param object|int $form
846 + * @since 2.0.9
730 847 *
731 - * @since 2.0.9
848 + * @param array|int|object $form
849 + *
850 + * @return void
732 851 */
733 852 public static function maybe_get_form( &$form ) {
734 - if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) {
853 + if ( ! is_object( $form ) && ! is_array( $form ) && $form ) {
735 854 $form = self::getOne( $form );
736 855 }
737 856 }
738 857
739 858 /**
740 - * @return object form
859 + * @param int|string $id
860 + * @param false|int $blog_id
861 + *
862 + * @return stdClass|null
741 863 */
742 864 public static function getOne( $id, $blog_id = false ) {
743 865 global $wpdb;
744 866
745 867 if ( $blog_id && is_multisite() ) {
746 - global $wpmuBaseTablePrefix;
747 - $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id );
748 -
868 + $prefix = $wpdb->get_blog_prefix( $blog_id );
749 869 $table_name = $prefix . 'frm_forms';
750 870 } else {
751 871 $table_name = $wpdb->prefix . 'frm_forms';
752 872 $cache = wp_cache_get( $id, 'frm_form' );
873 +
753 874 if ( $cache ) {
754 875 if ( isset( $cache->options ) ) {
755 876 FrmAppHelper::unserialize_or_decode( $cache->options );
756 877 }
757 878
758 - return apply_filters( 'frm_form_object', wp_unslash( $cache ) );
879 + return self::prepare_form_row_data( $cache );
759 880 }
760 881 }
761 882
762 - if ( is_numeric( $id ) ) {
763 - $where = array( 'id' => $id );
764 - } else {
765 - $where = array( 'form_key' => $id );
766 - }
767 -
883 + $where = is_numeric( $id ) ? array( 'id' => $id ) : array( 'form_key' => $id );
768 884 $results = FrmDb::get_row( $table_name, $where );
769 885
770 886 if ( isset( $results->options ) ) {
771 887 FrmDb::set_cache( $results->id, $results, 'frm_form' );
@@ -771,18 +887,50 @@
771 887 FrmDb::set_cache( $results->id, $results, 'frm_form' );
772 888 FrmAppHelper::unserialize_or_decode( $results->options );
773 889 }
774 890
775 - return apply_filters( 'frm_form_object', wp_unslash( $results ) );
891 + return self::prepare_form_row_data( $results );
776 892 }
777 893
778 894 /**
779 - * @return object|array of objects
895 + * Make sure that if $row is an object, that $row->options is an array and not a string.
896 + *
897 + * @since 6.8.3
898 + *
899 + * @param stdClass|null $row The database row for a target form.
900 + *
901 + * @return stdClass|null
780 902 */
903 + private static function prepare_form_row_data( $row ) {
904 + $row = wp_unslash( $row );
905 +
906 + if ( ! is_object( $row ) ) {
907 + return $row;
908 + }
909 +
910 + if ( ! is_array( $row->options ) ) {
911 + $row->options = FrmFormsHelper::get_default_opts();
912 + }
913 +
914 + /**
915 + * @since 4.03.02
916 + *
917 + * @param stdClass $row
918 + */
919 + return apply_filters( 'frm_form_object', $row );
920 + }
921 +
922 + /**
923 + * @param array|string $where Where conditions array or raw WHERE string.
924 + * @param string $order_by Order by clause.
925 + * @param int|string $limit Limit clause or number.
926 + *
927 + * @return array|object Array of objects. If $limit is 1, a single object is returned.
928 + */
781 929 public static function getAll( $where = array(), $order_by = '', $limit = '' ) {
782 - if ( is_array( $where ) && ! empty( $where ) ) {
783 - if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
784 - // don't get trashed templates
930 + if ( is_array( $where ) && $where ) {
931 + if ( ! empty( $where['is_template'] ) && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
932 + // Don't get trashed templates
785 933 $where['status'] = array( null, '', 'published' );
786 934 }
787 935
788 936 $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) );
@@ -788,10 +936,10 @@
788 936 $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) );
789 937 } else {
790 938 global $wpdb;
791 939
792 - // the query has already been prepared if this is not an array
793 - $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit );
940 + // The query has already been prepared if this is not an array.
941 + $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
794 942 $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
795 943 }
796 944
797 945 if ( $results ) {
@@ -800,10 +948,10 @@
800 948 FrmAppHelper::unserialize_or_decode( $result->options );
801 949 }
802 950 }
803 951
804 - if ( $limit == ' LIMIT 1' || $limit == 1 ) {
805 - // return the first form object if we are only getting one form
952 + if ( $limit === ' LIMIT 1' || (int) $limit === 1 ) {
953 + // Return the first form object if we are only getting one form
806 954 $results = reset( $results );
807 955 }
808 956
809 957 return wp_unslash( $results );
@@ -816,20 +964,20 @@
816 964 *
817 965 * @param array $query
818 966 * @param int $limit
819 967 * @param string $inc_children
820 - * @return array|object of forms A single form object would be passed if $limit was set to 1.
968 + *
969 + * @return array|object Array of forms. A single form object is returned if $limit is set to 1.
821 970 */
822 971 public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) {
823 972 $query['is_template'] = 0;
824 973 $query['status'] = array( null, '', 'published' );
825 - if ( $inc_children == 'exclude' ) {
974 +
975 + if ( $inc_children === 'exclude' ) {
826 976 $query['parent_form_id'] = array( null, 0 );
827 977 }
828 978
829 - $forms = self::getAll( $query, 'name', $limit );
830 -
831 - return $forms;
979 + return self::getAll( $query, 'name', $limit );
832 980 }
833 981
834 982 /**
835 983 * @return object count of forms
@@ -834,18 +982,16 @@
834 982 /**
835 983 * @return object count of forms
836 984 */
837 985 public static function get_count() {
838 - global $wpdb;
839 -
840 986 $cache_key = 'frm_form_counts';
987 + $counts = wp_cache_get( $cache_key, 'frm_form' );
841 988
842 - $counts = wp_cache_get( $cache_key, 'frm_form' );
843 989 if ( false !== $counts ) {
844 990 return $counts;
845 991 }
846 992
847 - $results = (array) FrmDb::get_results(
993 + $results = FrmDb::get_results(
848 994 'frm_forms',
849 995 array(
850 996 'or' => 1,
851 997 'parent_form_id' => null,
@@ -857,20 +1003,18 @@
857 1003 $statuses = array( 'published', 'draft', 'template', 'trash' );
858 1004 $counts = array_fill_keys( $statuses, 0 );
859 1005
860 1006 foreach ( $results as $row ) {
861 - if ( 'trash' != $row->status ) {
862 - if ( $row->is_template ) {
863 - $counts['template'] ++;
864 - } else {
865 - $counts['published'] ++;
866 - }
1007 + if ( 'trash' === $row->status ) {
1008 + ++$counts['trash'];
1009 + } elseif ( $row->is_template ) {
1010 + ++$counts['template'];
867 1011 } else {
868 - $counts['trash'] ++;
1012 + ++$counts['published'];
869 1013 }
870 1014
871 - if ( 'draft' == $row->status ) {
872 - $counts['draft'] ++;
1015 + if ( 'draft' === $row->status ) {
1016 + ++$counts['draft'];
873 1017 }
874 1018
875 1019 unset( $row );
876 1020 }
@@ -886,8 +1030,10 @@
886 1030 * Called when a form is created, updated, duplicated, or deleted
887 1031 * or when the form status is changed
888 1032 *
889 1033 * @since 2.0.4
1034 + *
1035 + * @return void
890 1036 */
891 1037 public static function clear_form_cache() {
892 1038 FrmDb::cache_delete_group( 'frm_form' );
893 1039 }
@@ -892,23 +1038,29 @@
892 1038 FrmDb::cache_delete_group( 'frm_form' );
893 1039 }
894 1040
895 1041 /**
1042 + * @param array $values Form values to validate.
1043 + *
896 1044 * @return array of errors
897 1045 */
898 1046 public static function validate( $values ) {
899 1047 $errors = array();
900 -
901 1048 return apply_filters( 'frm_validate_form', $errors, $values );
902 1049 }
903 1050
1051 + /**
1052 + * @param object|null $form
1053 + *
1054 + * @return array
1055 + */
904 1056 public static function get_params( $form = null ) {
905 1057 global $frm_vars;
906 1058
907 - if ( ! $form ) {
1059 + if ( $form ) {
1060 + self::maybe_get_form( $form );
1061 + } else {
908 1062 $form = self::getAll( array(), 'name', 1 );
909 - } else {
910 - self::maybe_get_form( $form );
911 1063 }
912 1064
913 1065 if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) {
914 1066 return $frm_vars['form_params'][ $form->id ];
@@ -931,16 +1083,18 @@
931 1083 );
932 1084
933 1085 $values = array();
934 1086 $values['posted_form_id'] = FrmAppHelper::get_param( 'form_id', '', 'get', 'absint' );
1087 +
935 1088 if ( ! $values['posted_form_id'] ) {
936 1089 $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
937 1090 }
938 1091
1092 + // phpcs:ignore Universal.Operators.StrictComparisons
939 1093 if ( $form->id == $values['posted_form_id'] ) {
940 - //if there are two forms on the same page, make sure not to submit both
1094 + // If there are two forms on the same page, make sure not to submit both.
941 1095 foreach ( $default_values as $var => $default ) {
942 - if ( $var == 'action' ) {
1096 + if ( $var === 'action' ) {
943 1097 $values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' );
944 1098 } else {
945 1099 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
946 1100 }
@@ -952,9 +1106,9 @@
952 1106 unset( $var, $default );
953 1107 }
954 1108 }
955 1109
956 - if ( in_array( $values['action'], array( 'create', 'update' ) ) &&
1110 + if ( in_array( $values['action'], array( 'create', 'update' ), true ) &&
957 1111 ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
958 1112 ) {
959 1113 $values['action'] = 'new';
960 1114 }
@@ -961,8 +1115,11 @@
961 1115
962 1116 return $values;
963 1117 }
964 1118
1119 + /**
1120 + * @return array
1121 + */
965 1122 public static function list_page_params() {
966 1123 $values = array();
967 1124 $defaults = array(
968 1125 'template' => 0,
@@ -972,8 +1129,9 @@
972 1129 'search' => '',
973 1130 'sort' => '',
974 1131 'sdir' => '',
975 1132 );
1133 +
976 1134 foreach ( $defaults as $var => $default ) {
977 1135 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
978 1136 }
979 1137
@@ -979,10 +1137,16 @@
979 1137
980 1138 return $values;
981 1139 }
982 1140
1141 + /**
1142 + * @param int|object|string|null $form
1143 + *
1144 + * @return array
1145 + */
983 1146 public static function get_admin_params( $form = null ) {
984 1147 $form_id = $form;
1148 +
985 1149 if ( $form === null ) {
986 1150 $form_id = self::get_current_form_id();
987 1151 } elseif ( $form && is_object( $form ) ) {
988 1152 $form_id = $form->id;
@@ -1000,8 +1164,9 @@
1000 1164 'sdir' => '',
1001 1165 'fid' => '',
1002 1166 'keep_post' => '',
1003 1167 );
1168 +
1004 1169 foreach ( $defaults as $var => $default ) {
1005 1170 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
1006 1171 }
1007 1172
@@ -1007,47 +1172,55 @@
1007 1172
1008 1173 return $values;
1009 1174 }
1010 1175
1176 + /**
1177 + * @param string $default_form
1178 + *
1179 + * @return int|string
1180 + */
1011 1181 public static function get_current_form_id( $default_form = 'none' ) {
1012 - if ( 'first' == $default_form ) {
1013 - $form = self::get_current_form();
1014 - } else {
1015 - $form = self::maybe_get_current_form();
1016 - }
1017 - $form_id = $form ? $form->id : 0;
1018 -
1019 - return $form_id;
1182 + $form = 'first' === $default_form ? self::get_current_form() : self::maybe_get_current_form();
1183 + return $form ? $form->id : 0;
1020 1184 }
1021 1185
1186 + /**
1187 + * @param int|string $form_id
1188 + *
1189 + * @return false|int|object|string
1190 + */
1022 1191 public static function maybe_get_current_form( $form_id = 0 ) {
1023 1192 global $frm_vars;
1024 1193
1025 - if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) {
1194 + if ( ! empty( $frm_vars['current_form'] ) && ( ! $form_id || (int) $form_id === (int) $frm_vars['current_form']->id ) ) {
1026 1195 return $frm_vars['current_form'];
1027 1196 }
1028 1197
1029 1198 $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' );
1030 - if ( $form_id ) {
1031 - $form_id = self::set_current_form( $form_id );
1032 - }
1033 1199
1034 - return $form_id;
1200 + return $form_id ? self::set_current_form( $form_id ) : $form_id;
1035 1201 }
1036 1202
1203 + /**
1204 + * @param int $form_id
1205 + *
1206 + * @return false|object
1207 + */
1037 1208 public static function get_current_form( $form_id = 0 ) {
1038 1209 $form = self::maybe_get_current_form( $form_id );
1039 - if ( is_numeric( $form ) ) {
1040 - $form = self::set_current_form( $form );
1041 - }
1042 -
1043 - return $form;
1210 + return is_numeric( $form ) ? self::set_current_form( $form ) : $form;
1044 1211 }
1045 1212
1213 + /**
1214 + * @param int $form_id
1215 + *
1216 + * @return false|object
1217 + */
1046 1218 public static function set_current_form( $form_id ) {
1047 1219 global $frm_vars;
1048 1220
1049 1221 $query = array();
1222 +
1050 1223 if ( $form_id ) {
1051 1224 $query['id'] = $form_id;
1052 1225 }
1053 1226
@@ -1055,11 +1228,19 @@
1055 1228
1056 1229 return $frm_vars['current_form'];
1057 1230 }
1058 1231
1232 + /**
1233 + * @param object $form
1234 + * @param int|string $this_load
1235 + * @param bool $global_load
1236 + *
1237 + * @return bool
1238 + */
1059 1239 public static function is_form_loaded( $form, $this_load, $global_load ) {
1060 1240 global $frm_vars;
1061 1241 $small_form = new stdClass();
1242 +
1062 1243 foreach ( array( 'id', 'form_key', 'name' ) as $var ) {
1063 1244 $small_form->{$var} = $form->{$var};
1064 1245 unset( $var );
1065 1246 }
@@ -1065,14 +1246,14 @@
1065 1246 }
1066 1247
1067 1248 $frm_vars['forms_loaded'][] = $small_form;
1068 1249
1069 - if ( $this_load && empty( $global_load ) ) {
1250 + if ( $this_load && ! $global_load ) {
1070 1251 $global_load = true;
1071 1252 $frm_vars['load_css'] = true;
1072 1253 }
1073 1254
1074 - return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load );
1255 + return empty( $frm_vars['css_loaded'] ) && $global_load;
1075 1256 }
1076 1257
1077 1258 /**
1078 1259 * @since 4.06.03
@@ -1080,9 +1261,9 @@
1080 1261 * @param object $form
1081 1262 *
1082 1263 * @return bool
1083 1264 */
1084 - public static function &is_visible_to_user( $form ) {
1265 + public static function is_visible_to_user( $form ) {
1085 1266 if ( $form->logged_in && isset( $form->options['logged_in_role'] ) ) {
1086 1267 $visible = FrmAppHelper::user_has_permission( $form->options['logged_in_role'] );
1087 1268 } else {
1088 1269 $visible = true;
@@ -1087,26 +1268,39 @@
1087 1268 } else {
1088 1269 $visible = true;
1089 1270 }
1090 1271
1091 - return $visible;
1272 + /**
1273 + * @since 6.25
1274 + *
1275 + * @param bool $visible
1276 + * @param object $form
1277 + */
1278 + return (bool) apply_filters( 'frm_form_is_visible', $visible, $form );
1092 1279 }
1093 1280
1281 + /**
1282 + * @param object $form
1283 + *
1284 + * @return bool
1285 + */
1094 1286 public static function show_submit( $form ) {
1095 - $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() );
1096 - $show = apply_filters( 'frm_show_submit_button', $show, $form );
1097 -
1098 - return $show;
1287 + $show = ! $form->is_template && $form->status === 'published' && ! FrmAppHelper::is_admin();
1288 + return apply_filters( 'frm_show_submit_button', $show, $form );
1099 1289 }
1100 1290
1101 1291 /**
1102 1292 * @since 2.3
1293 + *
1294 + * @param array $atts Attributes including form, option, and default.
1295 + *
1296 + * @return mixed
1103 1297 */
1104 1298 public static function get_option( $atts ) {
1105 - $form = $atts['form'];
1106 - $default = isset( $atts['default'] ) ? $atts['default'] : '';
1299 + $form = $atts['form'];
1300 + $default = $atts['default'] ?? '';
1107 1301
1108 - return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $default;
1302 + return $form->options[ $atts['option'] ] ?? $default;
1109 1303 }
1110 1304
1111 1305 /**
1112 1306 * Get the link to edit this form.
@@ -1111,9 +1305,12 @@
1111 1305 /**
1112 1306 * Get the link to edit this form.
1113 1307 *
1114 1308 * @since 4.0
1309 + *
1115 1310 * @param int $form_id The id of the form.
1311 + *
1312 + * @return string
1116 1313 */
1117 1314 public static function get_edit_link( $form_id ) {
1118 1315 return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id );
1119 1316 }
@@ -1123,8 +1320,9 @@
1123 1320 *
1124 1321 * @since 6.2
1125 1322 *
1126 1323 * @param stdClass $form
1324 + *
1127 1325 * @return bool
1128 1326 */
1129 1327 public static function is_ajax_on( $form ) {
1130 1328 return ! empty( $form->options['ajax_submit'] );
@@ -1130,12 +1328,56 @@
1130 1328 return ! empty( $form->options['ajax_submit'] );
1131 1329 }
1132 1330
1133 1331 /**
1332 + * Get the latest form available.
1333 + *
1334 + * @since 6.8
1335 + *
1336 + * @return object
1337 + */
1338 + public static function get_latest_form() {
1339 + $args = array(
1340 + array(
1341 + 'or' => 1,
1342 + 'parent_form_id' => null,
1343 + 'parent_form_id <' => 1,
1344 + ),
1345 + 'is_template' => 0,
1346 + 'status !' => 'trash',
1347 + );
1348 +
1349 + return self::getAll( $args, 'created_at desc', 1 );
1350 + }
1351 +
1352 + /**
1353 + * Count and return total forms.
1354 + *
1355 + * @since 6.8
1356 + *
1357 + * @return int
1358 + */
1359 + public static function get_forms_count() {
1360 + $args = array(
1361 + array(
1362 + 'or' => 1,
1363 + 'parent_form_id' => null,
1364 + 'parent_form_id <' => 1,
1365 + ),
1366 + 'is_template' => 0,
1367 + 'status !' => 'trash',
1368 + );
1369 +
1370 + return FrmDb::get_count( 'frm_forms', $args );
1371 + }
1372 +
1373 + /**
1134 1374 * @deprecated 2.03.05 This is still referenced in a few add ons (API, locations).
1375 + *
1135 1376 * @codeCoverageIgnore
1136 1377 *
1137 1378 * @param string $key
1379 + *
1138 1380 * @return int form id
1139 1381 */
1140 1382 public static function getIdByKey( $key ) {
1141 1383 _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_id_by_key' );
@@ -1143,11 +1385,13 @@
1143 1385 }
1144 1386
1145 1387 /**
1146 1388 * @deprecated 2.03.05 This is still referenced in the API add on as of v1.13.
1389 + *
1147 1390 * @codeCoverageIgnore
1148 1391 *
1149 - * @param string|int $id
1392 + * @param int|string $id
1393 + *
1150 1394 * @return string
1151 1395 */
1152 1396 public static function getKeyById( $id ) {
1153 1397 _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_key_by_id' );