PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.7
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.7
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 +171 -476 6.306.7 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 - * @param bool|string[] $exclude
18 - *
10 + * @param string[]|bool $exclude
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,30 +76,16 @@
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 - if ( isset( $values['item_key'] ) && $values['item_key'] != '' ) {
95 - return;
81 + if ( ! isset( $values['item_key'] ) || $values['item_key'] == '' ) {
82 + global $wpdb;
83 + $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
84 + $_POST['item_key'] = $values['item_key'];
96 85 }
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 86 }
102 87
103 - /**
104 - * @param array $values
105 - * @param array|string $exclude
106 - *
107 - * @return array
108 - */
109 88 private static function get_fields_to_validate( $values, $exclude ) {
110 89 $where = apply_filters( 'frm_posted_field_ids', array( 'fi.form_id' => $values['form_id'] ) );
111 90
112 91 // Don't get subfields
@@ -112,9 +91,9 @@
112 91 // Don't get subfields
113 92 $where['fr.parent_form_id'] = array( null, 0 );
114 93
115 94 // Don't get excluded fields (like file upload fields in the ajax validation)
116 - if ( $exclude ) {
95 + if ( ! empty( $exclude ) ) {
117 96 $where['fi.type not'] = $exclude;
118 97 }
119 98
120 99 $fields = FrmField::getAll( $where, 'field_order' );
@@ -129,29 +108,23 @@
129 108 */
130 109 return apply_filters( 'frm_fields_to_validate', $fields, compact( 'values', 'exclude', 'where' ) );
131 110 }
132 111
133 - /**
134 - * @param object $posted_field
135 - * @param array $errors
136 - * @param array $values
137 - * @param array $args
138 - *
139 - * @return void
140 - */
141 112 public static function validate_field( $posted_field, &$errors, $values, $args = array() ) {
142 113 $defaults = array(
143 114 'id' => $posted_field->id,
144 - // The id of the repeat or embed form.
145 - 'parent_field_id' => '',
146 - // The pointer in the posted array.
147 - 'key_pointer' => '',
148 - // Exclude these field types from validation.
149 - 'exclude' => array(),
115 + 'parent_field_id' => '', // the id of the repeat or embed form
116 + 'key_pointer' => '', // the pointer in the posted array
117 + 'exclude' => array(), // exclude these field types from validation
118 + );
119 + $args = wp_parse_args( $args, $defaults );
150 120
151 - );
152 - $args = wp_parse_args( $args, $defaults );
153 - $value = ! empty( $args['parent_field_id'] ) ? $values : ( $values['item_meta'][ $args['id'] ] ?? '' );
121 + if ( empty( $args['parent_field_id'] ) ) {
122 + $value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : '';
123 + } else {
124 + // value is from a nested form
125 + $value = $values;
126 + }
154 127
155 128 // Check for values in "Other" fields
156 129 FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
157 130
@@ -156,10 +129,9 @@
156 129 FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args );
157 130
158 131 self::maybe_clear_value_for_default_blank_setting( $posted_field, $value );
159 132
160 - $should_trim = is_array( $value ) && count( $value ) === 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
161 -
133 + $should_trim = is_array( $value ) && count( $value ) == 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox';
162 134 if ( $should_trim ) {
163 135 $value = reset( $value );
164 136 }
165 137
@@ -166,9 +138,8 @@
166 138 if ( ! is_array( $value ) ) {
167 139 $value = trim( $value );
168 140 }
169 141
170 - // phpcs:ignore Universal.Operators.StrictComparisons
171 142 if ( $posted_field->required == '1' && FrmAppHelper::is_empty_value( $value ) ) {
172 143 $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' );
173 144 } elseif ( ! isset( $_POST['item_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
174 145 self::maybe_add_item_name( $value, $posted_field );
@@ -175,9 +146,8 @@
175 146 }
176 147
177 148 FrmEntriesHelper::set_posted_value( $posted_field, $value, $args );
178 149
179 - self::validate_options( $errors, $posted_field, $value, $args );
180 150 self::validate_field_types( $errors, $posted_field, $value, $args );
181 151
182 152 // Field might want to modify value before other parts of the system
183 153 // e.g. trim off excess values like in the case of fields with limit.
@@ -182,9 +152,8 @@
182 152 // Field might want to modify value before other parts of the system
183 153 // e.g. trim off excess values like in the case of fields with limit.
184 154 $value = apply_filters( 'frm_modify_posted_field_value', $value, $errors, $posted_field, $args );
185 155
186 - // phpcs:ignore Universal.Operators.StrictComparisons
187 156 if ( $value != '' ) {
188 157 self::validate_phone_field( $errors, $posted_field, $value, $args );
189 158 }
190 159
@@ -189,212 +158,19 @@
189 158 }
190 159
191 160 $errors = apply_filters( 'frm_validate_' . $posted_field->type . '_field_entry', $errors, $posted_field, $value, $args );
192 161 $errors = apply_filters( 'frm_validate_field_entry', $errors, $posted_field, $value, $args );
193 -
194 - if ( ! FrmAppHelper::pro_is_installed() && empty( $args['other'] ) ) {
195 - FrmEntriesHelper::get_posted_value( $posted_field, $value, $args );
196 - }
197 162 }
198 163
199 164 /**
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 165 * Maybe add item_name to $_POST to save it in items table.
386 166 *
387 167 * @since 5.2.02
388 168 *
389 - * @param array|string $value Field value.
390 - * @param object $field Field object.
391 - *
392 - * @return void
169 + * @param object $field Field object.
393 170 */
394 171 private static function maybe_add_item_name( $value, $field ) {
395 172 $item_name = false;
396 -
397 173 if ( 'name' === $field->type ) {
398 174 $field_obj = FrmFieldFactory::get_field_object( $field );
399 175 $item_name = $field_obj->get_display_value( $value );
400 176 } elseif ( 'text' === $field->type ) {
@@ -402,9 +178,9 @@
402 178 }
403 179
404 180 if ( false !== $item_name ) {
405 181 // 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 );
182 + $_POST['item_name'] = substr( $item_name, 0, 255 );
407 183 }
408 184 }
409 185
410 186 /**
@@ -411,14 +187,11 @@
411 187 * Set $value to an empty string if it matches its label
412 188 *
413 189 * @param object $field
414 190 * @param string $value
415 - *
416 - * @return void
417 191 */
418 192 private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) {
419 193 $position = FrmField::get_option( $field, 'label' );
420 -
421 194 if ( ! $position ) {
422 195 $position = FrmStylesController::get_style_val( 'position', $field->form_id );
423 196 }
424 197
@@ -426,16 +199,8 @@
426 199 $value = '';
427 200 }
428 201 }
429 202
430 - /**
431 - * @param array $errors
432 - * @param object $posted_field
433 - * @param mixed $value
434 - * @param array $args
435 - *
436 - * @return void
437 - */
438 203 public static function validate_field_types( &$errors, $posted_field, $value, $args ) {
439 204 $field_obj = FrmFieldFactory::get_field_object( $posted_field );
440 205 $args['value'] = $value;
441 206 $args['errors'] = $errors;
@@ -440,41 +205,24 @@
440 205 $args['value'] = $value;
441 206 $args['errors'] = $errors;
442 207
443 208 $new_errors = $field_obj->validate( $args );
444 -
445 - if ( $new_errors ) {
209 + if ( ! empty( $new_errors ) ) {
446 210 $errors = array_merge( $errors, $new_errors );
447 211 }
448 212 }
449 213
450 - /**
451 - * @param array $errors
452 - * @param object $field
453 - * @param string $value
454 - * @param array $args
455 - *
456 - * @return void
457 - */
458 214 public static function validate_phone_field( &$errors, $field, $value, $args ) {
459 - $format_value = FrmField::get_option( $field, 'format' );
215 + if ( $field->type == 'phone' || ( $field->type == 'text' && FrmField::is_option_true_in_object( $field, 'format' ) ) ) {
460 216
461 - if ( $field->type !== 'phone' && ( $field->type !== 'text' || ! $format_value || FrmCurrencyHelper::is_currency_format( $format_value ) ) ) {
462 - return;
463 - }
217 + $pattern = self::phone_format( $field );
464 218
465 - $pattern = self::phone_format( $field );
466 -
467 - if ( ! preg_match( $pattern, $value ) ) {
468 - $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
219 + if ( ! preg_match( $pattern, $value ) ) {
220 + $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
221 + }
469 222 }
470 223 }
471 224
472 - /**
473 - * @param object $field
474 - *
475 - * @return string
476 - */
477 225 public static function phone_format( $field ) {
478 226 if ( FrmField::is_option_empty( $field, 'format' ) ) {
479 227 $pattern = self::default_phone_format();
480 228 } else {
@@ -480,25 +228,22 @@
480 228 } else {
481 229 $pattern = FrmField::get_option( $field, 'format' );
482 230 }
483 231
484 - // Ampersands are saved as &.
485 - // Reverse it here so we are checking for the correct character.
486 - $pattern = html_entity_decode( $pattern );
487 232 $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field );
488 233
489 234 // Create a regexp if format is not already a regexp
490 - if ( ! str_starts_with( $pattern, '^' ) ) {
235 + if ( strpos( $pattern, '^' ) !== 0 ) {
491 236 $pattern = self::create_regular_expression_from_format( $pattern );
492 237 }
493 238
494 - return '/' . $pattern . '/';
239 + $pattern = '/' . $pattern . '/';
240 +
241 + return $pattern;
495 242 }
496 243
497 244 /**
498 245 * @since 3.01
499 - *
500 - * @return string
501 246 */
502 247 private static function default_phone_format() {
503 248 return '^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$';
504 249 }
@@ -524,57 +269,44 @@
524 269 $pattern = str_replace( 'a', '[a-zA-Z]', $pattern );
525 270 $pattern = str_replace( '*', 'w', $pattern );
526 271 $pattern = str_replace( '/', '\/', $pattern );
527 272
528 - if ( str_contains( $pattern, '\?' ) ) {
273 + if ( strpos( $pattern, '\?' ) !== false ) {
529 274 $parts = explode( '\?', $pattern );
530 275 $pattern = '';
531 -
532 276 foreach ( $parts as $part ) {
533 - if ( $pattern ) {
277 + if ( empty( $pattern ) ) {
278 + $pattern .= $part;
279 + } else {
534 280 $pattern .= '(' . $part . ')?';
535 - } else {
536 - $pattern .= $part;
537 281 }
538 282 }
539 283 }
284 + $pattern = '^' . $pattern . '$';
540 285
541 - return '^' . $pattern . '$';
286 + return $pattern;
542 287 }
543 288
544 289 /**
545 - * Check for spam.
290 + * Check for spam
546 291 *
547 - * @param bool $exclude
292 + * @param boolean $exclude
548 293 * @param array $values
549 - * @param array $errors By reference.
550 - *
551 - * @return void
294 + * @param array $errors by reference
552 295 */
553 296 public static function spam_check( $exclude, $values, &$errors ) {
554 - if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
555 - // Do not check spam on importing.
297 + if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) {
298 + // only check spam if there are no other errors
556 299 return;
557 300 }
558 301
559 - if ( $exclude || empty( $values['item_meta'] ) || $errors ) {
560 - // Only check spam if there are no other errors
561 - return;
562 - }
563 -
564 302 $antispam_check = self::is_antispam_check( $values['form_id'] );
565 - $spam_msg = FrmAntiSpamController::get_default_spam_message();
566 -
567 303 if ( is_string( $antispam_check ) ) {
568 304 $errors['spam'] = $antispam_check;
569 305 } 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 - }
306 + $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' );
307 + } elseif ( self::blacklist_check( $values ) ) {
308 + $errors['spam'] = __( 'Your entry appears to be blocked spam!', 'formidable' );
577 309 }
578 310
579 311 if ( isset( $errors['spam'] ) || self::form_is_in_progress( $values ) ) {
580 312 return;
@@ -590,17 +322,14 @@
590 322 *
591 323 * @since 5.0.13
592 324 *
593 325 * @param array $values The values.
594 - *
595 326 * @return bool
596 327 */
597 328 private static function form_is_in_progress( $values ) {
598 - // phpcs:disable Generic.WhiteSpace.ScopeIndent
599 329 return FrmAppHelper::pro_is_installed() &&
600 330 ( isset( $values[ 'frm_page_order_' . $values['form_id'] ] ) || FrmAppHelper::get_post_param( 'frm_next_page' ) ) &&
601 331 FrmField::get_all_types_in_form( $values['form_id'], 'break' );
602 - // phpcs:enable Generic.WhiteSpace.ScopeIndent
603 332 }
604 333
605 334 /**
606 335 * @param int $form_id
@@ -613,10 +342,9 @@
613 342 }
614 343
615 344 /**
616 345 * @param array $values
617 - *
618 - * @return bool
346 + * @return boolean
619 347 */
620 348 private static function is_honeypot_spam( $values ) {
621 349 $honeypot = new FrmHoneypot( $values['form_id'] );
622 350 return ! $honeypot->validate();
@@ -622,51 +350,87 @@
622 350 return ! $honeypot->validate();
623 351 }
624 352
625 353 /**
626 - * @return bool
354 + * @return boolean
627 355 */
628 356 private static function is_spam_bot() {
629 - return ! FrmAppHelper::get_ip_address();
357 + $ip = FrmAppHelper::get_ip_address();
358 +
359 + return empty( $ip );
630 360 }
631 361
632 362 /**
633 363 * @param array $values
634 - *
635 - * @return bool
364 + * @return boolean
636 365 */
637 366 private static function is_akismet_spam( $values ) {
638 367 global $wpcom_api_key;
639 - return is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values );
368 +
369 + return ( is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values ) );
640 370 }
641 371
642 372 /**
643 373 * @param int $form_id
644 - *
645 374 * @return bool
646 375 */
647 376 private static function is_akismet_enabled_for_user( $form_id ) {
648 377 $form = FrmForm::getOne( $form_id );
649 - return ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() );
378 +
379 + return ( ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) );
650 380 }
651 381
382 + public static function blacklist_check( $values ) {
383 + if ( ! apply_filters( 'frm_check_blacklist', true, $values ) ) {
384 + return false;
385 + }
386 +
387 + $mod_keys = trim( self::get_disallowed_words() );
388 + if ( empty( $mod_keys ) ) {
389 + return false;
390 + }
391 +
392 + $content = FrmEntriesHelper::entry_array_to_string( $values );
393 +
394 + self::prepare_values_for_spam_check( $values );
395 + $ip = FrmAppHelper::get_ip_address();
396 + $user_agent = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
397 + $user_info = self::get_spam_check_user_info( $values );
398 +
399 + return self::check_disallowed_words( $user_info['comment_author'], $user_info['comment_author_email'], $user_info['comment_author_url'], $content, $ip, $user_agent );
400 + }
401 +
652 402 /**
653 - * Checks spam using WordPress disallowed words and Frm denylist.
403 + * For WP 5.5 compatibility.
654 404 *
655 - * @param array $values Entry values.
405 + * @since 4.06.02
406 + */
407 + private static function check_disallowed_words( $author, $email, $url, $content, $ip, $user_agent ) {
408 + if ( function_exists( 'wp_check_comment_disallowed_list' ) ) {
409 + return wp_check_comment_disallowed_list( $author, $email, $url, $content, $ip, $user_agent );
410 + } else {
411 + return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent );
412 + }
413 + }
414 +
415 + /**
416 + * For WP 5.5 compatibility.
656 417 *
657 - * @return bool
418 + * @since 4.06.02
658 419 */
659 - public static function blacklist_check( $values ) {
660 - return FrmAntiSpamController::contains_wp_disallowed_words( $values ) || FrmAntiSpamController::is_denylist_spam( $values );
420 + private static function get_disallowed_words() {
421 + $keys = get_option( 'disallowed_keys' );
422 + if ( false === $keys ) {
423 + // Fallback for WP < 5.5.
424 + $keys = get_option( 'blacklist_keys' );
425 + }
426 + return $keys;
661 427 }
662 428
663 429 /**
664 430 * Check entries for Akismet spam
665 431 *
666 - * @param array $values Entry values.
667 - *
668 - * @return bool true if is spam
432 + * @return boolean true if is spam
669 433 */
670 434 public static function akismet( $values ) {
671 435 if ( empty( $values['item_meta'] ) ) {
672 436 return false;
@@ -688,18 +452,13 @@
688 452
689 453 $query_string = _http_build_query( $datas, '', '&' );
690 454 $response = Akismet::http_post( $query_string, 'comment-check' );
691 455
692 - return is_array( $response ) && $response[1] === 'true';
456 + return ( is_array( $response ) && $response[1] == 'true' );
693 457 }
694 458
695 459 /**
696 460 * @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 461 */
703 462 private static function parse_akismet_array( &$datas, $values ) {
704 463 self::add_site_info_to_akismet( $datas );
705 464 self::add_server_values_to_akismet( $datas );
@@ -704,19 +463,13 @@
704 463 self::add_site_info_to_akismet( $datas );
705 464 self::add_server_values_to_akismet( $datas );
706 465
707 466 self::prepare_values_for_spam_check( $values );
708 - self::skip_adding_values_to_akismet( $values );
709 467
710 468 self::add_user_info_to_akismet( $datas, $values );
711 469 self::add_comment_content_to_akismet( $datas, $values );
712 470 }
713 471
714 - /**
715 - * @param array $datas
716 - *
717 - * @return void
718 - */
719 472 private static function add_site_info_to_akismet( &$datas ) {
720 473 $datas['blog'] = FrmAppHelper::site_url();
721 474 $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() );
722 475 $datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' );
@@ -728,14 +481,8 @@
728 481 $datas['is_test'] = 'true';
729 482 }
730 483 }
731 484
732 - /**
733 - * @param array $datas
734 - * @param array $values
735 - *
736 - * @return void
737 - */
738 485 private static function add_user_info_to_akismet( &$datas, $values ) {
739 486 $user_info = self::get_spam_check_user_info( $values );
740 487 $datas = $datas + $user_info;
741 488
@@ -747,15 +494,13 @@
747 494 /**
748 495 * Gets user info for Akismet spam check.
749 496 *
750 497 * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater.
751 - * @since 6.21 This changed from private to public.
752 498 *
753 499 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
754 - *
755 500 * @return array
756 501 */
757 - public static function get_spam_check_user_info( $values ) {
502 + private static function get_spam_check_user_info( $values ) {
758 503 if ( ! is_user_logged_in() ) {
759 504 return self::get_spam_check_user_info_for_guest( $values );
760 505 }
761 506
@@ -775,9 +520,8 @@
775 520 *
776 521 * @since 5.0.13
777 522 *
778 523 * @param array $values Entry values after flattened.
779 - *
780 524 * @return array
781 525 */
782 526 private static function get_spam_check_user_info_for_guest( $values ) {
783 527 $datas = array(
@@ -809,16 +553,13 @@
809 553 *
810 554 * @param array $datas Guest data.
811 555 * @param array $values The values.
812 556 * @param int|null $custom_index Custom index (or field ID).
813 - *
814 - * @return void
815 557 */
816 558 private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) {
817 559 foreach ( $values as $index => $value ) {
818 560 if ( ! $datas['missing_keys'] ) {
819 - // Found all info.
820 - return;
561 + return; // Found all info.
821 562 }
822 563
823 564 if ( is_array( $value ) ) {
824 565 self::recursive_add_akismet_guest_info( $datas, $value, $index );
@@ -825,21 +566,17 @@
825 566 continue;
826 567 }
827 568
828 569 $field_id = ! is_null( $custom_index ) ? $custom_index : $index;
829 -
830 570 foreach ( $datas['missing_keys'] as $key_index => $key ) {
831 - $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values );
832 -
833 - if ( ! $found ) {
834 - continue;
571 + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] );
572 + if ( $found ) {
573 + $datas[ $key ] = $value;
574 + $datas['frm_duplicated'][] = $field_id;
575 + unset( $datas['missing_keys'][ $key_index ] );
835 576 }
836 -
837 - $datas[ $key ] = $value;
838 - $datas['frm_duplicated'][] = $field_id;
839 - unset( $datas['missing_keys'][ $key_index ] );
840 577 }
841 - }//end foreach
578 + }
842 579 }
843 580
844 581 /**
845 582 * Checks if given value is an akismet guest info.
@@ -849,13 +586,11 @@
849 586 * @param string $key Guest info key.
850 587 * @param string $value Value to check.
851 588 * @param int $field_id Field ID.
852 589 * @param array $name_field_ids Name field IDs.
853 - * @param array $values Array of posted values.
854 - *
855 590 * @return bool
856 591 */
857 - private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) {
592 + private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) {
858 593 if ( ! $value || is_numeric( $value ) ) {
859 594 return false;
860 595 }
861 596
@@ -860,79 +595,24 @@
860 595 }
861 596
862 597 switch ( $key ) {
863 598 case 'comment_author_email':
864 - return str_contains( $value, '@' ) && is_email( $value );
599 + return strpos( $value, '@' ) && is_email( $value );
865 600
866 601 case 'comment_author_url':
867 - return str_starts_with( $value, 'http' );
602 + return 0 === strpos( $value, 'http' );
868 603
869 604 case 'comment_author':
870 - if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) {
605 + if ( $name_field_ids ) {
871 606 // If there is name field in the form, we should always use it as author name.
872 - return true;
607 + return in_array( $field_id, $name_field_ids, true );
873 608 }
609 + return strlen( $value ) < 200;
610 + }
874 611
875 - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
876 - $fields = self::get_name_text_fields( $form_id );
877 -
878 - foreach ( $fields as $index => $field ) {
879 - if ( 'Name' !== $field->name ) {
880 - continue;
881 - }
882 -
883 - if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) {
884 - if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) {
885 - continue;
886 - }
887 -
888 - $value .= ' ' . $values[ $fields[ $index + 1 ]->id ];
889 - return true;
890 - }
891 - }
892 - }//end switch
893 -
894 612 return false;
895 613 }
896 614
897 - /**
898 - * Returns fields that have 'Name' and 'Last' as their name.
899 - *
900 - * @since 6.17
901 - *
902 - * @param int $form_id
903 - *
904 - * @return array
905 - */
906 - private static function get_name_text_fields( $form_id ) {
907 - $name_text_fields_is_initialized = is_array( self::$name_text_fields );
908 -
909 - if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) {
910 - return self::$name_text_fields[ $form_id ];
911 - }
912 -
913 - if ( ! $name_text_fields_is_initialized ) {
914 - self::$name_text_fields = array();
915 - }
916 - self::$name_text_fields[ $form_id ] = FrmDb::get_results(
917 - 'frm_fields',
918 - array(
919 - 'form_id' => $form_id,
920 - 'type' => 'text',
921 - 'name' => array( 'Name', 'Last' ),
922 - ),
923 - 'id,name',
924 - array( 'order_by' => 'field_order ASC' )
925 - );
926 -
927 - return self::$name_text_fields[ $form_id ];
928 - }
929 -
930 - /**
931 - * @param array $datas
932 - *
933 - * @return void
934 - */
935 615 private static function add_server_values_to_akismet( &$datas ) {
936 616 foreach ( $_SERVER as $key => $value ) {
937 617 $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key );
938 618
@@ -950,10 +630,8 @@
950 630 * @since 5.0.09
951 631 *
952 632 * @param array $datas The array of values being sent to Akismet.
953 633 * @param array $values Entry values.
954 - *
955 - * @return void
956 634 */
957 635 private static function add_comment_content_to_akismet( &$datas, $values ) {
958 636 if ( isset( $datas['frm_duplicated'] ) ) {
959 637 foreach ( $datas['frm_duplicated'] as $index ) {
@@ -965,8 +643,10 @@
965 643 }
966 644 unset( $datas['frm_duplicated'] );
967 645 }
968 646
647 + self::skip_adding_values_to_akismet( $values );
648 +
969 649 $datas['comment_content'] = FrmEntriesHelper::entry_array_to_string( $values );
970 650 }
971 651
972 652 /**
@@ -974,28 +654,22 @@
974 654 *
975 655 * @since 5.0.09
976 656 *
977 657 * @param array $values Entry values.
978 - *
979 - * @return void
980 658 */
981 659 private static function skip_adding_values_to_akismet( &$values ) {
982 660 $skipped_fields = self::get_akismet_skipped_field_ids( $values );
983 -
984 661 foreach ( $skipped_fields as $skipped_field ) {
985 662 if ( ! isset( $values['item_meta'][ $skipped_field->id ] ) ) {
986 663 continue;
987 664 }
988 665
989 - if ( ! self::should_really_skip_field( $skipped_field, $values ) ) {
990 - continue;
666 + if ( self::should_really_skip_field( $skipped_field, $values ) ) {
667 + unset( $values['item_meta'][ $skipped_field->id ] );
668 + if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) {
669 + unset( $values['item_meta']['other'][ $skipped_field->id ] );
670 + }
991 671 }
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 672 }
999 673 }
1000 674
1001 675 /**
@@ -1004,28 +678,25 @@
1004 678 * @since 5.02.04
1005 679 *
1006 680 * @param object $field_data Object contains `id` and `options`.
1007 681 * @param array $values Entry values.
1008 - *
1009 682 * @return bool
1010 683 */
1011 684 private static function should_really_skip_field( $field_data, $values ) {
1012 - if ( empty( $field_data->options ) ) {
1013 - // This is skipped field types.
685 + if ( empty( $field_data->options ) ) { // This is skipped field types.
1014 686 return true;
1015 687 }
1016 688
1017 689 FrmAppHelper::unserialize_or_decode( $field_data->options );
1018 -
1019 - if ( ! $field_data->options ) {
1020 - // Check if an error happens when unserializing, or empty options.
690 + if ( ! $field_data->options ) { // Check if an error happens when unserializing, or empty options.
1021 691 return true;
1022 692 }
1023 693
1024 - $last_key = array_key_last( $field_data->options );
694 + end( $field_data->options );
695 + $last_key = key( $field_data->options );
1025 696
1026 697 // If a choice field has no Other option.
1027 - if ( is_numeric( $last_key ) || ! str_starts_with( $last_key, 'other_' ) ) {
698 + if ( is_numeric( $last_key ) || 0 !== strpos( $last_key, 'other_' ) ) {
1028 699 return true;
1029 700 }
1030 701
1031 702 // If a choice field has Other option, but Other is not selected.
@@ -1034,10 +705,9 @@
1034 705 }
1035 706
1036 707 // Check if submitted value is same as one of field option.
1037 708 foreach ( $field_data->options as $option ) {
1038 - $option_value = is_array( $option ) ? ( $option['value'] ?? '' ) : $option;
1039 -
709 + $option_value = ! is_array( $option ) ? $option : ( isset( $option['value'] ) ? $option['value'] : '' );
1040 710 if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) {
1041 711 return true;
1042 712 }
1043 713 }
@@ -1052,9 +722,8 @@
1052 722 * @since 5.0.13 Move out get_all_form_ids_and_flatten_meta() call and get `form_ids` from `$values`.
1053 723 * @since 5.2.04 This method returns array of object contains `id` and `options` instead of array of `id` only.
1054 724 *
1055 725 * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}.
1056 - *
1057 726 * @return array
1058 727 */
1059 728 private static function get_akismet_skipped_field_ids( $values ) {
1060 729 if ( empty( $values['form_ids'] ) ) {
@@ -1077,16 +746,14 @@
1077 746 /**
1078 747 * Prepares values array for spam check.
1079 748 *
1080 749 * @since 5.0.13
1081 - * @since 6.21 This changed from private to public.
1082 750 *
1083 751 * @param array $values Entry values.
1084 - *
1085 - * @return void
1086 752 */
1087 - public static function prepare_values_for_spam_check( &$values ) {
1088 - $values['form_ids'] = self::get_all_form_ids_and_flatten_meta( $values );
753 + private static function prepare_values_for_spam_check( &$values ) {
754 + $form_ids = self::get_all_form_ids_and_flatten_meta( $values );
755 + $values['form_ids'] = $form_ids;
1089 756 }
1090 757
1091 758 /**
1092 759 * Gets all form IDs (include child form IDs) and flatten item_meta array. Used for skipping values sent to Akismet.
@@ -1095,20 +762,17 @@
1095 762 * @since 5.0.09
1096 763 * @since 5.0.13 Convert name field value to string.
1097 764 *
1098 765 * @param array $values Entry values.
1099 - *
1100 766 * @return array Form IDs.
1101 767 */
1102 - private static function get_all_form_ids_and_flatten_meta( &$values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
768 + private static function get_all_form_ids_and_flatten_meta( &$values ) {
1103 769 $values['name_field_ids'] = array();
1104 770
1105 771 // Blacklist check for File field in the old version doesn't contain `form_id`.
1106 772 $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array();
1107 -
1108 773 foreach ( $values['item_meta'] as $field_id => $value ) {
1109 - if ( ! is_numeric( $field_id ) ) {
1110 - // Maybe `other`.
774 + if ( ! is_numeric( $field_id ) ) { // Maybe `other`.
1111 775 continue;
1112 776 }
1113 777
1114 778 // Convert name array to string.
@@ -1139,20 +803,51 @@
1139 803 }
1140 804
1141 805 // Convert name array to string.
1142 806 if ( isset( $subsubvalue['first'] ) && isset( $subsubvalue['last'] ) ) {
1143 - $subsubvalue = trim( implode( ' ', $subsubvalue ) );
807 + $subsubvalue = trim( implode( ' ', $subsubvalue ) );
808 +
1144 809 $values['name_field_ids'][] = $subsubindex;
1145 810 }
1146 811
1147 - if ( is_array( $values['item_meta'][ $subsubindex ] ) ) {
1148 - $values['item_meta'][ $subsubindex ][] = $subsubvalue;
1149 - }
812 + $values['item_meta'][ $subsubindex ][] = $subsubvalue;
1150 813 }
1151 - }//end foreach
814 + }
1152 815
1153 816 unset( $values['item_meta'][ $field_id ] );
1154 - }//end foreach
817 + }
1155 818
1156 819 return $form_ids;
820 + }
821 +
822 + /**
823 + * @deprecated 3.0
824 + * @codeCoverageIgnore
825 + */
826 + public static function validate_url_field( &$errors, $field, $value, $args ) {
827 + FrmDeprecated::validate_url_field( $errors, $field, $value, $args );
828 + }
829 +
830 + /**
831 + * @deprecated 3.0
832 + * @codeCoverageIgnore
833 + */
834 + public static function validate_email_field( &$errors, $field, $value, $args ) {
835 + FrmDeprecated::validate_email_field( $errors, $field, $value, $args );
836 + }
837 +
838 + /**
839 + * @deprecated 3.0
840 + * @codeCoverageIgnore
841 + */
842 + public static function validate_number_field( &$errors, $field, $value, $args ) {
843 + FrmDeprecated::validate_number_field( $errors, $field, $value, $args );
844 + }
845 +
846 + /**
847 + * @deprecated 3.0
848 + * @codeCoverageIgnore
849 + */
850 + public static function validate_recaptcha( &$errors, $field, $args ) {
851 + FrmDeprecated::validate_recaptcha( $errors, $field, $args );
1157 852 }
1158 853 }