PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.22
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.22
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/FrmEntryValidate.php +64 -191 6.286.22 View file →
@@ -14,9 +14,8 @@
14 14
15 15 /**
16 16 * @param array $values
17 17 * @param bool|string[] $exclude
18 - *
19 18 * @return array
20 19 */
21 20 public static function validate( $values, $exclude = false ) {
22 21 FrmEntry::sanitize_entry_post( $values );
@@ -23,12 +22,13 @@
23 22 $errors = array();
24 23
25 24 if ( ! isset( $values['form_id'] ) || ! isset( $values['item_meta'] ) ) {
26 25 $errors['form'] = __( 'There was a problem with your submission. Please try again.', 'formidable' );
26 +
27 27 return $errors;
28 28 }
29 29
30 - if ( FrmAppHelper::is_admin() && is_user_logged_in() && ( ! isset( $values[ 'frm_submit_entry_' . $values['form_id'] ] ) || ! wp_verify_nonce( $values[ 'frm_submit_entry_' . $values['form_id'] ], 'frm_submit_entry_nonce' ) ) ) { // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
30 + if ( FrmAppHelper::is_admin() && is_user_logged_in() && ( ! isset( $values[ 'frm_submit_entry_' . $values['form_id'] ] ) || ! wp_verify_nonce( $values[ 'frm_submit_entry_' . $values['form_id'] ], 'frm_submit_entry_nonce' ) ) ) {
31 31 $frm_settings = FrmAppHelper::get_settings();
32 32 $errors['form'] = $frm_settings->admin_permission;
33 33 }
34 34
@@ -44,9 +44,9 @@
44 44 self::validate_field( $posted_field, $errors, $values, $args );
45 45 unset( $posted_field );
46 46 }
47 47
48 - if ( ! $errors ) {
48 + if ( empty( $errors ) ) {
49 49 self::spam_check( $exclude, $values, $errors );
50 50 }
51 51
52 52 /**
@@ -83,15 +83,9 @@
83 83 $_POST['item_meta'] = array();
84 84 }
85 85 }
86 86
87 - /**
88 - * @param array $values
89 - *
90 - * @return void
91 - */
92 87 private static function set_item_key( &$values ) {
93 - // phpcs:ignore Universal.Operators.StrictComparisons
94 88 if ( ! isset( $values['item_key'] ) || $values['item_key'] == '' ) {
95 89 global $wpdb;
96 90 $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
97 91 $_POST['item_key'] = $values['item_key'];
@@ -97,14 +91,8 @@
97 91 $_POST['item_key'] = $values['item_key'];
98 92 }
99 93 }
100 94
101 - /**
102 - * @param array $values
103 - * @param array|string $exclude
104 - *
105 - * @return array
106 - */
107 95 private static function get_fields_to_validate( $values, $exclude ) {
108 96 $where = apply_filters( 'frm_posted_field_ids', array( 'fi.form_id' => $values['form_id'] ) );
109 97
110 98 // Don't get subfields
@@ -110,9 +98,9 @@
110 98 // Don't get subfields
111 99 $where['fr.parent_form_id'] = array( null, 0 );
112 100
113 101 // Don't get excluded fields (like file upload fields in the ajax validation)
114 - if ( $exclude ) {
102 + if ( ! empty( $exclude ) ) {
115 103 $where['fi.type not'] = $exclude;
116 104 }
117 105
118 106 $fields = FrmField::getAll( $where, 'field_order' );
@@ -127,16 +115,8 @@
127 115 */
128 116 return apply_filters( 'frm_fields_to_validate', $fields, compact( 'values', 'exclude', 'where' ) );
129 117 }
130 118
131 - /**
132 - * @param object $posted_field
133 - * @param array $errors
134 - * @param array $values
135 - * @param array $args
136 - *
137 - * @return void
138 - */
139 119 public static function validate_field( $posted_field, &$errors, $values, $args = array() ) {
140 120 $defaults = array(
141 121 'id' => $posted_field->id,
142 122 // The id of the repeat or embed form.
@@ -146,18 +126,23 @@
146 126 // Exclude these field types from validation.
147 127 'exclude' => array(),
148 128
149 129 );
150 - $args = wp_parse_args( $args, $defaults );
151 - $value = empty( $args['parent_field_id'] ) ? ( $values['item_meta'][ $args['id'] ] ?? '' ) : $values;
130 + $args = wp_parse_args( $args, $defaults );
152 131
132 + if ( empty( $args['parent_field_id'] ) ) {
133 + $value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : '';
134 + } else {
135 + // value is from a nested form
136 + $value = $values;
137 + }
138 +
153 139 // Check for values in "Other" fields
154 140 FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
155 141
156 142 self::maybe_clear_value_for_default_blank_setting( $posted_field, $value );
157 143
158 - $should_trim = is_array( $value ) && count( $value ) === 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
159 -
144 + $should_trim = is_array( $value ) && count( $value ) == 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
160 145 if ( $should_trim ) {
161 146 $value = reset( $value );
162 147 }
163 148
@@ -164,9 +149,8 @@
164 149 if ( ! is_array( $value ) ) {
165 150 $value = trim( $value );
166 151 }
167 152
168 - // phpcs:ignore Universal.Operators.StrictComparisons
169 153 if ( $posted_field->required == '1' && FrmAppHelper::is_empty_value( $value ) ) {
170 154 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' );
171 155 } elseif ( ! isset( $_POST['item_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
172 156 self::maybe_add_item_name( $value, $posted_field );
@@ -180,9 +164,8 @@
180 164 // Field might want to modify value before other parts of the system
181 165 // e.g. trim off excess values like in the case of fields with limit.
182 166 $value = apply_filters( 'frm_modify_posted_field_value', $value, $errors, $posted_field, $args );
183 167
184 - // phpcs:ignore Universal.Operators.StrictComparisons
185 168 if ( $value != '' ) {
186 169 self::validate_phone_field( $errors, $posted_field, $value, $args );
187 170 }
188 171
@@ -232,22 +215,15 @@
232 215 *
233 216 * @param stdClass $field
234 217 * @param array|string $value
235 218 * @param array $options
236 - *
237 219 * @return bool
238 220 */
239 - private static function option_is_valid( $field, $value, $options ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
221 + private static function option_is_valid( $field, $value, $options ) {
240 222 if ( '' === $value ) {
241 223 return true;
242 224 }
243 225
244 - $field_object = FrmFieldFactory::get_field_type( $field->type, $field );
245 -
246 - if ( ! $field_object->field_type_has_options_settings() ) {
247 - return true;
248 - }
249 -
250 226 if ( in_array( $field->type, array( 'likert', 'ranking' ), true ) ) {
251 227 // Ignore these field types automatically.
252 228 return true;
253 229 }
@@ -265,9 +241,9 @@
265 241 foreach ( $value as $current_value ) {
266 242 $match = false;
267 243
268 244 foreach ( $options as $key => $option ) {
269 - if ( str_starts_with( $key, 'other_' ) ) {
245 + if ( strpos( $key, 'other_' ) === 0 ) {
270 246 // Always return true if an other option is found.
271 247 return true;
272 248 }
273 249
@@ -277,38 +253,29 @@
277 253 } else {
278 254 $option_value = $option;
279 255 }
280 256
281 - /**
282 - * @var string $current_value
283 - */
284 257 $match = trim( $current_value ) === trim( $option_value );
285 -
286 258 if ( $match ) {
287 259 break;
288 260 }
289 261
290 262 $match = trim( $current_value ) === trim( do_shortcode( $option_value ) );
291 -
292 263 if ( $match ) {
293 264 break;
294 265 }
295 266
296 267 $match = self::is_filtered_match( $current_value, $option_value );
297 -
298 268 if ( $match ) {
299 269 break;
300 270 }
301 271
302 - if ( ! is_numeric( $current_value ) ) {
303 - continue;
272 + if ( is_numeric( $current_value ) ) {
273 + $match = (int) $current_value === (int) $option_value;
274 + if ( $match ) {
275 + break;
276 + }
304 277 }
305 -
306 - $match = (int) $current_value === (int) $option_value;
307 -
308 - if ( $match ) {
309 - break;
310 - }
311 278 }//end foreach
312 279
313 280 if ( ! $match ) {
314 281 return self::options_are_dynamic_based_on_hook( $field, $value );
@@ -326,25 +293,20 @@
326 293 * @since 6.22
327 294 *
328 295 * @param string $value
329 296 * @param string $option_value
330 - *
331 297 * @return bool
332 298 */
333 299 private static function is_filtered_match( $value, $option_value ) {
334 300 // First remove the wpautop filter so it doesn't add extra tags to $option_value.
335 301 $filter_priority = has_filter( 'the_content', 'wpautop' );
336 -
337 302 if ( is_numeric( $filter_priority ) ) {
338 303 remove_filter( 'the_content', 'wpautop', $filter_priority );
339 304 }
340 -
341 305 $filtered_option = apply_filters( 'the_content', $option_value );
342 -
343 306 if ( is_numeric( $filter_priority ) ) {
344 307 add_filter( 'the_content', 'wpautop', $filter_priority );
345 308 }
346 -
347 309 return trim( $value ) === trim( $filtered_option );
348 310 }
349 311
350 312 /**
@@ -352,11 +314,8 @@
352 314 * This is to help avoid issues where the options could be based on a URL param for example.
353 315 *
354 316 * @since 6.21
355 317 *
356 - * @param object $field_object The field object.
357 - * @param array|string $value The value to validate.
358 - *
359 318 * @return bool
360 319 */
361 320 private static function options_are_dynamic_based_on_hook( $field_object, $value ) {
362 321 $values = (array) $field_object;
@@ -369,9 +328,10 @@
369 328 $option_value = $separate_value ? $option['value'] : $option['label'];
370 329 } else {
371 330 $option_value = $option;
372 331 }
373 - return do_shortcode( $option_value );
332 + $option_value = do_shortcode( $option_value );
333 + return $option_value;
374 334 };
375 335
376 336 $values_options = array_map( $map_callback, $values['options'] );
377 337 $field_object_options = array_map( $map_callback, $field_object->options );
@@ -385,14 +345,11 @@
385 345 * @since 5.2.02
386 346 *
387 347 * @param array|string $value Field value.
388 348 * @param object $field Field object.
389 - *
390 - * @return void
391 349 */
392 350 private static function maybe_add_item_name( $value, $field ) {
393 351 $item_name = false;
394 -
395 352 if ( 'name' === $field->type ) {
396 353 $field_obj = FrmFieldFactory::get_field_object( $field );
397 354 $item_name = $field_obj->get_display_value( $value );
398 355 } elseif ( 'text' === $field->type ) {
@@ -409,14 +366,11 @@
409 366 * Set $value to an empty string if it matches its label
410 367 *
411 368 * @param object $field
412 369 * @param string $value
413 - *
414 - * @return void
415 370 */
416 371 private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) {
417 372 $position = FrmField::get_option( $field, 'label' );
418 -
419 373 if ( ! $position ) {
420 374 $position = FrmStylesController::get_style_val( 'position', $field->form_id );
421 375 }
422 376
@@ -424,16 +378,8 @@
424 378 $value = '';
425 379 }
426 380 }
427 381
428 - /**
429 - * @param array $errors
430 - * @param object $posted_field
431 - * @param mixed $value
432 - * @param array $args
433 - *
434 - * @return void
435 - */
436 382 public static function validate_field_types( &$errors, $posted_field, $value, $args ) {
437 383 $field_obj = FrmFieldFactory::get_field_object( $posted_field );
438 384 $args['value'] = $value;
439 385 $args['errors'] = $errors;
@@ -438,41 +384,25 @@
438 384 $args['value'] = $value;
439 385 $args['errors'] = $errors;
440 386
441 387 $new_errors = $field_obj->validate( $args );
442 -
443 - if ( $new_errors ) {
388 + if ( ! empty( $new_errors ) ) {
444 389 $errors = array_merge( $errors, $new_errors );
445 390 }
446 391 }
447 392
448 - /**
449 - * @param array $errors
450 - * @param object $field
451 - * @param string $value
452 - * @param array $args
453 - *
454 - * @return void
455 - */
456 393 public static function validate_phone_field( &$errors, $field, $value, $args ) {
457 394 $format_value = FrmField::get_option( $field, 'format' );
458 395
459 - if ( $field->type !== 'phone' && ( $field->type !== 'text' || ! $format_value || FrmCurrencyHelper::is_currency_format( $format_value ) ) ) {
460 - return;
461 - }
396 + if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) {
397 + $pattern = self::phone_format( $field );
462 398
463 - $pattern = self::phone_format( $field );
464 -
465 - if ( ! preg_match( $pattern, $value ) ) {
466 - $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
399 + if ( ! preg_match( $pattern, $value ) ) {
400 + $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
401 + }
467 402 }
468 403 }
469 404
470 - /**
471 - * @param object $field
472 - *
473 - * @return string
474 - */
475 405 public static function phone_format( $field ) {
476 406 if ( FrmField::is_option_empty( $field, 'format' ) ) {
477 407 $pattern = self::default_phone_format();
478 408 } else {
@@ -484,19 +414,19 @@
484 414 $pattern = html_entity_decode( $pattern );
485 415 $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field );
486 416
487 417 // Create a regexp if format is not already a regexp
488 - if ( ! str_starts_with( $pattern, '^' ) ) {
418 + if ( strpos( $pattern, '^' ) !== 0 ) {
489 419 $pattern = self::create_regular_expression_from_format( $pattern );
490 420 }
491 421
492 - return '/' . $pattern . '/';
422 + $pattern = '/' . $pattern . '/';
423 +
424 + return $pattern;
493 425 }
494 426
495 427 /**
496 428 * @since 3.01
497 - *
498 - * @return string
499 429 */
500 430 private static function default_phone_format() {
501 431 return '^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$';
502 432 }
@@ -522,14 +452,13 @@
522 452 $pattern = str_replace( 'a', '[a-zA-Z]', $pattern );
523 453 $pattern = str_replace( '*', 'w', $pattern );
524 454 $pattern = str_replace( '/', '\/', $pattern );
525 455
526 - if ( str_contains( $pattern, '\?' ) ) {
456 + if ( strpos( $pattern, '\?' ) !== false ) {
527 457 $parts = explode( '\?', $pattern );
528 458 $pattern = '';
529 -
530 459 foreach ( $parts as $part ) {
531 - if ( ! $pattern ) {
460 + if ( empty( $pattern ) ) {
532 461 $pattern .= $part;
533 462 } else {
534 463 $pattern .= '(' . $part . ')?';
535 464 }
@@ -534,10 +463,11 @@
534 463 $pattern .= '(' . $part . ')?';
535 464 }
536 465 }
537 466 }
467 + $pattern = '^' . $pattern . '$';
538 468
539 - return '^' . $pattern . '$';
469 + return $pattern;
540 470 }
541 471
542 472 /**
543 473 * Check for spam.
@@ -544,10 +474,8 @@
544 474 *
545 475 * @param bool $exclude
546 476 * @param array $values
547 477 * @param array $errors By reference.
548 - *
549 - * @return void
550 478 */
551 479 public static function spam_check( $exclude, $values, &$errors ) {
552 480 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
553 481 // Do not check spam on importing.
@@ -553,16 +481,15 @@
553 481 // Do not check spam on importing.
554 482 return;
555 483 }
556 484
557 - if ( $exclude || empty( $values['item_meta'] ) || $errors ) {
558 - // Only check spam if there are no other errors
485 + if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
486 + // only check spam if there are no other errors
559 487 return;
560 488 }
561 489
562 490 $antispam_check = self::is_antispam_check( $values['form_id'] );
563 491 $spam_msg = FrmAntiSpamController::get_default_spam_message();
564 -
565 492 if ( is_string( $antispam_check ) ) {
566 493 $errors['spam'] = $antispam_check;
567 494 } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) {
568 495 $errors['spam'] = $spam_msg;
@@ -567,9 +494,8 @@
567 494 } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) {
568 495 $errors['spam'] = $spam_msg;
569 496 } else {
570 497 $is_spam = FrmAntiSpamController::is_spam( $values );
571 -
572 498 if ( $is_spam ) {
573 499 $errors['spam'] = $is_spam;
574 500 }
575 501 }
@@ -588,17 +514,14 @@
588 514 *
589 515 * @since 5.0.13
590 516 *
591 517 * @param array $values The values.
592 - *
593 518 * @return bool
594 519 */
595 520 private static function form_is_in_progress( $values ) {
596 - // phpcs:disable Generic.WhiteSpace.ScopeIndent
597 521 return FrmAppHelper::pro_is_installed() &&
598 522 ( isset( $values[ 'frm_page_order_' . $values['form_id'] ] ) || FrmAppHelper::get_post_param( 'frm_next_page' ) ) &&
599 523 FrmField::get_all_types_in_form( $values['form_id'], 'break' );
600 - // phpcs:enable Generic.WhiteSpace.ScopeIndent
601 524 }
602 525
603 526 /**
604 527 * @param int $form_id
@@ -611,9 +534,8 @@
611 534 }
612 535
613 536 /**
614 537 * @param array $values
615 - *
616 538 * @return bool
617 539 */
618 540 private static function is_honeypot_spam( $values ) {
619 541 $honeypot = new FrmHoneypot( $values['form_id'] );
@@ -624,29 +546,30 @@
624 546 * @return bool
625 547 */
626 548 private static function is_spam_bot() {
627 549 $ip = FrmAppHelper::get_ip_address();
550 +
628 551 return empty( $ip );
629 552 }
630 553
631 554 /**
632 555 * @param array $values
633 - *
634 556 * @return bool
635 557 */
636 558 private static function is_akismet_spam( $values ) {
637 559 global $wpcom_api_key;
638 - return is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values );
560 +
561 + return ( is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values ) );
639 562 }
640 563
641 564 /**
642 565 * @param int $form_id
643 - *
644 566 * @return bool
645 567 */
646 568 private static function is_akismet_enabled_for_user( $form_id ) {
647 569 $form = FrmForm::getOne( $form_id );
648 - return ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() );
570 +
571 + return ( ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) );
649 572 }
650 573
651 574 /**
652 575 * Checks spam using WordPress disallowed words and Frm denylist.
@@ -661,10 +584,8 @@
661 584
662 585 /**
663 586 * Check entries for Akismet spam
664 587 *
665 - * @param array $values Entry values.
666 - *
667 588 * @return bool true if is spam
668 589 */
669 590 public static function akismet( $values ) {
670 591 if ( empty( $values['item_meta'] ) ) {
@@ -687,18 +608,13 @@
687 608
688 609 $query_string = _http_build_query( $datas, '', '&' );
689 610 $response = Akismet::http_post( $query_string, 'comment-check' );
690 611
691 - return is_array( $response ) && $response[1] === 'true';
612 + return ( is_array( $response ) && $response[1] === 'true' );
692 613 }
693 614
694 615 /**
695 616 * @since 2.0
696 - *
697 - * @param array $datas The array of values being sent to Akismet.
698 - * @param array $values Entry values.
699 - *
700 - * @return void
701 617 */
702 618 private static function parse_akismet_array( &$datas, $values ) {
703 619 self::add_site_info_to_akismet( $datas );
704 620 self::add_server_values_to_akismet( $datas );
@@ -709,13 +625,8 @@
709 625 self::add_user_info_to_akismet( $datas, $values );
710 626 self::add_comment_content_to_akismet( $datas, $values );
711 627 }
712 628
713 - /**
714 - * @param array $datas
715 - *
716 - * @return void
717 - */
718 629 private static function add_site_info_to_akismet( &$datas ) {
719 630 $datas['blog'] = FrmAppHelper::site_url();
720 631 $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
721 632 $datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
@@ -727,14 +638,8 @@
727 638 $datas['is_test'] = 'true';
728 639 }
729 640 }
730 641
731 - /**
732 - * @param array $datas
733 - * @param array $values
734 - *
735 - * @return void
736 - */
737 642 private static function add_user_info_to_akismet( &$datas, $values ) {
738 643 $user_info = self::get_spam_check_user_info( $values );
739 644 $datas = $datas + $user_info;
740 645
@@ -749,9 +654,8 @@
749 654 * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater.
750 655 * @since 6.21 This changed from private to public.
751 656 *
752 657 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
753 - *
754 658 * @return array
755 659 */
756 660 public static function get_spam_check_user_info( $values ) {
757 661 if ( ! is_user_logged_in() ) {
@@ -774,9 +678,8 @@
774 678 *
775 679 * @since 5.0.13
776 680 *
777 681 * @param array $values Entry values after flattened.
778 - *
779 682 * @return array
780 683 */
781 684 private static function get_spam_check_user_info_for_guest( $values ) {
782 685 $datas = array(
@@ -808,10 +711,8 @@
808 711 *
809 712 * @param array $datas Guest data.
810 713 * @param array $values The values.
811 714 * @param int|null $custom_index Custom index (or field ID).
812 - *
813 - * @return void
814 715 */
815 716 private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) {
816 717 foreach ( $values as $index => $value ) {
817 718 if ( ! $datas['missing_keys'] ) {
@@ -824,19 +725,15 @@
824 725 continue;
825 726 }
826 727
827 728 $field_id = ! is_null( $custom_index ) ? $custom_index : $index;
828 -
829 729 foreach ( $datas['missing_keys'] as $key_index => $key ) {
830 730 $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values );
831 -
832 - if ( ! $found ) {
833 - continue;
731 + if ( $found ) {
732 + $datas[ $key ] = $value;
733 + $datas['frm_duplicated'][] = $field_id;
734 + unset( $datas['missing_keys'][ $key_index ] );
834 735 }
835 -
836 - $datas[ $key ] = $value;
837 - $datas['frm_duplicated'][] = $field_id;
838 - unset( $datas['missing_keys'][ $key_index ] );
839 736 }
840 737 }//end foreach
841 738 }
842 739
@@ -859,12 +756,12 @@
859 756 }
860 757
861 758 switch ( $key ) {
862 759 case 'comment_author_email':
863 - return str_contains( $value, '@' ) && is_email( $value );
760 + return strpos( $value, '@' ) && is_email( $value );
864 761
865 762 case 'comment_author_url':
866 - return str_starts_with( $value, 'http' );
763 + return 0 === strpos( $value, 'http' );
867 764
868 765 case 'comment_author':
869 766 if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) {
870 767 // If there is name field in the form, we should always use it as author name.
@@ -869,9 +766,8 @@
869 766 if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) {
870 767 // If there is name field in the form, we should always use it as author name.
871 768 return true;
872 769 }
873 -
874 770 $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
875 771 $fields = self::get_name_text_fields( $form_id );
876 772
877 773 foreach ( $fields as $index => $field ) {
@@ -877,14 +773,12 @@
877 773 foreach ( $fields as $index => $field ) {
878 774 if ( 'Name' !== $field->name ) {
879 775 continue;
880 776 }
881 -
882 777 if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) {
883 778 if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) {
884 779 continue;
885 780 }
886 -
887 781 $value .= ' ' . $values[ $fields[ $index + 1 ]->id ];
888 782 return true;
889 783 }
890 784 }
@@ -898,18 +792,15 @@
898 792 *
899 793 * @since 6.17
900 794 *
901 795 * @param int $form_id
902 - *
903 796 * @return array
904 797 */
905 798 private static function get_name_text_fields( $form_id ) {
906 799 $name_text_fields_is_initialized = is_array( self::$name_text_fields );
907 -
908 800 if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) {
909 801 return self::$name_text_fields[ $form_id ];
910 802 }
911 -
912 803 if ( ! $name_text_fields_is_initialized ) {
913 804 self::$name_text_fields = array();
914 805 }
915 806 self::$name_text_fields[ $form_id ] = FrmDb::get_results(
@@ -925,13 +816,8 @@
925 816
926 817 return self::$name_text_fields[ $form_id ];
927 818 }
928 819
929 - /**
930 - * @param array $datas
931 - *
932 - * @return void
933 - */
934 820 private static function add_server_values_to_akismet( &$datas ) {
935 821 foreach ( $_SERVER as $key => $value ) {
936 822 $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key );
937 823
@@ -949,10 +835,8 @@
949 835 * @since 5.0.09
950 836 *
951 837 * @param array $datas The array of values being sent to Akismet.
952 838 * @param array $values Entry values.
953 - *
954 - * @return void
955 839 */
956 840 private static function add_comment_content_to_akismet( &$datas, $values ) {
957 841 if ( isset( $datas['frm_duplicated'] ) ) {
958 842 foreach ( $datas['frm_duplicated'] as $index ) {
@@ -973,28 +857,22 @@
973 857 *
974 858 * @since 5.0.09
975 859 *
976 860 * @param array $values Entry values.
977 - *
978 - * @return void
979 861 */
980 862 private static function skip_adding_values_to_akismet( &$values ) {
981 863 $skipped_fields = self::get_akismet_skipped_field_ids( $values );
982 -
983 864 foreach ( $skipped_fields as $skipped_field ) {
984 865 if ( ! isset( $values['item_meta'][ $skipped_field->id ] ) ) {
985 866 continue;
986 867 }
987 868
988 - if ( ! self::should_really_skip_field( $skipped_field, $values ) ) {
989 - continue;
869 + if ( self::should_really_skip_field( $skipped_field, $values ) ) {
870 + unset( $values['item_meta'][ $skipped_field->id ] );
871 + if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) {
872 + unset( $values['item_meta']['other'][ $skipped_field->id ] );
873 + }
990 874 }
991 -
992 - unset( $values['item_meta'][ $skipped_field->id ] );
993 -
994 - if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) {
995 - unset( $values['item_meta']['other'][ $skipped_field->id ] );
996 - }
997 875 }
998 876 }
999 877
1000 878 /**
@@ -1003,9 +881,8 @@
1003 881 * @since 5.02.04
1004 882 *
1005 883 * @param object $field_data Object contains `id` and `options`.
1006 884 * @param array $values Entry values.
1007 - *
1008 885 * @return bool
1009 886 */
1010 887 private static function should_really_skip_field( $field_data, $values ) {
1011 888 if ( empty( $field_data->options ) ) {
@@ -1013,18 +890,18 @@
1013 890 return true;
1014 891 }
1015 892
1016 893 FrmAppHelper::unserialize_or_decode( $field_data->options );
1017 -
1018 894 if ( ! $field_data->options ) {
1019 895 // Check if an error happens when unserializing, or empty options.
1020 896 return true;
1021 897 }
1022 898
1023 - $last_key = array_key_last( $field_data->options );
899 + end( $field_data->options );
900 + $last_key = key( $field_data->options );
1024 901
1025 902 // If a choice field has no Other option.
1026 - if ( is_numeric( $last_key ) || ! str_starts_with( $last_key, 'other_' ) ) {
903 + if ( is_numeric( $last_key ) || 0 !== strpos( $last_key, 'other_' ) ) {
1027 904 return true;
1028 905 }
1029 906
1030 907 // If a choice field has Other option, but Other is not selected.
@@ -1033,10 +910,9 @@
1033 910 }
1034 911
1035 912 // Check if submitted value is same as one of field option.
1036 913 foreach ( $field_data->options as $option ) {
1037 - $option_value = is_array( $option ) ? ( $option['value'] ?? '' ) : $option;
1038 -
914 + $option_value = ! is_array( $option ) ? $option : ( isset( $option['value'] ) ? $option['value'] : '' );
1039 915 if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) {
1040 916 return true;
1041 917 }
1042 918 }
@@ -1051,9 +927,8 @@
1051 927 * @since 5.0.13 Move out get_all_form_ids_and_flatten_meta() call and get `form_ids` from `$values`.
1052 928 * @since 5.2.04 This method returns array of object contains `id` and `options` instead of array of `id` only.
1053 929 *
1054 930 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
1055 - *
1056 931 * @return array
1057 932 */
1058 933 private static function get_akismet_skipped_field_ids( $values ) {
1059 934 if ( empty( $values['form_ids'] ) ) {
@@ -1079,13 +954,12 @@
1079 954 * @since 5.0.13
1080 955 * @since 6.21 This changed from private to public.
1081 956 *
1082 957 * @param array $values Entry values.
1083 - *
1084 - * @return void
1085 958 */
1086 959 public static function prepare_values_for_spam_check( &$values ) {
1087 - $values['form_ids'] = self::get_all_form_ids_and_flatten_meta( $values );
960 + $form_ids = self::get_all_form_ids_and_flatten_meta( $values );
961 + $values['form_ids'] = $form_ids;
1088 962 }
1089 963
1090 964 /**
1091 965 * Gets all form IDs (include child form IDs) and flatten item_meta array. Used for skipping values sent to Akismet.
@@ -1094,17 +968,15 @@
1094 968 * @since 5.0.09
1095 969 * @since 5.0.13 Convert name field value to string.
1096 970 *
1097 971 * @param array $values Entry values.
1098 - *
1099 972 * @return array Form IDs.
1100 973 */
1101 - private static function get_all_form_ids_and_flatten_meta( &$values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
974 + private static function get_all_form_ids_and_flatten_meta( &$values ) {
1102 975 $values['name_field_ids'] = array();
1103 976
1104 977 // Blacklist check for File field in the old version doesn't contain `form_id`.
1105 978 $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array();
1106 -
1107 979 foreach ( $values['item_meta'] as $field_id => $value ) {
1108 980 if ( ! is_numeric( $field_id ) ) {
1109 981 // Maybe `other`.
1110 982 continue;
@@ -1138,9 +1010,10 @@
1138 1010 }
1139 1011
1140 1012 // Convert name array to string.
1141 1013 if ( isset( $subsubvalue['first'] ) && isset( $subsubvalue['last'] ) ) {
1142 - $subsubvalue = trim( implode( ' ', $subsubvalue ) );
1014 + $subsubvalue = trim( implode( ' ', $subsubvalue ) );
1015 +
1143 1016 $values['name_field_ids'][] = $subsubindex;
1144 1017 }
1145 1018
1146 1019 if ( is_array( $values['item_meta'][ $subsubindex ] ) ) {