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