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