PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.12
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.12
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 +97 -404 6.276.12 View file →
@@ -5,18 +5,10 @@
5 5
6 6 class FrmEntryValidate {
7 7
8 8 /**
9 - * @since 6.17
10 - *
11 - * @var array|null
12 - */
13 - private static $name_text_fields;
14 -
15 - /**
16 9 * @param array $values
17 10 * @param bool|string[] $exclude
18 - *
19 11 * @return array
20 12 */
21 13 public static function validate( $values, $exclude = false ) {
22 14 FrmEntry::sanitize_entry_post( $values );
@@ -23,12 +15,13 @@
23 15 $errors = array();
24 16
25 17 if ( ! isset( $values['form_id'] ) || ! isset( $values['item_meta'] ) ) {
26 18 $errors['form'] = __( 'There was a problem with your submission. Please try again.', 'formidable' );
19 +
27 20 return $errors;
28 21 }
29 22
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
23 + 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 24 $frm_settings = FrmAppHelper::get_settings();
32 25 $errors['form'] = $frm_settings->admin_permission;
33 26 }
34 27
@@ -44,9 +37,9 @@
44 37 self::validate_field( $posted_field, $errors, $values, $args );
45 38 unset( $posted_field );
46 39 }
47 40
48 - if ( ! $errors ) {
41 + if ( empty( $errors ) ) {
49 42 self::spam_check( $exclude, $values, $errors );
50 43 }
51 44
52 45 /**
@@ -62,9 +55,9 @@
62 55
63 56 if ( is_array( $filtered_errors ) ) {
64 57 $errors = $filtered_errors;
65 58 } else {
66 - _doing_it_wrong( __METHOD__, 'Only arrays should be returned when using the frm_validate_entry filter.', '6.3' );
59 + _doing_it_wrong( __FUNCTION__, 'Only arrays should be returned when using the frm_validate_entry filter.', '6.3' );
67 60 }
68 61
69 62 return $errors;
70 63 }
@@ -83,15 +76,9 @@
83 76 $_POST['item_meta'] = array();
84 77 }
85 78 }
86 79
87 - /**
88 - * @param array $values
89 - *
90 - * @return void
91 - */
92 80 private static function set_item_key( &$values ) {
93 - // phpcs:ignore Universal.Operators.StrictComparisons
94 81 if ( ! isset( $values['item_key'] ) || $values['item_key'] == '' ) {
95 82 global $wpdb;
96 83 $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
97 84 $_POST['item_key'] = $values['item_key'];
@@ -97,14 +84,8 @@
97 84 $_POST['item_key'] = $values['item_key'];
98 85 }
99 86 }
100 87
101 - /**
102 - * @param array $values
103 - * @param array|string $exclude
104 - *
105 - * @return array
106 - */
107 88 private static function get_fields_to_validate( $values, $exclude ) {
108 89 $where = apply_filters( 'frm_posted_field_ids', array( 'fi.form_id' => $values['form_id'] ) );
109 90
110 91 // Don't get subfields
@@ -110,9 +91,9 @@
110 91 // Don't get subfields
111 92 $where['fr.parent_form_id'] = array( null, 0 );
112 93
113 94 // Don't get excluded fields (like file upload fields in the ajax validation)
114 - if ( $exclude ) {
95 + if ( ! empty( $exclude ) ) {
115 96 $where['fi.type not'] = $exclude;
116 97 }
117 98
118 99 $fields = FrmField::getAll( $where, 'field_order' );
@@ -127,16 +108,8 @@
127 108 */
128 109 return apply_filters( 'frm_fields_to_validate', $fields, compact( 'values', 'exclude', 'where' ) );
129 110 }
130 111
131 - /**
132 - * @param object $posted_field
133 - * @param array $errors
134 - * @param array $values
135 - * @param array $args
136 - *
137 - * @return void
138 - */
139 112 public static function validate_field( $posted_field, &$errors, $values, $args = array() ) {
140 113 $defaults = array(
141 114 'id' => $posted_field->id,
142 115 // The id of the repeat or embed form.
@@ -146,18 +119,23 @@
146 119 // Exclude these field types from validation.
147 120 'exclude' => array(),
148 121
149 122 );
150 - $args = wp_parse_args( $args, $defaults );
151 - $value = empty( $args['parent_field_id'] ) ? ( $values['item_meta'][ $args['id'] ] ?? '' ) : $values;
123 + $args = wp_parse_args( $args, $defaults );
152 124
125 + if ( empty( $args['parent_field_id'] ) ) {
126 + $value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : '';
127 + } else {
128 + // value is from a nested form
129 + $value = $values;
130 + }
131 +
153 132 // Check for values in "Other" fields
154 133 FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
155 134
156 135 self::maybe_clear_value_for_default_blank_setting( $posted_field, $value );
157 136
158 - $should_trim = is_array( $value ) && count( $value ) === 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
159 -
137 + $should_trim = is_array( $value ) && count( $value ) == 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
160 138 if ( $should_trim ) {
161 139 $value = reset( $value );
162 140 }
163 141
@@ -164,9 +142,8 @@
164 142 if ( ! is_array( $value ) ) {
165 143 $value = trim( $value );
166 144 }
167 145
168 - // phpcs:ignore Universal.Operators.StrictComparisons
169 146 if ( $posted_field->required == '1' && FrmAppHelper::is_empty_value( $value ) ) {
170 147 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' );
171 148 } elseif ( ! isset( $_POST['item_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
172 149 self::maybe_add_item_name( $value, $posted_field );
@@ -173,9 +150,8 @@
173 150 }
174 151
175 152 FrmEntriesHelper::set_posted_value( $posted_field, $value, $args );
176 153
177 - self::validate_options( $errors, $posted_field, $value, $args );
178 154 self::validate_field_types( $errors, $posted_field, $value, $args );
179 155
180 156 // Field might want to modify value before other parts of the system
181 157 // e.g. trim off excess values like in the case of fields with limit.
@@ -180,9 +156,8 @@
180 156 // Field might want to modify value before other parts of the system
181 157 // e.g. trim off excess values like in the case of fields with limit.
182 158 $value = apply_filters( 'frm_modify_posted_field_value', $value, $errors, $posted_field, $args );
183 159
184 - // phpcs:ignore Universal.Operators.StrictComparisons
185 160 if ( $value != '' ) {
186 161 self::validate_phone_field( $errors, $posted_field, $value, $args );
187 162 }
188 163
@@ -194,188 +169,8 @@
194 169 }
195 170 }
196 171
197 172 /**
198 - * @since 6.21
199 - *
200 - * @param array $errors
201 - * @param object $posted_field
202 - * @param array|string $value
203 - * @param array $args
204 - *
205 - * @return void
206 - */
207 - private static function validate_options( &$errors, $posted_field, $value, $args ) {
208 - if ( empty( $posted_field->options ) ) {
209 - return;
210 - }
211 -
212 - $option_is_valid = self::option_is_valid( $posted_field, $value, $posted_field->options );
213 -
214 - /**
215 - * @since 6.21
216 - *
217 - * @param bool $option_is_valid
218 - * @param array|string $value
219 - * @param object $field
220 - */
221 - $option_is_valid = (bool) apply_filters( 'frm_option_is_valid', $option_is_valid, $value, $posted_field );
222 -
223 - if ( ! $option_is_valid ) {
224 - $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'invalid' );
225 - }
226 - }
227 -
228 - /**
229 - * Validate that value matches one of the options for the field.
230 - *
231 - * @since 6.21
232 - *
233 - * @param stdClass $field
234 - * @param array|string $value
235 - * @param array $options
236 - *
237 - * @return bool
238 - */
239 - private static function option_is_valid( $field, $value, $options ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
240 - if ( '' === $value ) {
241 - return true;
242 - }
243 -
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 - if ( in_array( $field->type, array( 'likert', 'ranking' ), true ) ) {
251 - // Ignore these field types automatically.
252 - return true;
253 - }
254 -
255 - if ( 'product' === $field->type && 'user_def' === FrmField::get_option( $field, 'data_type' ) ) {
256 - return true;
257 - }
258 -
259 - if ( ! empty( $field->field_options['post_field'] ) ) {
260 - return true;
261 - }
262 -
263 - $value = (array) $value;
264 -
265 - foreach ( $value as $current_value ) {
266 - $match = false;
267 -
268 - foreach ( $options as $key => $option ) {
269 - if ( str_starts_with( $key, 'other_' ) ) {
270 - // Always return true if an other option is found.
271 - return true;
272 - }
273 -
274 - if ( is_array( $option ) ) {
275 - $separate_value = FrmField::get_option( $field, 'separate_value' );
276 - $option_value = $separate_value ? $option['value'] : $option['label'];
277 - } else {
278 - $option_value = $option;
279 - }
280 -
281 - $match = trim( $current_value ) === trim( $option_value );
282 -
283 - if ( $match ) {
284 - break;
285 - }
286 -
287 - $match = trim( $current_value ) === trim( do_shortcode( $option_value ) );
288 -
289 - if ( $match ) {
290 - break;
291 - }
292 -
293 - $match = self::is_filtered_match( $current_value, $option_value );
294 -
295 - if ( $match ) {
296 - break;
297 - }
298 -
299 - if ( is_numeric( $current_value ) ) {
300 - $match = (int) $current_value === (int) $option_value;
301 -
302 - if ( $match ) {
303 - break;
304 - }
305 - }
306 - }//end foreach
307 -
308 - if ( ! $match ) {
309 - return self::options_are_dynamic_based_on_hook( $field, $value );
310 - }
311 - }//end foreach
312 -
313 - return true;
314 - }
315 -
316 - /**
317 - * Make an extra check after passing $option_value through the_content filter.
318 - * This is to help catch cases where the option's formatting has been modified using
319 - * the_content filter.
320 - *
321 - * @since 6.22
322 - *
323 - * @param string $value
324 - * @param string $option_value
325 - *
326 - * @return bool
327 - */
328 - private static function is_filtered_match( $value, $option_value ) {
329 - // First remove the wpautop filter so it doesn't add extra tags to $option_value.
330 - $filter_priority = has_filter( 'the_content', 'wpautop' );
331 -
332 - if ( is_numeric( $filter_priority ) ) {
333 - remove_filter( 'the_content', 'wpautop', $filter_priority );
334 - }
335 -
336 - $filtered_option = apply_filters( 'the_content', $option_value );
337 -
338 - if ( is_numeric( $filter_priority ) ) {
339 - add_filter( 'the_content', 'wpautop', $filter_priority );
340 - }
341 -
342 - return trim( $value ) === trim( $filtered_option );
343 - }
344 -
345 - /**
346 - * Do not validate options if they have been modified with a hook.
347 - * This is to help avoid issues where the options could be based on a URL param for example.
348 - *
349 - * @since 6.21
350 - *
351 - * @param object $field_object The field object.
352 - * @param array|string $value The value to validate.
353 - *
354 - * @return bool
355 - */
356 - private static function options_are_dynamic_based_on_hook( $field_object, $value ) {
357 - $values = (array) $field_object;
358 - $values['value'] = $value;
359 - FrmFieldsHelper::prepare_new_front_field( $values, $field_object );
360 -
361 - $separate_value = FrmField::get_option( $field_object, 'separate_value' );
362 - $map_callback = function ( $option ) use ( $separate_value ) {
363 - if ( is_array( $option ) ) {
364 - $option_value = $separate_value ? $option['value'] : $option['label'];
365 - } else {
366 - $option_value = $option;
367 - }
368 - return do_shortcode( $option_value );
369 - };
370 -
371 - $values_options = array_map( $map_callback, $values['options'] );
372 - $field_object_options = array_map( $map_callback, $field_object->options );
373 -
374 - return $values_options !== $field_object_options;
375 - }
376 -
377 - /**
378 173 * Maybe add item_name to $_POST to save it in items table.
379 174 *
380 175 * @since 5.2.02
381 176 *
@@ -380,14 +175,11 @@
380 175 * @since 5.2.02
381 176 *
382 177 * @param array|string $value Field value.
383 178 * @param object $field Field object.
384 - *
385 - * @return void
386 179 */
387 180 private static function maybe_add_item_name( $value, $field ) {
388 181 $item_name = false;
389 -
390 182 if ( 'name' === $field->type ) {
391 183 $field_obj = FrmFieldFactory::get_field_object( $field );
392 184 $item_name = $field_obj->get_display_value( $value );
393 185 } elseif ( 'text' === $field->type ) {
@@ -404,14 +196,11 @@
404 196 * Set $value to an empty string if it matches its label
405 197 *
406 198 * @param object $field
407 199 * @param string $value
408 - *
409 - * @return void
410 200 */
411 201 private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) {
412 202 $position = FrmField::get_option( $field, 'label' );
413 -
414 203 if ( ! $position ) {
415 204 $position = FrmStylesController::get_style_val( 'position', $field->form_id );
416 205 }
417 206
@@ -419,16 +208,8 @@
419 208 $value = '';
420 209 }
421 210 }
422 211
423 - /**
424 - * @param array $errors
425 - * @param object $posted_field
426 - * @param mixed $value
427 - * @param array $args
428 - *
429 - * @return void
430 - */
431 212 public static function validate_field_types( &$errors, $posted_field, $value, $args ) {
432 213 $field_obj = FrmFieldFactory::get_field_object( $posted_field );
433 214 $args['value'] = $value;
434 215 $args['errors'] = $errors;
@@ -433,26 +214,16 @@
433 214 $args['value'] = $value;
434 215 $args['errors'] = $errors;
435 216
436 217 $new_errors = $field_obj->validate( $args );
437 -
438 - if ( $new_errors ) {
218 + if ( ! empty( $new_errors ) ) {
439 219 $errors = array_merge( $errors, $new_errors );
440 220 }
441 221 }
442 222
443 - /**
444 - * @param array $errors
445 - * @param object $field
446 - * @param string $value
447 - * @param array $args
448 - *
449 - * @return void
450 - */
451 223 public static function validate_phone_field( &$errors, $field, $value, $args ) {
452 - $format_value = FrmField::get_option( $field, 'format' );
224 + if ( $field->type === 'phone' || ( $field->type === 'text' && FrmField::is_option_true_in_object( $field, 'format' ) ) ) {
453 225
454 - if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) {
455 226 $pattern = self::phone_format( $field );
456 227
457 228 if ( ! preg_match( $pattern, $value ) ) {
458 229 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
@@ -459,13 +230,8 @@
459 230 }
460 231 }
461 232 }
462 233
463 - /**
464 - * @param object $field
465 - *
466 - * @return string
467 - */
468 234 public static function phone_format( $field ) {
469 235 if ( FrmField::is_option_empty( $field, 'format' ) ) {
470 236 $pattern = self::default_phone_format();
471 237 } else {
@@ -477,19 +243,19 @@
477 243 $pattern = html_entity_decode( $pattern );
478 244 $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field );
479 245
480 246 // Create a regexp if format is not already a regexp
481 - if ( ! str_starts_with( $pattern, '^' ) ) {
247 + if ( strpos( $pattern, '^' ) !== 0 ) {
482 248 $pattern = self::create_regular_expression_from_format( $pattern );
483 249 }
484 250
485 - return '/' . $pattern . '/';
251 + $pattern = '/' . $pattern . '/';
252 +
253 + return $pattern;
486 254 }
487 255
488 256 /**
489 257 * @since 3.01
490 - *
491 - * @return string
492 258 */
493 259 private static function default_phone_format() {
494 260 return '^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$';
495 261 }
@@ -515,14 +281,13 @@
515 281 $pattern = str_replace( 'a', '[a-zA-Z]', $pattern );
516 282 $pattern = str_replace( '*', 'w', $pattern );
517 283 $pattern = str_replace( '/', '\/', $pattern );
518 284
519 - if ( str_contains( $pattern, '\?' ) ) {
285 + if ( strpos( $pattern, '\?' ) !== false ) {
520 286 $parts = explode( '\?', $pattern );
521 287 $pattern = '';
522 -
523 288 foreach ( $parts as $part ) {
524 - if ( ! $pattern ) {
289 + if ( empty( $pattern ) ) {
525 290 $pattern .= $part;
526 291 } else {
527 292 $pattern .= '(' . $part . ')?';
528 293 }
@@ -527,45 +292,33 @@
527 292 $pattern .= '(' . $part . ')?';
528 293 }
529 294 }
530 295 }
296 + $pattern = '^' . $pattern . '$';
531 297
532 - return '^' . $pattern . '$';
298 + return $pattern;
533 299 }
534 300
535 301 /**
536 - * Check for spam.
302 + * Check for spam
537 303 *
538 304 * @param bool $exclude
539 305 * @param array $values
540 306 * @param array $errors By reference.
541 - *
542 - * @return void
543 307 */
544 308 public static function spam_check( $exclude, $values, &$errors ) {
545 - if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
546 - // Do not check spam on importing.
547 - return;
548 - }
549 -
550 - if ( $exclude || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
309 + if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
551 310 // only check spam if there are no other errors
552 311 return;
553 312 }
554 313
555 314 $antispam_check = self::is_antispam_check( $values['form_id'] );
556 - $spam_msg = FrmAntiSpamController::get_default_spam_message();
557 -
558 315 if ( is_string( $antispam_check ) ) {
559 316 $errors['spam'] = $antispam_check;
560 317 } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) {
561 - $errors['spam'] = $spam_msg;
562 - } else {
563 - $is_spam = FrmAntiSpamController::is_spam( $values );
564 -
565 - if ( $is_spam ) {
566 - $errors['spam'] = $is_spam;
567 - }
318 + $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
319 + } elseif ( self::blacklist_check( $values ) ) {
320 + $errors['spam'] = __( 'Your entry appears to be blocked spam!', 'formidable' );
568 321 }
569 322
570 323 if ( isset( $errors['spam'] ) || self::form_is_in_progress( $values ) ) {
571 324 return;
@@ -581,9 +334,8 @@
581 334 *
582 335 * @since 5.0.13
583 336 *
584 337 * @param array $values The values.
585 - *
586 338 * @return bool
587 339 */
588 340 private static function form_is_in_progress( $values ) {
589 341 return FrmAppHelper::pro_is_installed() &&
@@ -602,9 +354,8 @@
602 354 }
603 355
604 356 /**
605 357 * @param array $values
606 - *
607 358 * @return bool
608 359 */
609 360 private static function is_honeypot_spam( $values ) {
610 361 $honeypot = new FrmHoneypot( $values['form_id'] );
@@ -615,47 +366,83 @@
615 366 * @return bool
616 367 */
617 368 private static function is_spam_bot() {
618 369 $ip = FrmAppHelper::get_ip_address();
370 +
619 371 return empty( $ip );
620 372 }
621 373
622 374 /**
623 375 * @param array $values
624 - *
625 376 * @return bool
626 377 */
627 378 private static function is_akismet_spam( $values ) {
628 379 global $wpcom_api_key;
629 - return is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values );
380 +
381 + return ( is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values ) );
630 382 }
631 383
632 384 /**
633 385 * @param int $form_id
634 - *
635 386 * @return bool
636 387 */
637 388 private static function is_akismet_enabled_for_user( $form_id ) {
638 389 $form = FrmForm::getOne( $form_id );
639 - return ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() );
390 +
391 + return ( ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) );
640 392 }
641 393
394 + public static function blacklist_check( $values ) {
395 + if ( ! apply_filters( 'frm_check_blacklist', true, $values ) ) {
396 + return false;
397 + }
398 +
399 + $mod_keys = trim( self::get_disallowed_words() );
400 + if ( empty( $mod_keys ) ) {
401 + return false;
402 + }
403 +
404 + $content = FrmEntriesHelper::entry_array_to_string( $values );
405 +
406 + self::prepare_values_for_spam_check( $values );
407 + $ip = FrmAppHelper::get_ip_address();
408 + $user_agent = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
409 + $user_info = self::get_spam_check_user_info( $values );
410 +
411 + return self::check_disallowed_words( $user_info['comment_author'], $user_info['comment_author_email'], $user_info['comment_author_url'], $content, $ip, $user_agent );
412 + }
413 +
642 414 /**
643 - * Checks spam using WordPress disallowed words and Frm denylist.
415 + * For WP 5.5 compatibility.
644 416 *
645 - * @param array $values Entry values.
417 + * @since 4.06.02
418 + */
419 + private static function check_disallowed_words( $author, $email, $url, $content, $ip, $user_agent ) {
420 + if ( function_exists( 'wp_check_comment_disallowed_list' ) ) {
421 + return wp_check_comment_disallowed_list( $author, $email, $url, $content, $ip, $user_agent );
422 + }
423 + // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_blacklist_checkFound
424 + return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent );
425 + }
426 +
427 + /**
428 + * For WP 5.5 compatibility.
646 429 *
647 - * @return bool
430 + * @since 4.06.02
648 431 */
649 - public static function blacklist_check( $values ) {
650 - return FrmAntiSpamController::contains_wp_disallowed_words( $values ) || FrmAntiSpamController::is_denylist_spam( $values );
432 + private static function get_disallowed_words() {
433 + $keys = get_option( 'disallowed_keys' );
434 + if ( false === $keys ) {
435 + // Fallback for WP < 5.5.
436 + // phpcs:ignore WordPress.WP.DeprecatedParameterValues.Found
437 + $keys = get_option( 'blacklist_keys' );
438 + }
439 + return $keys;
651 440 }
652 441
653 442 /**
654 443 * Check entries for Akismet spam
655 444 *
656 - * @param array $values Entry values.
657 - *
658 445 * @return bool true if is spam
659 446 */
660 447 public static function akismet( $values ) {
661 448 if ( empty( $values['item_meta'] ) ) {
@@ -678,18 +465,13 @@
678 465
679 466 $query_string = _http_build_query( $datas, '', '&' );
680 467 $response = Akismet::http_post( $query_string, 'comment-check' );
681 468
682 - return is_array( $response ) && $response[1] === 'true';
469 + return ( is_array( $response ) && $response[1] === 'true' );
683 470 }
684 471
685 472 /**
686 473 * @since 2.0
687 - *
688 - * @param array $datas The array of values being sent to Akismet.
689 - * @param array $values Entry values.
690 - *
691 - * @return void
692 474 */
693 475 private static function parse_akismet_array( &$datas, $values ) {
694 476 self::add_site_info_to_akismet( $datas );
695 477 self::add_server_values_to_akismet( $datas );
@@ -694,19 +476,13 @@
694 476 self::add_site_info_to_akismet( $datas );
695 477 self::add_server_values_to_akismet( $datas );
696 478
697 479 self::prepare_values_for_spam_check( $values );
698 - self::skip_adding_values_to_akismet( $values );
699 480
700 481 self::add_user_info_to_akismet( $datas, $values );
701 482 self::add_comment_content_to_akismet( $datas, $values );
702 483 }
703 484
704 - /**
705 - * @param array $datas
706 - *
707 - * @return void
708 - */
709 485 private static function add_site_info_to_akismet( &$datas ) {
710 486 $datas['blog'] = FrmAppHelper::site_url();
711 487 $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
712 488 $datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
@@ -718,14 +494,8 @@
718 494 $datas['is_test'] = 'true';
719 495 }
720 496 }
721 497
722 - /**
723 - * @param array $datas
724 - * @param array $values
725 - *
726 - * @return void
727 - */
728 498 private static function add_user_info_to_akismet( &$datas, $values ) {
729 499 $user_info = self::get_spam_check_user_info( $values );
730 500 $datas = $datas + $user_info;
731 501
@@ -737,15 +507,13 @@
737 507 /**
738 508 * Gets user info for Akismet spam check.
739 509 *
740 510 * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater.
741 - * @since 6.21 This changed from private to public.
742 511 *
743 512 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
744 - *
745 513 * @return array
746 514 */
747 - public static function get_spam_check_user_info( $values ) {
515 + private static function get_spam_check_user_info( $values ) {
748 516 if ( ! is_user_logged_in() ) {
749 517 return self::get_spam_check_user_info_for_guest( $values );
750 518 }
751 519
@@ -765,9 +533,8 @@
765 533 *
766 534 * @since 5.0.13
767 535 *
768 536 * @param array $values Entry values after flattened.
769 - *
770 537 * @return array
771 538 */
772 539 private static function get_spam_check_user_info_for_guest( $values ) {
773 540 $datas = array(
@@ -799,10 +566,8 @@
799 566 *
800 567 * @param array $datas Guest data.
801 568 * @param array $values The values.
802 569 * @param int|null $custom_index Custom index (or field ID).
803 - *
804 - * @return void
805 570 */
806 571 private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) {
807 572 foreach ( $values as $index => $value ) {
808 573 if ( ! $datas['missing_keys'] ) {
@@ -815,12 +580,10 @@
815 580 continue;
816 581 }
817 582
818 583 $field_id = ! is_null( $custom_index ) ? $custom_index : $index;
819 -
820 584 foreach ( $datas['missing_keys'] as $key_index => $key ) {
821 - $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values );
822 -
585 + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] );
823 586 if ( $found ) {
824 587 $datas[ $key ] = $value;
825 588 $datas['frm_duplicated'][] = $field_id;
826 589 unset( $datas['missing_keys'][ $key_index ] );
@@ -837,13 +600,11 @@
837 600 * @param string $key Guest info key.
838 601 * @param string $value Value to check.
839 602 * @param int $field_id Field ID.
840 603 * @param array $name_field_ids Name field IDs.
841 - * @param array $values Array of posted values.
842 - *
843 604 * @return bool
844 605 */
845 - private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) {
606 + private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) {
846 607 if ( ! $value || is_numeric( $value ) ) {
847 608 return false;
848 609 }
849 610
@@ -848,79 +609,24 @@
848 609 }
849 610
850 611 switch ( $key ) {
851 612 case 'comment_author_email':
852 - return str_contains( $value, '@' ) && is_email( $value );
613 + return strpos( $value, '@' ) && is_email( $value );
853 614
854 615 case 'comment_author_url':
855 - return str_starts_with( $value, 'http' );
616 + return 0 === strpos( $value, 'http' );
856 617
857 618 case 'comment_author':
858 - if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) {
619 + if ( $name_field_ids ) {
859 620 // If there is name field in the form, we should always use it as author name.
860 - return true;
621 + return in_array( $field_id, $name_field_ids, true );
861 622 }
623 + return strlen( $value ) < 200;
624 + }
862 625
863 - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
864 - $fields = self::get_name_text_fields( $form_id );
865 -
866 - foreach ( $fields as $index => $field ) {
867 - if ( 'Name' !== $field->name ) {
868 - continue;
869 - }
870 -
871 - if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) {
872 - if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) {
873 - continue;
874 - }
875 -
876 - $value .= ' ' . $values[ $fields[ $index + 1 ]->id ];
877 - return true;
878 - }
879 - }
880 - }//end switch
881 -
882 626 return false;
883 627 }
884 628
885 - /**
886 - * Returns fields that have 'Name' and 'Last' as their name.
887 - *
888 - * @since 6.17
889 - *
890 - * @param int $form_id
891 - *
892 - * @return array
893 - */
894 - private static function get_name_text_fields( $form_id ) {
895 - $name_text_fields_is_initialized = is_array( self::$name_text_fields );
896 -
897 - if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) {
898 - return self::$name_text_fields[ $form_id ];
899 - }
900 -
901 - if ( ! $name_text_fields_is_initialized ) {
902 - self::$name_text_fields = array();
903 - }
904 - self::$name_text_fields[ $form_id ] = FrmDb::get_results(
905 - 'frm_fields',
906 - array(
907 - 'form_id' => $form_id,
908 - 'type' => 'text',
909 - 'name' => array( 'Name', 'Last' ),
910 - ),
911 - 'id,name',
912 - array( 'order_by' => 'field_order ASC' )
913 - );
914 -
915 - return self::$name_text_fields[ $form_id ];
916 - }
917 -
918 - /**
919 - * @param array $datas
920 - *
921 - * @return void
922 - */
923 629 private static function add_server_values_to_akismet( &$datas ) {
924 630 foreach ( $_SERVER as $key => $value ) {
925 631 $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key );
926 632
@@ -938,10 +644,8 @@
938 644 * @since 5.0.09
939 645 *
940 646 * @param array $datas The array of values being sent to Akismet.
941 647 * @param array $values Entry values.
942 - *
943 - * @return void
944 648 */
945 649 private static function add_comment_content_to_akismet( &$datas, $values ) {
946 650 if ( isset( $datas['frm_duplicated'] ) ) {
947 651 foreach ( $datas['frm_duplicated'] as $index ) {
@@ -953,8 +657,10 @@
953 657 }
954 658 unset( $datas['frm_duplicated'] );
955 659 }
956 660
661 + self::skip_adding_values_to_akismet( $values );
662 +
957 663 $datas['comment_content'] = FrmEntriesHelper::entry_array_to_string( $values );
958 664 }
959 665
960 666 /**
@@ -962,14 +668,11 @@
962 668 *
963 669 * @since 5.0.09
964 670 *
965 671 * @param array $values Entry values.
966 - *
967 - * @return void
968 672 */
969 673 private static function skip_adding_values_to_akismet( &$values ) {
970 674 $skipped_fields = self::get_akismet_skipped_field_ids( $values );
971 -
972 675 foreach ( $skipped_fields as $skipped_field ) {
973 676 if ( ! isset( $values['item_meta'][ $skipped_field->id ] ) ) {
974 677 continue;
975 678 }
@@ -975,9 +678,8 @@
975 678 }
976 679
977 680 if ( self::should_really_skip_field( $skipped_field, $values ) ) {
978 681 unset( $values['item_meta'][ $skipped_field->id ] );
979 -
980 682 if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) {
981 683 unset( $values['item_meta']['other'][ $skipped_field->id ] );
982 684 }
983 685 }
@@ -990,9 +692,8 @@
990 692 * @since 5.02.04
991 693 *
992 694 * @param object $field_data Object contains `id` and `options`.
993 695 * @param array $values Entry values.
994 - *
995 696 * @return bool
996 697 */
997 698 private static function should_really_skip_field( $field_data, $values ) {
998 699 if ( empty( $field_data->options ) ) {
@@ -1000,18 +701,18 @@
1000 701 return true;
1001 702 }
1002 703
1003 704 FrmAppHelper::unserialize_or_decode( $field_data->options );
1004 -
1005 705 if ( ! $field_data->options ) {
1006 706 // Check if an error happens when unserializing, or empty options.
1007 707 return true;
1008 708 }
1009 709
1010 - $last_key = array_key_last( $field_data->options );
710 + end( $field_data->options );
711 + $last_key = key( $field_data->options );
1011 712
1012 713 // If a choice field has no Other option.
1013 - if ( is_numeric( $last_key ) || ! str_starts_with( $last_key, 'other_' ) ) {
714 + if ( is_numeric( $last_key ) || 0 !== strpos( $last_key, 'other_' ) ) {
1014 715 return true;
1015 716 }
1016 717
1017 718 // If a choice field has Other option, but Other is not selected.
@@ -1020,10 +721,9 @@
1020 721 }
1021 722
1022 723 // Check if submitted value is same as one of field option.
1023 724 foreach ( $field_data->options as $option ) {
1024 - $option_value = ! is_array( $option ) ? $option : ( $option['value'] ?? '' );
1025 -
725 + $option_value = ! is_array( $option ) ? $option : ( isset( $option['value'] ) ? $option['value'] : '' );
1026 726 if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) {
1027 727 return true;
1028 728 }
1029 729 }
@@ -1038,9 +738,8 @@
1038 738 * @since 5.0.13 Move out get_all_form_ids_and_flatten_meta() call and get `form_ids` from `$values`.
1039 739 * @since 5.2.04 This method returns array of object contains `id` and `options` instead of array of `id` only.
1040 740 *
1041 741 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
1042 - *
1043 742 * @return array
1044 743 */
1045 744 private static function get_akismet_skipped_field_ids( $values ) {
1046 745 if ( empty( $values['form_ids'] ) ) {
@@ -1063,15 +762,12 @@
1063 762 /**
1064 763 * Prepares values array for spam check.
1065 764 *
1066 765 * @since 5.0.13
1067 - * @since 6.21 This changed from private to public.
1068 766 *
1069 767 * @param array $values Entry values.
1070 - *
1071 - * @return void
1072 768 */
1073 - public static function prepare_values_for_spam_check( &$values ) {
769 + private static function prepare_values_for_spam_check( &$values ) {
1074 770 $form_ids = self::get_all_form_ids_and_flatten_meta( $values );
1075 771 $values['form_ids'] = $form_ids;
1076 772 }
1077 773
@@ -1082,17 +778,15 @@
1082 778 * @since 5.0.09
1083 779 * @since 5.0.13 Convert name field value to string.
1084 780 *
1085 781 * @param array $values Entry values.
1086 - *
1087 782 * @return array Form IDs.
1088 783 */
1089 - private static function get_all_form_ids_and_flatten_meta( &$values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
784 + private static function get_all_form_ids_and_flatten_meta( &$values ) {
1090 785 $values['name_field_ids'] = array();
1091 786
1092 787 // Blacklist check for File field in the old version doesn't contain `form_id`.
1093 788 $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array();
1094 -
1095 789 foreach ( $values['item_meta'] as $field_id => $value ) {
1096 790 if ( ! is_numeric( $field_id ) ) {
1097 791 // Maybe `other`.
1098 792 continue;
@@ -1126,15 +820,14 @@
1126 820 }
1127 821
1128 822 // Convert name array to string.
1129 823 if ( isset( $subsubvalue['first'] ) && isset( $subsubvalue['last'] ) ) {
1130 - $subsubvalue = trim( implode( ' ', $subsubvalue ) );
824 + $subsubvalue = trim( implode( ' ', $subsubvalue ) );
825 +
1131 826 $values['name_field_ids'][] = $subsubindex;
1132 827 }
1133 828
1134 - if ( is_array( $values['item_meta'][ $subsubindex ] ) ) {
1135 - $values['item_meta'][ $subsubindex ][] = $subsubvalue;
1136 - }
829 + $values['item_meta'][ $subsubindex ][] = $subsubvalue;
1137 830 }
1138 831 }//end foreach
1139 832
1140 833 unset( $values['item_meta'][ $field_id ] );