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