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