| @@ -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,18 +215,16 @@ | ||
| 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 | 226 | $field_object = FrmFieldFactory::get_field_type( $field->type, $field ); |
| 247 | - | |
| 248 | 227 | if ( ! $field_object->field_type_has_options_settings() ) { |
| 249 | 228 | return true; |
| 250 | 229 | } |
| 251 | 230 | |
| @@ -267,9 +246,9 @@ | ||
| 267 | 246 | foreach ( $value as $current_value ) { |
| 268 | 247 | $match = false; |
| 269 | 248 | |
| 270 | 249 | foreach ( $options as $key => $option ) { |
| 271 | - if ( str_starts_with( $key, 'other_' ) ) { | |
| 250 | + if ( strpos( $key, 'other_' ) === 0 ) { | |
| 272 | 251 | // Always return true if an other option is found. |
| 273 | 252 | return true; |
| 274 | 253 | } |
| 275 | 254 | |
| @@ -279,38 +258,29 @@ | ||
| 279 | 258 | } else { |
| 280 | 259 | $option_value = $option; |
| 281 | 260 | } |
| 282 | 261 | |
| 283 | - /** | |
| 284 | - * @var string $current_value | |
| 285 | - */ | |
| 286 | 262 | $match = trim( $current_value ) === trim( $option_value ); |
| 287 | - | |
| 288 | 263 | if ( $match ) { |
| 289 | 264 | break; |
| 290 | 265 | } |
| 291 | 266 | |
| 292 | 267 | $match = trim( $current_value ) === trim( do_shortcode( $option_value ) ); |
| 293 | - | |
| 294 | 268 | if ( $match ) { |
| 295 | 269 | break; |
| 296 | 270 | } |
| 297 | 271 | |
| 298 | 272 | $match = self::is_filtered_match( $current_value, $option_value ); |
| 299 | - | |
| 300 | 273 | if ( $match ) { |
| 301 | 274 | break; |
| 302 | 275 | } |
| 303 | 276 | |
| 304 | - if ( ! is_numeric( $current_value ) ) { | |
| 305 | - continue; | |
| 277 | + if ( is_numeric( $current_value ) ) { | |
| 278 | + $match = (int) $current_value === (int) $option_value; | |
| 279 | + if ( $match ) { | |
| 280 | + break; | |
| 281 | + } | |
| 306 | 282 | } |
| 307 | - | |
| 308 | - $match = (int) $current_value === (int) $option_value; | |
| 309 | - | |
| 310 | - if ( $match ) { | |
| 311 | - break; | |
| 312 | - } | |
| 313 | 283 | }//end foreach |
| 314 | 284 | |
| 315 | 285 | if ( ! $match ) { |
| 316 | 286 | return self::options_are_dynamic_based_on_hook( $field, $value ); |
| @@ -328,25 +298,20 @@ | ||
| 328 | 298 | * @since 6.22 |
| 329 | 299 | * |
| 330 | 300 | * @param string $value |
| 331 | 301 | * @param string $option_value |
| 332 | - * | |
| 333 | 302 | * @return bool |
| 334 | 303 | */ |
| 335 | 304 | private static function is_filtered_match( $value, $option_value ) { |
| 336 | 305 | // First remove the wpautop filter so it doesn't add extra tags to $option_value. |
| 337 | 306 | $filter_priority = has_filter( 'the_content', 'wpautop' ); |
| 338 | - | |
| 339 | 307 | if ( is_numeric( $filter_priority ) ) { |
| 340 | 308 | remove_filter( 'the_content', 'wpautop', $filter_priority ); |
| 341 | 309 | } |
| 342 | - | |
| 343 | 310 | $filtered_option = apply_filters( 'the_content', $option_value ); |
| 344 | - | |
| 345 | 311 | if ( is_numeric( $filter_priority ) ) { |
| 346 | 312 | add_filter( 'the_content', 'wpautop', $filter_priority ); |
| 347 | 313 | } |
| 348 | - | |
| 349 | 314 | return trim( $value ) === trim( $filtered_option ); |
| 350 | 315 | } |
| 351 | 316 | |
| 352 | 317 | /** |
| @@ -354,11 +319,8 @@ | ||
| 354 | 319 | * This is to help avoid issues where the options could be based on a URL param for example. |
| 355 | 320 | * |
| 356 | 321 | * @since 6.21 |
| 357 | 322 | * |
| 358 | - * @param object $field_object The field object. | |
| 359 | - * @param array|string $value The value to validate. | |
| 360 | - * | |
| 361 | 323 | * @return bool |
| 362 | 324 | */ |
| 363 | 325 | private static function options_are_dynamic_based_on_hook( $field_object, $value ) { |
| 364 | 326 | $values = (array) $field_object; |
| @@ -371,9 +333,10 @@ | ||
| 371 | 333 | $option_value = $separate_value ? $option['value'] : $option['label']; |
| 372 | 334 | } else { |
| 373 | 335 | $option_value = $option; |
| 374 | 336 | } |
| 375 | - return do_shortcode( $option_value ); | |
| 337 | + $option_value = do_shortcode( $option_value ); | |
| 338 | + return $option_value; | |
| 376 | 339 | }; |
| 377 | 340 | |
| 378 | 341 | $values_options = array_map( $map_callback, $values['options'] ); |
| 379 | 342 | $field_object_options = array_map( $map_callback, $field_object->options ); |
| @@ -387,14 +350,11 @@ | ||
| 387 | 350 | * @since 5.2.02 |
| 388 | 351 | * |
| 389 | 352 | * @param array|string $value Field value. |
| 390 | 353 | * @param object $field Field object. |
| 391 | - * | |
| 392 | - * @return void | |
| 393 | 354 | */ |
| 394 | 355 | private static function maybe_add_item_name( $value, $field ) { |
| 395 | 356 | $item_name = false; |
| 396 | - | |
| 397 | 357 | if ( 'name' === $field->type ) { |
| 398 | 358 | $field_obj = FrmFieldFactory::get_field_object( $field ); |
| 399 | 359 | $item_name = $field_obj->get_display_value( $value ); |
| 400 | 360 | } elseif ( 'text' === $field->type ) { |
| @@ -402,9 +362,9 @@ | ||
| 402 | 362 | } |
| 403 | 363 | |
| 404 | 364 | if ( false !== $item_name ) { |
| 405 | 365 | // 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 ); | |
| 366 | + $_POST['item_name'] = substr( $item_name, 0, 255 ); | |
| 407 | 367 | } |
| 408 | 368 | } |
| 409 | 369 | |
| 410 | 370 | /** |
| @@ -411,14 +371,11 @@ | ||
| 411 | 371 | * Set $value to an empty string if it matches its label |
| 412 | 372 | * |
| 413 | 373 | * @param object $field |
| 414 | 374 | * @param string $value |
| 415 | - * | |
| 416 | - * @return void | |
| 417 | 375 | */ |
| 418 | 376 | private static function maybe_clear_value_for_default_blank_setting( $field, &$value ) { |
| 419 | 377 | $position = FrmField::get_option( $field, 'label' ); |
| 420 | - | |
| 421 | 378 | if ( ! $position ) { |
| 422 | 379 | $position = FrmStylesController::get_style_val( 'position', $field->form_id ); |
| 423 | 380 | } |
| 424 | 381 | |
| @@ -426,16 +383,8 @@ | ||
| 426 | 383 | $value = ''; |
| 427 | 384 | } |
| 428 | 385 | } |
| 429 | 386 | |
| 430 | - /** | |
| 431 | - * @param array $errors | |
| 432 | - * @param object $posted_field | |
| 433 | - * @param mixed $value | |
| 434 | - * @param array $args | |
| 435 | - * | |
| 436 | - * @return void | |
| 437 | - */ | |
| 438 | 387 | public static function validate_field_types( &$errors, $posted_field, $value, $args ) { |
| 439 | 388 | $field_obj = FrmFieldFactory::get_field_object( $posted_field ); |
| 440 | 389 | $args['value'] = $value; |
| 441 | 390 | $args['errors'] = $errors; |
| @@ -440,41 +389,25 @@ | ||
| 440 | 389 | $args['value'] = $value; |
| 441 | 390 | $args['errors'] = $errors; |
| 442 | 391 | |
| 443 | 392 | $new_errors = $field_obj->validate( $args ); |
| 444 | - | |
| 445 | - if ( $new_errors ) { | |
| 393 | + if ( ! empty( $new_errors ) ) { | |
| 446 | 394 | $errors = array_merge( $errors, $new_errors ); |
| 447 | 395 | } |
| 448 | 396 | } |
| 449 | 397 | |
| 450 | - /** | |
| 451 | - * @param array $errors | |
| 452 | - * @param object $field | |
| 453 | - * @param string $value | |
| 454 | - * @param array $args | |
| 455 | - * | |
| 456 | - * @return void | |
| 457 | - */ | |
| 458 | 398 | public static function validate_phone_field( &$errors, $field, $value, $args ) { |
| 459 | 399 | $format_value = FrmField::get_option( $field, 'format' ); |
| 460 | 400 | |
| 461 | - if ( $field->type !== 'phone' && ( $field->type !== 'text' || ! $format_value || FrmCurrencyHelper::is_currency_format( $format_value ) ) ) { | |
| 462 | - return; | |
| 463 | - } | |
| 401 | + if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) { | |
| 402 | + $pattern = self::phone_format( $field ); | |
| 464 | 403 | |
| 465 | - $pattern = self::phone_format( $field ); | |
| 466 | - | |
| 467 | - if ( ! preg_match( $pattern, $value ) ) { | |
| 468 | - $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); | |
| 404 | + if ( ! preg_match( $pattern, $value ) ) { | |
| 405 | + $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); | |
| 406 | + } | |
| 469 | 407 | } |
| 470 | 408 | } |
| 471 | 409 | |
| 472 | - /** | |
| 473 | - * @param object $field | |
| 474 | - * | |
| 475 | - * @return string | |
| 476 | - */ | |
| 477 | 410 | public static function phone_format( $field ) { |
| 478 | 411 | if ( FrmField::is_option_empty( $field, 'format' ) ) { |
| 479 | 412 | $pattern = self::default_phone_format(); |
| 480 | 413 | } else { |
| @@ -486,19 +419,19 @@ | ||
| 486 | 419 | $pattern = html_entity_decode( $pattern ); |
| 487 | 420 | $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field ); |
| 488 | 421 | |
| 489 | 422 | // Create a regexp if format is not already a regexp |
| 490 | - if ( ! str_starts_with( $pattern, '^' ) ) { | |
| 423 | + if ( strpos( $pattern, '^' ) !== 0 ) { | |
| 491 | 424 | $pattern = self::create_regular_expression_from_format( $pattern ); |
| 492 | 425 | } |
| 493 | 426 | |
| 494 | - return '/' . $pattern . '/'; | |
| 427 | + $pattern = '/' . $pattern . '/'; | |
| 428 | + | |
| 429 | + return $pattern; | |
| 495 | 430 | } |
| 496 | 431 | |
| 497 | 432 | /** |
| 498 | 433 | * @since 3.01 |
| 499 | - * | |
| 500 | - * @return string | |
| 501 | 434 | */ |
| 502 | 435 | private static function default_phone_format() { |
| 503 | 436 | return '^((\+\d{1,3}(-|.| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-|.| )?(\d{3,4})(-|.| )?(\d{4})(( x| ext)\d{1,5}){0,1}$'; |
| 504 | 437 | } |
| @@ -524,22 +457,22 @@ | ||
| 524 | 457 | $pattern = str_replace( 'a', '[a-zA-Z]', $pattern ); |
| 525 | 458 | $pattern = str_replace( '*', 'w', $pattern ); |
| 526 | 459 | $pattern = str_replace( '/', '\/', $pattern ); |
| 527 | 460 | |
| 528 | - if ( str_contains( $pattern, '\?' ) ) { | |
| 461 | + if ( strpos( $pattern, '\?' ) !== false ) { | |
| 529 | 462 | $parts = explode( '\?', $pattern ); |
| 530 | 463 | $pattern = ''; |
| 531 | - | |
| 532 | 464 | foreach ( $parts as $part ) { |
| 533 | - if ( $pattern ) { | |
| 465 | + if ( empty( $pattern ) ) { | |
| 466 | + $pattern .= $part; | |
| 467 | + } else { | |
| 534 | 468 | $pattern .= '(' . $part . ')?'; |
| 535 | - } else { | |
| 536 | - $pattern .= $part; | |
| 537 | 469 | } |
| 538 | 470 | } |
| 539 | 471 | } |
| 472 | + $pattern = '^' . $pattern . '$'; | |
| 540 | 473 | |
| 541 | - return '^' . $pattern . '$'; | |
| 474 | + return $pattern; | |
| 542 | 475 | } |
| 543 | 476 | |
| 544 | 477 | /** |
| 545 | 478 | * Check for spam. |
| @@ -546,10 +479,8 @@ | ||
| 546 | 479 | * |
| 547 | 480 | * @param bool $exclude |
| 548 | 481 | * @param array $values |
| 549 | 482 | * @param array $errors By reference. |
| 550 | - * | |
| 551 | - * @return void | |
| 552 | 483 | */ |
| 553 | 484 | public static function spam_check( $exclude, $values, &$errors ) { |
| 554 | 485 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 555 | 486 | // Do not check spam on importing. |
| @@ -555,16 +486,15 @@ | ||
| 555 | 486 | // Do not check spam on importing. |
| 556 | 487 | return; |
| 557 | 488 | } |
| 558 | 489 | |
| 559 | - if ( $exclude || empty( $values['item_meta'] ) || $errors ) { | |
| 560 | - // Only check spam if there are no other errors | |
| 490 | + if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { | |
| 491 | + // only check spam if there are no other errors | |
| 561 | 492 | return; |
| 562 | 493 | } |
| 563 | 494 | |
| 564 | 495 | $antispam_check = self::is_antispam_check( $values['form_id'] ); |
| 565 | 496 | $spam_msg = FrmAntiSpamController::get_default_spam_message(); |
| 566 | - | |
| 567 | 497 | if ( is_string( $antispam_check ) ) { |
| 568 | 498 | $errors['spam'] = $antispam_check; |
| 569 | 499 | } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) { |
| 570 | 500 | $errors['spam'] = $spam_msg; |
| @@ -569,9 +499,8 @@ | ||
| 569 | 499 | } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) { |
| 570 | 500 | $errors['spam'] = $spam_msg; |
| 571 | 501 | } else { |
| 572 | 502 | $is_spam = FrmAntiSpamController::is_spam( $values ); |
| 573 | - | |
| 574 | 503 | if ( $is_spam ) { |
| 575 | 504 | $errors['spam'] = $is_spam; |
| 576 | 505 | } |
| 577 | 506 | } |
| @@ -590,17 +519,14 @@ | ||
| 590 | 519 | * |
| 591 | 520 | * @since 5.0.13 |
| 592 | 521 | * |
| 593 | 522 | * @param array $values The values. |
| 594 | - * | |
| 595 | 523 | * @return bool |
| 596 | 524 | */ |
| 597 | 525 | private static function form_is_in_progress( $values ) { |
| 598 | - // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 599 | 526 | return FrmAppHelper::pro_is_installed() && |
| 600 | 527 | ( isset( $values[ 'frm_page_order_' . $values['form_id'] ] ) || FrmAppHelper::get_post_param( 'frm_next_page' ) ) && |
| 601 | 528 | FrmField::get_all_types_in_form( $values['form_id'], 'break' ); |
| 602 | - // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 603 | 529 | } |
| 604 | 530 | |
| 605 | 531 | /** |
| 606 | 532 | * @param int $form_id |
| @@ -613,9 +539,8 @@ | ||
| 613 | 539 | } |
| 614 | 540 | |
| 615 | 541 | /** |
| 616 | 542 | * @param array $values |
| 617 | - * | |
| 618 | 543 | * @return bool |
| 619 | 544 | */ |
| 620 | 545 | private static function is_honeypot_spam( $values ) { |
| 621 | 546 | $honeypot = new FrmHoneypot( $values['form_id'] ); |
| @@ -625,29 +550,31 @@ | ||
| 625 | 550 | /** |
| 626 | 551 | * @return bool |
| 627 | 552 | */ |
| 628 | 553 | private static function is_spam_bot() { |
| 629 | - return ! FrmAppHelper::get_ip_address(); | |
| 554 | + $ip = FrmAppHelper::get_ip_address(); | |
| 555 | + | |
| 556 | + return empty( $ip ); | |
| 630 | 557 | } |
| 631 | 558 | |
| 632 | 559 | /** |
| 633 | 560 | * @param array $values |
| 634 | - * | |
| 635 | 561 | * @return bool |
| 636 | 562 | */ |
| 637 | 563 | private static function is_akismet_spam( $values ) { |
| 638 | 564 | global $wpcom_api_key; |
| 639 | - return is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values ); | |
| 565 | + | |
| 566 | + return ( is_callable( 'Akismet::http_post' ) && ( get_option( 'wordpress_api_key' ) || $wpcom_api_key ) && self::akismet( $values ) ); | |
| 640 | 567 | } |
| 641 | 568 | |
| 642 | 569 | /** |
| 643 | 570 | * @param int $form_id |
| 644 | - * | |
| 645 | 571 | * @return bool |
| 646 | 572 | */ |
| 647 | 573 | private static function is_akismet_enabled_for_user( $form_id ) { |
| 648 | 574 | $form = FrmForm::getOne( $form_id ); |
| 649 | - return ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ); | |
| 575 | + | |
| 576 | + return ( ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) ); | |
| 650 | 577 | } |
| 651 | 578 | |
| 652 | 579 | /** |
| 653 | 580 | * Checks spam using WordPress disallowed words and Frm denylist. |
| @@ -662,10 +589,8 @@ | ||
| 662 | 589 | |
| 663 | 590 | /** |
| 664 | 591 | * Check entries for Akismet spam |
| 665 | 592 | * |
| 666 | - * @param array $values Entry values. | |
| 667 | - * | |
| 668 | 593 | * @return bool true if is spam |
| 669 | 594 | */ |
| 670 | 595 | public static function akismet( $values ) { |
| 671 | 596 | if ( empty( $values['item_meta'] ) ) { |
| @@ -688,18 +613,13 @@ | ||
| 688 | 613 | |
| 689 | 614 | $query_string = _http_build_query( $datas, '', '&' ); |
| 690 | 615 | $response = Akismet::http_post( $query_string, 'comment-check' ); |
| 691 | 616 | |
| 692 | - return is_array( $response ) && $response[1] === 'true'; | |
| 617 | + return ( is_array( $response ) && $response[1] === 'true' ); | |
| 693 | 618 | } |
| 694 | 619 | |
| 695 | 620 | /** |
| 696 | 621 | * @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 | 622 | */ |
| 703 | 623 | private static function parse_akismet_array( &$datas, $values ) { |
| 704 | 624 | self::add_site_info_to_akismet( $datas ); |
| 705 | 625 | self::add_server_values_to_akismet( $datas ); |
| @@ -710,13 +630,8 @@ | ||
| 710 | 630 | self::add_user_info_to_akismet( $datas, $values ); |
| 711 | 631 | self::add_comment_content_to_akismet( $datas, $values ); |
| 712 | 632 | } |
| 713 | 633 | |
| 714 | - /** | |
| 715 | - * @param array $datas | |
| 716 | - * | |
| 717 | - * @return void | |
| 718 | - */ | |
| 719 | 634 | private static function add_site_info_to_akismet( &$datas ) { |
| 720 | 635 | $datas['blog'] = FrmAppHelper::site_url(); |
| 721 | 636 | $datas['user_ip'] = preg_replace( '/[^0-9., ]/', '', FrmAppHelper::get_ip_address() ); |
| 722 | 637 | $datas['user_agent'] = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ); |
| @@ -728,14 +643,8 @@ | ||
| 728 | 643 | $datas['is_test'] = 'true'; |
| 729 | 644 | } |
| 730 | 645 | } |
| 731 | 646 | |
| 732 | - /** | |
| 733 | - * @param array $datas | |
| 734 | - * @param array $values | |
| 735 | - * | |
| 736 | - * @return void | |
| 737 | - */ | |
| 738 | 647 | private static function add_user_info_to_akismet( &$datas, $values ) { |
| 739 | 648 | $user_info = self::get_spam_check_user_info( $values ); |
| 740 | 649 | $datas = $datas + $user_info; |
| 741 | 650 | |
| @@ -750,9 +659,8 @@ | ||
| 750 | 659 | * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater. |
| 751 | 660 | * @since 6.21 This changed from private to public. |
| 752 | 661 | * |
| 753 | 662 | * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}. |
| 754 | - * | |
| 755 | 663 | * @return array |
| 756 | 664 | */ |
| 757 | 665 | public static function get_spam_check_user_info( $values ) { |
| 758 | 666 | if ( ! is_user_logged_in() ) { |
| @@ -775,9 +683,8 @@ | ||
| 775 | 683 | * |
| 776 | 684 | * @since 5.0.13 |
| 777 | 685 | * |
| 778 | 686 | * @param array $values Entry values after flattened. |
| 779 | - * | |
| 780 | 687 | * @return array |
| 781 | 688 | */ |
| 782 | 689 | private static function get_spam_check_user_info_for_guest( $values ) { |
| 783 | 690 | $datas = array( |
| @@ -809,10 +716,8 @@ | ||
| 809 | 716 | * |
| 810 | 717 | * @param array $datas Guest data. |
| 811 | 718 | * @param array $values The values. |
| 812 | 719 | * @param int|null $custom_index Custom index (or field ID). |
| 813 | - * | |
| 814 | - * @return void | |
| 815 | 720 | */ |
| 816 | 721 | private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) { |
| 817 | 722 | foreach ( $values as $index => $value ) { |
| 818 | 723 | if ( ! $datas['missing_keys'] ) { |
| @@ -825,19 +730,15 @@ | ||
| 825 | 730 | continue; |
| 826 | 731 | } |
| 827 | 732 | |
| 828 | 733 | $field_id = ! is_null( $custom_index ) ? $custom_index : $index; |
| 829 | - | |
| 830 | 734 | foreach ( $datas['missing_keys'] as $key_index => $key ) { |
| 831 | 735 | $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values ); |
| 832 | - | |
| 833 | - if ( ! $found ) { | |
| 834 | - continue; | |
| 736 | + if ( $found ) { | |
| 737 | + $datas[ $key ] = $value; | |
| 738 | + $datas['frm_duplicated'][] = $field_id; | |
| 739 | + unset( $datas['missing_keys'][ $key_index ] ); | |
| 835 | 740 | } |
| 836 | - | |
| 837 | - $datas[ $key ] = $value; | |
| 838 | - $datas['frm_duplicated'][] = $field_id; | |
| 839 | - unset( $datas['missing_keys'][ $key_index ] ); | |
| 840 | 741 | } |
| 841 | 742 | }//end foreach |
| 842 | 743 | } |
| 843 | 744 | |
| @@ -860,12 +761,12 @@ | ||
| 860 | 761 | } |
| 861 | 762 | |
| 862 | 763 | switch ( $key ) { |
| 863 | 764 | case 'comment_author_email': |
| 864 | - return str_contains( $value, '@' ) && is_email( $value ); | |
| 765 | + return strpos( $value, '@' ) && is_email( $value ); | |
| 865 | 766 | |
| 866 | 767 | case 'comment_author_url': |
| 867 | - return str_starts_with( $value, 'http' ); | |
| 768 | + return 0 === strpos( $value, 'http' ); | |
| 868 | 769 | |
| 869 | 770 | case 'comment_author': |
| 870 | 771 | if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { |
| 871 | 772 | // If there is name field in the form, we should always use it as author name. |
| @@ -870,9 +771,8 @@ | ||
| 870 | 771 | if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { |
| 871 | 772 | // If there is name field in the form, we should always use it as author name. |
| 872 | 773 | return true; |
| 873 | 774 | } |
| 874 | - | |
| 875 | 775 | $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); |
| 876 | 776 | $fields = self::get_name_text_fields( $form_id ); |
| 877 | 777 | |
| 878 | 778 | foreach ( $fields as $index => $field ) { |
| @@ -878,14 +778,12 @@ | ||
| 878 | 778 | foreach ( $fields as $index => $field ) { |
| 879 | 779 | if ( 'Name' !== $field->name ) { |
| 880 | 780 | continue; |
| 881 | 781 | } |
| 882 | - | |
| 883 | 782 | if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { |
| 884 | 783 | if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) { |
| 885 | 784 | continue; |
| 886 | 785 | } |
| 887 | - | |
| 888 | 786 | $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; |
| 889 | 787 | return true; |
| 890 | 788 | } |
| 891 | 789 | } |
| @@ -899,18 +797,15 @@ | ||
| 899 | 797 | * |
| 900 | 798 | * @since 6.17 |
| 901 | 799 | * |
| 902 | 800 | * @param int $form_id |
| 903 | - * | |
| 904 | 801 | * @return array |
| 905 | 802 | */ |
| 906 | 803 | private static function get_name_text_fields( $form_id ) { |
| 907 | 804 | $name_text_fields_is_initialized = is_array( self::$name_text_fields ); |
| 908 | - | |
| 909 | 805 | if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) { |
| 910 | 806 | return self::$name_text_fields[ $form_id ]; |
| 911 | 807 | } |
| 912 | - | |
| 913 | 808 | if ( ! $name_text_fields_is_initialized ) { |
| 914 | 809 | self::$name_text_fields = array(); |
| 915 | 810 | } |
| 916 | 811 | self::$name_text_fields[ $form_id ] = FrmDb::get_results( |
| @@ -926,13 +821,8 @@ | ||
| 926 | 821 | |
| 927 | 822 | return self::$name_text_fields[ $form_id ]; |
| 928 | 823 | } |
| 929 | 824 | |
| 930 | - /** | |
| 931 | - * @param array $datas | |
| 932 | - * | |
| 933 | - * @return void | |
| 934 | - */ | |
| 935 | 825 | private static function add_server_values_to_akismet( &$datas ) { |
| 936 | 826 | foreach ( $_SERVER as $key => $value ) { |
| 937 | 827 | $include_value = is_string( $value ) && ! preg_match( '/^HTTP_COOKIE/', $key ) && preg_match( '/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/', $key ); |
| 938 | 828 | |
| @@ -950,10 +840,8 @@ | ||
| 950 | 840 | * @since 5.0.09 |
| 951 | 841 | * |
| 952 | 842 | * @param array $datas The array of values being sent to Akismet. |
| 953 | 843 | * @param array $values Entry values. |
| 954 | - * | |
| 955 | - * @return void | |
| 956 | 844 | */ |
| 957 | 845 | private static function add_comment_content_to_akismet( &$datas, $values ) { |
| 958 | 846 | if ( isset( $datas['frm_duplicated'] ) ) { |
| 959 | 847 | foreach ( $datas['frm_duplicated'] as $index ) { |
| @@ -974,28 +862,22 @@ | ||
| 974 | 862 | * |
| 975 | 863 | * @since 5.0.09 |
| 976 | 864 | * |
| 977 | 865 | * @param array $values Entry values. |
| 978 | - * | |
| 979 | - * @return void | |
| 980 | 866 | */ |
| 981 | 867 | private static function skip_adding_values_to_akismet( &$values ) { |
| 982 | 868 | $skipped_fields = self::get_akismet_skipped_field_ids( $values ); |
| 983 | - | |
| 984 | 869 | foreach ( $skipped_fields as $skipped_field ) { |
| 985 | 870 | if ( ! isset( $values['item_meta'][ $skipped_field->id ] ) ) { |
| 986 | 871 | continue; |
| 987 | 872 | } |
| 988 | 873 | |
| 989 | - if ( ! self::should_really_skip_field( $skipped_field, $values ) ) { | |
| 990 | - continue; | |
| 874 | + if ( self::should_really_skip_field( $skipped_field, $values ) ) { | |
| 875 | + unset( $values['item_meta'][ $skipped_field->id ] ); | |
| 876 | + if ( isset( $values['item_meta']['other'][ $skipped_field->id ] ) ) { | |
| 877 | + unset( $values['item_meta']['other'][ $skipped_field->id ] ); | |
| 878 | + } | |
| 991 | 879 | } |
| 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 | 880 | } |
| 999 | 881 | } |
| 1000 | 882 | |
| 1001 | 883 | /** |
| @@ -1004,9 +886,8 @@ | ||
| 1004 | 886 | * @since 5.02.04 |
| 1005 | 887 | * |
| 1006 | 888 | * @param object $field_data Object contains `id` and `options`. |
| 1007 | 889 | * @param array $values Entry values. |
| 1008 | - * | |
| 1009 | 890 | * @return bool |
| 1010 | 891 | */ |
| 1011 | 892 | private static function should_really_skip_field( $field_data, $values ) { |
| 1012 | 893 | if ( empty( $field_data->options ) ) { |
| @@ -1014,18 +895,18 @@ | ||
| 1014 | 895 | return true; |
| 1015 | 896 | } |
| 1016 | 897 | |
| 1017 | 898 | FrmAppHelper::unserialize_or_decode( $field_data->options ); |
| 1018 | - | |
| 1019 | 899 | if ( ! $field_data->options ) { |
| 1020 | 900 | // Check if an error happens when unserializing, or empty options. |
| 1021 | 901 | return true; |
| 1022 | 902 | } |
| 1023 | 903 | |
| 1024 | - $last_key = array_key_last( $field_data->options ); | |
| 904 | + end( $field_data->options ); | |
| 905 | + $last_key = key( $field_data->options ); | |
| 1025 | 906 | |
| 1026 | 907 | // If a choice field has no Other option. |
| 1027 | - if ( is_numeric( $last_key ) || ! str_starts_with( $last_key, 'other_' ) ) { | |
| 908 | + if ( is_numeric( $last_key ) || 0 !== strpos( $last_key, 'other_' ) ) { | |
| 1028 | 909 | return true; |
| 1029 | 910 | } |
| 1030 | 911 | |
| 1031 | 912 | // If a choice field has Other option, but Other is not selected. |
| @@ -1034,10 +915,9 @@ | ||
| 1034 | 915 | } |
| 1035 | 916 | |
| 1036 | 917 | // Check if submitted value is same as one of field option. |
| 1037 | 918 | foreach ( $field_data->options as $option ) { |
| 1038 | - $option_value = is_array( $option ) ? ( $option['value'] ?? '' ) : $option; | |
| 1039 | - | |
| 919 | + $option_value = ! is_array( $option ) ? $option : ( isset( $option['value'] ) ? $option['value'] : '' ); | |
| 1040 | 920 | if ( $values['item_meta']['other'][ $field_data->id ] === $option_value ) { |
| 1041 | 921 | return true; |
| 1042 | 922 | } |
| 1043 | 923 | } |
| @@ -1052,9 +932,8 @@ | ||
| 1052 | 932 | * @since 5.0.13 Move out get_all_form_ids_and_flatten_meta() call and get `form_ids` from `$values`. |
| 1053 | 933 | * @since 5.2.04 This method returns array of object contains `id` and `options` instead of array of `id` only. |
| 1054 | 934 | * |
| 1055 | 935 | * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}. |
| 1056 | - * | |
| 1057 | 936 | * @return array |
| 1058 | 937 | */ |
| 1059 | 938 | private static function get_akismet_skipped_field_ids( $values ) { |
| 1060 | 939 | if ( empty( $values['form_ids'] ) ) { |
| @@ -1080,13 +959,12 @@ | ||
| 1080 | 959 | * @since 5.0.13 |
| 1081 | 960 | * @since 6.21 This changed from private to public. |
| 1082 | 961 | * |
| 1083 | 962 | * @param array $values Entry values. |
| 1084 | - * | |
| 1085 | - * @return void | |
| 1086 | 963 | */ |
| 1087 | 964 | public static function prepare_values_for_spam_check( &$values ) { |
| 1088 | - $values['form_ids'] = self::get_all_form_ids_and_flatten_meta( $values ); | |
| 965 | + $form_ids = self::get_all_form_ids_and_flatten_meta( $values ); | |
| 966 | + $values['form_ids'] = $form_ids; | |
| 1089 | 967 | } |
| 1090 | 968 | |
| 1091 | 969 | /** |
| 1092 | 970 | * Gets all form IDs (include child form IDs) and flatten item_meta array. Used for skipping values sent to Akismet. |
| @@ -1095,17 +973,15 @@ | ||
| 1095 | 973 | * @since 5.0.09 |
| 1096 | 974 | * @since 5.0.13 Convert name field value to string. |
| 1097 | 975 | * |
| 1098 | 976 | * @param array $values Entry values. |
| 1099 | - * | |
| 1100 | 977 | * @return array Form IDs. |
| 1101 | 978 | */ |
| 1102 | - private static function get_all_form_ids_and_flatten_meta( &$values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh | |
| 979 | + private static function get_all_form_ids_and_flatten_meta( &$values ) { | |
| 1103 | 980 | $values['name_field_ids'] = array(); |
| 1104 | 981 | |
| 1105 | 982 | // Blacklist check for File field in the old version doesn't contain `form_id`. |
| 1106 | 983 | $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array(); |
| 1107 | - | |
| 1108 | 984 | foreach ( $values['item_meta'] as $field_id => $value ) { |
| 1109 | 985 | if ( ! is_numeric( $field_id ) ) { |
| 1110 | 986 | // Maybe `other`. |
| 1111 | 987 | continue; |
| @@ -1139,9 +1015,10 @@ | ||
| 1139 | 1015 | } |
| 1140 | 1016 | |
| 1141 | 1017 | // Convert name array to string. |
| 1142 | 1018 | if ( isset( $subsubvalue['first'] ) && isset( $subsubvalue['last'] ) ) { |
| 1143 | - $subsubvalue = trim( implode( ' ', $subsubvalue ) ); | |
| 1019 | + $subsubvalue = trim( implode( ' ', $subsubvalue ) ); | |
| 1020 | + | |
| 1144 | 1021 | $values['name_field_ids'][] = $subsubindex; |
| 1145 | 1022 | } |
| 1146 | 1023 | |
| 1147 | 1024 | if ( is_array( $values['item_meta'][ $subsubindex ] ) ) { |