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