| @@ -5,10 +5,17 @@ | ||
| 5 | 5 | |
| 6 | 6 | class FrmEntryValidate { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | + * @since 6.17 | |
| 10 | + * | |
| 11 | + * @var array|null | |
| 12 | + */ | |
| 13 | + private static $name_text_fields; | |
| 14 | + | |
| 15 | + /** | |
| 9 | 16 | * @param array $values |
| 10 | - * @param string[]|bool $exclude | |
| 17 | + * @param bool|string[] $exclude | |
| 11 | 18 | * @return array |
| 12 | 19 | */ |
| 13 | 20 | public static function validate( $values, $exclude = false ) { |
| 14 | 21 | FrmEntry::sanitize_entry_post( $values ); |
| @@ -24,8 +31,9 @@ | ||
| 24 | 31 | $frm_settings = FrmAppHelper::get_settings(); |
| 25 | 32 | $errors['form'] = $frm_settings->admin_permission; |
| 26 | 33 | } |
| 27 | 34 | |
| 35 | + self::maybe_fix_item_meta(); | |
| 28 | 36 | self::set_item_key( $values ); |
| 29 | 37 | |
| 30 | 38 | $posted_fields = self::get_fields_to_validate( $values, $exclude ); |
| 31 | 39 | |
| @@ -54,14 +62,29 @@ | ||
| 54 | 62 | |
| 55 | 63 | if ( is_array( $filtered_errors ) ) { |
| 56 | 64 | $errors = $filtered_errors; |
| 57 | 65 | } else { |
| 58 | - _doing_it_wrong( __FUNCTION__, 'Only arrays should be returned when using the frm_validate_entry filter.', '6.3' ); | |
| 66 | + _doing_it_wrong( __METHOD__, 'Only arrays should be returned when using the frm_validate_entry filter.', '6.3' ); | |
| 59 | 67 | } |
| 60 | 68 | |
| 61 | 69 | return $errors; |
| 62 | 70 | } |
| 63 | 71 | |
| 72 | + /** | |
| 73 | + * In case $_POST['item_meta'] is not an array, change it to an empty array. | |
| 74 | + * This helps to avoid some warnings and errors when $_POST['item_meta'] is updated. | |
| 75 | + * | |
| 76 | + * @since 6.6 | |
| 77 | + * | |
| 78 | + * @return void | |
| 79 | + */ | |
| 80 | + private static function maybe_fix_item_meta() { | |
| 81 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotValidated | |
| 82 | + if ( ! isset( $_POST['item_meta'] ) || ! is_array( $_POST['item_meta'] ) ) { | |
| 83 | + $_POST['item_meta'] = array(); | |
| 84 | + } | |
| 85 | + } | |
| 86 | + | |
| 64 | 87 | private static function set_item_key( &$values ) { |
| 65 | 88 | if ( ! isset( $values['item_key'] ) || $values['item_key'] == '' ) { |
| 66 | 89 | global $wpdb; |
| 67 | 90 | $values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' ); |
| @@ -95,13 +118,17 @@ | ||
| 95 | 118 | |
| 96 | 119 | public static function validate_field( $posted_field, &$errors, $values, $args = array() ) { |
| 97 | 120 | $defaults = array( |
| 98 | 121 | 'id' => $posted_field->id, |
| 99 | - 'parent_field_id' => '', // the id of the repeat or embed form | |
| 100 | - 'key_pointer' => '', // the pointer in the posted array | |
| 101 | - 'exclude' => array(), // exclude these field types from validation | |
| 122 | + // The id of the repeat or embed form. | |
| 123 | + 'parent_field_id' => '', | |
| 124 | + // The pointer in the posted array. | |
| 125 | + 'key_pointer' => '', | |
| 126 | + // Exclude these field types from validation. | |
| 127 | + 'exclude' => array(), | |
| 128 | + | |
| 102 | 129 | ); |
| 103 | - $args = wp_parse_args( $args, $defaults ); | |
| 130 | + $args = wp_parse_args( $args, $defaults ); | |
| 104 | 131 | |
| 105 | 132 | if ( empty( $args['parent_field_id'] ) ) { |
| 106 | 133 | $value = isset( $values['item_meta'][ $args['id'] ] ) ? $values['item_meta'][ $args['id'] ] : ''; |
| 107 | 134 | } else { |
| @@ -130,8 +157,9 @@ | ||
| 130 | 157 | } |
| 131 | 158 | |
| 132 | 159 | FrmEntriesHelper::set_posted_value( $posted_field, $value, $args ); |
| 133 | 160 | |
| 161 | + self::validate_options( $errors, $posted_field, $value, $args ); | |
| 134 | 162 | self::validate_field_types( $errors, $posted_field, $value, $args ); |
| 135 | 163 | |
| 136 | 164 | // Field might want to modify value before other parts of the system |
| 137 | 165 | // e.g. trim off excess values like in the case of fields with limit. |
| @@ -142,16 +170,188 @@ | ||
| 142 | 170 | } |
| 143 | 171 | |
| 144 | 172 | $errors = apply_filters( 'frm_validate_' . $posted_field->type . '_field_entry', $errors, $posted_field, $value, $args ); |
| 145 | 173 | $errors = apply_filters( 'frm_validate_field_entry', $errors, $posted_field, $value, $args ); |
| 174 | + | |
| 175 | + if ( ! FrmAppHelper::pro_is_installed() && empty( $args['other'] ) ) { | |
| 176 | + FrmEntriesHelper::get_posted_value( $posted_field, $value, $args ); | |
| 177 | + } | |
| 146 | 178 | } |
| 147 | 179 | |
| 148 | 180 | /** |
| 181 | + * @since 6.21 | |
| 182 | + * | |
| 183 | + * @param array $errors | |
| 184 | + * @param object $posted_field | |
| 185 | + * @param array|string $value | |
| 186 | + * @param array $args | |
| 187 | + * | |
| 188 | + * @return void | |
| 189 | + */ | |
| 190 | + private static function validate_options( &$errors, $posted_field, $value, $args ) { | |
| 191 | + if ( empty( $posted_field->options ) ) { | |
| 192 | + return; | |
| 193 | + } | |
| 194 | + | |
| 195 | + $option_is_valid = self::option_is_valid( $posted_field, $value, $posted_field->options ); | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * @since 6.21 | |
| 199 | + * | |
| 200 | + * @param bool $option_is_valid | |
| 201 | + * @param array|string $value | |
| 202 | + * @param object $field | |
| 203 | + */ | |
| 204 | + $option_is_valid = (bool) apply_filters( 'frm_option_is_valid', $option_is_valid, $value, $posted_field ); | |
| 205 | + | |
| 206 | + if ( ! $option_is_valid ) { | |
| 207 | + $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $posted_field, 'invalid' ); | |
| 208 | + } | |
| 209 | + } | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Validate that value matches one of the options for the field. | |
| 213 | + * | |
| 214 | + * @since 6.21 | |
| 215 | + * | |
| 216 | + * @param stdClass $field | |
| 217 | + * @param array|string $value | |
| 218 | + * @param array $options | |
| 219 | + * @return bool | |
| 220 | + */ | |
| 221 | + private static function option_is_valid( $field, $value, $options ) { | |
| 222 | + if ( '' === $value ) { | |
| 223 | + return true; | |
| 224 | + } | |
| 225 | + | |
| 226 | + $field_object = FrmFieldFactory::get_field_type( $field->type, $field ); | |
| 227 | + if ( ! $field_object->field_type_has_options_settings() ) { | |
| 228 | + return true; | |
| 229 | + } | |
| 230 | + | |
| 231 | + if ( in_array( $field->type, array( 'likert', 'ranking' ), true ) ) { | |
| 232 | + // Ignore these field types automatically. | |
| 233 | + return true; | |
| 234 | + } | |
| 235 | + | |
| 236 | + if ( 'product' === $field->type && 'user_def' === FrmField::get_option( $field, 'data_type' ) ) { | |
| 237 | + return true; | |
| 238 | + } | |
| 239 | + | |
| 240 | + if ( ! empty( $field->field_options['post_field'] ) ) { | |
| 241 | + return true; | |
| 242 | + } | |
| 243 | + | |
| 244 | + $value = (array) $value; | |
| 245 | + | |
| 246 | + foreach ( $value as $current_value ) { | |
| 247 | + $match = false; | |
| 248 | + | |
| 249 | + foreach ( $options as $key => $option ) { | |
| 250 | + if ( strpos( $key, 'other_' ) === 0 ) { | |
| 251 | + // Always return true if an other option is found. | |
| 252 | + return true; | |
| 253 | + } | |
| 254 | + | |
| 255 | + if ( is_array( $option ) ) { | |
| 256 | + $separate_value = FrmField::get_option( $field, 'separate_value' ); | |
| 257 | + $option_value = $separate_value ? $option['value'] : $option['label']; | |
| 258 | + } else { | |
| 259 | + $option_value = $option; | |
| 260 | + } | |
| 261 | + | |
| 262 | + $match = trim( $current_value ) === trim( $option_value ); | |
| 263 | + if ( $match ) { | |
| 264 | + break; | |
| 265 | + } | |
| 266 | + | |
| 267 | + $match = trim( $current_value ) === trim( do_shortcode( $option_value ) ); | |
| 268 | + if ( $match ) { | |
| 269 | + break; | |
| 270 | + } | |
| 271 | + | |
| 272 | + $match = self::is_filtered_match( $current_value, $option_value ); | |
| 273 | + if ( $match ) { | |
| 274 | + break; | |
| 275 | + } | |
| 276 | + | |
| 277 | + if ( is_numeric( $current_value ) ) { | |
| 278 | + $match = (int) $current_value === (int) $option_value; | |
| 279 | + if ( $match ) { | |
| 280 | + break; | |
| 281 | + } | |
| 282 | + } | |
| 283 | + }//end foreach | |
| 284 | + | |
| 285 | + if ( ! $match ) { | |
| 286 | + return self::options_are_dynamic_based_on_hook( $field, $value ); | |
| 287 | + } | |
| 288 | + }//end foreach | |
| 289 | + | |
| 290 | + return true; | |
| 291 | + } | |
| 292 | + | |
| 293 | + /** | |
| 294 | + * Make an extra check after passing $option_value through the_content filter. | |
| 295 | + * This is to help catch cases where the option's formatting has been modified using | |
| 296 | + * the_content filter. | |
| 297 | + * | |
| 298 | + * @since 6.22 | |
| 299 | + * | |
| 300 | + * @param string $value | |
| 301 | + * @param string $option_value | |
| 302 | + * @return bool | |
| 303 | + */ | |
| 304 | + private static function is_filtered_match( $value, $option_value ) { | |
| 305 | + // First remove the wpautop filter so it doesn't add extra tags to $option_value. | |
| 306 | + $filter_priority = has_filter( 'the_content', 'wpautop' ); | |
| 307 | + if ( is_numeric( $filter_priority ) ) { | |
| 308 | + remove_filter( 'the_content', 'wpautop', $filter_priority ); | |
| 309 | + } | |
| 310 | + $filtered_option = apply_filters( 'the_content', $option_value ); | |
| 311 | + if ( is_numeric( $filter_priority ) ) { | |
| 312 | + add_filter( 'the_content', 'wpautop', $filter_priority ); | |
| 313 | + } | |
| 314 | + return trim( $value ) === trim( $filtered_option ); | |
| 315 | + } | |
| 316 | + | |
| 317 | + /** | |
| 318 | + * Do not validate options if they have been modified with a hook. | |
| 319 | + * This is to help avoid issues where the options could be based on a URL param for example. | |
| 320 | + * | |
| 321 | + * @since 6.21 | |
| 322 | + * | |
| 323 | + * @return bool | |
| 324 | + */ | |
| 325 | + private static function options_are_dynamic_based_on_hook( $field_object, $value ) { | |
| 326 | + $values = (array) $field_object; | |
| 327 | + $values['value'] = $value; | |
| 328 | + FrmFieldsHelper::prepare_new_front_field( $values, $field_object ); | |
| 329 | + | |
| 330 | + $separate_value = FrmField::get_option( $field_object, 'separate_value' ); | |
| 331 | + $map_callback = function ( $option ) use ( $separate_value ) { | |
| 332 | + if ( is_array( $option ) ) { | |
| 333 | + $option_value = $separate_value ? $option['value'] : $option['label']; | |
| 334 | + } else { | |
| 335 | + $option_value = $option; | |
| 336 | + } | |
| 337 | + $option_value = do_shortcode( $option_value ); | |
| 338 | + return $option_value; | |
| 339 | + }; | |
| 340 | + | |
| 341 | + $values_options = array_map( $map_callback, $values['options'] ); | |
| 342 | + $field_object_options = array_map( $map_callback, $field_object->options ); | |
| 343 | + | |
| 344 | + return $values_options !== $field_object_options; | |
| 345 | + } | |
| 346 | + | |
| 347 | + /** | |
| 149 | 348 | * Maybe add item_name to $_POST to save it in items table. |
| 150 | 349 | * |
| 151 | 350 | * @since 5.2.02 |
| 152 | 351 | * |
| 153 | - * @param object $field Field object. | |
| 352 | + * @param array|string $value Field value. | |
| 353 | + * @param object $field Field object. | |
| 154 | 354 | */ |
| 155 | 355 | private static function maybe_add_item_name( $value, $field ) { |
| 156 | 356 | $item_name = false; |
| 157 | 357 | if ( 'name' === $field->type ) { |
| @@ -195,10 +395,11 @@ | ||
| 195 | 395 | } |
| 196 | 396 | } |
| 197 | 397 | |
| 198 | 398 | public static function validate_phone_field( &$errors, $field, $value, $args ) { |
| 199 | - if ( $field->type == 'phone' || ( $field->type == 'text' && FrmField::is_option_true_in_object( $field, 'format' ) ) ) { | |
| 399 | + $format_value = FrmField::get_option( $field, 'format' ); | |
| 200 | 400 | |
| 401 | + if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) { | |
| 201 | 402 | $pattern = self::phone_format( $field ); |
| 202 | 403 | |
| 203 | 404 | if ( ! preg_match( $pattern, $value ) ) { |
| 204 | 405 | $errors[ 'field' . $args['id'] ] = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); |
| @@ -212,8 +413,11 @@ | ||
| 212 | 413 | } else { |
| 213 | 414 | $pattern = FrmField::get_option( $field, 'format' ); |
| 214 | 415 | } |
| 215 | 416 | |
| 417 | + // Ampersands are saved as &. | |
| 418 | + // Reverse it here so we are checking for the correct character. | |
| 419 | + $pattern = html_entity_decode( $pattern ); | |
| 216 | 420 | $pattern = apply_filters( 'frm_phone_pattern', $pattern, $field ); |
| 217 | 421 | |
| 218 | 422 | // Create a regexp if format is not already a regexp |
| 219 | 423 | if ( strpos( $pattern, '^' ) !== 0 ) { |
| @@ -270,27 +474,36 @@ | ||
| 270 | 474 | return $pattern; |
| 271 | 475 | } |
| 272 | 476 | |
| 273 | 477 | /** |
| 274 | - * Check for spam | |
| 478 | + * Check for spam. | |
| 275 | 479 | * |
| 276 | - * @param boolean $exclude | |
| 480 | + * @param bool $exclude | |
| 277 | 481 | * @param array $values |
| 278 | - * @param array $errors by reference | |
| 482 | + * @param array $errors By reference. | |
| 279 | 483 | */ |
| 280 | 484 | public static function spam_check( $exclude, $values, &$errors ) { |
| 281 | - if ( ! empty( $exclude ) || ! isset( $values['item_meta'] ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { | |
| 485 | + if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { | |
| 486 | + // Do not check spam on importing. | |
| 487 | + return; | |
| 488 | + } | |
| 489 | + | |
| 490 | + if ( ! empty( $exclude ) || empty( $values['item_meta'] ) || ! empty( $errors ) ) { | |
| 282 | 491 | // only check spam if there are no other errors |
| 283 | 492 | return; |
| 284 | 493 | } |
| 285 | 494 | |
| 286 | 495 | $antispam_check = self::is_antispam_check( $values['form_id'] ); |
| 496 | + $spam_msg = FrmAntiSpamController::get_default_spam_message(); | |
| 287 | 497 | if ( is_string( $antispam_check ) ) { |
| 288 | 498 | $errors['spam'] = $antispam_check; |
| 289 | 499 | } elseif ( self::is_honeypot_spam( $values ) || self::is_spam_bot() ) { |
| 290 | - $errors['spam'] = __( 'Your entry appears to be spam!', 'formidable' ); | |
| 291 | - } elseif ( self::blacklist_check( $values ) ) { | |
| 292 | - $errors['spam'] = __( 'Your entry appears to be blocked spam!', 'formidable' ); | |
| 500 | + $errors['spam'] = $spam_msg; | |
| 501 | + } else { | |
| 502 | + $is_spam = FrmAntiSpamController::is_spam( $values ); | |
| 503 | + if ( $is_spam ) { | |
| 504 | + $errors['spam'] = $is_spam; | |
| 505 | + } | |
| 293 | 506 | } |
| 294 | 507 | |
| 295 | 508 | if ( isset( $errors['spam'] ) || self::form_is_in_progress( $values ) ) { |
| 296 | 509 | return; |
| @@ -326,9 +539,9 @@ | ||
| 326 | 539 | } |
| 327 | 540 | |
| 328 | 541 | /** |
| 329 | 542 | * @param array $values |
| 330 | - * @return boolean | |
| 543 | + * @return bool | |
| 331 | 544 | */ |
| 332 | 545 | private static function is_honeypot_spam( $values ) { |
| 333 | 546 | $honeypot = new FrmHoneypot( $values['form_id'] ); |
| 334 | 547 | return ! $honeypot->validate(); |
| @@ -334,9 +547,9 @@ | ||
| 334 | 547 | return ! $honeypot->validate(); |
| 335 | 548 | } |
| 336 | 549 | |
| 337 | 550 | /** |
| 338 | - * @return boolean | |
| 551 | + * @return bool | |
| 339 | 552 | */ |
| 340 | 553 | private static function is_spam_bot() { |
| 341 | 554 | $ip = FrmAppHelper::get_ip_address(); |
| 342 | 555 | |
| @@ -344,9 +557,9 @@ | ||
| 344 | 557 | } |
| 345 | 558 | |
| 346 | 559 | /** |
| 347 | 560 | * @param array $values |
| 348 | - * @return boolean | |
| 561 | + * @return bool | |
| 349 | 562 | */ |
| 350 | 563 | private static function is_akismet_spam( $values ) { |
| 351 | 564 | global $wpcom_api_key; |
| 352 | 565 | |
| @@ -362,59 +575,23 @@ | ||
| 362 | 575 | |
| 363 | 576 | return ( ! empty( $form->options['akismet'] ) && ( $form->options['akismet'] !== 'logged' || ! is_user_logged_in() ) ); |
| 364 | 577 | } |
| 365 | 578 | |
| 366 | - public static function blacklist_check( $values ) { | |
| 367 | - if ( ! apply_filters( 'frm_check_blacklist', true, $values ) ) { | |
| 368 | - return false; | |
| 369 | - } | |
| 370 | - | |
| 371 | - $mod_keys = trim( self::get_disallowed_words() ); | |
| 372 | - if ( empty( $mod_keys ) ) { | |
| 373 | - return false; | |
| 374 | - } | |
| 375 | - | |
| 376 | - $content = FrmEntriesHelper::entry_array_to_string( $values ); | |
| 377 | - | |
| 378 | - self::prepare_values_for_spam_check( $values ); | |
| 379 | - $ip = FrmAppHelper::get_ip_address(); | |
| 380 | - $user_agent = FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ); | |
| 381 | - $user_info = self::get_spam_check_user_info( $values ); | |
| 382 | - | |
| 383 | - return self::check_disallowed_words( $user_info['comment_author'], $user_info['comment_author_email'], $user_info['comment_author_url'], $content, $ip, $user_agent ); | |
| 384 | - } | |
| 385 | - | |
| 386 | 579 | /** |
| 387 | - * For WP 5.5 compatibility. | |
| 580 | + * Checks spam using WordPress disallowed words and Frm denylist. | |
| 388 | 581 | * |
| 389 | - * @since 4.06.02 | |
| 390 | - */ | |
| 391 | - private static function check_disallowed_words( $author, $email, $url, $content, $ip, $user_agent ) { | |
| 392 | - if ( function_exists( 'wp_check_comment_disallowed_list' ) ) { | |
| 393 | - return wp_check_comment_disallowed_list( $author, $email, $url, $content, $ip, $user_agent ); | |
| 394 | - } else { | |
| 395 | - return wp_blacklist_check( $author, $email, $url, $content, $ip, $user_agent ); | |
| 396 | - } | |
| 397 | - } | |
| 398 | - | |
| 399 | - /** | |
| 400 | - * For WP 5.5 compatibility. | |
| 582 | + * @param array $values Entry values. | |
| 401 | 583 | * |
| 402 | - * @since 4.06.02 | |
| 584 | + * @return bool | |
| 403 | 585 | */ |
| 404 | - private static function get_disallowed_words() { | |
| 405 | - $keys = get_option( 'disallowed_keys' ); | |
| 406 | - if ( false === $keys ) { | |
| 407 | - // Fallback for WP < 5.5. | |
| 408 | - $keys = get_option( 'blacklist_keys' ); | |
| 409 | - } | |
| 410 | - return $keys; | |
| 586 | + public static function blacklist_check( $values ) { | |
| 587 | + return FrmAntiSpamController::contains_wp_disallowed_words( $values ) || FrmAntiSpamController::is_denylist_spam( $values ); | |
| 411 | 588 | } |
| 412 | 589 | |
| 413 | 590 | /** |
| 414 | 591 | * Check entries for Akismet spam |
| 415 | 592 | * |
| 416 | - * @return boolean true if is spam | |
| 593 | + * @return bool true if is spam | |
| 417 | 594 | */ |
| 418 | 595 | public static function akismet( $values ) { |
| 419 | 596 | if ( empty( $values['item_meta'] ) ) { |
| 420 | 597 | return false; |
| @@ -436,9 +613,9 @@ | ||
| 436 | 613 | |
| 437 | 614 | $query_string = _http_build_query( $datas, '', '&' ); |
| 438 | 615 | $response = Akismet::http_post( $query_string, 'comment-check' ); |
| 439 | 616 | |
| 440 | - return ( is_array( $response ) && $response[1] == 'true' ); | |
| 617 | + return ( is_array( $response ) && $response[1] === 'true' ); | |
| 441 | 618 | } |
| 442 | 619 | |
| 443 | 620 | /** |
| 444 | 621 | * @since 2.0 |
| @@ -447,8 +624,9 @@ | ||
| 447 | 624 | self::add_site_info_to_akismet( $datas ); |
| 448 | 625 | self::add_server_values_to_akismet( $datas ); |
| 449 | 626 | |
| 450 | 627 | self::prepare_values_for_spam_check( $values ); |
| 628 | + self::skip_adding_values_to_akismet( $values ); | |
| 451 | 629 | |
| 452 | 630 | self::add_user_info_to_akismet( $datas, $values ); |
| 453 | 631 | self::add_comment_content_to_akismet( $datas, $values ); |
| 454 | 632 | } |
| @@ -478,13 +656,14 @@ | ||
| 478 | 656 | /** |
| 479 | 657 | * Gets user info for Akismet spam check. |
| 480 | 658 | * |
| 481 | 659 | * @since 5.0.13 Separate code for guest. Handle value of embedded|repeater. |
| 660 | + * @since 6.21 This changed from private to public. | |
| 482 | 661 | * |
| 483 | 662 | * @param array $values Entry values after running through {@see FrmEntryValidate::prepare_values_for_spam_check()}. |
| 484 | 663 | * @return array |
| 485 | 664 | */ |
| 486 | - private static function get_spam_check_user_info( $values ) { | |
| 665 | + public static function get_spam_check_user_info( $values ) { | |
| 487 | 666 | if ( ! is_user_logged_in() ) { |
| 488 | 667 | return self::get_spam_check_user_info_for_guest( $values ); |
| 489 | 668 | } |
| 490 | 669 | |
| @@ -541,9 +720,10 @@ | ||
| 541 | 720 | */ |
| 542 | 721 | private static function recursive_add_akismet_guest_info( &$datas, $values, $custom_index = null ) { |
| 543 | 722 | foreach ( $values as $index => $value ) { |
| 544 | 723 | if ( ! $datas['missing_keys'] ) { |
| 545 | - return; // Found all info. | |
| 724 | + // Found all info. | |
| 725 | + return; | |
| 546 | 726 | } |
| 547 | 727 | |
| 548 | 728 | if ( is_array( $value ) ) { |
| 549 | 729 | self::recursive_add_akismet_guest_info( $datas, $value, $index ); |
| @@ -551,9 +731,9 @@ | ||
| 551 | 731 | } |
| 552 | 732 | |
| 553 | 733 | $field_id = ! is_null( $custom_index ) ? $custom_index : $index; |
| 554 | 734 | foreach ( $datas['missing_keys'] as $key_index => $key ) { |
| 555 | - $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'] ); | |
| 735 | + $found = self::is_akismet_guest_info_value( $key, $value, $field_id, $datas['name_field_ids'], $values ); | |
| 556 | 736 | if ( $found ) { |
| 557 | 737 | $datas[ $key ] = $value; |
| 558 | 738 | $datas['frm_duplicated'][] = $field_id; |
| 559 | 739 | unset( $datas['missing_keys'][ $key_index ] ); |
| @@ -558,9 +738,9 @@ | ||
| 558 | 738 | $datas['frm_duplicated'][] = $field_id; |
| 559 | 739 | unset( $datas['missing_keys'][ $key_index ] ); |
| 560 | 740 | } |
| 561 | 741 | } |
| 562 | - } | |
| 742 | + }//end foreach | |
| 563 | 743 | } |
| 564 | 744 | |
| 565 | 745 | /** |
| 566 | 746 | * Checks if given value is an akismet guest info. |
| @@ -570,11 +750,13 @@ | ||
| 570 | 750 | * @param string $key Guest info key. |
| 571 | 751 | * @param string $value Value to check. |
| 572 | 752 | * @param int $field_id Field ID. |
| 573 | 753 | * @param array $name_field_ids Name field IDs. |
| 754 | + * @param array $values Array of posted values. | |
| 755 | + * | |
| 574 | 756 | * @return bool |
| 575 | 757 | */ |
| 576 | - private static function is_akismet_guest_info_value( $key, $value, $field_id, $name_field_ids ) { | |
| 758 | + private static function is_akismet_guest_info_value( $key, &$value, $field_id, $name_field_ids, $values ) { | |
| 577 | 759 | if ( ! $value || is_numeric( $value ) ) { |
| 578 | 760 | return false; |
| 579 | 761 | } |
| 580 | 762 | |
| @@ -585,16 +767,60 @@ | ||
| 585 | 767 | case 'comment_author_url': |
| 586 | 768 | return 0 === strpos( $value, 'http' ); |
| 587 | 769 | |
| 588 | 770 | case 'comment_author': |
| 589 | - if ( $name_field_ids ) { | |
| 771 | + if ( $name_field_ids && in_array( $field_id, $name_field_ids, true ) ) { | |
| 590 | 772 | // If there is name field in the form, we should always use it as author name. |
| 591 | - return in_array( $field_id, $name_field_ids, true ); | |
| 773 | + return true; | |
| 592 | 774 | } |
| 593 | - return strlen( $value ) < 200; | |
| 775 | + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); | |
| 776 | + $fields = self::get_name_text_fields( $form_id ); | |
| 777 | + | |
| 778 | + foreach ( $fields as $index => $field ) { | |
| 779 | + if ( 'Name' !== $field->name ) { | |
| 780 | + continue; | |
| 781 | + } | |
| 782 | + if ( isset( $fields[ $index + 1 ] ) && 'Last' === $fields[ $index + 1 ]->name ) { | |
| 783 | + if ( empty( $values[ absint( $fields[ $index + 1 ]->id ) ] ) ) { | |
| 784 | + continue; | |
| 785 | + } | |
| 786 | + $value .= ' ' . $values[ $fields[ $index + 1 ]->id ]; | |
| 787 | + return true; | |
| 788 | + } | |
| 789 | + } | |
| 790 | + }//end switch | |
| 791 | + | |
| 792 | + return false; | |
| 793 | + } | |
| 794 | + | |
| 795 | + /** | |
| 796 | + * Returns fields that have 'Name' and 'Last' as their name. | |
| 797 | + * | |
| 798 | + * @since 6.17 | |
| 799 | + * | |
| 800 | + * @param int $form_id | |
| 801 | + * @return array | |
| 802 | + */ | |
| 803 | + private static function get_name_text_fields( $form_id ) { | |
| 804 | + $name_text_fields_is_initialized = is_array( self::$name_text_fields ); | |
| 805 | + if ( $name_text_fields_is_initialized && isset( self::$name_text_fields[ $form_id ] ) ) { | |
| 806 | + return self::$name_text_fields[ $form_id ]; | |
| 594 | 807 | } |
| 808 | + if ( ! $name_text_fields_is_initialized ) { | |
| 809 | + self::$name_text_fields = array(); | |
| 810 | + } | |
| 811 | + self::$name_text_fields[ $form_id ] = FrmDb::get_results( | |
| 812 | + 'frm_fields', | |
| 813 | + array( | |
| 814 | + 'form_id' => $form_id, | |
| 815 | + 'type' => 'text', | |
| 816 | + 'name' => array( 'Name', 'Last' ), | |
| 817 | + ), | |
| 818 | + 'id,name', | |
| 819 | + array( 'order_by' => 'field_order ASC' ) | |
| 820 | + ); | |
| 595 | 821 | |
| 596 | - return false; | |
| 822 | + return self::$name_text_fields[ $form_id ]; | |
| 597 | 823 | } |
| 598 | 824 | |
| 599 | 825 | private static function add_server_values_to_akismet( &$datas ) { |
| 600 | 826 | foreach ( $_SERVER as $key => $value ) { |
| @@ -627,10 +853,8 @@ | ||
| 627 | 853 | } |
| 628 | 854 | unset( $datas['frm_duplicated'] ); |
| 629 | 855 | } |
| 630 | 856 | |
| 631 | - self::skip_adding_values_to_akismet( $values ); | |
| 632 | - | |
| 633 | 857 | $datas['comment_content'] = FrmEntriesHelper::entry_array_to_string( $values ); |
| 634 | 858 | } |
| 635 | 859 | |
| 636 | 860 | /** |
| @@ -665,14 +889,16 @@ | ||
| 665 | 889 | * @param array $values Entry values. |
| 666 | 890 | * @return bool |
| 667 | 891 | */ |
| 668 | 892 | private static function should_really_skip_field( $field_data, $values ) { |
| 669 | - if ( empty( $field_data->options ) ) { // This is skipped field types. | |
| 893 | + if ( empty( $field_data->options ) ) { | |
| 894 | + // This is skipped field types. | |
| 670 | 895 | return true; |
| 671 | 896 | } |
| 672 | 897 | |
| 673 | 898 | FrmAppHelper::unserialize_or_decode( $field_data->options ); |
| 674 | - if ( ! $field_data->options ) { // Check if an error happens when unserializing, or empty options. | |
| 899 | + if ( ! $field_data->options ) { | |
| 900 | + // Check if an error happens when unserializing, or empty options. | |
| 675 | 901 | return true; |
| 676 | 902 | } |
| 677 | 903 | |
| 678 | 904 | end( $field_data->options ); |
| @@ -730,12 +956,13 @@ | ||
| 730 | 956 | /** |
| 731 | 957 | * Prepares values array for spam check. |
| 732 | 958 | * |
| 733 | 959 | * @since 5.0.13 |
| 960 | + * @since 6.21 This changed from private to public. | |
| 734 | 961 | * |
| 735 | 962 | * @param array $values Entry values. |
| 736 | 963 | */ |
| 737 | - private static function prepare_values_for_spam_check( &$values ) { | |
| 964 | + public static function prepare_values_for_spam_check( &$values ) { | |
| 738 | 965 | $form_ids = self::get_all_form_ids_and_flatten_meta( $values ); |
| 739 | 966 | $values['form_ids'] = $form_ids; |
| 740 | 967 | } |
| 741 | 968 | |
| @@ -754,9 +981,10 @@ | ||
| 754 | 981 | |
| 755 | 982 | // Blacklist check for File field in the old version doesn't contain `form_id`. |
| 756 | 983 | $form_ids = isset( $values['form_id'] ) ? array( absint( $values['form_id'] ) ) : array(); |
| 757 | 984 | foreach ( $values['item_meta'] as $field_id => $value ) { |
| 758 | - if ( ! is_numeric( $field_id ) ) { // Maybe `other`. | |
| 985 | + if ( ! is_numeric( $field_id ) ) { | |
| 986 | + // Maybe `other`. | |
| 759 | 987 | continue; |
| 760 | 988 | } |
| 761 | 989 | |
| 762 | 990 | // Convert name array to string. |
| @@ -792,46 +1020,16 @@ | ||
| 792 | 1020 | |
| 793 | 1021 | $values['name_field_ids'][] = $subsubindex; |
| 794 | 1022 | } |
| 795 | 1023 | |
| 796 | - $values['item_meta'][ $subsubindex ][] = $subsubvalue; | |
| 1024 | + if ( is_array( $values['item_meta'][ $subsubindex ] ) ) { | |
| 1025 | + $values['item_meta'][ $subsubindex ][] = $subsubvalue; | |
| 1026 | + } | |
| 797 | 1027 | } |
| 798 | - } | |
| 1028 | + }//end foreach | |
| 799 | 1029 | |
| 800 | 1030 | unset( $values['item_meta'][ $field_id ] ); |
| 801 | - } | |
| 1031 | + }//end foreach | |
| 802 | 1032 | |
| 803 | 1033 | return $form_ids; |
| 804 | - } | |
| 805 | - | |
| 806 | - /** | |
| 807 | - * @deprecated 3.0 | |
| 808 | - * @codeCoverageIgnore | |
| 809 | - */ | |
| 810 | - public static function validate_url_field( &$errors, $field, $value, $args ) { | |
| 811 | - FrmDeprecated::validate_url_field( $errors, $field, $value, $args ); | |
| 812 | - } | |
| 813 | - | |
| 814 | - /** | |
| 815 | - * @deprecated 3.0 | |
| 816 | - * @codeCoverageIgnore | |
| 817 | - */ | |
| 818 | - public static function validate_email_field( &$errors, $field, $value, $args ) { | |
| 819 | - FrmDeprecated::validate_email_field( $errors, $field, $value, $args ); | |
| 820 | - } | |
| 821 | - | |
| 822 | - /** | |
| 823 | - * @deprecated 3.0 | |
| 824 | - * @codeCoverageIgnore | |
| 825 | - */ | |
| 826 | - public static function validate_number_field( &$errors, $field, $value, $args ) { | |
| 827 | - FrmDeprecated::validate_number_field( $errors, $field, $value, $args ); | |
| 828 | - } | |
| 829 | - | |
| 830 | - /** | |
| 831 | - * @deprecated 3.0 | |
| 832 | - * @codeCoverageIgnore | |
| 833 | - */ | |
| 834 | - public static function validate_recaptcha( &$errors, $field, $args ) { | |
| 835 | - FrmDeprecated::validate_recaptcha( $errors, $field, $args ); | |
| 836 | 1034 | } |
| 837 | 1035 | } |