PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/controllers/FrmFieldsController.php +278 -86 6.26 → trunk View file →
@@ -11,48 +11,53 @@
11 11 * @var FrmFieldSelectionData|null
12 12 */
13 13 private static $field_selection_data;
14 14
15 + /**
16 + * Render the fields the form builder asked for over ajax.
17 + *
18 + * The browser sends field ids only. The fields themselves are read from the form, which is one
19 + * indexed query shared with the rest of the request through the field cache, and cheaper than
20 + * shipping every field's data down to the page and straight back up again.
21 + *
22 + * @return void
23 + */
15 24 public static function load_field() {
16 25 FrmAppHelper::permission_check( 'frm_edit_forms' );
17 26 check_ajax_referer( 'frm_ajax', 'nonce' );
18 27
19 - // Javascript may be included in some field settings.
20 - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
21 - $fields = isset( $_POST['field'] ) ? wp_unslash( $_POST['field'] ) : array();
28 + $field_ids = FrmAppHelper::get_post_param( 'field_ids', array(), 'absint' );
29 + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
22 30
23 - if ( empty( $fields ) ) {
31 + if ( ! $form_id || ! is_array( $field_ids ) || ! $field_ids ) {
24 32 wp_die();
25 33 }
26 34
27 35 $_GET['page'] = 'formidable';
28 -
29 - $values = array(
30 - 'id' => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ),
36 + $fields = self::get_builder_fields_by_id( $form_id );
37 + $values = array(
38 + 'id' => $form_id,
31 39 'doing_ajax' => true,
32 40 );
33 - $field_html = array();
41 + $field_html = array();
34 42
35 - foreach ( $fields as $field ) {
36 - $field = htmlspecialchars_decode( nl2br( $field ) );
37 - $field = json_decode( $field );
38 -
39 - if ( ! isset( $field->id ) || ! is_numeric( $field->id ) ) {
40 - // this field may have already been loaded
43 + foreach ( $field_ids as $field_id ) {
44 + if ( ! isset( $fields[ $field_id ] ) ) {
45 + // This field may have already been loaded, or is no longer in the form.
41 46 continue;
42 47 }
43 48
44 - if ( ! isset( $field->value ) ) {
45 - $field->value = '';
46 - }
47 - $field->field_options = json_decode( json_encode( $field->field_options ), true );
48 - $field->options = json_decode( json_encode( $field->options ), true );
49 - $field->default_value = json_decode( json_encode( $field->default_value ), true );
49 + $field = $fields[ $field_id ];
50 50
51 51 ob_start();
52 52 self::load_single_field( $field, $values );
53 - $field_html[ absint( $field->id ) ] = ob_get_contents();
54 - ob_end_clean();
53 +
54 + $field_html[ $field_id ] = array(
55 + // The type travels with the html so the js can report it to frm_ajax_loaded_field
56 + // listeners without a copy of the field.
57 + 'type' => $field->type,
58 + 'html' => ob_get_clean(),
59 + );
55 60 }//end foreach
56 61
57 62 echo json_encode( $field_html );
58 63
@@ -59,8 +64,40 @@
59 64 wp_die();
60 65 }
61 66
62 67 /**
68 + * Get a form's fields, as the form builder sees them, indexed by field id.
69 + *
70 + * @since 6.35
71 + *
72 + * @param int $form_id
73 + *
74 + * @return array Field objects keyed by field id. Empty if the form is gone.
75 + */
76 + private static function get_builder_fields_by_id( $form_id ) {
77 + $form = FrmForm::getOne( $form_id );
78 +
79 + if ( ! $form ) {
80 + return array();
81 + }
82 +
83 + $fields = FrmField::get_all_for_form( $form_id );
84 +
85 + /** This filter is documented in classes/controllers/FrmFormsController.php */
86 + $fields = apply_filters( 'frm_fields_in_form_builder', $fields, compact( 'form' ) );
87 +
88 + $fields_by_id = array();
89 +
90 + foreach ( (array) $fields as $field ) {
91 + if ( is_object( $field ) && ! empty( $field->id ) ) {
92 + $fields_by_id[ (int) $field->id ] = $field;
93 + }
94 + }
95 +
96 + return $fields_by_id;
97 + }
98 +
99 + /**
63 100 * Create a new field with ajax
64 101 */
65 102 public static function create() {
66 103 FrmAppHelper::permission_check( 'frm_edit_forms' );
@@ -73,9 +110,9 @@
73 110 do_action( 'frm_before_create_field', $field_type, $form_id );
74 111
75 112 $field = self::include_new_field( $field_type, $form_id, $field_options );
76 113
77 - // this hook will allow for multiple fields to be added at once
114 + // This hook will allow for multiple fields to be added at once
78 115 do_action( 'frm_after_field_created', $field, $form_id );
79 116
80 117 wp_die();
81 118 }
@@ -91,9 +128,9 @@
91 128 */
92 129 public static function include_new_field( $field_type, $form_id, $field_options = array() ) {
93 130 $field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
94 131
95 - if ( ! empty( $field_options ) ) {
132 + if ( $field_options ) {
96 133 $field_values['field_options'] = array_merge( $field_values['field_options'], $field_options );
97 134 }
98 135
99 136 // When a new field is added to the form, flag it as draft and hide it from the front-end.
@@ -108,17 +145,15 @@
108 145 if ( ! $field_id ) {
109 146 return false;
110 147 }
111 148
112 - $field = self::get_field_array_from_id( $field_id );
113 -
149 + $field = self::get_field_array_from_id( $field_id );
114 150 $values = array();
115 151
116 152 if ( FrmAppHelper::pro_is_installed() ) {
117 153 $values['post_type'] = FrmProFormsHelper::post_type( $form_id );
154 + $parent_form_id = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'parent_form_id' );
118 155
119 - $parent_form_id = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'parent_form_id' );
120 -
121 156 if ( $parent_form_id ) {
122 157 $field['parent_form_id'] = $parent_form_id;
123 158 }
124 159 }
@@ -131,15 +166,14 @@
131 166 public static function duplicate() {
132 167 FrmAppHelper::permission_check( 'frm_edit_forms' );
133 168 check_ajax_referer( 'frm_ajax', 'nonce' );
134 169
135 - $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
136 - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
137 -
170 + $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
171 + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
138 172 $new_field = FrmField::duplicate_single_field( $field_id, $form_id );
139 173
140 174 if ( is_array( $new_field ) && ! empty( $new_field['field_id'] ) ) {
141 - self::load_single_field( $new_field['field_id'], $new_field['values'] );
175 + self::load_single_field( $new_field['field_id'], $new_field['values'], $form_id );
142 176 }
143 177
144 178 wp_die();
145 179 }
@@ -152,9 +186,8 @@
152 186 * @return array
153 187 */
154 188 public static function get_field_array_from_id( $field_id ) {
155 189 $field = FrmField::getOne( $field_id );
156 -
157 190 return FrmFieldsHelper::setup_edit_vars( $field );
158 191 }
159 192
160 193 /**
@@ -159,11 +192,11 @@
159 192
160 193 /**
161 194 * @since 3.0
162 195 *
163 - * @param array|int|object $field_object
164 - * @param array $values
165 - * @param int $form_id
196 + * @param array|int|object|string $field_object
197 + * @param array $values
198 + * @param int $form_id
166 199 *
167 200 * @return void
168 201 */
169 202 public static function load_single_field( $field_object, $values, $form_id = 0 ) {
@@ -176,11 +209,10 @@
176 209 $field = $field_object;
177 210 $field_object = FrmField::getOne( $field['id'] );
178 211 }
179 212
180 - $field_obj = FrmFieldFactory::get_field_factory( $field_object );
181 - $display = self::display_field_options( array(), $field_obj );
182 -
213 + $field_obj = FrmFieldFactory::get_field_factory( $field_object );
214 + $display = self::display_field_options( array(), $field_obj );
183 215 $ajax_loading = ! empty( $values['ajax_load'] );
184 216 $ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ), true );
185 217
186 218 if ( $ajax_loading && $ajax_this_field ) {
@@ -189,9 +221,12 @@
189 221 return;
190 222 }
191 223
192 224 if ( ! isset( $field ) && is_object( $field_object ) ) {
193 - $field_object->parent_form_id = $values['id'] ?? $field_object->form_id;
225 + // Prefer the explicit form id. $values['id'] is not always a form id (for example when
226 + // duplicating a field it is the copied field's id), so trusting it would set parent_form_id
227 + // to a field id and break settings that resolve fields against the parent form.
228 + $field_object->parent_form_id = $form_id ? $form_id : ( $values['id'] ?? $field_object->form_id );
194 229 $field = FrmFieldsHelper::setup_edit_vars( $field_object );
195 230 }
196 231
197 232 /**
@@ -223,8 +258,14 @@
223 258 private static function get_classes_for_builder_field( $field, $display, $field_info ) {
224 259 $li_classes = $field_info->form_builder_classes( $display['type'] );
225 260 $li_classes .= ' frm_form_field frmstart ';
226 261
262 + $style_align_class = self::get_builder_field_style_align_class( $field, $field_info );
263 +
264 + if ( $style_align_class ) {
265 + $li_classes .= $style_align_class . ' ';
266 + }
267 +
227 268 if ( isset( $field['classes'] ) ) {
228 269 $li_classes .= trim( $field['classes'] ) . ' ';
229 270 }
230 271
@@ -230,14 +271,45 @@
230 271
231 272 $li_classes .= 'frmend';
232 273
233 274 if ( $field ) {
234 - $li_classes = apply_filters( 'frm_build_field_class', $li_classes, $field );
275 + return apply_filters( 'frm_build_field_class', $li_classes, $field );
235 276 }
236 277
237 278 return $li_classes;
238 279 }
239 280
281 + /**
282 + * Apply the active style alignment to a radio or checkbox builder preview on load.
283 + *
284 + * The per-field alignment setting only exists in Pro, so on load the style setting is
285 + * used. When the field has an explicit alignment (Pro), the frm_build_field_class filter
286 + * applies it instead. When Pro is not installed, any saved alignment is treated as empty.
287 + *
288 + * @since 6.32
289 + *
290 + * @param array $field
291 + * @param FrmFieldType $field_info
292 + *
293 + * @return string
294 + */
295 + private static function get_builder_field_style_align_class( $field, $field_info ) {
296 + if ( ! $field || ! is_array( $field ) || ( ! FrmField::is_radio( $field ) && ! FrmField::is_checkbox( $field ) ) ) {
297 + return '';
298 + }
299 +
300 + $align = FrmAppHelper::pro_is_installed() ? FrmField::get_option( $field, 'align' ) : '';
301 +
302 + if ( $align ) {
303 + return '';
304 + }
305 +
306 + $align = FrmStylesHelper::get_align_from_active_style( $field );
307 + $field_info->prepare_align_class( $align );
308 +
309 + return $align;
310 + }
311 +
240 312 public static function destroy() {
241 313 FrmAppHelper::permission_check( 'frm_edit_forms' );
242 314 check_ajax_referer( 'frm_ajax', 'nonce' );
243 315
@@ -276,18 +348,19 @@
276 348 return;
277 349 }
278 350
279 351 $field = FrmFieldsHelper::setup_edit_vars( $field );
280 - $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
281 - $opts = explode( "\n", rtrim( $opts, "\n" ) );
282 - $opts = array_map( 'trim', $opts );
283 352
353 + $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
354 + $opts = explode( "\n", rtrim( $opts, "\n" ) );
355 + $opts = array_map( 'trim', $opts );
356 +
284 357 $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' );
285 358 $field['separate_value'] = $separate === 'true';
286 359
287 360 if ( $field['separate_value'] ) {
288 361 foreach ( $opts as $opt_key => $opt ) {
289 - if ( strpos( $opt, '|' ) !== false ) {
362 + if ( str_contains( $opt, '|' ) ) {
290 363 $vals = explode( '|', $opt );
291 364 $opts[ $opt_key ] = array(
292 365 'label' => trim( $vals[0] ),
293 366 'value' => trim( $vals[1] ),
@@ -298,9 +371,9 @@
298 371 }
299 372 }
300 373
301 374 // Keep other options after bulk update.
302 - if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) {
375 + if ( ! empty( $field['field_options']['other'] ) ) {
303 376 $other_array = array();
304 377
305 378 foreach ( $field['options'] as $opt_key => $opt ) {
306 379 if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
@@ -308,9 +381,9 @@
308 381 }
309 382 unset( $opt_key, $opt );
310 383 }
311 384
312 - if ( ! empty( $other_array ) ) {
385 + if ( $other_array ) {
313 386 $opts = array_merge( $opts, $other_array );
314 387 }
315 388 }
316 389
@@ -352,9 +425,9 @@
352 425 if ( ! isset( $all_field_types[ $field['type'] ] ) ) {
353 426 // Add fallback for an add-on field type that has been deactivated.
354 427 $all_field_types[ $field['type'] ] = array(
355 428 'name' => ucfirst( $field['type'] ),
356 - 'icon' => 'frm_icon_font frm_pencil_icon',
429 + 'icon' => 'frmfont frm_pencil_icon',
357 430 );
358 431 } elseif ( ! is_array( $all_field_types[ $field['type'] ] ) ) {
359 432 // Fallback for fields added in a more basic way.
360 433 FrmFormsHelper::prepare_field_type( $all_field_types[ $field['type'] ] );
@@ -373,8 +446,79 @@
373 446 if ( $display['clear_on_focus'] && is_array( $field['placeholder'] ) ) {
374 447 $field['placeholder'] = implode( ', ', $field['placeholder'] );
375 448 }
376 449
450 + $pro_is_installed = FrmAppHelper::pro_is_installed();
451 +
452 + $unique_values_label_atts = array(
453 + 'for' => 'frm_uniq_field_' . $field['id'],
454 + 'class' => 'frm_help frm-mb-0',
455 + 'title' => __(
456 + 'Unique: Do not allow the same response multiple times. For example, if one user enters \'Joe\', then no one else will be allowed to enter the same name.',
457 + 'formidable'
458 + ),
459 + 'data-trigger' => 'hover',
460 + );
461 +
462 + $read_only_label_atts = array(
463 + 'for' => 'frm_read_only_field_' . $field['id'],
464 + 'class' => 'frm_help frm-mb-0',
465 + 'title' => __( 'Read Only: Show this field but do not allow the field value to be edited from the front-end.', 'formidable' ),
466 + 'data-trigger' => 'hover',
467 + );
468 +
469 + if ( ! $pro_is_installed ) {
470 + $visibility_upsell_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
471 + array(
472 + 'id' => 'field_options_admin_only_' . $field['id'],
473 + 'readonly' => '1',
474 + ),
475 + 'field_visibility',
476 + __( 'Visibility options', 'formidable' ),
477 + '/field-options/#kb-visibility'
478 + );
479 +
480 + $autocomplete_upsell_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
481 + array( 'id' => 'field_options_autocomplete_' . $field['id'] ),
482 + 'autocomplete',
483 + __( 'Autocomplete options', 'formidable' ),
484 + '/email-address/#kb-autocomplete-attribute'
485 + );
486 +
487 + $before_after_content_upsell_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
488 + array(
489 + 'type' => 'text',
490 + 'readonly' => '1',
491 + ),
492 + 'before_after_contents',
493 + __( 'Before and after field contents', 'formidable' ),
494 + '/field-options/#kb-before-after-input'
495 + );
496 +
497 + $show_upsell_for_unique_value = in_array(
498 + $field['type'],
499 + array( 'checkbox', 'email', 'name', 'number', 'phone', 'radio', 'text', 'textarea', 'url' ),
500 + true
501 + );
502 + $show_upsell_for_read_only = in_array( $field['type'], array( 'address', 'email', 'hidden', 'number', 'phone', 'radio', 'text', 'textarea', 'url' ), true );
503 + $show_upsell_for_before_after_contents = in_array( $field['type'], array( 'email', 'number', 'phone', 'quantity', 'select', 'tag', 'text', 'total', 'url' ), true );
504 + $show_upsell_for_autocomplete = in_array( $field['type'], array( 'text', 'email', 'number' ), true );
505 + $show_upsell_for_visibility = $field['type'] !== 'hidden';
506 +
507 + $unique_values_label_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
508 + $unique_values_label_atts,
509 + 'unique_values',
510 + __( 'Unique Values', 'formidable' ),
511 + '/field-options/#kb-unique'
512 + );
513 + $read_only_label_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
514 + $read_only_label_atts,
515 + 'read_only',
516 + __( 'Read Only Values', 'formidable' ),
517 + '/field-options/#kb-read-only'
518 + );
519 + }//end if
520 +
377 521 include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php';
378 522 }
379 523
380 524 /**
@@ -402,9 +546,9 @@
402 546 private static function default_value_types( $field, $atts ) {
403 547 $types = array(
404 548 'default_value' => array(
405 549 'class' => '',
406 - 'icon' => 'frm_icon_font frm_text2_icon',
550 + 'icon' => 'frmfont frm_text2_icon',
407 551 'title' => __( 'Default Value', 'formidable' ),
408 552 'data' => array(
409 553 'frmshow' => '#default-value-for-',
410 554 ),
@@ -411,9 +555,9 @@
411 555 ),
412 556 'calc' => array(
413 557 'class' => 'frm_show_upgrade frm_noallow',
414 558 'title' => __( 'Calculate Value', 'formidable' ),
415 - 'icon' => 'frm_icon_font frm_calculator_icon',
559 + 'icon' => 'frmfont frm_calculator_icon',
416 560 'data' => array(
417 561 'medium' => 'calculations',
418 562 'upgrade' => __( 'Calculator forms', 'formidable' ),
419 563 ),
@@ -421,9 +565,9 @@
421 565 ),
422 566 'get_values_field' => array(
423 567 'class' => 'frm_show_upgrade frm_noallow',
424 568 'title' => __( 'Lookup', 'formidable' ),
425 - 'icon' => 'frm_icon_font frm_search_icon',
569 + 'icon' => 'frmfont frm_search_icon',
426 570 'data' => array(
427 571 'medium' => 'lookup',
428 572 'upgrade' => __( 'Lookup fields', 'formidable' ),
429 573 ),
@@ -472,15 +616,11 @@
472 616
473 617 $pro_fields = FrmField::pro_field_selection();
474 618 // We want to keep credit_card types as credit card types for Stripe Lite.
475 619 // The credit_card key is set for backward compatibility.
476 - unset( $pro_fields['credit_card'] );
620 + FrmField::remove_moved_field_types_from_pro( $pro_fields );
477 621
478 - if ( array_key_exists( $type, $pro_fields ) ) {
479 - $type = 'text';
480 - }
481 -
482 - return $type;
622 + return array_key_exists( $type, $pro_fields ) ? 'text' : $type;
483 623 }
484 624
485 625 /**
486 626 * @param array $settings
@@ -543,8 +683,9 @@
543 683 FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
544 684
545 685 self::add_shortcodes_to_html( $field, $add_html );
546 686 self::add_pattern_attribute( $field, $add_html );
687 + self::add_currency_field_attributes( $field, $add_html );
547 688
548 689 $add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
549 690 $add_html = ' ' . implode( ' ', $add_html ) . ' ';
550 691
@@ -628,9 +769,9 @@
628 769 'rem' => 0.444,
629 770 'em' => 0.544,
630 771 );
631 772
632 - // include "col" for valid html
773 + // Include "col" for valid html
633 774 $unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) );
634 775
635 776 if ( ! isset( $calc[ $unit ] ) ) {
636 777 return;
@@ -635,10 +776,9 @@
635 776 if ( ! isset( $calc[ $unit ] ) ) {
636 777 return;
637 778 }
638 779
639 - $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
640 -
780 + $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
641 781 $add_html['cols'] = 'cols="' . absint( $size ) . '"';
642 782 }
643 783
644 784 /**
@@ -675,8 +815,9 @@
675 815 *
676 816 * @return void
677 817 */
678 818 private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
819 + // phpcs:ignore Universal.Operators.StrictComparisons
679 820 if ( $field['default_value'] != '' ) {
680 821 if ( is_array( $field['default_value'] ) ) {
681 822 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( json_encode( $field['default_value'] ) ) . '"';
682 823 } else {
@@ -685,10 +826,11 @@
685 826 }
686 827
687 828 $field['placeholder'] = self::prepare_placeholder( $field );
688 829
830 + // phpcs:ignore Universal.Operators.StrictComparisons
689 831 if ( $field['placeholder'] == '' || is_array( $field['placeholder'] ) ) {
690 - // don't include a json placeholder
832 + // Don't include a json placeholder
691 833 return;
692 834 }
693 835
694 836 self::add_placeholder_to_input( $field, $add_html );
@@ -703,11 +845,9 @@
703 845 *
704 846 * @return string
705 847 */
706 848 private static function prepare_placeholder( $field ) {
707 - $placeholder = $field['placeholder'] ?? '';
708 -
709 - return $placeholder;
849 + return $field['placeholder'] ?? '';
710 850 }
711 851
712 852 /**
713 853 * If the label position is "inside",
@@ -736,9 +876,9 @@
736 876 */
737 877 public static function add_placeholder_to_select( $field ) {
738 878 $placeholder = FrmField::get_option( $field, 'placeholder' );
739 879
740 - if ( empty( $placeholder ) ) {
880 + if ( ! $placeholder ) {
741 881 $placeholder = self::get_default_value_from_name( $field );
742 882 }
743 883
744 884 $use_placeholder = $placeholder;
@@ -786,8 +926,9 @@
786 926 *
787 927 * @return void
788 928 */
789 929 private static function add_frmval_to_input( $field, &$add_html ) {
930 + // phpcs:ignore Universal.Operators.StrictComparisons
790 931 if ( $field['placeholder'] != '' ) {
791 932 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['placeholder'] ) . '"';
792 933
793 934 if ( 'select' === $field['type'] ) {
@@ -794,8 +935,9 @@
794 935 $add_html['data-frmplaceholder'] = 'data-frmplaceholder="' . esc_attr( $field['placeholder'] ) . '"';
795 936 }
796 937 }
797 938
939 + // phpcs:ignore Universal.Operators.StrictComparisons
798 940 if ( $field['default_value'] != '' ) {
799 941 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
800 942 }
801 943 }
@@ -874,12 +1016,14 @@
874 1016 }
875 1017
876 1018 $error_body = self::pull_custom_error_body_from_custom_html( $form, $field );
877 1019
878 - if ( false !== $error_body ) {
879 - $error_body = urlencode( $error_body );
880 - $add_html['data-error-html'] = 'data-error-html="' . esc_attr( $error_body ) . '"';
1020 + if ( false === $error_body ) {
1021 + return;
881 1022 }
1023 +
1024 + $error_body = urlencode( $error_body );
1025 + $add_html['data-error-html'] = 'data-error-html="' . esc_attr( $error_body ) . '"';
882 1026 }
883 1027
884 1028 /**
885 1029 * @since 5.0.03
@@ -899,8 +1043,9 @@
899 1043 if ( ! empty( $field['parent_form_id'] ) && isset( $frm_vars['js_validate_forms'][ $field['parent_form_id'] ] ) ) {
900 1044 return $frm_vars['js_validate_forms'][ $field['parent_form_id'] ];
901 1045 }
902 1046 }
1047 +
903 1048 return false;
904 1049 }
905 1050
906 1051 /**
@@ -916,11 +1061,10 @@
916 1061 }
917 1062
918 1063 $custom_html = $field['custom_html'];
919 1064 $custom_html = apply_filters( 'frm_before_replace_shortcodes', $custom_html, $field, $errors, $form );
1065 + $start = strpos( $custom_html, '[if error]' );
920 1066
921 - $start = strpos( $custom_html, '[if error]' );
922 -
923 1067 if ( false === $start ) {
924 1068 return false;
925 1069 }
926 1070
@@ -937,13 +1081,9 @@
937 1081 '<div class="frm_error">[error]</div>',
938 1082 '<div class="frm_error" role="alert">[error]</div>',
939 1083 );
940 1084
941 - if ( in_array( $error_body, $default_html, true ) ) {
942 - return false;
943 - }
944 -
945 - return $error_body;
1085 + return in_array( $error_body, $default_html, true ) ? false : $error_body;
946 1086 }
947 1087
948 1088 /**
949 1089 * If 'required' is added to a conditionally hidden field, the form won't
@@ -957,8 +1097,9 @@
957 1097 *
958 1098 * @return void
959 1099 */
960 1100 private static function maybe_add_html_required( $field, array &$add_html ) {
1101 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
961 1102 $excluded_field_types =
962 1103 FrmField::is_radio( $field ) ||
963 1104 FrmField::is_checkbox( $field ) ||
964 1105 FrmField::is_field_type( $field, 'file' ) ||
@@ -963,8 +1104,9 @@
963 1104 FrmField::is_checkbox( $field ) ||
964 1105 FrmField::is_field_type( $field, 'file' ) ||
965 1106 FrmField::is_field_type( $field, 'nps' ) ||
966 1107 FrmField::is_field_type( $field, 'scale' );
1108 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
967 1109
968 1110 if ( $excluded_field_types ) {
969 1111 return;
970 1112 }
@@ -987,9 +1129,9 @@
987 1129 unset( $field['shortcodes']['autocomplete'] );
988 1130 }
989 1131
990 1132 foreach ( $field['shortcodes'] as $k => $v ) {
991 - if ( isset( $field['subfield_name'] ) && 0 === strpos( $k, 'aria-invalid' ) ) {
1133 + if ( isset( $field['subfield_name'] ) && str_starts_with( $k, 'aria-invalid' ) ) {
992 1134 $subfield_name = $field['subfield_name'];
993 1135
994 1136 if ( ! isset( $field['shortcodes'][ 'aria-invalid-' . $subfield_name ] ) ) {
995 1137 continue;
@@ -1003,9 +1145,9 @@
1003 1145 if ( 'opt' === $k || ! self::should_allow_input_attribute( $k ) ) {
1004 1146 continue;
1005 1147 }
1006 1148
1007 - if ( is_numeric( $k ) && strpos( $v, '=' ) ) {
1149 + if ( is_numeric( $k ) && str_contains( $v, '=' ) ) {
1008 1150 $add_html[] = $v;
1009 1151 } elseif ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
1010 1152 $add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
1011 1153 } else {
@@ -1046,17 +1188,71 @@
1046 1188 $format_value = FrmField::get_option( $field, 'format' );
1047 1189 $has_format = $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value );
1048 1190 $format_field = FrmField::is_field_type( $field, 'text' );
1049 1191
1050 - if ( $field['type'] === 'phone' || ( $has_format && $format_field ) ) {
1051 - $format = FrmEntryValidate::phone_format( $field );
1052 - $format = substr( $format, 2, - 1 );
1192 + if ( $field['type'] !== 'phone' && ( ! $has_format || ! $format_field ) ) {
1193 + return;
1194 + }
1053 1195
1054 - $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"';
1196 + $format = FrmEntryValidate::phone_format( $field );
1197 + $format = substr( $format, 2, - 1 );
1198 +
1199 + $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"';
1200 + }
1201 +
1202 + /**
1203 + * Add product attributes to fields in multi-paged forms.
1204 + *
1205 + * @param array $field
1206 + * @param array $add_html
1207 + *
1208 + * @return void
1209 + */
1210 + private static function add_currency_field_attributes( $field, &$add_html ) {
1211 + $type = $field['original_type'] ?? $field['type'];
1212 + $is_product_field = in_array( $type, array( 'total', 'quantity', 'product' ), true );
1213 +
1214 + if ( ! $is_product_field ) {
1215 + return;
1055 1216 }
1217 +
1218 + if ( $type === 'total' ) {
1219 + $add_html['data-frmtotal'] = 'data-frmtotal';
1220 + } elseif ( $type === 'quantity' ) {
1221 + $product_field = FrmField::get_option( $field, 'product_field' );
1222 + $add_html['data-frmproduct'] = 'data-frmproduct="' . esc_attr( json_encode( $product_field ) ) . '"';
1223 + } elseif ( $type === 'product' && 'hidden' === $field['type'] ) {
1224 + // We want to do this only for fields that are hidden because it's
1225 + // not their page, hence the check : 'hidden' === $field['type'].
1226 + $price = empty( $field['value'] ) ? 0 : self::get_product_price( $field );
1227 +
1228 + $add_html['data-frmprice'] = 'data-frmprice="' . esc_attr( $price ) . '"';
1229 + }
1056 1230 }
1057 1231
1058 1232 /**
1233 + * @param array $field
1234 + */
1235 + private static function get_product_price( $field ) {
1236 + if ( is_array( $field['value'] ) ) {
1237 + // '' is unlikely though, let's just do it to prevent warnings
1238 + $value = isset( $field['opt_key'] ) && isset( $field['value'][ $field['opt_key'] ] )
1239 + ? $field['value'][ $field['opt_key'] ]
1240 + : '';
1241 + } else {
1242 + $value = $field['value'];
1243 + }
1244 +
1245 + $field_obj = FrmFieldFactory::get_field_object( $field['id'] );
1246 +
1247 + if ( method_exists( $field_obj, 'get_posted_price' ) ) {
1248 + return $field_obj->get_posted_price( $value );
1249 + }
1250 +
1251 + return $value;
1252 + }
1253 +
1254 + /**
1059 1255 * @param mixed $opt
1060 1256 * @param mixed $opt_key
1061 1257 * @param array|object $field
1062 1258 *
@@ -1079,11 +1275,7 @@
1079 1275 *
1080 1276 * @return mixed
1081 1277 */
1082 1278 public static function check_label( $opt ) {
1083 - if ( is_array( $opt ) ) {
1084 - $opt = $opt['label'] ?? reset( $opt );
1085 - }
1086 -
1087 - return $opt;
1279 + return is_array( $opt ) ? ( $opt['label'] ?? reset( $opt ) ) : $opt;
1088 1280 }
1089 1281 }