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 +347 -84 6.25.1 → trunk View file →
@@ -11,48 +11,55 @@
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();
22 - if ( empty( $fields ) ) {
28 + $field_ids = FrmAppHelper::get_post_param( 'field_ids', array(), 'absint' );
29 + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
30 +
31 + if ( ! $form_id || ! is_array( $field_ids ) || ! $field_ids ) {
23 32 wp_die();
24 33 }
25 34
26 35 $_GET['page'] = 'formidable';
27 -
28 - $values = array(
29 - '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,
30 39 'doing_ajax' => true,
31 40 );
32 - $field_html = array();
41 + $field_html = array();
33 42
34 - foreach ( $fields as $field ) {
35 - $field = htmlspecialchars_decode( nl2br( $field ) );
36 - $field = json_decode( $field );
37 - if ( ! isset( $field->id ) || ! is_numeric( $field->id ) ) {
38 - // 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.
39 46 continue;
40 47 }
41 48
42 - if ( ! isset( $field->value ) ) {
43 - $field->value = '';
44 - }
45 - $field->field_options = json_decode( json_encode( $field->field_options ), true );
46 - $field->options = json_decode( json_encode( $field->options ), true );
47 - $field->default_value = json_decode( json_encode( $field->default_value ), true );
49 + $field = $fields[ $field_id ];
48 50
49 51 ob_start();
50 52 self::load_single_field( $field, $values );
51 - $field_html[ absint( $field->id ) ] = ob_get_contents();
52 - ob_end_clean();
53 - }
54 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 + );
60 + }//end foreach
61 +
55 62 echo json_encode( $field_html );
56 63
57 64 wp_die();
58 65 }
@@ -57,8 +64,40 @@
57 64 wp_die();
58 65 }
59 66
60 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 + /**
61 100 * Create a new field with ajax
62 101 */
63 102 public static function create() {
64 103 FrmAppHelper::permission_check( 'frm_edit_forms' );
@@ -71,9 +110,9 @@
71 110 do_action( 'frm_before_create_field', $field_type, $form_id );
72 111
73 112 $field = self::include_new_field( $field_type, $form_id, $field_options );
74 113
75 - // 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
76 115 do_action( 'frm_after_field_created', $field, $form_id );
77 116
78 117 wp_die();
79 118 }
@@ -89,9 +128,9 @@
89 128 */
90 129 public static function include_new_field( $field_type, $form_id, $field_options = array() ) {
91 130 $field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
92 131
93 - if ( ! empty( $field_options ) ) {
132 + if ( $field_options ) {
94 133 $field_values['field_options'] = array_merge( $field_values['field_options'], $field_options );
95 134 }
96 135
97 136 // When a new field is added to the form, flag it as draft and hide it from the front-end.
@@ -106,15 +145,15 @@
106 145 if ( ! $field_id ) {
107 146 return false;
108 147 }
109 148
110 - $field = self::get_field_array_from_id( $field_id );
149 + $field = self::get_field_array_from_id( $field_id );
150 + $values = array();
111 151
112 - $values = array();
113 152 if ( FrmAppHelper::pro_is_installed() ) {
114 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' );
115 155
116 - $parent_form_id = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'parent_form_id' );
117 156 if ( $parent_form_id ) {
118 157 $field['parent_form_id'] = $parent_form_id;
119 158 }
120 159 }
@@ -127,15 +166,14 @@
127 166 public static function duplicate() {
128 167 FrmAppHelper::permission_check( 'frm_edit_forms' );
129 168 check_ajax_referer( 'frm_ajax', 'nonce' );
130 169
131 - $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
132 - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
133 -
170 + $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
171 + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
134 172 $new_field = FrmField::duplicate_single_field( $field_id, $form_id );
135 173
136 174 if ( is_array( $new_field ) && ! empty( $new_field['field_id'] ) ) {
137 - 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 );
138 176 }
139 177
140 178 wp_die();
141 179 }
@@ -148,9 +186,8 @@
148 186 * @return array
149 187 */
150 188 public static function get_field_array_from_id( $field_id ) {
151 189 $field = FrmField::getOne( $field_id );
152 -
153 190 return FrmFieldsHelper::setup_edit_vars( $field );
154 191 }
155 192
156 193 /**
@@ -155,11 +192,13 @@
155 192
156 193 /**
157 194 * @since 3.0
158 195 *
159 - * @param array|int|object $field_object
160 - * @param array $values
161 - * @param int $form_id
196 + * @param array|int|object|string $field_object
197 + * @param array $values
198 + * @param int $form_id
199 + *
200 + * @return void
162 201 */
163 202 public static function load_single_field( $field_object, $values, $form_id = 0 ) {
164 203 global $frm_vars;
165 204 $frm_vars['is_admin'] = true;
@@ -170,11 +209,10 @@
170 209 $field = $field_object;
171 210 $field_object = FrmField::getOne( $field['id'] );
172 211 }
173 212
174 - $field_obj = FrmFieldFactory::get_field_factory( $field_object );
175 - $display = self::display_field_options( array(), $field_obj );
176 -
213 + $field_obj = FrmFieldFactory::get_field_factory( $field_object );
214 + $display = self::display_field_options( array(), $field_obj );
177 215 $ajax_loading = ! empty( $values['ajax_load'] );
178 216 $ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ), true );
179 217
180 218 if ( $ajax_loading && $ajax_this_field ) {
@@ -183,9 +221,12 @@
183 221 return;
184 222 }
185 223
186 224 if ( ! isset( $field ) && is_object( $field_object ) ) {
187 - $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 );
188 229 $field = FrmFieldsHelper::setup_edit_vars( $field_object );
189 230 }
190 231
191 232 /**
@@ -210,8 +251,9 @@
210 251 *
211 252 * @param array $field
212 253 * @param array $display
213 254 * @param FrmFieldType $field_info
255 + *
214 256 * @return string
215 257 */
216 258 private static function get_classes_for_builder_field( $field, $display, $field_info ) {
217 259 $li_classes = $field_info->form_builder_classes( $display['type'] );
@@ -216,8 +258,14 @@
216 258 private static function get_classes_for_builder_field( $field, $display, $field_info ) {
217 259 $li_classes = $field_info->form_builder_classes( $display['type'] );
218 260 $li_classes .= ' frm_form_field frmstart ';
219 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 +
220 268 if ( isset( $field['classes'] ) ) {
221 269 $li_classes .= trim( $field['classes'] ) . ' ';
222 270 }
223 271
@@ -223,14 +271,45 @@
223 271
224 272 $li_classes .= 'frmend';
225 273
226 274 if ( $field ) {
227 - $li_classes = apply_filters( 'frm_build_field_class', $li_classes, $field );
275 + return apply_filters( 'frm_build_field_class', $li_classes, $field );
228 276 }
229 277
230 278 return $li_classes;
231 279 }
232 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 +
233 312 public static function destroy() {
234 313 FrmAppHelper::permission_check( 'frm_edit_forms' );
235 314 check_ajax_referer( 'frm_ajax', 'nonce' );
236 315
@@ -259,8 +338,9 @@
259 338 *
260 339 * @since 6.8.4
261 340 *
262 341 * @param array $bulk_edit_types
342 + *
263 343 * @return array
264 344 */
265 345 $bulk_edit_types = apply_filters( 'frm_bulk_edit_field_types', $bulk_edit_types );
266 346
@@ -268,18 +348,19 @@
268 348 return;
269 349 }
270 350
271 351 $field = FrmFieldsHelper::setup_edit_vars( $field );
272 - $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
273 - $opts = explode( "\n", rtrim( $opts, "\n" ) );
274 - $opts = array_map( 'trim', $opts );
275 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 +
276 357 $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' );
277 358 $field['separate_value'] = $separate === 'true';
278 359
279 360 if ( $field['separate_value'] ) {
280 361 foreach ( $opts as $opt_key => $opt ) {
281 - if ( strpos( $opt, '|' ) !== false ) {
362 + if ( str_contains( $opt, '|' ) ) {
282 363 $vals = explode( '|', $opt );
283 364 $opts[ $opt_key ] = array(
284 365 'label' => trim( $vals[0] ),
285 366 'value' => trim( $vals[1] ),
@@ -290,10 +371,11 @@
290 371 }
291 372 }
292 373
293 374 // Keep other options after bulk update.
294 - if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) {
375 + if ( ! empty( $field['field_options']['other'] ) ) {
295 376 $other_array = array();
377 +
296 378 foreach ( $field['options'] as $opt_key => $opt ) {
297 379 if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
298 380 $other_array[ $opt_key ] = $opt;
299 381 }
@@ -298,9 +380,10 @@
298 380 $other_array[ $opt_key ] = $opt;
299 381 }
300 382 unset( $opt_key, $opt );
301 383 }
302 - if ( ! empty( $other_array ) ) {
384 +
385 + if ( $other_array ) {
303 386 $opts = array_merge( $opts, $other_array );
304 387 }
305 388 }
306 389
@@ -314,8 +397,9 @@
314 397 /**
315 398 * @since 4.0
316 399 *
317 400 * @param array $atts - Includes field array, field_obj, display array, values array.
401 + *
318 402 * @return void
319 403 */
320 404 public static function load_single_field_settings( $atts ) {
321 405 $field = $atts['field'];
@@ -341,9 +425,9 @@
341 425 if ( ! isset( $all_field_types[ $field['type'] ] ) ) {
342 426 // Add fallback for an add-on field type that has been deactivated.
343 427 $all_field_types[ $field['type'] ] = array(
344 428 'name' => ucfirst( $field['type'] ),
345 - 'icon' => 'frm_icon_font frm_pencil_icon',
429 + 'icon' => 'frmfont frm_pencil_icon',
346 430 );
347 431 } elseif ( ! is_array( $all_field_types[ $field['type'] ] ) ) {
348 432 // Fallback for fields added in a more basic way.
349 433 FrmFormsHelper::prepare_field_type( $all_field_types[ $field['type'] ] );
@@ -349,8 +433,9 @@
349 433 FrmFormsHelper::prepare_field_type( $all_field_types[ $field['type'] ] );
350 434 }
351 435
352 436 $type_name = $all_field_types[ $field['type'] ]['name'];
437 +
353 438 if ( $field['type'] === 'divider' && FrmField::is_option_true( $field, 'repeat' ) ) {
354 439 $type_name = $all_field_types['divider|repeat']['name'];
355 440 }
356 441
@@ -361,8 +446,79 @@
361 446 if ( $display['clear_on_focus'] && is_array( $field['placeholder'] ) ) {
362 447 $field['placeholder'] = implode( ', ', $field['placeholder'] );
363 448 }
364 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 +
365 521 include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php';
366 522 }
367 523
368 524 /**
@@ -383,8 +539,9 @@
383 539 * @since 4.0
384 540 *
385 541 * @param array $field
386 542 * @param array $atts
543 + *
387 544 * @return array
388 545 */
389 546 private static function default_value_types( $field, $atts ) {
390 547 $types = array(
@@ -389,9 +546,9 @@
389 546 private static function default_value_types( $field, $atts ) {
390 547 $types = array(
391 548 'default_value' => array(
392 549 'class' => '',
393 - 'icon' => 'frm_icon_font frm_text2_icon',
550 + 'icon' => 'frmfont frm_text2_icon',
394 551 'title' => __( 'Default Value', 'formidable' ),
395 552 'data' => array(
396 553 'frmshow' => '#default-value-for-',
397 554 ),
@@ -398,9 +555,9 @@
398 555 ),
399 556 'calc' => array(
400 557 'class' => 'frm_show_upgrade frm_noallow',
401 558 'title' => __( 'Calculate Value', 'formidable' ),
402 - 'icon' => 'frm_icon_font frm_calculator_icon',
559 + 'icon' => 'frmfont frm_calculator_icon',
403 560 'data' => array(
404 561 'medium' => 'calculations',
405 562 'upgrade' => __( 'Calculator forms', 'formidable' ),
406 563 ),
@@ -408,9 +565,9 @@
408 565 ),
409 566 'get_values_field' => array(
410 567 'class' => 'frm_show_upgrade frm_noallow',
411 568 'title' => __( 'Lookup', 'formidable' ),
412 - 'icon' => 'frm_icon_font frm_search_icon',
569 + 'icon' => 'frmfont frm_search_icon',
413 570 'data' => array(
414 571 'medium' => 'lookup',
415 572 'upgrade' => __( 'Lookup fields', 'formidable' ),
416 573 ),
@@ -439,8 +596,9 @@
439 596 }
440 597
441 598 /**
442 599 * @param string $type
600 + *
443 601 * @return string
444 602 */
445 603 public static function change_type( $type ) {
446 604 $type_switch = array(
@@ -450,8 +608,9 @@
450 608 'rte' => 'textarea',
451 609 'website' => 'url',
452 610 'image' => 'url',
453 611 );
612 +
454 613 if ( isset( $type_switch[ $type ] ) ) {
455 614 $type = $type_switch[ $type ];
456 615 }
457 616
@@ -457,15 +616,11 @@
457 616
458 617 $pro_fields = FrmField::pro_field_selection();
459 618 // We want to keep credit_card types as credit card types for Stripe Lite.
460 619 // The credit_card key is set for backward compatibility.
461 - unset( $pro_fields['credit_card'] );
620 + FrmField::remove_moved_field_types_from_pro( $pro_fields );
462 621
463 - if ( array_key_exists( $type, $pro_fields ) ) {
464 - $type = 'text';
465 - }
466 -
467 - return $type;
622 + return array_key_exists( $type, $pro_fields ) ? 'text' : $type;
468 623 }
469 624
470 625 /**
471 626 * @param array $settings
@@ -490,8 +645,9 @@
490 645 * @since 3.0
491 646 *
492 647 * @param array $field Field data.
493 648 * @param bool $is_hidden Whether the format option should be hidden.
649 + *
494 650 * @return void
495 651 */
496 652 public static function show_format_option( $field, $is_hidden = false ) {
497 653 $attributes = array(
@@ -505,8 +661,14 @@
505 661
506 662 include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php';
507 663 }
508 664
665 + /**
666 + * @param array $field
667 + * @param bool $echo
668 + *
669 + * @return string
670 + */
509 671 public static function input_html( $field, $echo = true ) {
510 672 $class = array();
511 673 self::add_input_classes( $field, $class );
512 674
@@ -521,8 +683,9 @@
521 683 FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
522 684
523 685 self::add_shortcodes_to_html( $field, $add_html );
524 686 self::add_pattern_attribute( $field, $add_html );
687 + self::add_currency_field_attributes( $field, $add_html );
525 688
526 689 $add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
527 690 $add_html = ' ' . implode( ' ', $add_html ) . ' ';
528 691
@@ -535,8 +698,9 @@
535 698
536 699 /**
537 700 * @param array $field
538 701 * @param array $class
702 + *
539 703 * @return void
540 704 */
541 705 private static function add_input_classes( $field, array &$class ) {
542 706 if ( ! empty( $field['input_class'] ) ) {
@@ -554,8 +718,9 @@
554 718
555 719 /**
556 720 * @param array $field
557 721 * @param array $add_html
722 + *
558 723 * @return void
559 724 */
560 725 private static function add_html_size( $field, array &$add_html ) {
561 726 $size_fields = array(
@@ -588,8 +753,9 @@
588 753
589 754 /**
590 755 * @param array $field
591 756 * @param array $add_html
757 + *
592 758 * @return void
593 759 */
594 760 private static function add_html_cols( $field, array &$add_html ) {
595 761 if ( ! in_array( $field['type'], array( 'textarea', 'rte' ), true ) ) {
@@ -603,9 +769,9 @@
603 769 'rem' => 0.444,
604 770 'em' => 0.544,
605 771 );
606 772
607 - // include "col" for valid html
773 + // Include "col" for valid html
608 774 $unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) );
609 775
610 776 if ( ! isset( $calc[ $unit ] ) ) {
611 777 return;
@@ -610,10 +776,9 @@
610 776 if ( ! isset( $calc[ $unit ] ) ) {
611 777 return;
612 778 }
613 779
614 - $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
615 -
780 + $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
616 781 $add_html['cols'] = 'cols="' . absint( $size ) . '"';
617 782 }
618 783
619 784 /**
@@ -618,8 +783,9 @@
618 783
619 784 /**
620 785 * @param array $field
621 786 * @param array $add_html
787 + *
622 788 * @return void
623 789 */
624 790 private static function add_html_length( $field, array &$add_html ) {
625 791 // Check for max setting and if this field accepts maxlength.
@@ -645,11 +811,13 @@
645 811 /**
646 812 * @param array $field
647 813 * @param array $add_html
648 814 * @param array $class
815 + *
649 816 * @return void
650 817 */
651 818 private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
819 + // phpcs:ignore Universal.Operators.StrictComparisons
652 820 if ( $field['default_value'] != '' ) {
653 821 if ( is_array( $field['default_value'] ) ) {
654 822 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( json_encode( $field['default_value'] ) ) . '"';
655 823 } else {
@@ -657,10 +825,12 @@
657 825 }
658 826 }
659 827
660 828 $field['placeholder'] = self::prepare_placeholder( $field );
829 +
830 + // phpcs:ignore Universal.Operators.StrictComparisons
661 831 if ( $field['placeholder'] == '' || is_array( $field['placeholder'] ) ) {
662 - // don't include a json placeholder
832 + // Don't include a json placeholder
663 833 return;
664 834 }
665 835
666 836 self::add_placeholder_to_input( $field, $add_html );
@@ -671,14 +841,13 @@
671 841 *
672 842 * @since 5.4 This doesn't call `FrmFieldsController::get_default_value_from_name()` anymore.
673 843 *
674 844 * @param array $field Field array.
845 + *
675 846 * @return string
676 847 */
677 848 private static function prepare_placeholder( $field ) {
678 - $placeholder = $field['placeholder'] ?? '';
679 -
680 - return $placeholder;
849 + return $field['placeholder'] ?? '';
681 850 }
682 851
683 852 /**
684 853 * If the label position is "inside",
@@ -699,13 +868,17 @@
699 868 * Maybe add a blank placeholder option before any options
700 869 * in a dropdown.
701 870 *
702 871 * @since 4.04
872 + *
873 + * @param array|object $field
874 + *
703 875 * @return bool True if placeholder was added.
704 876 */
705 877 public static function add_placeholder_to_select( $field ) {
706 878 $placeholder = FrmField::get_option( $field, 'placeholder' );
707 - if ( empty( $placeholder ) ) {
879 +
880 + if ( ! $placeholder ) {
708 881 $placeholder = self::get_default_value_from_name( $field );
709 882 }
710 883
711 884 $use_placeholder = $placeholder;
@@ -712,8 +885,9 @@
712 885 $autocomplete = FrmField::get_option( $field, 'autocom' );
713 886
714 887 if ( $autocomplete ) {
715 888 $use_chosen = ! is_callable( 'FrmProAppHelper::use_chosen_js' ) || FrmProAppHelper::use_chosen_js();
889 +
716 890 if ( $use_chosen ) {
717 891 $use_placeholder = '';
718 892 }
719 893 }
@@ -736,8 +910,9 @@
736 910 * Use HTML5 placeholder with js fallback.
737 911 *
738 912 * @param array $field
739 913 * @param array $add_html
914 + *
740 915 * @return void
741 916 */
742 917 private static function add_placeholder_to_input( $field, &$add_html ) {
743 918 if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) {
@@ -747,11 +922,13 @@
747 922
748 923 /**
749 924 * @param array $field
750 925 * @param array $add_html
926 + *
751 927 * @return void
752 928 */
753 929 private static function add_frmval_to_input( $field, &$add_html ) {
930 + // phpcs:ignore Universal.Operators.StrictComparisons
754 931 if ( $field['placeholder'] != '' ) {
755 932 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['placeholder'] ) . '"';
756 933
757 934 if ( 'select' === $field['type'] ) {
@@ -758,13 +935,20 @@
758 935 $add_html['data-frmplaceholder'] = 'data-frmplaceholder="' . esc_attr( $field['placeholder'] ) . '"';
759 936 }
760 937 }
761 938
939 + // phpcs:ignore Universal.Operators.StrictComparisons
762 940 if ( $field['default_value'] != '' ) {
763 941 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
764 942 }
765 943 }
766 944
945 + /**
946 + * @param array $field
947 + * @param array $add_html
948 + *
949 + * @return void
950 + */
767 951 private static function add_validation_messages( $field, array &$add_html ) {
768 952 $field_validation_messages_status = self::get_validation_data_attribute_visibility_info( $field );
769 953
770 954 if ( FrmField::is_required( $field ) && ! empty( $field_validation_messages_status['data-reqmsg'] ) ) {
@@ -788,8 +972,9 @@
788 972 *
789 973 * @since 6.9
790 974 *
791 975 * @param array|object $field
976 + *
792 977 * @return array
793 978 */
794 979 private static function get_validation_data_attribute_visibility_info( $field ) {
795 980 if ( FrmField::get_field_type( $field ) === 'hidden' ) {
@@ -819,21 +1004,26 @@
819 1004 * @since 5.0.03
820 1005 *
821 1006 * @param array $field
822 1007 * @param array $add_html
1008 + *
823 1009 * @return void
824 1010 */
825 1011 private static function maybe_add_error_html_for_js_validation( $field, array &$add_html ) {
826 1012 $form = self::get_form_for_js_validation( $field );
1013 +
827 1014 if ( false === $form ) {
828 1015 return;
829 1016 }
830 1017
831 1018 $error_body = self::pull_custom_error_body_from_custom_html( $form, $field );
832 - if ( false !== $error_body ) {
833 - $error_body = urlencode( $error_body );
834 - $add_html['data-error-html'] = 'data-error-html="' . esc_attr( $error_body ) . '"';
1019 +
1020 + if ( false === $error_body ) {
1021 + return;
835 1022 }
1023 +
1024 + $error_body = urlencode( $error_body );
1025 + $add_html['data-error-html'] = 'data-error-html="' . esc_attr( $error_body ) . '"';
836 1026 }
837 1027
838 1028 /**
839 1029 * @since 5.0.03
@@ -838,20 +1028,24 @@
838 1028 /**
839 1029 * @since 5.0.03
840 1030 *
841 1031 * @param array $field
1032 + *
842 1033 * @return false|stdClass false if there is no form object found with JS validation active.
843 1034 */
844 1035 private static function get_form_for_js_validation( $field ) {
845 1036 global $frm_vars;
1037 +
846 1038 if ( ! empty( $frm_vars['js_validate_forms'] ) ) {
847 1039 if ( isset( $frm_vars['js_validate_forms'][ $field['form_id'] ] ) ) {
848 1040 return $frm_vars['js_validate_forms'][ $field['form_id'] ];
849 1041 }
1042 +
850 1043 if ( ! empty( $field['parent_form_id'] ) && isset( $frm_vars['js_validate_forms'][ $field['parent_form_id'] ] ) ) {
851 1044 return $frm_vars['js_validate_forms'][ $field['parent_form_id'] ];
852 1045 }
853 1046 }
1047 +
854 1048 return false;
855 1049 }
856 1050
857 1051 /**
@@ -857,8 +1051,9 @@
857 1051 /**
858 1052 * @param stdClass $form
859 1053 * @param array $field
860 1054 * @param array $errors
1055 + *
861 1056 * @return false|string
862 1057 */
863 1058 public static function pull_custom_error_body_from_custom_html( $form, $field, $errors = array() ) {
864 1059 if ( empty( $field['custom_html'] ) ) {
@@ -866,15 +1061,16 @@
866 1061 }
867 1062
868 1063 $custom_html = $field['custom_html'];
869 1064 $custom_html = apply_filters( 'frm_before_replace_shortcodes', $custom_html, $field, $errors, $form );
1065 + $start = strpos( $custom_html, '[if error]' );
870 1066
871 - $start = strpos( $custom_html, '[if error]' );
872 1067 if ( false === $start ) {
873 1068 return false;
874 1069 }
875 1070
876 1071 $end = strpos( $custom_html, '[/if error]' );
1072 +
877 1073 if ( false === $end || $end < $start ) {
878 1074 return false;
879 1075 }
880 1076
@@ -885,13 +1081,9 @@
885 1081 '<div class="frm_error">[error]</div>',
886 1082 '<div class="frm_error" role="alert">[error]</div>',
887 1083 );
888 1084
889 - if ( in_array( $error_body, $default_html, true ) ) {
890 - return false;
891 - }
892 -
893 - return $error_body;
1085 + return in_array( $error_body, $default_html, true ) ? false : $error_body;
894 1086 }
895 1087
896 1088 /**
897 1089 * If 'required' is added to a conditionally hidden field, the form won't
@@ -898,13 +1090,16 @@
898 1090 * submit in many browsers. Check to make sure the javascript to conditionally
899 1091 * remove it is present if needed.
900 1092 *
901 1093 * @since 3.06.01
1094 + *
902 1095 * @param array $field
903 1096 * @param array $add_html
1097 + *
904 1098 * @return void
905 1099 */
906 1100 private static function maybe_add_html_required( $field, array &$add_html ) {
1101 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
907 1102 $excluded_field_types =
908 1103 FrmField::is_radio( $field ) ||
909 1104 FrmField::is_checkbox( $field ) ||
910 1105 FrmField::is_field_type( $field, 'file' ) ||
@@ -909,8 +1104,9 @@
909 1104 FrmField::is_checkbox( $field ) ||
910 1105 FrmField::is_field_type( $field, 'file' ) ||
911 1106 FrmField::is_field_type( $field, 'nps' ) ||
912 1107 FrmField::is_field_type( $field, 'scale' );
1108 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
913 1109
914 1110 if ( $excluded_field_types ) {
915 1111 return;
916 1112 }
@@ -920,8 +1116,9 @@
920 1116
921 1117 /**
922 1118 * @param array $field
923 1119 * @param array $add_html
1120 + *
924 1121 * @return void
925 1122 */
926 1123 private static function add_shortcodes_to_html( $field, array &$add_html ) {
927 1124 if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) {
@@ -932,10 +1129,11 @@
932 1129 unset( $field['shortcodes']['autocomplete'] );
933 1130 }
934 1131
935 1132 foreach ( $field['shortcodes'] as $k => $v ) {
936 - if ( isset( $field['subfield_name'] ) && 0 === strpos( $k, 'aria-invalid' ) ) {
1133 + if ( isset( $field['subfield_name'] ) && str_starts_with( $k, 'aria-invalid' ) ) {
937 1134 $subfield_name = $field['subfield_name'];
1135 +
938 1136 if ( ! isset( $field['shortcodes'][ 'aria-invalid-' . $subfield_name ] ) ) {
939 1137 continue;
940 1138 }
941 1139 // Change the key to the correct aria-invalid value so that $add_html is set correctly for the current subfield of a combo field.
@@ -942,13 +1140,14 @@
942 1140 $k = 'aria-invalid';
943 1141 $v = $field['shortcodes'][ 'aria-invalid-' . $subfield_name ];
944 1142 unset( $field['shortcodes'][ 'aria-invalid-' . $subfield_name ] );
945 1143 }
1144 +
946 1145 if ( 'opt' === $k || ! self::should_allow_input_attribute( $k ) ) {
947 1146 continue;
948 1147 }
949 1148
950 - if ( is_numeric( $k ) && strpos( $v, '=' ) ) {
1149 + if ( is_numeric( $k ) && str_contains( $v, '=' ) ) {
951 1150 $add_html[] = $v;
952 1151 } elseif ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
953 1152 $add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
954 1153 } else {
@@ -964,8 +1163,9 @@
964 1163 *
965 1164 * @since 6.11.2
966 1165 *
967 1166 * @param string $key The option key.
1167 + *
968 1168 * @return bool
969 1169 */
970 1170 private static function should_allow_input_attribute( $key ) {
971 1171 if ( ! FrmAppHelper::should_never_allow_unfiltered_html() ) {
@@ -980,8 +1180,9 @@
980 1180 * @since 3.0
981 1181 *
982 1182 * @param array $field
983 1183 * @param array $add_html
1184 + *
984 1185 * @return void
985 1186 */
986 1187 private static function add_pattern_attribute( $field, array &$add_html ) {
987 1188 $format_value = FrmField::get_option( $field, 'format' );
@@ -987,16 +1188,77 @@
987 1188 $format_value = FrmField::get_option( $field, 'format' );
988 1189 $has_format = $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value );
989 1190 $format_field = FrmField::is_field_type( $field, 'text' );
990 1191
991 - if ( $field['type'] === 'phone' || ( $has_format && $format_field ) ) {
992 - $format = FrmEntryValidate::phone_format( $field );
993 - $format = substr( $format, 2, - 1 );
1192 + if ( $field['type'] !== 'phone' && ( ! $has_format || ! $format_field ) ) {
1193 + return;
1194 + }
994 1195
995 - $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;
996 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 + }
997 1230 }
998 1231
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 + /**
1255 + * @param mixed $opt
1256 + * @param mixed $opt_key
1257 + * @param array|object $field
1258 + *
1259 + * @return mixed
1260 + */
999 1261 public static function check_value( $opt, $opt_key, $field ) {
1000 1262 if ( is_array( $opt ) ) {
1001 1263 if ( FrmField::is_option_true( $field, 'separate_value' ) ) {
1002 1264 $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt );
@@ -1007,12 +1269,13 @@
1007 1269
1008 1270 return $opt;
1009 1271 }
1010 1272
1273 + /**
1274 + * @param mixed $opt
1275 + *
1276 + * @return mixed
1277 + */
1011 1278 public static function check_label( $opt ) {
1012 - if ( is_array( $opt ) ) {
1013 - $opt = $opt['label'] ?? reset( $opt );
1014 - }
1015 -
1016 - return $opt;
1279 + return is_array( $opt ) ? ( $opt['label'] ?? reset( $opt ) ) : $opt;
1017 1280 }
1018 1281 }