| @@ -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,15 +80,18 @@ | ||
| 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 ); |
| 83 | 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 | + | |
| 84 | 94 | /** |
| 85 | 95 | * @param array $field_values |
| 86 | 96 | */ |
| 87 | 97 | $field_values = apply_filters( 'frm_before_field_created', $field_values ); |
| @@ -139,11 +149,11 @@ | ||
| 139 | 149 | |
| 140 | 150 | /** |
| 141 | 151 | * @since 3.0 |
| 142 | 152 | * |
| 143 | - * @param int|array|object $field_object | |
| 144 | - * @param array $values | |
| 145 | - * @param int $form_id | |
| 153 | + * @param array|int|object $field_object | |
| 154 | + * @param array $values | |
| 155 | + * @param int $form_id | |
| 146 | 156 | */ |
| 147 | 157 | public static function load_single_field( $field_object, $values, $form_id = 0 ) { |
| 148 | 158 | global $frm_vars; |
| 149 | 159 | $frm_vars['is_admin'] = true; |
| @@ -157,30 +167,35 @@ | ||
| 157 | 167 | |
| 158 | 168 | $field_obj = FrmFieldFactory::get_field_factory( $field_object ); |
| 159 | 169 | $display = self::display_field_options( array(), $field_obj ); |
| 160 | 170 | |
| 161 | - $ajax_loading = isset( $values['ajax_load'] ) && $values['ajax_load']; | |
| 162 | - $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 ); | |
| 163 | 173 | |
| 164 | 174 | if ( $ajax_loading && $ajax_this_field ) { |
| 165 | 175 | $li_classes = self::get_classes_for_builder_field( array(), $display, $field_obj ); |
| 166 | - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php' ); | |
| 167 | - } else { | |
| 168 | - if ( ! isset( $field ) && is_object( $field_object ) ) { | |
| 169 | - $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 | + } | |
| 170 | 179 | |
| 171 | - $field = FrmFieldsHelper::setup_edit_vars( $field_object ); | |
| 172 | - } | |
| 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 | + } | |
| 173 | 184 | |
| 174 | - $li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj ); | |
| 175 | - $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'; | |
| 176 | 187 | |
| 177 | - require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' ); | |
| 178 | - } | |
| 188 | + require FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php'; | |
| 179 | 189 | } |
| 180 | 190 | |
| 181 | 191 | /** |
| 182 | 192 | * @since 3.0 |
| 193 | + * | |
| 194 | + * @param array $field | |
| 195 | + * @param array $display | |
| 196 | + * @param FrmFieldType $field_info | |
| 197 | + * @return string | |
| 183 | 198 | */ |
| 184 | 199 | private static function get_classes_for_builder_field( $field, $display, $field_info ) { |
| 185 | 200 | $li_classes = $field_info->form_builder_classes( $display['type'] ); |
| 186 | 201 | $li_classes .= ' frm_form_field frmstart '; |
| @@ -206,10 +221,11 @@ | ||
| 206 | 221 | FrmField::destroy( $field_id ); |
| 207 | 222 | wp_die(); |
| 208 | 223 | } |
| 209 | 224 | |
| 210 | - /* Field Options */ | |
| 211 | - | |
| 225 | + /** | |
| 226 | + * Field Options. | |
| 227 | + */ | |
| 212 | 228 | public static function import_options() { |
| 213 | 229 | FrmAppHelper::permission_check( 'frm_edit_forms' ); |
| 214 | 230 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 215 | 231 | |
| @@ -216,12 +232,23 @@ | ||
| 216 | 232 | if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) { |
| 217 | 233 | return; |
| 218 | 234 | } |
| 219 | 235 | |
| 220 | - $field_id = FrmAppHelper::get_param( 'field_id', '', 'post', 'absint' ); | |
| 221 | - $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' ); | |
| 222 | 239 | |
| 223 | - 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 ) ) { | |
| 224 | 251 | return; |
| 225 | 252 | } |
| 226 | 253 | |
| 227 | 254 | $field = FrmFieldsHelper::setup_edit_vars( $field ); |
| @@ -228,15 +255,15 @@ | ||
| 228 | 255 | $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' ); |
| 229 | 256 | $opts = explode( "\n", rtrim( $opts, "\n" ) ); |
| 230 | 257 | $opts = array_map( 'trim', $opts ); |
| 231 | 258 | |
| 232 | - $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' ); | |
| 233 | - $field['separate_value'] = ( $separate === 'true' ); | |
| 259 | + $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' ); | |
| 260 | + $field['separate_value'] = $separate === 'true'; | |
| 234 | 261 | |
| 235 | 262 | if ( $field['separate_value'] ) { |
| 236 | 263 | foreach ( $opts as $opt_key => $opt ) { |
| 237 | 264 | if ( strpos( $opt, '|' ) !== false ) { |
| 238 | - $vals = explode( '|', $opt ); | |
| 265 | + $vals = explode( '|', $opt ); | |
| 239 | 266 | $opts[ $opt_key ] = array( |
| 240 | 267 | 'label' => trim( $vals[0] ), |
| 241 | 268 | 'value' => trim( $vals[1] ), |
| 242 | 269 | ); |
| @@ -270,8 +297,9 @@ | ||
| 270 | 297 | /** |
| 271 | 298 | * @since 4.0 |
| 272 | 299 | * |
| 273 | 300 | * @param array $atts - Includes field array, field_obj, display array, values array. |
| 301 | + * @return void | |
| 274 | 302 | */ |
| 275 | 303 | public static function load_single_field_settings( $atts ) { |
| 276 | 304 | $field = $atts['field']; |
| 277 | 305 | $field_obj = $atts['field_obj']; |
| @@ -286,13 +314,13 @@ | ||
| 286 | 314 | if ( ! isset( $field['read_only'] ) ) { |
| 287 | 315 | $field['read_only'] = false; |
| 288 | 316 | } |
| 289 | 317 | |
| 290 | - $field_types = FrmFieldsHelper::get_field_types( $field['type'] ); | |
| 291 | - $pro_field_selection = FrmField::pro_field_selection(); | |
| 292 | - $all_field_types = array_merge( $pro_field_selection, FrmField::field_selection() ); | |
| 293 | - $disabled_fields = FrmAppHelper::pro_is_installed() ? array() : $pro_field_selection; | |
| 294 | - $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'] ); | |
| 295 | 323 | |
| 296 | 324 | if ( ! isset( $all_field_types[ $field['type'] ] ) ) { |
| 297 | 325 | // Add fallback for an add-on field type that has been deactivated. |
| 298 | 326 | $all_field_types[ $field['type'] ] = array( |
| @@ -320,16 +348,31 @@ | ||
| 320 | 348 | include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php'; |
| 321 | 349 | } |
| 322 | 350 | |
| 323 | 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 | + /** | |
| 324 | 364 | * Get the list of default value types that can be toggled in the builder. |
| 325 | 365 | * |
| 326 | 366 | * @since 4.0 |
| 367 | + * | |
| 368 | + * @param array $field | |
| 369 | + * @param array $atts | |
| 327 | 370 | * @return array |
| 328 | 371 | */ |
| 329 | 372 | private static function default_value_types( $field, $atts ) { |
| 330 | 373 | $types = array( |
| 331 | - 'default_value' => array( | |
| 374 | + 'default_value' => array( | |
| 332 | 375 | 'class' => '', |
| 333 | 376 | 'icon' => 'frm_icon_font frm_text2_icon', |
| 334 | 377 | 'title' => __( 'Default Value (Text)', 'formidable' ), |
| 335 | 378 | 'data' => array( |
| @@ -335,9 +378,9 @@ | ||
| 335 | 378 | 'data' => array( |
| 336 | 379 | 'frmshow' => '#default-value-for-', |
| 337 | 380 | ), |
| 338 | 381 | ), |
| 339 | - 'calc' => array( | |
| 382 | + 'calc' => array( | |
| 340 | 383 | 'class' => 'frm_show_upgrade frm_noallow', |
| 341 | 384 | 'title' => __( 'Default Value (Calculation)', 'formidable' ), |
| 342 | 385 | 'icon' => 'frm_icon_font frm_calculator_icon', |
| 343 | 386 | 'data' => array( |
| @@ -357,13 +400,8 @@ | ||
| 357 | 400 | ); |
| 358 | 401 | |
| 359 | 402 | $types = apply_filters( 'frm_default_value_types', $types, $atts ); |
| 360 | 403 | |
| 361 | - if ( FrmAppHelper::pro_is_installed() && ! FrmAppHelper::meets_min_pro_version( '4.0' ) ) { | |
| 362 | - // Prevent settings from showing in 2 spots. | |
| 363 | - unset( $types['calc'], $types['get_values_field'] ); | |
| 364 | - } | |
| 365 | - | |
| 366 | 404 | // Set active class. |
| 367 | 405 | $settings = array_keys( $types ); |
| 368 | 406 | $active = 'default_value'; |
| 369 | 407 | |
| @@ -378,8 +416,12 @@ | ||
| 378 | 416 | |
| 379 | 417 | return $types; |
| 380 | 418 | } |
| 381 | 419 | |
| 420 | + /** | |
| 421 | + * @param string $type | |
| 422 | + * @return string | |
| 423 | + */ | |
| 382 | 424 | public static function change_type( $type ) { |
| 383 | 425 | $type_switch = array( |
| 384 | 426 | 'scale' => 'radio', |
| 385 | 427 | 'star' => 'radio', |
| @@ -404,16 +446,18 @@ | ||
| 404 | 446 | return $type; |
| 405 | 447 | } |
| 406 | 448 | |
| 407 | 449 | /** |
| 408 | - * @param array $settings | |
| 409 | - * @param object $field_info | |
| 450 | + * @param array $settings | |
| 451 | + * @param FrmFieldType|null $field_info | |
| 410 | 452 | * |
| 411 | 453 | * @return array |
| 412 | 454 | */ |
| 413 | 455 | public static function display_field_options( $settings, $field_info = null ) { |
| 414 | 456 | if ( $field_info ) { |
| 415 | - $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. | |
| 416 | 460 | $settings['field_data'] = $field_info->field; |
| 417 | 461 | } |
| 418 | 462 | |
| 419 | 463 | return apply_filters( 'frm_display_field_options', $settings ); |
| @@ -423,12 +467,23 @@ | ||
| 423 | 467 | * Display the format option |
| 424 | 468 | * |
| 425 | 469 | * @since 3.0 |
| 426 | 470 | * |
| 427 | - * @param array $field | |
| 471 | + * @param array $field Field data. | |
| 472 | + * @param bool $is_hidden Whether the format option should be hidden. | |
| 473 | + * @return void | |
| 428 | 474 | */ |
| 429 | - public static function show_format_option( $field ) { | |
| 430 | - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php' ); | |
| 475 | + public static function show_format_option( $field, $is_hidden = false ) { | |
| 476 | + $attributes = array( | |
| 477 | + 'class' => 'frm-has-modal', | |
| 478 | + 'id' => 'frm-field-format-custom-' . $field['id'], | |
| 479 | + ); | |
| 480 | + | |
| 481 | + if ( $is_hidden ) { | |
| 482 | + $attributes['class'] .= ' frm_hidden'; | |
| 483 | + } | |
| 484 | + | |
| 485 | + include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php'; | |
| 431 | 486 | } |
| 432 | 487 | |
| 433 | 488 | public static function input_html( $field, $echo = true ) { |
| 434 | 489 | $class = array(); |
| @@ -456,14 +511,19 @@ | ||
| 456 | 511 | |
| 457 | 512 | return $add_html; |
| 458 | 513 | } |
| 459 | 514 | |
| 515 | + /** | |
| 516 | + * @param array $field | |
| 517 | + * @param array $class | |
| 518 | + * @return void | |
| 519 | + */ | |
| 460 | 520 | private static function add_input_classes( $field, array &$class ) { |
| 461 | - if ( isset( $field['input_class'] ) && ! empty( $field['input_class'] ) ) { | |
| 521 | + if ( ! empty( $field['input_class'] ) ) { | |
| 462 | 522 | $class[] = $field['input_class']; |
| 463 | 523 | } |
| 464 | 524 | |
| 465 | - if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) { | |
| 525 | + if ( $field['type'] === 'hidden' || $field['type'] === 'user_id' ) { | |
| 466 | 526 | return; |
| 467 | 527 | } |
| 468 | 528 | |
| 469 | 529 | if ( isset( $field['size'] ) && $field['size'] > 0 ) { |
| @@ -470,8 +530,13 @@ | ||
| 470 | 530 | $class[] = 'auto_width'; |
| 471 | 531 | } |
| 472 | 532 | } |
| 473 | 533 | |
| 534 | + /** | |
| 535 | + * @param array $field | |
| 536 | + * @param array $add_html | |
| 537 | + * @return void | |
| 538 | + */ | |
| 474 | 539 | private static function add_html_size( $field, array &$add_html ) { |
| 475 | 540 | $size_fields = array( |
| 476 | 541 | 'select', |
| 477 | 542 | 'data', |
| @@ -480,9 +545,9 @@ | ||
| 480 | 545 | 'file', |
| 481 | 546 | 'lookup', |
| 482 | 547 | ); |
| 483 | 548 | |
| 484 | - if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields ) ) { | |
| 549 | + if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields, true ) ) { | |
| 485 | 550 | return; |
| 486 | 551 | } |
| 487 | 552 | |
| 488 | 553 | if ( FrmAppHelper::is_admin_page( 'formidable' ) ) { |
| @@ -499,8 +564,13 @@ | ||
| 499 | 564 | |
| 500 | 565 | self::add_html_cols( $field, $add_html ); |
| 501 | 566 | } |
| 502 | 567 | |
| 568 | + /** | |
| 569 | + * @param array $field | |
| 570 | + * @param array $add_html | |
| 571 | + * @return void | |
| 572 | + */ | |
| 503 | 573 | private static function add_html_cols( $field, array &$add_html ) { |
| 504 | 574 | if ( ! in_array( $field['type'], array( 'textarea', 'rte' ), true ) ) { |
| 505 | 575 | return; |
| 506 | 576 | } |
| @@ -524,8 +594,13 @@ | ||
| 524 | 594 | |
| 525 | 595 | $add_html['cols'] = 'cols="' . absint( $size ) . '"'; |
| 526 | 596 | } |
| 527 | 597 | |
| 598 | + /** | |
| 599 | + * @param array $field | |
| 600 | + * @param array $add_html | |
| 601 | + * @return void | |
| 602 | + */ | |
| 528 | 603 | private static function add_html_length( $field, array &$add_html ) { |
| 529 | 604 | // Check for max setting and if this field accepts maxlength. |
| 530 | 605 | $fields = array( |
| 531 | 606 | 'textarea', |
| @@ -533,9 +608,9 @@ | ||
| 533 | 608 | 'hidden', |
| 534 | 609 | 'file', |
| 535 | 610 | ); |
| 536 | 611 | |
| 537 | - if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields ) ) { | |
| 612 | + if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields, true ) ) { | |
| 538 | 613 | return; |
| 539 | 614 | } |
| 540 | 615 | |
| 541 | 616 | if ( FrmAppHelper::is_admin_page( 'formidable' ) ) { |
| @@ -545,8 +620,14 @@ | ||
| 545 | 620 | |
| 546 | 621 | $add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"'; |
| 547 | 622 | } |
| 548 | 623 | |
| 624 | + /** | |
| 625 | + * @param array $field | |
| 626 | + * @param array $add_html | |
| 627 | + * @param array $class | |
| 628 | + * @return void | |
| 629 | + */ | |
| 549 | 630 | private static function add_html_placeholder( $field, array &$add_html, array &$class ) { |
| 550 | 631 | if ( $field['default_value'] != '' ) { |
| 551 | 632 | if ( is_array( $field['default_value'] ) ) { |
| 552 | 633 | $add_html['data-frmval'] = 'data-frmval="' . esc_attr( json_encode( $field['default_value'] ) ) . '"'; |
| @@ -560,21 +641,9 @@ | ||
| 560 | 641 | // don't include a json placeholder |
| 561 | 642 | return; |
| 562 | 643 | } |
| 563 | 644 | |
| 564 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 565 | - | |
| 566 | - if ( $frm_settings->use_html ) { | |
| 567 | - self::add_placeholder_to_input( $field, $add_html ); | |
| 568 | - } else { | |
| 569 | - self::add_frmval_to_input( $field, $add_html ); | |
| 570 | - | |
| 571 | - $class[] = 'frm_toggle_default'; | |
| 572 | - | |
| 573 | - if ( $field['value'] == $field['placeholder'] ) { | |
| 574 | - $class[] = 'frm_default'; | |
| 575 | - } | |
| 576 | - } | |
| 645 | + self::add_placeholder_to_input( $field, $add_html ); | |
| 577 | 646 | } |
| 578 | 647 | |
| 579 | 648 | /** |
| 580 | 649 | * Prepares field placeholder. |
| @@ -617,14 +686,26 @@ | ||
| 617 | 686 | if ( empty( $placeholder ) ) { |
| 618 | 687 | $placeholder = self::get_default_value_from_name( $field ); |
| 619 | 688 | } |
| 620 | 689 | |
| 690 | + $use_placeholder = $placeholder; | |
| 691 | + $autocomplete = FrmField::get_option( $field, 'autocom' ); | |
| 692 | + | |
| 693 | + if ( $autocomplete ) { | |
| 694 | + $use_chosen = ! is_callable( 'FrmProAppHelper::use_chosen_js' ) || FrmProAppHelper::use_chosen_js(); | |
| 695 | + if ( $use_chosen ) { | |
| 696 | + $use_placeholder = ''; | |
| 697 | + } | |
| 698 | + } | |
| 699 | + | |
| 621 | 700 | if ( $placeholder !== '' ) { |
| 622 | - ?> | |
| 623 | - <option class="frm-select-placeholder" value=""> | |
| 624 | - <?php echo esc_html( FrmField::get_option( $field, 'autocom' ) ? '' : $placeholder ); ?> | |
| 625 | - </option> | |
| 626 | - <?php | |
| 701 | + $placeholder_attributes = array( | |
| 702 | + 'class' => 'frm-select-placeholder', | |
| 703 | + 'value' => '', | |
| 704 | + 'data-placeholder' => 'true', | |
| 705 | + ); | |
| 706 | + | |
| 707 | + FrmHtmlHelper::echo_dropdown_option( $use_placeholder, false, $placeholder_attributes ); | |
| 627 | 708 | return true; |
| 628 | 709 | } |
| 629 | 710 | |
| 630 | 711 | return false; |
| @@ -630,12 +711,13 @@ | ||
| 630 | 711 | return false; |
| 631 | 712 | } |
| 632 | 713 | |
| 633 | 714 | /** |
| 634 | - * Use HMTL5 placeholder with js fallback | |
| 715 | + * Use HTML5 placeholder with js fallback. | |
| 635 | 716 | * |
| 636 | 717 | * @param array $field |
| 637 | 718 | * @param array $add_html |
| 719 | + * @return void | |
| 638 | 720 | */ |
| 639 | 721 | private static function add_placeholder_to_input( $field, &$add_html ) { |
| 640 | 722 | if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) { |
| 641 | 723 | $add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"'; |
| @@ -641,8 +723,13 @@ | ||
| 641 | 723 | $add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"'; |
| 642 | 724 | } |
| 643 | 725 | } |
| 644 | 726 | |
| 727 | + /** | |
| 728 | + * @param array $field | |
| 729 | + * @param array $add_html | |
| 730 | + * @return void | |
| 731 | + */ | |
| 645 | 732 | private static function add_frmval_to_input( $field, &$add_html ) { |
| 646 | 733 | if ( $field['placeholder'] != '' ) { |
| 647 | 734 | $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['placeholder'] ) . '"'; |
| 648 | 735 | |
| @@ -656,15 +743,17 @@ | ||
| 656 | 743 | } |
| 657 | 744 | } |
| 658 | 745 | |
| 659 | 746 | private static function add_validation_messages( $field, array &$add_html ) { |
| 660 | - if ( FrmField::is_required( $field ) ) { | |
| 747 | + $field_validation_messages_status = self::get_validation_data_attribute_visibility_info( $field ); | |
| 748 | + | |
| 749 | + if ( FrmField::is_required( $field ) && ! empty( $field_validation_messages_status['data-reqmsg'] ) ) { | |
| 661 | 750 | $required_message = FrmFieldsHelper::get_error_msg( $field, 'blank' ); |
| 662 | 751 | $add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"'; |
| 663 | 752 | self::maybe_add_html_required( $field, $add_html ); |
| 664 | 753 | } |
| 665 | 754 | |
| 666 | - if ( ! FrmField::is_option_empty( $field, 'invalid' ) ) { | |
| 755 | + if ( ! FrmField::is_option_empty( $field, 'invalid' ) && ! empty( $field_validation_messages_status['data-invmsg'] ) ) { | |
| 667 | 756 | $invalid_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); |
| 668 | 757 | $add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"'; |
| 669 | 758 | } |
| 670 | 759 | |
| @@ -673,12 +762,45 @@ | ||
| 673 | 762 | } |
| 674 | 763 | } |
| 675 | 764 | |
| 676 | 765 | /** |
| 766 | + * Returns an array that contains field validation messages status. | |
| 767 | + * | |
| 768 | + * @since 6.9 | |
| 769 | + * | |
| 770 | + * @param array|object $field | |
| 771 | + * @return array | |
| 772 | + */ | |
| 773 | + private static function get_validation_data_attribute_visibility_info( $field ) { | |
| 774 | + if ( FrmField::get_field_type( $field ) === 'hidden' ) { | |
| 775 | + $field_validation_data_attributes = array( | |
| 776 | + 'data-invmsg' => false, | |
| 777 | + 'data-reqmsg' => false, | |
| 778 | + ); | |
| 779 | + } else { | |
| 780 | + $field_validation_data_attributes = array( | |
| 781 | + 'data-invmsg' => true, | |
| 782 | + 'data-reqmsg' => true, | |
| 783 | + ); | |
| 784 | + } | |
| 785 | + | |
| 786 | + /** | |
| 787 | + * Allows controlling which field validation messages would be included in the field html. | |
| 788 | + * | |
| 789 | + * @since 6.9 | |
| 790 | + * | |
| 791 | + * @param array $field_validation_messages_status | |
| 792 | + * @param array|object $field | |
| 793 | + */ | |
| 794 | + return apply_filters( 'frm_field_validation_include_data_attributes', $field_validation_data_attributes, $field ); | |
| 795 | + } | |
| 796 | + | |
| 797 | + /** | |
| 677 | 798 | * @since 5.0.03 |
| 678 | 799 | * |
| 679 | 800 | * @param array $field |
| 680 | 801 | * @param array $add_html |
| 802 | + * @return void | |
| 681 | 803 | */ |
| 682 | 804 | private static function maybe_add_error_html_for_js_validation( $field, array &$add_html ) { |
| 683 | 805 | $form = self::get_form_for_js_validation( $field ); |
| 684 | 806 | if ( false === $form ) { |
| @@ -695,9 +817,9 @@ | ||
| 695 | 817 | /** |
| 696 | 818 | * @since 5.0.03 |
| 697 | 819 | * |
| 698 | 820 | * @param array $field |
| 699 | - * @return stdClass|false false if there is no form object found with JS validation active. | |
| 821 | + * @return false|stdClass false if there is no form object found with JS validation active. | |
| 700 | 822 | */ |
| 701 | 823 | private static function get_form_for_js_validation( $field ) { |
| 702 | 824 | global $frm_vars; |
| 703 | 825 | if ( ! empty( $frm_vars['js_validate_forms'] ) ) { |
| @@ -714,9 +836,9 @@ | ||
| 714 | 836 | /** |
| 715 | 837 | * @param stdClass $form |
| 716 | 838 | * @param array $field |
| 717 | 839 | * @param array $errors |
| 718 | - * @return string|false | |
| 840 | + * @return false|string | |
| 719 | 841 | */ |
| 720 | 842 | public static function pull_custom_error_body_from_custom_html( $form, $field, $errors = array() ) { |
| 721 | 843 | if ( empty( $field['custom_html'] ) ) { |
| 722 | 844 | return false; |
| @@ -757,8 +879,9 @@ | ||
| 757 | 879 | * |
| 758 | 880 | * @since 3.06.01 |
| 759 | 881 | * @param array $field |
| 760 | 882 | * @param array $add_html |
| 883 | + * @return void | |
| 761 | 884 | */ |
| 762 | 885 | private static function maybe_add_html_required( $field, array &$add_html ) { |
| 763 | 886 | $excluded_field_types = |
| 764 | 887 | FrmField::is_radio( $field ) || |
| @@ -770,14 +893,16 @@ | ||
| 770 | 893 | if ( $excluded_field_types ) { |
| 771 | 894 | return; |
| 772 | 895 | } |
| 773 | 896 | |
| 774 | - $include_html = FrmAppHelper::meets_min_pro_version( '3.06.01' ); | |
| 775 | - if ( $include_html ) { | |
| 776 | - $add_html['aria-required'] = 'aria-required="true"'; | |
| 777 | - } | |
| 897 | + $add_html['aria-required'] = 'aria-required="true"'; | |
| 778 | 898 | } |
| 779 | 899 | |
| 900 | + /** | |
| 901 | + * @param array $field | |
| 902 | + * @param array $add_html | |
| 903 | + * @return void | |
| 904 | + */ | |
| 780 | 905 | private static function add_shortcodes_to_html( $field, array &$add_html ) { |
| 781 | 906 | if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) { |
| 782 | 907 | return; |
| 783 | 908 | } |
| @@ -786,9 +911,19 @@ | ||
| 786 | 911 | unset( $field['shortcodes']['autocomplete'] ); |
| 787 | 912 | } |
| 788 | 913 | |
| 789 | 914 | foreach ( $field['shortcodes'] as $k => $v ) { |
| 790 | - if ( 'opt' === $k ) { | |
| 915 | + if ( isset( $field['subfield_name'] ) && 0 === strpos( $k, 'aria-invalid' ) ) { | |
| 916 | + $subfield_name = $field['subfield_name']; | |
| 917 | + if ( ! isset( $field['shortcodes'][ 'aria-invalid-' . $subfield_name ] ) ) { | |
| 918 | + continue; | |
| 919 | + } | |
| 920 | + // Change the key to the correct aria-invalid value so that $add_html is set correctly for the current subfield of a combo field. | |
| 921 | + $k = 'aria-invalid'; | |
| 922 | + $v = $field['shortcodes'][ 'aria-invalid-' . $subfield_name ]; | |
| 923 | + unset( $field['shortcodes'][ 'aria-invalid-' . $subfield_name ] ); | |
| 924 | + } | |
| 925 | + if ( 'opt' === $k || ! self::should_allow_input_attribute( $k ) ) { | |
| 791 | 926 | continue; |
| 792 | 927 | } |
| 793 | 928 | |
| 794 | 929 | if ( is_numeric( $k ) && strpos( $v, '=' ) ) { |
| @@ -799,32 +934,45 @@ | ||
| 799 | 934 | $add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"'; |
| 800 | 935 | } |
| 801 | 936 | |
| 802 | 937 | unset( $k, $v ); |
| 938 | + }//end foreach | |
| 939 | + } | |
| 940 | + | |
| 941 | + /** | |
| 942 | + * Disallow possibly unsafe attributees (that trigger JavaScript) when unasfe HTML is not allowed. | |
| 943 | + * | |
| 944 | + * @since 6.11.2 | |
| 945 | + * | |
| 946 | + * @param string $key The option key. | |
| 947 | + * @return bool | |
| 948 | + */ | |
| 949 | + private static function should_allow_input_attribute( $key ) { | |
| 950 | + if ( ! FrmAppHelper::should_never_allow_unfiltered_html() ) { | |
| 951 | + return true; | |
| 803 | 952 | } |
| 953 | + return FrmAppHelper::input_key_is_safe( $key ); | |
| 804 | 954 | } |
| 805 | 955 | |
| 806 | 956 | /** |
| 807 | - * Add pattern attribute | |
| 957 | + * Add pattern attribute. | |
| 808 | 958 | * |
| 809 | 959 | * @since 3.0 |
| 810 | 960 | * |
| 811 | 961 | * @param array $field |
| 812 | 962 | * @param array $add_html |
| 963 | + * @return void | |
| 813 | 964 | */ |
| 814 | 965 | private static function add_pattern_attribute( $field, array &$add_html ) { |
| 815 | - $has_format = FrmField::is_option_true_in_array( $field, 'format' ); | |
| 966 | + $format_value = FrmField::get_option( $field, 'format' ); | |
| 967 | + $has_format = $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value ); | |
| 816 | 968 | $format_field = FrmField::is_field_type( $field, 'text' ); |
| 817 | 969 | |
| 818 | - if ( $field['type'] == 'phone' || ( $has_format && $format_field ) ) { | |
| 819 | - $frm_settings = FrmAppHelper::get_settings(); | |
| 970 | + if ( $field['type'] === 'phone' || ( $has_format && $format_field ) ) { | |
| 971 | + $format = FrmEntryValidate::phone_format( $field ); | |
| 972 | + $format = substr( $format, 2, - 1 ); | |
| 820 | 973 | |
| 821 | - if ( $frm_settings->use_html ) { | |
| 822 | - $format = FrmEntryValidate::phone_format( $field ); | |
| 823 | - $format = substr( $format, 2, - 1 ); | |
| 824 | - | |
| 825 | - $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"'; | |
| 826 | - } | |
| 974 | + $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"'; | |
| 827 | 975 | } |
| 828 | 976 | } |
| 829 | 977 | |
| 830 | 978 | public static function check_value( $opt, $opt_key, $field ) { |
| @@ -840,88 +988,10 @@ | ||
| 840 | 988 | } |
| 841 | 989 | |
| 842 | 990 | public static function check_label( $opt ) { |
| 843 | 991 | if ( is_array( $opt ) ) { |
| 844 | - $opt = ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) ); | |
| 992 | + $opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt ); | |
| 845 | 993 | } |
| 846 | 994 | |
| 847 | 995 | return $opt; |
| 848 | - } | |
| 849 | - | |
| 850 | - /** | |
| 851 | - * @deprecated 4.0 | |
| 852 | - */ | |
| 853 | - public static function update_ajax_option() { | |
| 854 | - _deprecated_function( __METHOD__, '4.0' ); | |
| 855 | - FrmAppHelper::permission_check( 'frm_edit_forms' ); | |
| 856 | - check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 857 | - | |
| 858 | - $field_id = FrmAppHelper::get_post_param( 'field', 0, 'absint' ); | |
| 859 | - if ( ! $field_id ) { | |
| 860 | - wp_die(); | |
| 861 | - } | |
| 862 | - | |
| 863 | - $field = FrmField::getOne( $field_id ); | |
| 864 | - | |
| 865 | - if ( isset( $_POST['separate_value'] ) ) { | |
| 866 | - $new_val = FrmField::is_option_true( $field, 'separate_value' ) ? 0 : 1; | |
| 867 | - | |
| 868 | - $field->field_options['separate_value'] = $new_val; | |
| 869 | - unset( $new_val ); | |
| 870 | - } | |
| 871 | - | |
| 872 | - FrmField::update( | |
| 873 | - $field_id, | |
| 874 | - array( | |
| 875 | - 'field_options' => $field->field_options, | |
| 876 | - 'form_id' => $field->form_id, | |
| 877 | - ) | |
| 878 | - ); | |
| 879 | - wp_die(); | |
| 880 | - } | |
| 881 | - | |
| 882 | - /** | |
| 883 | - * @deprecated 4.0 | |
| 884 | - */ | |
| 885 | - public static function import_choices() { | |
| 886 | - _deprecated_function( __METHOD__, '4.0' ); | |
| 887 | - wp_die(); | |
| 888 | - } | |
| 889 | - | |
| 890 | - /** | |
| 891 | - * Add Single Option or Other Option. | |
| 892 | - * | |
| 893 | - * @deprecated 4.0 Moved to Pro for Other option only. | |
| 894 | - */ | |
| 895 | - public static function add_option() { | |
| 896 | - _deprecated_function( __METHOD__, '4.0', 'FrmProFieldsController::add_other_option' ); | |
| 897 | - } | |
| 898 | - | |
| 899 | - /** | |
| 900 | - * @deprecated 4.0 | |
| 901 | - */ | |
| 902 | - public static function update_order() { | |
| 903 | - FrmDeprecated::update_order(); | |
| 904 | - } | |
| 905 | - | |
| 906 | - /** | |
| 907 | - * @deprecated 3.0 | |
| 908 | - * @codeCoverageIgnore | |
| 909 | - */ | |
| 910 | - public static function edit_name( $field = 'name', $id = '' ) { | |
| 911 | - FrmDeprecated::edit_name( $field, $id ); | |
| 912 | - } | |
| 913 | - | |
| 914 | - /** | |
| 915 | - * @deprecated 3.0 | |
| 916 | - * @codeCoverageIgnore | |
| 917 | - * | |
| 918 | - * @param int $field_id | |
| 919 | - * @param array $values | |
| 920 | - * @param int $form_id | |
| 921 | - * | |
| 922 | - * @return array | |
| 923 | - */ | |
| 924 | - public static function include_single_field( $field_id, $values, $form_id = 0 ) { | |
| 925 | - return FrmDeprecated::include_single_field( $field_id, $values, $form_id ); | |
| 926 | 996 | } |
| 927 | 997 | } |