PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.18
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.18
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 +113 -380 6.316.18 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 );
@@ -175,9 +157,8 @@
175 157 }
176 158
177 159 FrmEntriesHelper::set_posted_value( $posted_field, $value, $args );
178 160
179 - self::validate_options( $errors, $posted_field, $value, $args );
180 161 self::validate_field_types( $errors, $posted_field, $value, $args );
181 162
182 163 // Field might want to modify value before other parts of the system
183 164 // e.g. trim off excess values like in the case of fields with limit.
@@ -182,9 +163,8 @@
182 163 // Field might want to modify value before other parts of the system
183 164 // e.g. trim off excess values like in the case of fields with limit.
184 165 $value = apply_filters( 'frm_modify_posted_field_value', $value, $errors, $posted_field, $args );
185 166
186 - // phpcs:ignore Universal.Operators.StrictComparisons
187 167 if ( $value != '' ) {
188 168 self::validate_phone_field( $errors, $posted_field, $value, $args );
189 169 }
190 170
@@ -196,193 +176,8 @@
196 176 }
197 177 }
198 178
199 179 /**
200 - * @since 6.21
201 - *
202 - * @param array $errors
203 - * @param object $posted_field
204 - * @param array|string $value
205 - * @param array $args
206 - *
207 - * @return void
208 - */
209 - private static function validate_options( &$errors, $posted_field, $value, $args ) {
210 - if ( empty( $posted_field->options ) ) {
211 - return;
212 - }
213 -
214 - $option_is_valid = self::option_is_valid( $posted_field, $value, $posted_field->options );
215 -
216 - /**
217 - * @since 6.21
218 - *
219 - * @param bool $option_is_valid
220 - * @param array|string $value
221 - * @param object $field
222 - */
223 - $option_is_valid = (bool) apply_filters( 'frm_option_is_valid', $option_is_valid, $value, $posted_field );
224 -
225 - if ( ! $option_is_valid ) {
226 - $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'invalid' );
227 - }
228 - }
229 -
230 - /**
231 - * Validate that value matches one of the options for the field.
232 - *
233 - * @since 6.21
234 - *
235 - * @param stdClass $field
236 - * @param array|string $value
237 - * @param array $options
238 - *
239 - * @return bool
240 - */
241 - private static function option_is_valid( $field, $value, $options ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
242 - if ( '' === $value ) {
243 - return true;
244 - }
245 -
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 - if ( in_array( $field->type, array( 'likert', 'ranking' ), true ) ) {
253 - // Ignore these field types automatically.
254 - return true;
255 - }
256 -
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 - $value = (array) $value;
266 -
267 - foreach ( $value as $current_value ) {
268 - $match = false;
269 -
270 - foreach ( $options as $key => $option ) {
271 - if ( str_starts_with( $key, 'other_' ) ) {
272 - // Always return true if an other option is found.
273 - return true;
274 - }
275 -
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 -
288 - if ( $match ) {
289 - break;
290 - }
291 -
292 - $match = trim( $current_value ) === trim( do_shortcode( $option_value ) );
293 -
294 - if ( $match ) {
295 - break;
296 - }
297 -
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 - if ( ! $match ) {
316 - return self::options_are_dynamic_based_on_hook( $field, $value );
317 - }
318 - }//end foreach
319 -
320 - return true;
321 - }
322 -
323 - /**
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 - * Do not validate options if they have been modified with a hook.
354 - * This is to help avoid issues where the options could be based on a URL param for example.
355 - *
356 - * @since 6.21
357 - *
358 - * @param object $field_object The field object.
359 - * @param array|string $value The value to validate.
360 - *
361 - * @return bool
362 - */
363 - private static function options_are_dynamic_based_on_hook( $field_object, $value ) {
364 - $values = (array) $field_object;
365 - $values['value'] = $value;
366 - FrmFieldsHelper::prepare_new_front_field( $values, $field_object );
367 -
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 );
376 - };
377 -
378 - $values_options = array_map( $map_callback, $values['options'] );
379 - $field_object_options = array_map( $map_callback, $field_object->options );
380 -
381 - return $values_options !== $field_object_options;
382 - }
383 -
384 - /**
385 180 * Maybe add item_name to $_POST to save it in items table.
386 181 *
387 182 * @since 5.2.02
388 183 *
@@ -387,14 +182,11 @@
387 182 * @since 5.2.02
388 183 *
389 184 * @param array|string $value Field value.
390 185 * @param object $field Field object.
391 - *
392 - * @return void
393 186 */
394 187 private static function maybe_add_item_name( $value, $field ) {
395 188 $item_name = false;
396 -
397 189 if ( 'name' === $field->type ) {
398 190 $field_obj = FrmFieldFactory::get_field_object( $field );
399 191 $item_name = $field_obj->get_display_value( $value );
400 192 } elseif ( 'text' === $field->type ) {
@@ -402,9 +194,9 @@
402 194 }
403 195
404 196 if ( false !== $item_name ) {
405 197 // 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 );
198 + $_POST['item_name'] = substr( $item_name, 0, 255 );
407 199 }
408 200 }
409 201
410 202 /**
@@ -411,14 +203,11 @@
411 203 * Set $value to an empty string if it matches its label
412 204 *
413 205 * @param object $field
414 206 * @param string $value
415 - *
416 - * @return void
417 207 */
418 208 private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) {
419 209 $position = FrmField::get_option( $field, 'label' );
420 -
421 210 if ( ! $position ) {
422 211 $position = FrmStylesController::get_style_val( 'position', $field->form_id );
423 212 }
424 213
@@ -426,16 +215,8 @@
426 215 $value = '';
427 216 }
428 217 }
429 218
430 - /**
431 - * @param array $errors
432 - * @param object $posted_field
433 - * @param mixed $value
434 - * @param array $args
435 - *
436 - * @return void
437 - */
438 219 public static function validate_field_types( &$errors, $posted_field, $value, $args ) {
439 220 $field_obj = FrmFieldFactory::get_field_object( $posted_field );
440 221 $args['value'] = $value;
441 222 $args['errors'] = $errors;
@@ -440,41 +221,25 @@
440 221 $args['value'] = $value;
441 222 $args['errors'] = $errors;
442 223
443 224 $new_errors = $field_obj->validate( $args );
444 -
445 - if ( $new_errors ) {
225 + if ( ! empty( $new_errors ) ) {
446 226 $errors = array_merge( $errors, $new_errors );
447 227 }
448 228 }
449 229
450 - /**
451 - * @param array $errors
452 - * @param object $field
453 - * @param string $value
454 - * @param array $args
455 - *
456 - * @return void
457 - */
458 230 public static function validate_phone_field( &$errors, $field, $value, $args ) {
459 231 $format_value = FrmField::get_option( $field, 'format' );
460 232
461 - if ( $field->type !== 'phone' && ( $field->type !== 'text' || ! $format_value || FrmCurrencyHelper::is_currency_format( $format_value ) ) ) {
462 - return;
463 - }
233 + if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) {
234 + $pattern = self::phone_format( $field );
464 235
465 - $pattern = self::phone_format( $field );
466 -
467 - if ( ! preg_match( $pattern, $value ) ) {
468 - $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
236 + if ( ! preg_match( $pattern, $value ) ) {
237 + $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
238 + }
469 239 }
470 240 }
471 241
472 - /**
473 - * @param object $field
474 - *
475 - * @return string
476 - */
477 242 public static function phone_format( $field ) {
478 243 if ( FrmField::is_option_empty( $field, 'format' ) ) {
479 244 $pattern = self::default_phone_format();
480 245 } else {
@@ -486,19 +251,19 @@
486 251 $pattern = html_entity_decode( $pattern );
487 252 $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field );
488 253
489 254 // Create a regexp if format is not already a regexp
490 - if ( ! str_starts_with( $pattern, '^' ) ) {
255 + if ( strpos( $pattern, '^' ) !== 0 ) {
491 256 $pattern = self::create_regular_expression_from_format( $pattern );
492 257 }
493 258
494 - return '/' . $pattern . '/';
259 + $pattern = '/' . $pattern . '/';
260 +
261 + return $pattern;
495 262 }
496 263
497 264 /**
498 265 * @since 3.01
499 - *
500 - * @return string
501 266 */
502 267 private static function default_phone_format() {
503 268 return '^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$';
504 269 }
@@ -524,57 +289,44 @@
524 289 $pattern = str_replace( 'a', '[a-zA-Z]', $pattern );
525 290 $pattern = str_replace( '*', 'w', $pattern );
526 291 $pattern = str_replace( '/', '\/', $pattern );
527 292
528 - if ( str_contains( $pattern, '\?' ) ) {
293 + if ( strpos( $pattern, '\?' ) !== false ) {
529 294 $parts = explode( '\?', $pattern );
530 295 $pattern = '';
531 -
532 296 foreach ( $parts as $part ) {
533 - if ( $pattern ) {
297 + if ( empty( $pattern ) ) {
298 + $pattern .= $part;
299 + } else {
534 300 $pattern .= '(' . $part . ')?';
535 - } else {
536 - $pattern .= $part;
537 301 }
538 302 }
539 303 }
304 + $pattern = '^' . $pattern . '$';
540 305
541 - return '^' . $pattern . '$';
306 + return $pattern;
542 307 }
543 308
544 309 /**
545 - * Check for spam.
310 + * Check for spam
546 311 *
547 312 * @param bool $exclude
548 313 * @param array $values
549 314 * @param array $errors By reference.
550 - *
551 - * @return void
552 315 */
553 316 public static function spam_check( $exclude, $values, &$errors ) {
554 - if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
555 - // Do not check spam on importing.
317 + if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
318 + // only check spam if there are no other errors
556 319 return;
557 320 }
558 321
559 - if ( $exclude || empty( $values['item_meta'] ) || $errors ) {
560 - // Only check spam if there are no other errors
561 - return;
562 - }
563 -
564 322 $antispam_check = self::is_antispam_check( $values['form_id'] );
565 - $spam_msg = FrmAntiSpamController::get_default_spam_message();
566 -
567 323 if ( is_string( $antispam_check ) ) {
568 324 $errors['spam'] = $antispam_check;
569 325 } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) {
570 - $errors['spam'] = $spam_msg;
571 - } else {
572 - $is_spam = FrmAntiSpamController::is_spam( $values );
573 -
574 - if ( $is_spam ) {
575 - $errors['spam'] = $is_spam;
576 - }
326 + $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
327 + } elseif ( self::blacklist_check( $values ) ) {
328 + $errors['spam'] = __( 'Your entry appears to be blocked spam!', 'formidable' );
577 329 }
578 330
579 331 if ( isset( $errors['spam'] ) || self::form_is_in_progress( $values ) ) {
580 332 return;
@@ -590,17 +342,14 @@
590 342 *
591 343 * @since 5.0.13
592 344 *
593 345 * @param array $values The values.
594 - *
595 346 * @return bool
596 347 */
597 348 private static function form_is_in_progress( $values ) {
598 - // phpcs:disable Generic.WhiteSpace.ScopeIndent
599 349 return FrmAppHelper::pro_is_installed() &&
600 350 ( isset( $values[ 'frm_page_order_' . $values['form_id'] ] ) || FrmAppHelper::get_post_param( 'frm_next_page' ) ) &&
601 351 FrmField::get_all_types_in_form( $values['form_id'], 'break' );
602 - // phpcs:enable Generic.WhiteSpace.ScopeIndent
603 352 }
604 353
605 354 /**
606 355 * @param int $form_id
@@ -613,9 +362,8 @@
613 362 }
614 363
615 364 /**
616 365 * @param array $values
617 - *
618 366 * @return bool
619 367 */
620 368 private static function is_honeypot_spam( $values ) {
621 369 $honeypot = new FrmHoneypot( $values['form_id'] );
@@ -625,47 +373,84 @@
625 373 /**
626 374 * @return bool
627 375 */
628 376 private static function is_spam_bot() {
629 - return ! FrmAppHelper::get_ip_address();
377 + $ip = FrmAppHelper::get_ip_address();
378 +
379 + return empty( $ip );
630 380 }
631 381
632 382 /**
633 383 * @param array $values
634 - *
635 384 * @return bool
636 385 */
637 386 private static function is_akismet_spam( $values ) {
638 387 global $wpcom_api_key;
639 - return is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values );
388 +
389 + return ( is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values ) );
640 390 }
641 391
642 392 /**
643 393 * @param int $form_id
644 - *
645 394 * @return bool
646 395 */
647 396 private static function is_akismet_enabled_for_user( $form_id ) {
648 397 $form = FrmForm::getOne( $form_id );
649 - return ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() );
398 +
399 + return ( ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) );
650 400 }
651 401
402 + public static function blacklist_check( $values ) {
403 + if ( ! apply_filters( 'frm_check_blacklist', true, $values ) ) {
404 + return false;
405 + }
406 +
407 + $mod_keys = trim( self::get_disallowed_words() );
408 + if ( empty( $mod_keys ) ) {
409 + return false;
410 + }
411 +
412 + $content = FrmEntriesHelper::entry_array_to_string( $values );
413 +
414 + self::prepare_values_for_spam_check( $values );
415 + $ip = FrmAppHelper::get_ip_address();
416 + $user_agent = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
417 + $user_info = self::get_spam_check_user_info( $values );
418 +
419 + return self::check_disallowed_words( $user_info['comment_author'], $user_info['comment_author_email'], $user_info['comment_author_url'], $content, $ip, $user_agent );
420 + }
421 +
652 422 /**
653 - * Checks spam using WordPress disallowed words and Frm denylist.
423 + * For WP 5.5 compatibility.
654 424 *
655 - * @param array $values Entry values.
425 + * @since 4.06.02
426 + */
427 + private static function check_disallowed_words( $author, $email, $url, $content, $ip, $user_agent ) {
428 + if ( function_exists( 'wp_check_comment_disallowed_list' ) ) {
429 + return wp_check_comment_disallowed_list( $author, $email, $url, $content, $ip, $user_agent );
430 + }
431 + // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_blacklist_checkFound
432 + return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent );
433 + }
434 +
435 + /**
436 + * For WP 5.5 compatibility.
656 437 *
657 - * @return bool
438 + * @since 4.06.02
658 439 */
659 - public static function blacklist_check( $values ) {
660 - return FrmAntiSpamController::contains_wp_disallowed_words( $values ) || FrmAntiSpamController::is_denylist_spam( $values );
440 + private static function get_disallowed_words() {
441 + $keys = get_option( 'disallowed_keys' );
442 + if ( false === $keys ) {
443 + // Fallback for WP < 5.5.
444 + // phpcs:ignore WordPress.WP.DeprecatedParameterValues.Found
445 + $keys = get_option( 'blacklist_keys' );
446 + }
447 + return $keys;
661 448 }
662 449
663 450 /**
664 451 * Check entries for Akismet spam
665 452 *
666 - * @param array $values Entry values.
667 - *
668 453 * @return bool true if is spam
669 454 */
670 455 public static function akismet( $values ) {
671 456 if ( empty( $values['item_meta'] ) ) {
@@ -688,18 +473,13 @@
688 473
689 474 $query_string = _http_build_query( $datas, '', '&' );
690 475 $response = Akismet::http_post( $query_string, 'comment-check' );
691 476
692 - return is_array( $response ) && $response[1] === 'true';
477 + return ( is_array( $response ) && $response[1] === 'true' );
693 478 }
694 479
695 480 /**
696 481 * @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 482 */
703 483 private static function parse_akismet_array( &$datas, $values ) {
704 484 self::add_site_info_to_akismet( $datas );
705 485 self::add_server_values_to_akismet( $datas );
@@ -710,13 +490,8 @@
710 490 self::add_user_info_to_akismet( $datas, $values );
711 491 self::add_comment_content_to_akismet( $datas, $values );
712 492 }
713 493
714 - /**
715 - * @param array $datas
716 - *
717 - * @return void
718 - */
719 494 private static function add_site_info_to_akismet( &$datas ) {
720 495 $datas['blog'] = FrmAppHelper::site_url();
721 496 $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
722 497 $datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
@@ -728,14 +503,8 @@
728 503 $datas['is_test'] = 'true';
729 504 }
730 505 }
731 506
732 - /**
733 - * @param array $datas
734 - * @param array $values
735 - *
736 - * @return void
737 - */
738 507 private static function add_user_info_to_akismet( &$datas, $values ) {
739 508 $user_info = self::get_spam_check_user_info( $values );
740 509 $datas = $datas + $user_info;
741 510
@@ -747,15 +516,13 @@
747 516 /**
748 517 * Gets user info for Akismet spam check.
749 518 *
750 519 * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater.
751 - * @since 6.21 This changed from private to public.
752 520 *
753 521 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
754 - *
755 522 * @return array
756 523 */
757 - public static function get_spam_check_user_info( $values ) {
524 + private static function get_spam_check_user_info( $values ) {
758 525 if ( ! is_user_logged_in() ) {
759 526 return self::get_spam_check_user_info_for_guest( $values );
760 527 }
761 528
@@ -775,9 +542,8 @@
775 542 *
776 543 * @since 5.0.13
777 544 *
778 545 * @param array $values Entry values after flattened.
779 - *
780 546 * @return array
781 547 */
782 548 private static function get_spam_check_user_info_for_guest( $values ) {
783 549 $datas = array(
@@ -809,10 +575,8 @@
809 575 *
810 576 * @param array $datas Guest data.
811 577 * @param array $values The values.
812 578 * @param int|null $custom_index Custom index (or field ID).
813 - *
814 - * @return void
815 579 */
816 580 private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) {
817 581 foreach ( $values as $index => $value ) {
818 582 if ( ! $datas['missing_keys'] ) {
@@ -825,19 +589,15 @@
825 589 continue;
826 590 }
827 591
828 592 $field_id = ! is_null( $custom_index ) ? $custom_index : $index;
829 -
830 593 foreach ( $datas['missing_keys'] as $key_index => $key ) {
831 594 $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values );
832 -
833 - if ( ! $found ) {
834 - continue;
595 + if ( $found ) {
596 + $datas[ $key ] = $value;
597 + $datas['frm_duplicated'][] = $field_id;
598 + unset( $datas['missing_keys'][ $key_index ] );
835 599 }
836 -
837 - $datas[ $key ] = $value;
838 - $datas['frm_duplicated'][] = $field_id;
839 - unset( $datas['missing_keys'][ $key_index ] );
840 600 }
841 601 }//end foreach
842 602 }
843 603
@@ -860,12 +620,12 @@
860 620 }
861 621
862 622 switch ( $key ) {
863 623 case 'comment_author_email':
864 - return str_contains( $value, '@' ) && is_email( $value );
624 + return strpos( $value, '@' ) && is_email( $value );
865 625
866 626 case 'comment_author_url':
867 - return str_starts_with( $value, 'http' );
627 + return 0 === strpos( $value, 'http' );
868 628
869 629 case 'comment_author':
870 630 if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) {
871 631 // If there is name field in the form, we should always use it as author name.
@@ -870,9 +630,8 @@
870 630 if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) {
871 631 // If there is name field in the form, we should always use it as author name.
872 632 return true;
873 633 }
874 -
875 634 $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
876 635 $fields = self::get_name_text_fields( $form_id );
877 636
878 637 foreach ( $fields as $index => $field ) {
@@ -878,14 +637,12 @@
878 637 foreach ( $fields as $index => $field ) {
879 638 if ( 'Name' !== $field->name ) {
880 639 continue;
881 640 }
882 -
883 641 if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) {
884 642 if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) {
885 643 continue;
886 644 }
887 -
888 645 $value .= ' ' . $values[ $fields[ $index + 1 ]->id ];
889 646 return true;
890 647 }
891 648 }
@@ -899,18 +656,15 @@
899 656 *
900 657 * @since 6.17
901 658 *
902 659 * @param int $form_id
903 - *
904 660 * @return array
905 661 */
906 662 private static function get_name_text_fields( $form_id ) {
907 663 $name_text_fields_is_initialized = is_array( self::$name_text_fields );
908 -
909 664 if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) {
910 665 return self::$name_text_fields[ $form_id ];
911 666 }
912 -
913 667 if ( ! $name_text_fields_is_initialized ) {
914 668 self::$name_text_fields = array();
915 669 }
916 670 self::$name_text_fields[ $form_id ] = FrmDb::get_results(
@@ -926,13 +680,8 @@
926 680
927 681 return self::$name_text_fields[ $form_id ];
928 682 }
929 683
930 - /**
931 - * @param array $datas
932 - *
933 - * @return void
934 - */
935 684 private static function add_server_values_to_akismet( &$datas ) {
936 685 foreach ( $_SERVER as $key => $value ) {
937 686 $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key );
938 687
@@ -950,10 +699,8 @@
950 699 * @since 5.0.09
951 700 *
952 701 * @param array $datas The array of values being sent to Akismet.
953 702 * @param array $values Entry values.
954 - *
955 - * @return void
956 703 */
957 704 private static function add_comment_content_to_akismet( &$datas, $values ) {
958 705 if ( isset( $datas['frm_duplicated'] ) ) {
959 706 foreach ( $datas['frm_duplicated'] as $index ) {
@@ -974,28 +721,22 @@
974 721 *
975 722 * @since 5.0.09
976 723 *
977 724 * @param array $values Entry values.
978 - *
979 - * @return void
980 725 */
981 726 private static function skip_adding_values_to_akismet( &$values ) {
982 727 $skipped_fields = self::get_akismet_skipped_field_ids( $values );
983 -
984 728 foreach ( $skipped_fields as $skipped_field ) {
985 729 if ( ! isset( $values['item_meta'][ $skipped_field->id ] ) ) {
986 730 continue;
987 731 }
988 732
989 - if ( ! self::should_really_skip_field( $skipped_field, $values ) ) {
990 - continue;
733 + if ( self::should_really_skip_field( $skipped_field, $values ) ) {
734 + unset( $values['item_meta'][ $skipped_field->id ] );
735 + if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) {
736 + unset( $values['item_meta']['other'][ $skipped_field->id ] );
737 + }
991 738 }
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 739 }
999 740 }
1000 741
1001 742 /**
@@ -1004,9 +745,8 @@
1004 745 * @since 5.02.04
1005 746 *
1006 747 * @param object $field_data Object contains `id` and `options`.
1007 748 * @param array $values Entry values.
1008 - *
1009 749 * @return bool
1010 750 */
1011 751 private static function should_really_skip_field( $field_data, $values ) {
1012 752 if ( empty( $field_data->options ) ) {
@@ -1014,18 +754,18 @@
1014 754 return true;
1015 755 }
1016 756
1017 757 FrmAppHelper::unserialize_or_decode( $field_data->options );
1018 -
1019 758 if ( ! $field_data->options ) {
1020 759 // Check if an error happens when unserializing, or empty options.
1021 760 return true;
1022 761 }
1023 762
1024 - $last_key = array_key_last( $field_data->options );
763 + end( $field_data->options );
764 + $last_key = key( $field_data->options );
1025 765
1026 766 // If a choice field has no Other option.
1027 - if ( is_numeric( $last_key ) || ! str_starts_with( $last_key, 'other_' ) ) {
767 + if ( is_numeric( $last_key ) || 0 !== strpos( $last_key, 'other_' ) ) {
1028 768 return true;
1029 769 }
1030 770
1031 771 // If a choice field has Other option, but Other is not selected.
@@ -1034,10 +774,9 @@
1034 774 }
1035 775
1036 776 // Check if submitted value is same as one of field option.
1037 777 foreach ( $field_data->options as $option ) {
1038 - $option_value = is_array( $option ) ? ( $option['value'] ?? '' ) : $option;
1039 -
778 + $option_value = ! is_array( $option ) ? $option : ( isset( $option['value'] ) ? $option['value'] : '' );
1040 779 if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) {
1041 780 return true;
1042 781 }
1043 782 }
@@ -1052,9 +791,8 @@
1052 791 * @since 5.0.13 Move out get_all_form_ids_and_flatten_meta() call and get `form_ids` from `$values`.
1053 792 * @since 5.2.04 This method returns array of object contains `id` and `options` instead of array of `id` only.
1054 793 *
1055 794 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
1056 - *
1057 795 * @return array
1058 796 */
1059 797 private static function get_akismet_skipped_field_ids( $values ) {
1060 798 if ( empty( $values['form_ids'] ) ) {
@@ -1077,16 +815,14 @@
1077 815 /**
1078 816 * Prepares values array for spam check.
1079 817 *
1080 818 * @since 5.0.13
1081 - * @since 6.21 This changed from private to public.
1082 819 *
1083 820 * @param array $values Entry values.
1084 - *
1085 - * @return void
1086 821 */
1087 - public static function prepare_values_for_spam_check( &$values ) {
1088 - $values['form_ids'] = self::get_all_form_ids_and_flatten_meta( $values );
822 + private static function prepare_values_for_spam_check( &$values ) {
823 + $form_ids = self::get_all_form_ids_and_flatten_meta( $values );
824 + $values['form_ids'] = $form_ids;
1089 825 }
1090 826
1091 827 /**
1092 828 * Gets all form IDs (include child form IDs) and flatten item_meta array. Used for skipping values sent to Akismet.
@@ -1095,17 +831,15 @@
1095 831 * @since 5.0.09
1096 832 * @since 5.0.13 Convert name field value to string.
1097 833 *
1098 834 * @param array $values Entry values.
1099 - *
1100 835 * @return array Form IDs.
1101 836 */
1102 - private static function get_all_form_ids_and_flatten_meta( &$values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
837 + private static function get_all_form_ids_and_flatten_meta( &$values ) {
1103 838 $values['name_field_ids'] = array();
1104 839
1105 840 // Blacklist check for File field in the old version doesn't contain `form_id`.
1106 841 $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array();
1107 -
1108 842 foreach ( $values['item_meta'] as $field_id => $value ) {
1109 843 if ( ! is_numeric( $field_id ) ) {
1110 844 // Maybe `other`.
1111 845 continue;
@@ -1139,15 +873,14 @@
1139 873 }
1140 874
1141 875 // Convert name array to string.
1142 876 if ( isset( $subsubvalue['first'] ) && isset( $subsubvalue['last'] ) ) {
1143 - $subsubvalue = trim( implode( ' ', $subsubvalue ) );
877 + $subsubvalue = trim( implode( ' ', $subsubvalue ) );
878 +
1144 879 $values['name_field_ids'][] = $subsubindex;
1145 880 }
1146 881
1147 - if ( is_array( $values['item_meta'][ $subsubindex ] ) ) {
1148 - $values['item_meta'][ $subsubindex ][] = $subsubvalue;
1149 - }
882 + $values['item_meta'][ $subsubindex ][] = $subsubvalue;
1150 883 }
1151 884 }//end foreach
1152 885
1153 886 unset( $values['item_meta'][ $field_id ] );