| @@ -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,15 +83,9 @@ | ||
| 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 | 88 | if ( ! isset( $values['item_key'] ) || $values['item_key'] == '' ) { |
| 95 | 89 | global $wpdb; |
| 96 | 90 | $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' ); |
| 97 | 91 | $_POST['item_key'] = $values['item_key']; |
| @@ -97,14 +91,8 @@ | ||
| 97 | 91 | $_POST['item_key'] = $values['item_key']; |
| 98 | 92 | } |
| 99 | 93 | } |
| 100 | 94 | |
| 101 | - /** | |
| 102 | - * @param array $values | |
| 103 | - * @param array|string $exclude | |
| 104 | - * | |
| 105 | - * @return array | |
| 106 | - */ | |
| 107 | 95 | private static function get_fields_to_validate( $values, $exclude ) { |
| 108 | 96 | $where = apply_filters( 'frm_posted_field_ids', array( 'fi.form_id' => $values['form_id'] ) ); |
| 109 | 97 | |
| 110 | 98 | // Don't get subfields |
| @@ -110,9 +98,9 @@ | ||
| 110 | 98 | // Don't get subfields |
| 111 | 99 | $where['fr.parent_form_id'] = array( null, 0 ); |
| 112 | 100 | |
| 113 | 101 | // Don't get excluded fields (like file upload fields in the ajax validation) |
| 114 | - if ( $exclude ) { | |
| 102 | + if ( ! empty( $exclude ) ) { | |
| 115 | 103 | $where['fi.type not'] = $exclude; |
| 116 | 104 | } |
| 117 | 105 | |
| 118 | 106 | $fields = FrmField::getAll( $where, 'field_order' ); |
| @@ -127,16 +115,8 @@ | ||
| 127 | 115 | */ |
| 128 | 116 | return apply_filters( 'frm_fields_to_validate', $fields, compact( 'values', 'exclude', 'where' ) ); |
| 129 | 117 | } |
| 130 | 118 | |
| 131 | - /** | |
| 132 | - * @param object $posted_field | |
| 133 | - * @param array $errors | |
| 134 | - * @param array $values | |
| 135 | - * @param array $args | |
| 136 | - * | |
| 137 | - * @return void | |
| 138 | - */ | |
| 139 | 119 | public static function validate_field( $posted_field, &$errors, $values, $args = array() ) { |
| 140 | 120 | $defaults = array( |
| 141 | 121 | 'id' => $posted_field->id, |
| 142 | 122 | // The id of the repeat or embed form. |
| @@ -146,18 +126,23 @@ | ||
| 146 | 126 | // Exclude these field types from validation. |
| 147 | 127 | 'exclude' => array(), |
| 148 | 128 | |
| 149 | 129 | ); |
| 150 | - $args = wp_parse_args( $args, $defaults ); | |
| 151 | - $value = empty( $args['parent_field_id'] ) ? ( $values['item_meta'][ $args['id'] ] ?? '' ) : $values; | |
| 130 | + $args = wp_parse_args( $args, $defaults ); | |
| 152 | 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 | + | |
| 153 | 139 | // Check for values in "Other" fields |
| 154 | 140 | FrmEntriesHelper::maybe_set_other_validation( $posted_field, $value, $args ); |
| 155 | 141 | |
| 156 | 142 | self::maybe_clear_value_for_default_blank_setting( $posted_field, $value ); |
| 157 | 143 | |
| 158 | - $should_trim = is_array( $value ) && count( $value ) === 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox'; | |
| 159 | - | |
| 144 | + $should_trim = is_array( $value ) && count( $value ) == 1 && isset( $value[0] ) && $posted_field->type !== 'checkbox'; | |
| 160 | 145 | if ( $should_trim ) { |
| 161 | 146 | $value = reset( $value ); |
| 162 | 147 | } |
| 163 | 148 | |
| @@ -164,9 +149,8 @@ | ||
| 164 | 149 | if ( ! is_array( $value ) ) { |
| 165 | 150 | $value = trim( $value ); |
| 166 | 151 | } |
| 167 | 152 | |
| 168 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 169 | 153 | if ( $posted_field->required == '1' && FrmAppHelper::is_empty_value( $value ) ) { |
| 170 | 154 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'blank' ); |
| 171 | 155 | } elseif ( ! isset( $_POST['item_name'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 172 | 156 | self::maybe_add_item_name( $value, $posted_field ); |
| @@ -180,9 +164,8 @@ | ||
| 180 | 164 | // Field might want to modify value before other parts of the system |
| 181 | 165 | // e.g. trim off excess values like in the case of fields with limit. |
| 182 | 166 | $value = apply_filters( 'frm_modify_posted_field_value', $value, $errors, $posted_field, $args ); |
| 183 | 167 | |
| 184 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 185 | 168 | if ( $value != '' ) { |
| 186 | 169 | self::validate_phone_field( $errors, $posted_field, $value, $args ); |
| 187 | 170 | } |
| 188 | 171 | |
| @@ -232,22 +215,15 @@ | ||
| 232 | 215 | * |
| 233 | 216 | * @param stdClass $field |
| 234 | 217 | * @param array|string $value |
| 235 | 218 | * @param array $options |
| 236 | - * | |
| 237 | 219 | * @return bool |
| 238 | 220 | */ |
| 239 | - 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 ) { | |
| 240 | 222 | if ( '' === $value ) { |
| 241 | 223 | return true; |
| 242 | 224 | } |
| 243 | 225 | |
| 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 | 226 | if ( in_array( $field->type, array( 'likert', 'ranking' ), true ) ) { |
| 251 | 227 | // Ignore these field types automatically. |
| 252 | 228 | return true; |
| 253 | 229 | } |
| @@ -265,9 +241,9 @@ | ||
| 265 | 241 | foreach ( $value as $current_value ) { |
| 266 | 242 | $match = false; |
| 267 | 243 | |
| 268 | 244 | foreach ( $options as $key => $option ) { |
| 269 | - if ( str_starts_with( $key, 'other_' ) ) { | |
| 245 | + if ( strpos( $key, 'other_' ) === 0 ) { | |
| 270 | 246 | // Always return true if an other option is found. |
| 271 | 247 | return true; |
| 272 | 248 | } |
| 273 | 249 | |
| @@ -278,21 +254,18 @@ | ||
| 278 | 254 | $option_value = $option; |
| 279 | 255 | } |
| 280 | 256 | |
| 281 | 257 | $match = trim( $current_value ) === trim( $option_value ); |
| 282 | - | |
| 283 | 258 | if ( $match ) { |
| 284 | 259 | break; |
| 285 | 260 | } |
| 286 | 261 | |
| 287 | 262 | $match = trim( $current_value ) === trim( do_shortcode( $option_value ) ); |
| 288 | - | |
| 289 | 263 | if ( $match ) { |
| 290 | 264 | break; |
| 291 | 265 | } |
| 292 | 266 | |
| 293 | 267 | $match = self::is_filtered_match( $current_value, $option_value ); |
| 294 | - | |
| 295 | 268 | if ( $match ) { |
| 296 | 269 | break; |
| 297 | 270 | } |
| 298 | 271 | |
| @@ -297,9 +270,8 @@ | ||
| 297 | 270 | } |
| 298 | 271 | |
| 299 | 272 | if ( is_numeric( $current_value ) ) { |
| 300 | 273 | $match = (int) $current_value === (int) $option_value; |
| 301 | - | |
| 302 | 274 | if ( $match ) { |
| 303 | 275 | break; |
| 304 | 276 | } |
| 305 | 277 | } |
| @@ -321,25 +293,20 @@ | ||
| 321 | 293 | * @since 6.22 |
| 322 | 294 | * |
| 323 | 295 | * @param string $value |
| 324 | 296 | * @param string $option_value |
| 325 | - * | |
| 326 | 297 | * @return bool |
| 327 | 298 | */ |
| 328 | 299 | private static function is_filtered_match( $value, $option_value ) { |
| 329 | 300 | // First remove the wpautop filter so it doesn't add extra tags to $option_value. |
| 330 | 301 | $filter_priority = has_filter( 'the_content', 'wpautop' ); |
| 331 | - | |
| 332 | 302 | if ( is_numeric( $filter_priority ) ) { |
| 333 | 303 | remove_filter( 'the_content', 'wpautop', $filter_priority ); |
| 334 | 304 | } |
| 335 | - | |
| 336 | 305 | $filtered_option = apply_filters( 'the_content', $option_value ); |
| 337 | - | |
| 338 | 306 | if ( is_numeric( $filter_priority ) ) { |
| 339 | 307 | add_filter( 'the_content', 'wpautop', $filter_priority ); |
| 340 | 308 | } |
| 341 | - | |
| 342 | 309 | return trim( $value ) === trim( $filtered_option ); |
| 343 | 310 | } |
| 344 | 311 | |
| 345 | 312 | /** |
| @@ -347,11 +314,8 @@ | ||
| 347 | 314 | * This is to help avoid issues where the options could be based on a URL param for example. |
| 348 | 315 | * |
| 349 | 316 | * @since 6.21 |
| 350 | 317 | * |
| 351 | - * @param object $field_object The field object. | |
| 352 | - * @param array|string $value The value to validate. | |
| 353 | - * | |
| 354 | 318 | * @return bool |
| 355 | 319 | */ |
| 356 | 320 | private static function options_are_dynamic_based_on_hook( $field_object, $value ) { |
| 357 | 321 | $values = (array) $field_object; |
| @@ -364,9 +328,10 @@ | ||
| 364 | 328 | $option_value = $separate_value ? $option['value'] : $option['label']; |
| 365 | 329 | } else { |
| 366 | 330 | $option_value = $option; |
| 367 | 331 | } |
| 368 | - return do_shortcode( $option_value ); | |
| 332 | + $option_value = do_shortcode( $option_value ); | |
| 333 | + return $option_value; | |
| 369 | 334 | }; |
| 370 | 335 | |
| 371 | 336 | $values_options = array_map( $map_callback, $values['options'] ); |
| 372 | 337 | $field_object_options = array_map( $map_callback, $field_object->options ); |
| @@ -380,14 +345,11 @@ | ||
| 380 | 345 | * @since 5.2.02 |
| 381 | 346 | * |
| 382 | 347 | * @param array|string $value Field value. |
| 383 | 348 | * @param object $field Field object. |
| 384 | - * | |
| 385 | - * @return void | |
| 386 | 349 | */ |
| 387 | 350 | private static function maybe_add_item_name( $value, $field ) { |
| 388 | 351 | $item_name = false; |
| 389 | - | |
| 390 | 352 | if ( 'name' === $field->type ) { |
| 391 | 353 | $field_obj = FrmFieldFactory::get_field_object( $field ); |
| 392 | 354 | $item_name = $field_obj->get_display_value( $value ); |
| 393 | 355 | } elseif ( 'text' === $field->type ) { |
| @@ -404,14 +366,11 @@ | ||
| 404 | 366 | * Set $value to an empty string if it matches its label |
| 405 | 367 | * |
| 406 | 368 | * @param object $field |
| 407 | 369 | * @param string $value |
| 408 | - * | |
| 409 | - * @return void | |
| 410 | 370 | */ |
| 411 | 371 | private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) { |
| 412 | 372 | $position = FrmField::get_option( $field, 'label' ); |
| 413 | - | |
| 414 | 373 | if ( ! $position ) { |
| 415 | 374 | $position = FrmStylesController::get_style_val( 'position', $field->form_id ); |
| 416 | 375 | } |
| 417 | 376 | |
| @@ -419,16 +378,8 @@ | ||
| 419 | 378 | $value = ''; |
| 420 | 379 | } |
| 421 | 380 | } |
| 422 | 381 | |
| 423 | - /** | |
| 424 | - * @param array $errors | |
| 425 | - * @param object $posted_field | |
| 426 | - * @param mixed $value | |
| 427 | - * @param array $args | |
| 428 | - * | |
| 429 | - * @return void | |
| 430 | - */ | |
| 431 | 382 | public static function validate_field_types( &$errors, $posted_field, $value, $args ) { |
| 432 | 383 | $field_obj = FrmFieldFactory::get_field_object( $posted_field ); |
| 433 | 384 | $args['value'] = $value; |
| 434 | 385 | $args['errors'] = $errors; |
| @@ -433,22 +384,13 @@ | ||
| 433 | 384 | $args['value'] = $value; |
| 434 | 385 | $args['errors'] = $errors; |
| 435 | 386 | |
| 436 | 387 | $new_errors = $field_obj->validate( $args ); |
| 437 | - | |
| 438 | - if ( $new_errors ) { | |
| 388 | + if ( ! empty( $new_errors ) ) { | |
| 439 | 389 | $errors = array_merge( $errors, $new_errors ); |
| 440 | 390 | } |
| 441 | 391 | } |
| 442 | 392 | |
| 443 | - /** | |
| 444 | - * @param array $errors | |
| 445 | - * @param object $field | |
| 446 | - * @param string $value | |
| 447 | - * @param array $args | |
| 448 | - * | |
| 449 | - * @return void | |
| 450 | - */ | |
| 451 | 393 | public static function validate_phone_field( &$errors, $field, $value, $args ) { |
| 452 | 394 | $format_value = FrmField::get_option( $field, 'format' ); |
| 453 | 395 | |
| 454 | 396 | if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) { |
| @@ -459,13 +401,8 @@ | ||
| 459 | 401 | } |
| 460 | 402 | } |
| 461 | 403 | } |
| 462 | 404 | |
| 463 | - /** | |
| 464 | - * @param object $field | |
| 465 | - * | |
| 466 | - * @return string | |
| 467 | - */ | |
| 468 | 405 | public static function phone_format( $field ) { |
| 469 | 406 | if ( FrmField::is_option_empty( $field, 'format' ) ) { |
| 470 | 407 | $pattern = self::default_phone_format(); |
| 471 | 408 | } else { |
| @@ -477,19 +414,19 @@ | ||
| 477 | 414 | $pattern = html_entity_decode( $pattern ); |
| 478 | 415 | $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field ); |
| 479 | 416 | |
| 480 | 417 | // Create a regexp if format is not already a regexp |
| 481 | - if ( ! str_starts_with( $pattern, '^' ) ) { | |
| 418 | + if ( strpos( $pattern, '^' ) !== 0 ) { | |
| 482 | 419 | $pattern = self::create_regular_expression_from_format( $pattern ); |
| 483 | 420 | } |
| 484 | 421 | |
| 485 | - return '/' . $pattern . '/'; | |
| 422 | + $pattern = '/' . $pattern . '/'; | |
| 423 | + | |
| 424 | + return $pattern; | |
| 486 | 425 | } |
| 487 | 426 | |
| 488 | 427 | /** |
| 489 | 428 | * @since 3.01 |
| 490 | - * | |
| 491 | - * @return string | |
| 492 | 429 | */ |
| 493 | 430 | private static function default_phone_format() { |
| 494 | 431 | return '^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$'; |
| 495 | 432 | } |
| @@ -515,14 +452,13 @@ | ||
| 515 | 452 | $pattern = str_replace( 'a', '[a-zA-Z]', $pattern ); |
| 516 | 453 | $pattern = str_replace( '*', 'w', $pattern ); |
| 517 | 454 | $pattern = str_replace( '/', '\/', $pattern ); |
| 518 | 455 | |
| 519 | - if ( str_contains( $pattern, '\?' ) ) { | |
| 456 | + if ( strpos( $pattern, '\?' ) !== false ) { | |
| 520 | 457 | $parts = explode( '\?', $pattern ); |
| 521 | 458 | $pattern = ''; |
| 522 | - | |
| 523 | 459 | foreach ( $parts as $part ) { |
| 524 | - if ( ! $pattern ) { | |
| 460 | + if ( empty( $pattern ) ) { | |
| 525 | 461 | $pattern .= $part; |
| 526 | 462 | } else { |
| 527 | 463 | $pattern .= '(' . $part . ')?'; |
| 528 | 464 | } |
| @@ -527,10 +463,11 @@ | ||
| 527 | 463 | $pattern .= '(' . $part . ')?'; |
| 528 | 464 | } |
| 529 | 465 | } |
| 530 | 466 | } |
| 467 | + $pattern = '^' . $pattern . '$'; | |
| 531 | 468 | |
| 532 | - return '^' . $pattern . '$'; | |
| 469 | + return $pattern; | |
| 533 | 470 | } |
| 534 | 471 | |
| 535 | 472 | /** |
| 536 | 473 | * Check for spam. |
| @@ -537,10 +474,8 @@ | ||
| 537 | 474 | * |
| 538 | 475 | * @param bool $exclude |
| 539 | 476 | * @param array $values |
| 540 | 477 | * @param array $errors By reference. |
| 541 | - * | |
| 542 | - * @return void | |
| 543 | 478 | */ |
| 544 | 479 | public static function spam_check( $exclude, $values, &$errors ) { |
| 545 | 480 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 546 | 481 | // Do not check spam on importing. |
| @@ -546,9 +481,9 @@ | ||
| 546 | 481 | // Do not check spam on importing. |
| 547 | 482 | return; |
| 548 | 483 | } |
| 549 | 484 | |
| 550 | - if ( $exclude || empty( $values['item_meta'] ) || ! empty( $errors ) ) { | |
| 485 | + if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { | |
| 551 | 486 | // only check spam if there are no other errors |
| 552 | 487 | return; |
| 553 | 488 | } |
| 554 | 489 | |
| @@ -553,9 +488,8 @@ | ||
| 553 | 488 | } |
| 554 | 489 | |
| 555 | 490 | $antispam_check = self::is_antispam_check( $values['form_id'] ); |
| 556 | 491 | $spam_msg = FrmAntiSpamController::get_default_spam_message(); |
| 557 | - | |
| 558 | 492 | if ( is_string( $antispam_check ) ) { |
| 559 | 493 | $errors['spam'] = $antispam_check; |
| 560 | 494 | } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) { |
| 561 | 495 | $errors['spam'] = $spam_msg; |
| @@ -560,9 +494,8 @@ | ||
| 560 | 494 | } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) { |
| 561 | 495 | $errors['spam'] = $spam_msg; |
| 562 | 496 | } else { |
| 563 | 497 | $is_spam = FrmAntiSpamController::is_spam( $values ); |
| 564 | - | |
| 565 | 498 | if ( $is_spam ) { |
| 566 | 499 | $errors['spam'] = $is_spam; |
| 567 | 500 | } |
| 568 | 501 | } |
| @@ -581,9 +514,8 @@ | ||
| 581 | 514 | * |
| 582 | 515 | * @since 5.0.13 |
| 583 | 516 | * |
| 584 | 517 | * @param array $values The values. |
| 585 | - * | |
| 586 | 518 | * @return bool |
| 587 | 519 | */ |
| 588 | 520 | private static function form_is_in_progress( $values ) { |
| 589 | 521 | return FrmAppHelper::pro_is_installed() && |
| @@ -602,9 +534,8 @@ | ||
| 602 | 534 | } |
| 603 | 535 | |
| 604 | 536 | /** |
| 605 | 537 | * @param array $values |
| 606 | - * | |
| 607 | 538 | * @return bool |
| 608 | 539 | */ |
| 609 | 540 | private static function is_honeypot_spam( $values ) { |
| 610 | 541 | $honeypot = new FrmHoneypot( $values['form_id'] ); |
| @@ -615,29 +546,30 @@ | ||
| 615 | 546 | * @return bool |
| 616 | 547 | */ |
| 617 | 548 | private static function is_spam_bot() { |
| 618 | 549 | $ip = FrmAppHelper::get_ip_address(); |
| 550 | + | |
| 619 | 551 | return empty( $ip ); |
| 620 | 552 | } |
| 621 | 553 | |
| 622 | 554 | /** |
| 623 | 555 | * @param array $values |
| 624 | - * | |
| 625 | 556 | * @return bool |
| 626 | 557 | */ |
| 627 | 558 | private static function is_akismet_spam( $values ) { |
| 628 | 559 | global $wpcom_api_key; |
| 629 | - 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 ) ); | |
| 630 | 562 | } |
| 631 | 563 | |
| 632 | 564 | /** |
| 633 | 565 | * @param int $form_id |
| 634 | - * | |
| 635 | 566 | * @return bool |
| 636 | 567 | */ |
| 637 | 568 | private static function is_akismet_enabled_for_user( $form_id ) { |
| 638 | 569 | $form = FrmForm::getOne( $form_id ); |
| 639 | - 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() ) ); | |
| 640 | 572 | } |
| 641 | 573 | |
| 642 | 574 | /** |
| 643 | 575 | * Checks spam using WordPress disallowed words and Frm denylist. |
| @@ -652,10 +584,8 @@ | ||
| 652 | 584 | |
| 653 | 585 | /** |
| 654 | 586 | * Check entries for Akismet spam |
| 655 | 587 | * |
| 656 | - * @param array $values Entry values. | |
| 657 | - * | |
| 658 | 588 | * @return bool true if is spam |
| 659 | 589 | */ |
| 660 | 590 | public static function akismet( $values ) { |
| 661 | 591 | if ( empty( $values['item_meta'] ) ) { |
| @@ -678,18 +608,13 @@ | ||
| 678 | 608 | |
| 679 | 609 | $query_string = _http_build_query( $datas, '', '&' ); |
| 680 | 610 | $response = Akismet::http_post( $query_string, 'comment-check' ); |
| 681 | 611 | |
| 682 | - return is_array( $response ) && $response[1] === 'true'; | |
| 612 | + return ( is_array( $response ) && $response[1] === 'true' ); | |
| 683 | 613 | } |
| 684 | 614 | |
| 685 | 615 | /** |
| 686 | 616 | * @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 | 617 | */ |
| 693 | 618 | private static function parse_akismet_array( &$datas, $values ) { |
| 694 | 619 | self::add_site_info_to_akismet( $datas ); |
| 695 | 620 | self::add_server_values_to_akismet( $datas ); |
| @@ -700,13 +625,8 @@ | ||
| 700 | 625 | self::add_user_info_to_akismet( $datas, $values ); |
| 701 | 626 | self::add_comment_content_to_akismet( $datas, $values ); |
| 702 | 627 | } |
| 703 | 628 | |
| 704 | - /** | |
| 705 | - * @param array $datas | |
| 706 | - * | |
| 707 | - * @return void | |
| 708 | - */ | |
| 709 | 629 | private static function add_site_info_to_akismet( &$datas ) { |
| 710 | 630 | $datas['blog'] = FrmAppHelper::site_url(); |
| 711 | 631 | $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() ); |
| 712 | 632 | $datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ); |
| @@ -718,14 +638,8 @@ | ||
| 718 | 638 | $datas['is_test'] = 'true'; |
| 719 | 639 | } |
| 720 | 640 | } |
| 721 | 641 | |
| 722 | - /** | |
| 723 | - * @param array $datas | |
| 724 | - * @param array $values | |
| 725 | - * | |
| 726 | - * @return void | |
| 727 | - */ | |
| 728 | 642 | private static function add_user_info_to_akismet( &$datas, $values ) { |
| 729 | 643 | $user_info = self::get_spam_check_user_info( $values ); |
| 730 | 644 | $datas = $datas + $user_info; |
| 731 | 645 | |
| @@ -740,9 +654,8 @@ | ||
| 740 | 654 | * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater. |
| 741 | 655 | * @since 6.21 This changed from private to public. |
| 742 | 656 | * |
| 743 | 657 | * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}. |
| 744 | - * | |
| 745 | 658 | * @return array |
| 746 | 659 | */ |
| 747 | 660 | public static function get_spam_check_user_info( $values ) { |
| 748 | 661 | if ( ! is_user_logged_in() ) { |
| @@ -765,9 +678,8 @@ | ||
| 765 | 678 | * |
| 766 | 679 | * @since 5.0.13 |
| 767 | 680 | * |
| 768 | 681 | * @param array $values Entry values after flattened. |
| 769 | - * | |
| 770 | 682 | * @return array |
| 771 | 683 | */ |
| 772 | 684 | private static function get_spam_check_user_info_for_guest( $values ) { |
| 773 | 685 | $datas = array( |
| @@ -799,10 +711,8 @@ | ||
| 799 | 711 | * |
| 800 | 712 | * @param array $datas Guest data. |
| 801 | 713 | * @param array $values The values. |
| 802 | 714 | * @param int|null $custom_index Custom index (or field ID). |
| 803 | - * | |
| 804 | - * @return void | |
| 805 | 715 | */ |
| 806 | 716 | private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) { |
| 807 | 717 | foreach ( $values as $index => $value ) { |
| 808 | 718 | if ( ! $datas['missing_keys'] ) { |
| @@ -815,12 +725,10 @@ | ||
| 815 | 725 | continue; |
| 816 | 726 | } |
| 817 | 727 | |
| 818 | 728 | $field_id = ! is_null( $custom_index ) ? $custom_index : $index; |
| 819 | - | |
| 820 | 729 | foreach ( $datas['missing_keys'] as $key_index => $key ) { |
| 821 | 730 | $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values ); |
| 822 | - | |
| 823 | 731 | if ( $found ) { |
| 824 | 732 | $datas[ $key ] = $value; |
| 825 | 733 | $datas['frm_duplicated'][] = $field_id; |
| 826 | 734 | unset( $datas['missing_keys'][ $key_index ] ); |
| @@ -848,12 +756,12 @@ | ||
| 848 | 756 | } |
| 849 | 757 | |
| 850 | 758 | switch ( $key ) { |
| 851 | 759 | case 'comment_author_email': |
| 852 | - return str_contains( $value, '@' ) && is_email( $value ); | |
| 760 | + return strpos( $value, '@' ) && is_email( $value ); | |
| 853 | 761 | |
| 854 | 762 | case 'comment_author_url': |
| 855 | - return str_starts_with( $value, 'http' ); | |
| 763 | + return 0 === strpos( $value, 'http' ); | |
| 856 | 764 | |
| 857 | 765 | case 'comment_author': |
| 858 | 766 | if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { |
| 859 | 767 | // If there is name field in the form, we should always use it as author name. |
| @@ -858,9 +766,8 @@ | ||
| 858 | 766 | if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { |
| 859 | 767 | // If there is name field in the form, we should always use it as author name. |
| 860 | 768 | return true; |
| 861 | 769 | } |
| 862 | - | |
| 863 | 770 | $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); |
| 864 | 771 | $fields = self::get_name_text_fields( $form_id ); |
| 865 | 772 | |
| 866 | 773 | foreach ( $fields as $index => $field ) { |
| @@ -866,14 +773,12 @@ | ||
| 866 | 773 | foreach ( $fields as $index => $field ) { |
| 867 | 774 | if ( 'Name' !== $field->name ) { |
| 868 | 775 | continue; |
| 869 | 776 | } |
| 870 | - | |
| 871 | 777 | if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { |
| 872 | 778 | if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) { |
| 873 | 779 | continue; |
| 874 | 780 | } |
| 875 | - | |
| 876 | 781 | $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; |
| 877 | 782 | return true; |
| 878 | 783 | } |
| 879 | 784 | } |
| @@ -887,18 +792,15 @@ | ||
| 887 | 792 | * |
| 888 | 793 | * @since 6.17 |
| 889 | 794 | * |
| 890 | 795 | * @param int $form_id |
| 891 | - * | |
| 892 | 796 | * @return array |
| 893 | 797 | */ |
| 894 | 798 | private static function get_name_text_fields( $form_id ) { |
| 895 | 799 | $name_text_fields_is_initialized = is_array( self::$name_text_fields ); |
| 896 | - | |
| 897 | 800 | if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) { |
| 898 | 801 | return self::$name_text_fields[ $form_id ]; |
| 899 | 802 | } |
| 900 | - | |
| 901 | 803 | if ( ! $name_text_fields_is_initialized ) { |
| 902 | 804 | self::$name_text_fields = array(); |
| 903 | 805 | } |
| 904 | 806 | self::$name_text_fields[ $form_id ] = FrmDb::get_results( |
| @@ -914,13 +816,8 @@ | ||
| 914 | 816 | |
| 915 | 817 | return self::$name_text_fields[ $form_id ]; |
| 916 | 818 | } |
| 917 | 819 | |
| 918 | - /** | |
| 919 | - * @param array $datas | |
| 920 | - * | |
| 921 | - * @return void | |
| 922 | - */ | |
| 923 | 820 | private static function add_server_values_to_akismet( &$datas ) { |
| 924 | 821 | foreach ( $_SERVER as $key => $value ) { |
| 925 | 822 | $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key ); |
| 926 | 823 | |
| @@ -938,10 +835,8 @@ | ||
| 938 | 835 | * @since 5.0.09 |
| 939 | 836 | * |
| 940 | 837 | * @param array $datas The array of values being sent to Akismet. |
| 941 | 838 | * @param array $values Entry values. |
| 942 | - * | |
| 943 | - * @return void | |
| 944 | 839 | */ |
| 945 | 840 | private static function add_comment_content_to_akismet( &$datas, $values ) { |
| 946 | 841 | if ( isset( $datas['frm_duplicated'] ) ) { |
| 947 | 842 | foreach ( $datas['frm_duplicated'] as $index ) { |
| @@ -962,14 +857,11 @@ | ||
| 962 | 857 | * |
| 963 | 858 | * @since 5.0.09 |
| 964 | 859 | * |
| 965 | 860 | * @param array $values Entry values. |
| 966 | - * | |
| 967 | - * @return void | |
| 968 | 861 | */ |
| 969 | 862 | private static function skip_adding_values_to_akismet( &$values ) { |
| 970 | 863 | $skipped_fields = self::get_akismet_skipped_field_ids( $values ); |
| 971 | - | |
| 972 | 864 | foreach ( $skipped_fields as $skipped_field ) { |
| 973 | 865 | if ( ! isset( $values['item_meta'][ $skipped_field->id ] ) ) { |
| 974 | 866 | continue; |
| 975 | 867 | } |
| @@ -975,9 +867,8 @@ | ||
| 975 | 867 | } |
| 976 | 868 | |
| 977 | 869 | if ( self::should_really_skip_field( $skipped_field, $values ) ) { |
| 978 | 870 | unset( $values['item_meta'][ $skipped_field->id ] ); |
| 979 | - | |
| 980 | 871 | if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) { |
| 981 | 872 | unset( $values['item_meta']['other'][ $skipped_field->id ] ); |
| 982 | 873 | } |
| 983 | 874 | } |
| @@ -990,9 +881,8 @@ | ||
| 990 | 881 | * @since 5.02.04 |
| 991 | 882 | * |
| 992 | 883 | * @param object $field_data Object contains `id` and `options`. |
| 993 | 884 | * @param array $values Entry values. |
| 994 | - * | |
| 995 | 885 | * @return bool |
| 996 | 886 | */ |
| 997 | 887 | private static function should_really_skip_field( $field_data, $values ) { |
| 998 | 888 | if ( empty( $field_data->options ) ) { |
| @@ -1000,18 +890,18 @@ | ||
| 1000 | 890 | return true; |
| 1001 | 891 | } |
| 1002 | 892 | |
| 1003 | 893 | FrmAppHelper::unserialize_or_decode( $field_data->options ); |
| 1004 | - | |
| 1005 | 894 | if ( ! $field_data->options ) { |
| 1006 | 895 | // Check if an error happens when unserializing, or empty options. |
| 1007 | 896 | return true; |
| 1008 | 897 | } |
| 1009 | 898 | |
| 1010 | - $last_key = array_key_last( $field_data->options ); | |
| 899 | + end( $field_data->options ); | |
| 900 | + $last_key = key( $field_data->options ); | |
| 1011 | 901 | |
| 1012 | 902 | // If a choice field has no Other option. |
| 1013 | - if ( is_numeric( $last_key ) || ! str_starts_with( $last_key, 'other_' ) ) { | |
| 903 | + if ( is_numeric( $last_key ) || 0 !== strpos( $last_key, 'other_' ) ) { | |
| 1014 | 904 | return true; |
| 1015 | 905 | } |
| 1016 | 906 | |
| 1017 | 907 | // If a choice field has Other option, but Other is not selected. |
| @@ -1020,10 +910,9 @@ | ||
| 1020 | 910 | } |
| 1021 | 911 | |
| 1022 | 912 | // Check if submitted value is same as one of field option. |
| 1023 | 913 | foreach ( $field_data->options as $option ) { |
| 1024 | - $option_value = ! is_array( $option ) ? $option : ( $option['value'] ?? '' ); | |
| 1025 | - | |
| 914 | + $option_value = ! is_array( $option ) ? $option : ( isset( $option['value'] ) ? $option['value'] : '' ); | |
| 1026 | 915 | if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) { |
| 1027 | 916 | return true; |
| 1028 | 917 | } |
| 1029 | 918 | } |
| @@ -1038,9 +927,8 @@ | ||
| 1038 | 927 | * @since 5.0.13 Move out get_all_form_ids_and_flatten_meta() call and get `form_ids` from `$values`. |
| 1039 | 928 | * @since 5.2.04 This method returns array of object contains `id` and `options` instead of array of `id` only. |
| 1040 | 929 | * |
| 1041 | 930 | * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}. |
| 1042 | - * | |
| 1043 | 931 | * @return array |
| 1044 | 932 | */ |
| 1045 | 933 | private static function get_akismet_skipped_field_ids( $values ) { |
| 1046 | 934 | if ( empty( $values['form_ids'] ) ) { |
| @@ -1066,10 +954,8 @@ | ||
| 1066 | 954 | * @since 5.0.13 |
| 1067 | 955 | * @since 6.21 This changed from private to public. |
| 1068 | 956 | * |
| 1069 | 957 | * @param array $values Entry values. |
| 1070 | - * | |
| 1071 | - * @return void | |
| 1072 | 958 | */ |
| 1073 | 959 | public static function prepare_values_for_spam_check( &$values ) { |
| 1074 | 960 | $form_ids = self::get_all_form_ids_and_flatten_meta( $values ); |
| 1075 | 961 | $values['form_ids'] = $form_ids; |
| @@ -1082,17 +968,15 @@ | ||
| 1082 | 968 | * @since 5.0.09 |
| 1083 | 969 | * @since 5.0.13 Convert name field value to string. |
| 1084 | 970 | * |
| 1085 | 971 | * @param array $values Entry values. |
| 1086 | - * | |
| 1087 | 972 | * @return array Form IDs. |
| 1088 | 973 | */ |
| 1089 | - 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 ) { | |
| 1090 | 975 | $values['name_field_ids'] = array(); |
| 1091 | 976 | |
| 1092 | 977 | // Blacklist check for File field in the old version doesn't contain `form_id`. |
| 1093 | 978 | $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array(); |
| 1094 | - | |
| 1095 | 979 | foreach ( $values['item_meta'] as $field_id => $value ) { |
| 1096 | 980 | if ( ! is_numeric( $field_id ) ) { |
| 1097 | 981 | // Maybe `other`. |
| 1098 | 982 | continue; |
| @@ -1126,9 +1010,10 @@ | ||
| 1126 | 1010 | } |
| 1127 | 1011 | |
| 1128 | 1012 | // Convert name array to string. |
| 1129 | 1013 | if ( isset( $subsubvalue['first'] ) && isset( $subsubvalue['last'] ) ) { |
| 1130 | - $subsubvalue = trim( implode( ' ', $subsubvalue ) ); | |
| 1014 | + $subsubvalue = trim( implode( ' ', $subsubvalue ) ); | |
| 1015 | + | |
| 1131 | 1016 | $values['name_field_ids'][] = $subsubindex; |
| 1132 | 1017 | } |
| 1133 | 1018 | |
| 1134 | 1019 | if ( is_array( $values['item_meta'][ $subsubindex ] ) ) { |