| @@ -4,8 +4,15 @@ | ||
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | class FrmFieldsController { |
| 7 | 7 | |
| 8 | + /** | |
| 9 | + * This is stored statically so we can re-use this data for every field. | |
| 10 | + * | |
| 11 | + * @var FrmFieldSelectionData|null | |
| 12 | + */ | |
| 13 | + private static $field_selection_data; | |
| 14 | + | |
| 8 | 15 | public static function load_field() { |
| 9 | 16 | FrmAppHelper::permission_check( 'frm_edit_forms' ); |
| 10 | 17 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 11 | 18 | |
| @@ -73,18 +80,24 @@ | ||
| 73 | 80 | /** |
| 74 | 81 | * Set up and create a new field |
| 75 | 82 | * |
| 76 | 83 | * @param string $field_type |
| 77 | - * @param integer $form_id | |
| 84 | + * @param int $form_id | |
| 78 | 85 | * |
| 79 | - * @return array|bool | |
| 86 | + * @return array|false | |
| 80 | 87 | */ |
| 81 | 88 | public static function include_new_field( $field_type, $form_id ) { |
| 82 | 89 | $field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id ); |
| 90 | + | |
| 91 | + // When a new field is added to the form, flag it as draft and hide it from the front-end. | |
| 92 | + $field_values['field_options']['draft'] = 1; | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * @param array $field_values | |
| 96 | + */ | |
| 83 | 97 | $field_values = apply_filters( 'frm_before_field_created', $field_values ); |
| 98 | + $field_id = FrmField::create( $field_values ); | |
| 84 | 99 | |
| 85 | - $field_id = FrmField::create( $field_values ); | |
| 86 | - | |
| 87 | 100 | if ( ! $field_id ) { |
| 88 | 101 | return false; |
| 89 | 102 | } |
| 90 | 103 | |
| @@ -136,11 +149,11 @@ | ||
| 136 | 149 | |
| 137 | 150 | /** |
| 138 | 151 | * @since 3.0 |
| 139 | 152 | * |
| 140 | - * @param int|array|object $field_object | |
| 141 | - * @param array $values | |
| 142 | - * @param int $form_id | |
| 153 | + * @param array|int|object $field_object | |
| 154 | + * @param array $values | |
| 155 | + * @param int $form_id | |
| 143 | 156 | */ |
| 144 | 157 | public static function load_single_field( $field_object, $values, $form_id = 0 ) { |
| 145 | 158 | global $frm_vars; |
| 146 | 159 | $frm_vars['is_admin'] = true; |
| @@ -154,30 +167,35 @@ | ||
| 154 | 167 | |
| 155 | 168 | $field_obj = FrmFieldFactory::get_field_factory( $field_object ); |
| 156 | 169 | $display = self::display_field_options( array(), $field_obj ); |
| 157 | 170 | |
| 158 | - $ajax_loading = isset( $values['ajax_load'] ) && $values['ajax_load']; | |
| 159 | - $ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ) ); | |
| 171 | + $ajax_loading = ! empty( $values['ajax_load'] ); | |
| 172 | + $ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ), true ); | |
| 160 | 173 | |
| 161 | 174 | if ( $ajax_loading && $ajax_this_field ) { |
| 162 | 175 | $li_classes = self::get_classes_for_builder_field( array(), $display, $field_obj ); |
| 163 | - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php' ); | |
| 164 | - } else { | |
| 165 | - if ( ! isset( $field ) && is_object( $field_object ) ) { | |
| 166 | - $field_object->parent_form_id = isset( $values['id'] ) ? $values['id'] : $field_object->form_id; | |
| 176 | + include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php'; | |
| 177 | + return; | |
| 178 | + } | |
| 167 | 179 | |
| 168 | - $field = FrmFieldsHelper::setup_edit_vars( $field_object ); | |
| 169 | - } | |
| 180 | + if ( ! isset( $field ) && is_object( $field_object ) ) { | |
| 181 | + $field_object->parent_form_id = isset( $values['id'] ) ? $values['id'] : $field_object->form_id; | |
| 182 | + $field = FrmFieldsHelper::setup_edit_vars( $field_object ); | |
| 183 | + } | |
| 170 | 184 | |
| 171 | - $li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj ); | |
| 172 | - $li_classes .= ' ui-state-default widgets-holder-wrap'; | |
| 185 | + $li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj ); | |
| 186 | + $li_classes .= ' ui-state-default widgets-holder-wrap'; | |
| 173 | 187 | |
| 174 | - require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' ); | |
| 175 | - } | |
| 188 | + require FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php'; | |
| 176 | 189 | } |
| 177 | 190 | |
| 178 | 191 | /** |
| 179 | 192 | * @since 3.0 |
| 193 | + * | |
| 194 | + * @param array $field | |
| 195 | + * @param array $display | |
| 196 | + * @param FrmFieldType $field_info | |
| 197 | + * @return string | |
| 180 | 198 | */ |
| 181 | 199 | private static function get_classes_for_builder_field( $field, $display, $field_info ) { |
| 182 | 200 | $li_classes = $field_info->form_builder_classes( $display['type'] ); |
| 183 | 201 | $li_classes .= ' frm_form_field frmstart '; |
| @@ -203,10 +221,11 @@ | ||
| 203 | 221 | FrmField::destroy( $field_id ); |
| 204 | 222 | wp_die(); |
| 205 | 223 | } |
| 206 | 224 | |
| 207 | - /* Field Options */ | |
| 208 | - | |
| 225 | + /** | |
| 226 | + * Field Options. | |
| 227 | + */ | |
| 209 | 228 | public static function import_options() { |
| 210 | 229 | FrmAppHelper::permission_check( 'frm_edit_forms' ); |
| 211 | 230 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 212 | 231 | |
| @@ -213,12 +232,23 @@ | ||
| 213 | 232 | if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) { |
| 214 | 233 | return; |
| 215 | 234 | } |
| 216 | 235 | |
| 217 | - $field_id = FrmAppHelper::get_param( 'field_id', '', 'post', 'absint' ); | |
| 218 | - $field = FrmField::getOne( $field_id ); | |
| 236 | + $field_id = FrmAppHelper::get_param( 'field_id', '', 'post', 'absint' ); | |
| 237 | + $field = FrmField::getOne( $field_id ); | |
| 238 | + $bulk_edit_types = array( 'radio', 'checkbox', 'select' ); | |
| 219 | 239 | |
| 220 | - if ( ! in_array( $field->type, array( 'radio', 'checkbox', 'select' ) ) ) { | |
| 240 | + /** | |
| 241 | + * Filter to add new fields that will support import_options/Bulk Edit Options. | |
| 242 | + * | |
| 243 | + * @since 6.8.4 | |
| 244 | + * | |
| 245 | + * @param array $bulk_edit_types | |
| 246 | + * @return array | |
| 247 | + */ | |
| 248 | + $bulk_edit_types = apply_filters( 'frm_bulk_edit_field_types', $bulk_edit_types ); | |
| 249 | + | |
| 250 | + if ( ! in_array( $field->type, $bulk_edit_types, true ) ) { | |
| 221 | 251 | return; |
| 222 | 252 | } |
| 223 | 253 | |
| 224 | 254 | $field = FrmFieldsHelper::setup_edit_vars( $field ); |
| @@ -225,15 +255,15 @@ | ||
| 225 | 255 | $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' ); |
| 226 | 256 | $opts = explode( "\n", rtrim( $opts, "\n" ) ); |
| 227 | 257 | $opts = array_map( 'trim', $opts ); |
| 228 | 258 | |
| 229 | - $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' ); | |
| 230 | - $field['separate_value'] = ( $separate === 'true' ); | |
| 259 | + $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' ); | |
| 260 | + $field['separate_value'] = $separate === 'true'; | |
| 231 | 261 | |
| 232 | 262 | if ( $field['separate_value'] ) { |
| 233 | 263 | foreach ( $opts as $opt_key => $opt ) { |
| 234 | 264 | if ( strpos( $opt, '|' ) !== false ) { |
| 235 | - $vals = explode( '|', $opt ); | |
| 265 | + $vals = explode( '|', $opt ); | |
| 236 | 266 | $opts[ $opt_key ] = array( |
| 237 | 267 | 'label' => trim( $vals[0] ), |
| 238 | 268 | 'value' => trim( $vals[1] ), |
| 239 | 269 | ); |
| @@ -267,8 +297,9 @@ | ||
| 267 | 297 | /** |
| 268 | 298 | * @since 4.0 |
| 269 | 299 | * |
| 270 | 300 | * @param array $atts - Includes field array, field_obj, display array, values array. |
| 301 | + * @return void | |
| 271 | 302 | */ |
| 272 | 303 | public static function load_single_field_settings( $atts ) { |
| 273 | 304 | $field = $atts['field']; |
| 274 | 305 | $field_obj = $atts['field_obj']; |
| @@ -283,13 +314,13 @@ | ||
| 283 | 314 | if ( ! isset( $field['read_only'] ) ) { |
| 284 | 315 | $field['read_only'] = false; |
| 285 | 316 | } |
| 286 | 317 | |
| 287 | - $field_types = FrmFieldsHelper::get_field_types( $field['type'] ); | |
| 288 | - $pro_field_selection = FrmField::pro_field_selection(); | |
| 289 | - $all_field_types = array_merge( $pro_field_selection, FrmField::field_selection() ); | |
| 290 | - $disabled_fields = FrmAppHelper::pro_is_installed() ? array() : $pro_field_selection; | |
| 291 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 318 | + $field_selection_data = self::maybe_define_field_selection_data(); | |
| 319 | + $all_field_types = $field_selection_data->all_field_types; | |
| 320 | + $disabled_fields = $field_selection_data->disabled_fields; | |
| 321 | + $frm_settings = FrmAppHelper::get_settings(); | |
| 322 | + $field_types = FrmFieldTypeOptionData::get_field_types( $field['type'] ); | |
| 292 | 323 | |
| 293 | 324 | if ( ! isset( $all_field_types[ $field['type'] ] ) ) { |
| 294 | 325 | // Add fallback for an add-on field type that has been deactivated. |
| 295 | 326 | $all_field_types[ $field['type'] ] = array( |
| @@ -312,20 +343,36 @@ | ||
| 312 | 343 | |
| 313 | 344 | if ( $display['clear_on_focus'] && is_array( $field['placeholder'] ) ) { |
| 314 | 345 | $field['placeholder'] = implode( ', ', $field['placeholder'] ); |
| 315 | 346 | } |
| 316 | - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php' ); | |
| 347 | + | |
| 348 | + include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php'; | |
| 317 | 349 | } |
| 318 | 350 | |
| 319 | 351 | /** |
| 352 | + * @since 6.9.1 | |
| 353 | + * | |
| 354 | + * @return FrmFieldSelectionData | |
| 355 | + */ | |
| 356 | + private static function maybe_define_field_selection_data() { | |
| 357 | + if ( ! isset( self::$field_selection_data ) ) { | |
| 358 | + self::$field_selection_data = new FrmFieldSelectionData(); | |
| 359 | + } | |
| 360 | + return self::$field_selection_data; | |
| 361 | + } | |
| 362 | + | |
| 363 | + /** | |
| 320 | 364 | * Get the list of default value types that can be toggled in the builder. |
| 321 | 365 | * |
| 322 | 366 | * @since 4.0 |
| 367 | + * | |
| 368 | + * @param array $field | |
| 369 | + * @param array $atts | |
| 323 | 370 | * @return array |
| 324 | 371 | */ |
| 325 | 372 | private static function default_value_types( $field, $atts ) { |
| 326 | 373 | $types = array( |
| 327 | - 'default_value' => array( | |
| 374 | + 'default_value' => array( | |
| 328 | 375 | 'class' => '', |
| 329 | 376 | 'icon' => 'frm_icon_font frm_text2_icon', |
| 330 | 377 | 'title' => __( 'Default Value (Text)', 'formidable' ), |
| 331 | 378 | 'data' => array( |
| @@ -331,9 +378,9 @@ | ||
| 331 | 378 | 'data' => array( |
| 332 | 379 | 'frmshow' => '#default-value-for-', |
| 333 | 380 | ), |
| 334 | 381 | ), |
| 335 | - 'calc' => array( | |
| 382 | + 'calc' => array( | |
| 336 | 383 | 'class' => 'frm_show_upgrade frm_noallow', |
| 337 | 384 | 'title' => __( 'Default Value (Calculation)', 'formidable' ), |
| 338 | 385 | 'icon' => 'frm_icon_font frm_calculator_icon', |
| 339 | 386 | 'data' => array( |
| @@ -353,13 +400,8 @@ | ||
| 353 | 400 | ); |
| 354 | 401 | |
| 355 | 402 | $types = apply_filters( 'frm_default_value_types', $types, $atts ); |
| 356 | 403 | |
| 357 | - if ( FrmAppHelper::pro_is_installed() && ! FrmAppHelper::meets_min_pro_version( '4.0' ) ) { | |
| 358 | - // Prevent settings from showing in 2 spots. | |
| 359 | - unset( $types['calc'], $types['get_values_field'] ); | |
| 360 | - } | |
| 361 | - | |
| 362 | 404 | // Set active class. |
| 363 | 405 | $settings = array_keys( $types ); |
| 364 | 406 | $active = 'default_value'; |
| 365 | 407 | |
| @@ -374,8 +416,12 @@ | ||
| 374 | 416 | |
| 375 | 417 | return $types; |
| 376 | 418 | } |
| 377 | 419 | |
| 420 | + /** | |
| 421 | + * @param string $type | |
| 422 | + * @return string | |
| 423 | + */ | |
| 378 | 424 | public static function change_type( $type ) { |
| 379 | 425 | $type_switch = array( |
| 380 | 426 | 'scale' => 'radio', |
| 381 | 427 | 'star' => 'radio', |
| @@ -388,10 +434,13 @@ | ||
| 388 | 434 | $type = $type_switch[ $type ]; |
| 389 | 435 | } |
| 390 | 436 | |
| 391 | 437 | $pro_fields = FrmField::pro_field_selection(); |
| 392 | - $types = array_keys( $pro_fields ); | |
| 393 | - if ( in_array( $type, $types ) ) { | |
| 438 | + // We want to keep credit_card types as credit card types for Stripe Lite. | |
| 439 | + // The credit_card key is set for backward compatibility. | |
| 440 | + unset( $pro_fields['credit_card'] ); | |
| 441 | + | |
| 442 | + if ( array_key_exists( $type, $pro_fields ) ) { | |
| 394 | 443 | $type = 'text'; |
| 395 | 444 | } |
| 396 | 445 | |
| 397 | 446 | return $type; |
| @@ -397,16 +446,18 @@ | ||
| 397 | 446 | return $type; |
| 398 | 447 | } |
| 399 | 448 | |
| 400 | 449 | /** |
| 401 | - * @param array $settings | |
| 402 | - * @param object $field_info | |
| 450 | + * @param array $settings | |
| 451 | + * @param FrmFieldType|null $field_info | |
| 403 | 452 | * |
| 404 | 453 | * @return array |
| 405 | 454 | */ |
| 406 | 455 | public static function display_field_options( $settings, $field_info = null ) { |
| 407 | 456 | if ( $field_info ) { |
| 408 | - $settings = $field_info->display_field_settings(); | |
| 457 | + $settings = $field_info->display_field_settings(); | |
| 458 | + | |
| 459 | + // Field appears to be protected but there is a __get function in FrmFieldType that makes this public. | |
| 409 | 460 | $settings['field_data'] = $field_info->field; |
| 410 | 461 | } |
| 411 | 462 | |
| 412 | 463 | return apply_filters( 'frm_display_field_options', $settings ); |
| @@ -417,11 +468,20 @@ | ||
| 417 | 468 | * |
| 418 | 469 | * @since 3.0 |
| 419 | 470 | * |
| 420 | 471 | * @param array $field |
| 472 | + * @return void | |
| 421 | 473 | */ |
| 422 | 474 | public static function show_format_option( $field ) { |
| 423 | - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php' ); | |
| 475 | + $attributes = array(); | |
| 476 | + $attributes['class'] = 'frm-has-modal'; | |
| 477 | + | |
| 478 | + if ( 'phone' === $field['type'] ) { | |
| 479 | + $attributes['id'] = 'frm-phone-field-custom-format-' . $field['id']; | |
| 480 | + $attributes['class'] .= ' frm_hidden'; | |
| 481 | + } | |
| 482 | + | |
| 483 | + include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php'; | |
| 424 | 484 | } |
| 425 | 485 | |
| 426 | 486 | public static function input_html( $field, $echo = true ) { |
| 427 | 487 | $class = array(); |
| @@ -449,14 +509,19 @@ | ||
| 449 | 509 | |
| 450 | 510 | return $add_html; |
| 451 | 511 | } |
| 452 | 512 | |
| 513 | + /** | |
| 514 | + * @param array $field | |
| 515 | + * @param array $class | |
| 516 | + * @return void | |
| 517 | + */ | |
| 453 | 518 | private static function add_input_classes( $field, array &$class ) { |
| 454 | - if ( isset( $field['input_class'] ) && ! empty( $field['input_class'] ) ) { | |
| 519 | + if ( ! empty( $field['input_class'] ) ) { | |
| 455 | 520 | $class[] = $field['input_class']; |
| 456 | 521 | } |
| 457 | 522 | |
| 458 | - if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) { | |
| 523 | + if ( $field['type'] === 'hidden' || $field['type'] === 'user_id' ) { | |
| 459 | 524 | return; |
| 460 | 525 | } |
| 461 | 526 | |
| 462 | 527 | if ( isset( $field['size'] ) && $field['size'] > 0 ) { |
| @@ -463,8 +528,13 @@ | ||
| 463 | 528 | $class[] = 'auto_width'; |
| 464 | 529 | } |
| 465 | 530 | } |
| 466 | 531 | |
| 532 | + /** | |
| 533 | + * @param array $field | |
| 534 | + * @param array $add_html | |
| 535 | + * @return void | |
| 536 | + */ | |
| 467 | 537 | private static function add_html_size( $field, array &$add_html ) { |
| 468 | 538 | $size_fields = array( |
| 469 | 539 | 'select', |
| 470 | 540 | 'data', |
| @@ -473,9 +543,9 @@ | ||
| 473 | 543 | 'file', |
| 474 | 544 | 'lookup', |
| 475 | 545 | ); |
| 476 | 546 | |
| 477 | - if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields ) ) { | |
| 547 | + if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields, true ) ) { | |
| 478 | 548 | return; |
| 479 | 549 | } |
| 480 | 550 | |
| 481 | 551 | if ( FrmAppHelper::is_admin_page( 'formidable' ) ) { |
| @@ -492,10 +562,15 @@ | ||
| 492 | 562 | |
| 493 | 563 | self::add_html_cols( $field, $add_html ); |
| 494 | 564 | } |
| 495 | 565 | |
| 566 | + /** | |
| 567 | + * @param array $field | |
| 568 | + * @param array $add_html | |
| 569 | + * @return void | |
| 570 | + */ | |
| 496 | 571 | private static function add_html_cols( $field, array &$add_html ) { |
| 497 | - if ( ! in_array( $field['type'], array( 'textarea', 'rte' ) ) ) { | |
| 572 | + if ( ! in_array( $field['type'], array( 'textarea', 'rte' ), true ) ) { | |
| 498 | 573 | return; |
| 499 | 574 | } |
| 500 | 575 | |
| 501 | 576 | // Convert to cols for textareas. |
| @@ -517,8 +592,13 @@ | ||
| 517 | 592 | |
| 518 | 593 | $add_html['cols'] = 'cols="' . absint( $size ) . '"'; |
| 519 | 594 | } |
| 520 | 595 | |
| 596 | + /** | |
| 597 | + * @param array $field | |
| 598 | + * @param array $add_html | |
| 599 | + * @return void | |
| 600 | + */ | |
| 521 | 601 | private static function add_html_length( $field, array &$add_html ) { |
| 522 | 602 | // Check for max setting and if this field accepts maxlength. |
| 523 | 603 | $fields = array( |
| 524 | 604 | 'textarea', |
| @@ -526,9 +606,9 @@ | ||
| 526 | 606 | 'hidden', |
| 527 | 607 | 'file', |
| 528 | 608 | ); |
| 529 | 609 | |
| 530 | - if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields ) ) { | |
| 610 | + if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields, true ) ) { | |
| 531 | 611 | return; |
| 532 | 612 | } |
| 533 | 613 | |
| 534 | 614 | if ( FrmAppHelper::is_admin_page( 'formidable' ) ) { |
| @@ -538,8 +618,14 @@ | ||
| 538 | 618 | |
| 539 | 619 | $add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"'; |
| 540 | 620 | } |
| 541 | 621 | |
| 622 | + /** | |
| 623 | + * @param array $field | |
| 624 | + * @param array $add_html | |
| 625 | + * @param array $class | |
| 626 | + * @return void | |
| 627 | + */ | |
| 542 | 628 | private static function add_html_placeholder( $field, array &$add_html, array &$class ) { |
| 543 | 629 | if ( $field['default_value'] != '' ) { |
| 544 | 630 | if ( is_array( $field['default_value'] ) ) { |
| 545 | 631 | $add_html['data-frmval'] = 'data-frmval="' . esc_attr( json_encode( $field['default_value'] ) ) . '"'; |
| @@ -553,21 +639,9 @@ | ||
| 553 | 639 | // don't include a json placeholder |
| 554 | 640 | return; |
| 555 | 641 | } |
| 556 | 642 | |
| 557 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 558 | - | |
| 559 | - if ( $frm_settings->use_html ) { | |
| 560 | - self::add_placeholder_to_input( $field, $add_html ); | |
| 561 | - } else { | |
| 562 | - self::add_frmval_to_input( $field, $add_html ); | |
| 563 | - | |
| 564 | - $class[] = 'frm_toggle_default'; | |
| 565 | - | |
| 566 | - if ( $field['value'] == $field['placeholder'] ) { | |
| 567 | - $class[] = 'frm_default'; | |
| 568 | - } | |
| 569 | - } | |
| 643 | + self::add_placeholder_to_input( $field, $add_html ); | |
| 570 | 644 | } |
| 571 | 645 | |
| 572 | 646 | /** |
| 573 | 647 | * Prepares field placeholder. |
| @@ -610,14 +684,31 @@ | ||
| 610 | 684 | if ( empty( $placeholder ) ) { |
| 611 | 685 | $placeholder = self::get_default_value_from_name( $field ); |
| 612 | 686 | } |
| 613 | 687 | |
| 688 | + $use_placeholder = $placeholder; | |
| 689 | + $autocomplete = FrmField::get_option( $field, 'autocom' ); | |
| 690 | + | |
| 691 | + if ( $autocomplete ) { | |
| 692 | + $use_chosen = ! is_callable( 'FrmProAppHelper::use_chosen_js' ) || FrmProAppHelper::use_chosen_js(); | |
| 693 | + if ( $use_chosen ) { | |
| 694 | + $use_placeholder = ''; | |
| 695 | + } | |
| 696 | + } | |
| 697 | + | |
| 614 | 698 | if ( $placeholder !== '' ) { |
| 615 | - ?> | |
| 616 | - <option value=""> | |
| 617 | - <?php echo esc_html( FrmField::get_option( $field, 'autocom' ) ? '' : $placeholder ); ?> | |
| 618 | - </option> | |
| 619 | - <?php | |
| 699 | + $placeholder_attributes = array( | |
| 700 | + 'class' => 'frm-select-placeholder', | |
| 701 | + 'value' => '', | |
| 702 | + 'data-placeholder' => 'true', | |
| 703 | + ); | |
| 704 | + | |
| 705 | + if ( $autocomplete && empty( $use_chosen ) ) { | |
| 706 | + // This is required for Slim Select. | |
| 707 | + $placeholder_attributes['data-placeholder'] = 'true'; | |
| 708 | + } | |
| 709 | + | |
| 710 | + FrmHtmlHelper::echo_dropdown_option( $use_placeholder, false, $placeholder_attributes ); | |
| 620 | 711 | return true; |
| 621 | 712 | } |
| 622 | 713 | |
| 623 | 714 | return false; |
| @@ -623,12 +714,13 @@ | ||
| 623 | 714 | return false; |
| 624 | 715 | } |
| 625 | 716 | |
| 626 | 717 | /** |
| 627 | - * Use HMTL5 placeholder with js fallback | |
| 718 | + * Use HTML5 placeholder with js fallback. | |
| 628 | 719 | * |
| 629 | 720 | * @param array $field |
| 630 | 721 | * @param array $add_html |
| 722 | + * @return void | |
| 631 | 723 | */ |
| 632 | 724 | private static function add_placeholder_to_input( $field, &$add_html ) { |
| 633 | 725 | if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) { |
| 634 | 726 | $add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"'; |
| @@ -634,8 +726,13 @@ | ||
| 634 | 726 | $add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"'; |
| 635 | 727 | } |
| 636 | 728 | } |
| 637 | 729 | |
| 730 | + /** | |
| 731 | + * @param array $field | |
| 732 | + * @param array $add_html | |
| 733 | + * @return void | |
| 734 | + */ | |
| 638 | 735 | private static function add_frmval_to_input( $field, &$add_html ) { |
| 639 | 736 | if ( $field['placeholder'] != '' ) { |
| 640 | 737 | $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['placeholder'] ) . '"'; |
| 641 | 738 | |
| @@ -649,15 +746,17 @@ | ||
| 649 | 746 | } |
| 650 | 747 | } |
| 651 | 748 | |
| 652 | 749 | private static function add_validation_messages( $field, array &$add_html ) { |
| 653 | - if ( FrmField::is_required( $field ) ) { | |
| 750 | + $field_validation_messages_status = self::get_validation_data_attribute_visibility_info( $field ); | |
| 751 | + | |
| 752 | + if ( FrmField::is_required( $field ) && ! empty( $field_validation_messages_status['data-reqmsg'] ) ) { | |
| 654 | 753 | $required_message = FrmFieldsHelper::get_error_msg( $field, 'blank' ); |
| 655 | 754 | $add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"'; |
| 656 | 755 | self::maybe_add_html_required( $field, $add_html ); |
| 657 | 756 | } |
| 658 | 757 | |
| 659 | - if ( ! FrmField::is_option_empty( $field, 'invalid' ) ) { | |
| 758 | + if ( ! FrmField::is_option_empty( $field, 'invalid' ) && ! empty( $field_validation_messages_status['data-invmsg'] ) ) { | |
| 660 | 759 | $invalid_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); |
| 661 | 760 | $add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"'; |
| 662 | 761 | } |
| 663 | 762 | |
| @@ -666,12 +765,45 @@ | ||
| 666 | 765 | } |
| 667 | 766 | } |
| 668 | 767 | |
| 669 | 768 | /** |
| 769 | + * Returns an array that contains field validation messages status. | |
| 770 | + * | |
| 771 | + * @since 6.9 | |
| 772 | + * | |
| 773 | + * @param array|object $field | |
| 774 | + * @return array | |
| 775 | + */ | |
| 776 | + private static function get_validation_data_attribute_visibility_info( $field ) { | |
| 777 | + if ( FrmField::get_field_type( $field ) === 'hidden' ) { | |
| 778 | + $field_validation_data_attributes = array( | |
| 779 | + 'data-invmsg' => false, | |
| 780 | + 'data-reqmsg' => false, | |
| 781 | + ); | |
| 782 | + } else { | |
| 783 | + $field_validation_data_attributes = array( | |
| 784 | + 'data-invmsg' => true, | |
| 785 | + 'data-reqmsg' => true, | |
| 786 | + ); | |
| 787 | + } | |
| 788 | + | |
| 789 | + /** | |
| 790 | + * Allows controlling which field validation messages would be included in the field html. | |
| 791 | + * | |
| 792 | + * @since 6.9 | |
| 793 | + * | |
| 794 | + * @param array $field_validation_messages_status | |
| 795 | + * @param array|object $field | |
| 796 | + */ | |
| 797 | + return apply_filters( 'frm_field_validation_include_data_attributes', $field_validation_data_attributes, $field ); | |
| 798 | + } | |
| 799 | + | |
| 800 | + /** | |
| 670 | 801 | * @since 5.0.03 |
| 671 | 802 | * |
| 672 | 803 | * @param array $field |
| 673 | 804 | * @param array $add_html |
| 805 | + * @return void | |
| 674 | 806 | */ |
| 675 | 807 | private static function maybe_add_error_html_for_js_validation( $field, array &$add_html ) { |
| 676 | 808 | $form = self::get_form_for_js_validation( $field ); |
| 677 | 809 | if ( false === $form ) { |
| @@ -688,9 +820,9 @@ | ||
| 688 | 820 | /** |
| 689 | 821 | * @since 5.0.03 |
| 690 | 822 | * |
| 691 | 823 | * @param array $field |
| 692 | - * @return stdClass|false false if there is no form object found with JS validation active. | |
| 824 | + * @return false|stdClass false if there is no form object found with JS validation active. | |
| 693 | 825 | */ |
| 694 | 826 | private static function get_form_for_js_validation( $field ) { |
| 695 | 827 | global $frm_vars; |
| 696 | 828 | if ( ! empty( $frm_vars['js_validate_forms'] ) ) { |
| @@ -707,9 +839,9 @@ | ||
| 707 | 839 | /** |
| 708 | 840 | * @param stdClass $form |
| 709 | 841 | * @param array $field |
| 710 | 842 | * @param array $errors |
| 711 | - * @return string|false | |
| 843 | + * @return false|string | |
| 712 | 844 | */ |
| 713 | 845 | public static function pull_custom_error_body_from_custom_html( $form, $field, $errors = array() ) { |
| 714 | 846 | if ( empty( $field['custom_html'] ) ) { |
| 715 | 847 | return false; |
| @@ -750,20 +882,30 @@ | ||
| 750 | 882 | * |
| 751 | 883 | * @since 3.06.01 |
| 752 | 884 | * @param array $field |
| 753 | 885 | * @param array $add_html |
| 886 | + * @return void | |
| 754 | 887 | */ |
| 755 | 888 | private static function maybe_add_html_required( $field, array &$add_html ) { |
| 756 | - if ( in_array( $field['type'], array( 'file', 'data', 'lookup' ), true ) ) { | |
| 889 | + $excluded_field_types = | |
| 890 | + FrmField::is_radio( $field ) || | |
| 891 | + FrmField::is_checkbox( $field ) || | |
| 892 | + FrmField::is_field_type( $field, 'file' ) || | |
| 893 | + FrmField::is_field_type( $field, 'nps' ) || | |
| 894 | + FrmField::is_field_type( $field, 'scale' ); | |
| 895 | + | |
| 896 | + if ( $excluded_field_types ) { | |
| 757 | 897 | return; |
| 758 | 898 | } |
| 759 | 899 | |
| 760 | - $include_html = FrmAppHelper::meets_min_pro_version( '3.06.01' ); | |
| 761 | - if ( $include_html ) { | |
| 762 | - $add_html['aria-required'] = 'aria-required="true"'; | |
| 763 | - } | |
| 900 | + $add_html['aria-required'] = 'aria-required="true"'; | |
| 764 | 901 | } |
| 765 | 902 | |
| 903 | + /** | |
| 904 | + * @param array $field | |
| 905 | + * @param array $add_html | |
| 906 | + * @return void | |
| 907 | + */ | |
| 766 | 908 | private static function add_shortcodes_to_html( $field, array &$add_html ) { |
| 767 | 909 | if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) { |
| 768 | 910 | return; |
| 769 | 911 | } |
| @@ -772,9 +914,9 @@ | ||
| 772 | 914 | unset( $field['shortcodes']['autocomplete'] ); |
| 773 | 915 | } |
| 774 | 916 | |
| 775 | 917 | foreach ( $field['shortcodes'] as $k => $v ) { |
| 776 | - if ( 'opt' === $k ) { | |
| 918 | + if ( 'opt' === $k || ! self::should_allow_input_attribute( $k ) ) { | |
| 777 | 919 | continue; |
| 778 | 920 | } |
| 779 | 921 | |
| 780 | 922 | if ( is_numeric( $k ) && strpos( $v, '=' ) ) { |
| @@ -789,28 +931,40 @@ | ||
| 789 | 931 | } |
| 790 | 932 | } |
| 791 | 933 | |
| 792 | 934 | /** |
| 793 | - * Add pattern attribute | |
| 935 | + * Disallow possibly unsafe attributees (that trigger JavaScript) when unasfe HTML is not allowed. | |
| 794 | 936 | * |
| 937 | + * @since 6.11.2 | |
| 938 | + * | |
| 939 | + * @param string $key The option key. | |
| 940 | + * @return bool | |
| 941 | + */ | |
| 942 | + private static function should_allow_input_attribute( $key ) { | |
| 943 | + if ( ! FrmAppHelper::should_never_allow_unfiltered_html() ) { | |
| 944 | + return true; | |
| 945 | + } | |
| 946 | + return FrmAppHelper::input_key_is_safe( $key ); | |
| 947 | + } | |
| 948 | + | |
| 949 | + /** | |
| 950 | + * Add pattern attribute. | |
| 951 | + * | |
| 795 | 952 | * @since 3.0 |
| 796 | 953 | * |
| 797 | 954 | * @param array $field |
| 798 | 955 | * @param array $add_html |
| 956 | + * @return void | |
| 799 | 957 | */ |
| 800 | 958 | private static function add_pattern_attribute( $field, array &$add_html ) { |
| 801 | 959 | $has_format = FrmField::is_option_true_in_array( $field, 'format' ); |
| 802 | 960 | $format_field = FrmField::is_field_type( $field, 'text' ); |
| 803 | 961 | |
| 804 | - if ( $field['type'] == 'phone' || ( $has_format && $format_field ) ) { | |
| 805 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 962 | + if ( $field['type'] === 'phone' || ( $has_format && $format_field ) ) { | |
| 963 | + $format = FrmEntryValidate::phone_format( $field ); | |
| 964 | + $format = substr( $format, 2, - 1 ); | |
| 806 | 965 | |
| 807 | - if ( $frm_settings->use_html ) { | |
| 808 | - $format = FrmEntryValidate::phone_format( $field ); | |
| 809 | - $format = substr( $format, 2, - 1 ); | |
| 810 | - | |
| 811 | - $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"'; | |
| 812 | - } | |
| 966 | + $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"'; | |
| 813 | 967 | } |
| 814 | 968 | } |
| 815 | 969 | |
| 816 | 970 | public static function check_value( $opt, $opt_key, $field ) { |
| @@ -826,88 +980,10 @@ | ||
| 826 | 980 | } |
| 827 | 981 | |
| 828 | 982 | public static function check_label( $opt ) { |
| 829 | 983 | if ( is_array( $opt ) ) { |
| 830 | - $opt = ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) ); | |
| 984 | + $opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt ); | |
| 831 | 985 | } |
| 832 | 986 | |
| 833 | 987 | return $opt; |
| 834 | - } | |
| 835 | - | |
| 836 | - /** | |
| 837 | - * @deprecated 4.0 | |
| 838 | - */ | |
| 839 | - public static function update_ajax_option() { | |
| 840 | - _deprecated_function( __METHOD__, '4.0' ); | |
| 841 | - FrmAppHelper::permission_check( 'frm_edit_forms' ); | |
| 842 | - check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 843 | - | |
| 844 | - $field_id = FrmAppHelper::get_post_param( 'field', 0, 'absint' ); | |
| 845 | - if ( ! $field_id ) { | |
| 846 | - wp_die(); | |
| 847 | - } | |
| 848 | - | |
| 849 | - $field = FrmField::getOne( $field_id ); | |
| 850 | - | |
| 851 | - if ( isset( $_POST['separate_value'] ) ) { | |
| 852 | - $new_val = FrmField::is_option_true( $field, 'separate_value' ) ? 0 : 1; | |
| 853 | - | |
| 854 | - $field->field_options['separate_value'] = $new_val; | |
| 855 | - unset( $new_val ); | |
| 856 | - } | |
| 857 | - | |
| 858 | - FrmField::update( | |
| 859 | - $field_id, | |
| 860 | - array( | |
| 861 | - 'field_options' => $field->field_options, | |
| 862 | - 'form_id' => $field->form_id, | |
| 863 | - ) | |
| 864 | - ); | |
| 865 | - wp_die(); | |
| 866 | - } | |
| 867 | - | |
| 868 | - /** | |
| 869 | - * @deprecated 4.0 | |
| 870 | - */ | |
| 871 | - public static function import_choices() { | |
| 872 | - _deprecated_function( __METHOD__, '4.0' ); | |
| 873 | - wp_die(); | |
| 874 | - } | |
| 875 | - | |
| 876 | - /** | |
| 877 | - * Add Single Option or Other Option. | |
| 878 | - * | |
| 879 | - * @deprecated 4.0 Moved to Pro for Other option only. | |
| 880 | - */ | |
| 881 | - public static function add_option() { | |
| 882 | - _deprecated_function( __METHOD__, '4.0', 'FrmProFieldsController::add_other_option' ); | |
| 883 | - } | |
| 884 | - | |
| 885 | - /** | |
| 886 | - * @deprecated 4.0 | |
| 887 | - */ | |
| 888 | - public static function update_order() { | |
| 889 | - FrmDeprecated::update_order(); | |
| 890 | - } | |
| 891 | - | |
| 892 | - /** | |
| 893 | - * @deprecated 3.0 | |
| 894 | - * @codeCoverageIgnore | |
| 895 | - */ | |
| 896 | - public static function edit_name( $field = 'name', $id = '' ) { | |
| 897 | - FrmDeprecated::edit_name( $field, $id ); | |
| 898 | - } | |
| 899 | - | |
| 900 | - /** | |
| 901 | - * @deprecated 3.0 | |
| 902 | - * @codeCoverageIgnore | |
| 903 | - * | |
| 904 | - * @param int $field_id | |
| 905 | - * @param array $values | |
| 906 | - * @param int $form_id | |
| 907 | - * | |
| 908 | - * @return array | |
| 909 | - */ | |
| 910 | - public static function include_single_field( $field_id, $values, $form_id = 0 ) { | |
| 911 | - return FrmDeprecated::include_single_field( $field_id, $values, $form_id ); | |
| 912 | 988 | } |
| 913 | 989 | } |