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 +612 -244 6.3.1 → trunk View file →
@@ -4,48 +4,62 @@
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 +
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 + */
8 24 public static function load_field() {
9 25 FrmAppHelper::permission_check( 'frm_edit_forms' );
10 26 check_ajax_referer( 'frm_ajax', 'nonce' );
11 27
12 - // Javascript may be included in some field settings.
13 - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
14 - $fields = isset( $_POST['field'] ) ? wp_unslash( $_POST['field'] ) : array();
15 - 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 ) {
16 32 wp_die();
17 33 }
18 34
19 35 $_GET['page'] = 'formidable';
20 -
21 - $values = array(
22 - '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,
23 39 'doing_ajax' => true,
24 40 );
25 - $field_html = array();
41 + $field_html = array();
26 42
27 - foreach ( $fields as $field ) {
28 - $field = htmlspecialchars_decode( nl2br( $field ) );
29 - $field = json_decode( $field );
30 - if ( ! isset( $field->id ) || ! is_numeric( $field->id ) ) {
31 - // 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.
32 46 continue;
33 47 }
34 48
35 - if ( ! isset( $field->value ) ) {
36 - $field->value = '';
37 - }
38 - $field->field_options = json_decode( json_encode( $field->field_options ), true );
39 - $field->options = json_decode( json_encode( $field->options ), true );
40 - $field->default_value = json_decode( json_encode( $field->default_value ), true );
49 + $field = $fields[ $field_id ];
41 50
42 51 ob_start();
43 52 self::load_single_field( $field, $values );
44 - $field_html[ absint( $field->id ) ] = ob_get_contents();
45 - ob_end_clean();
46 - }
47 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 +
48 62 echo json_encode( $field_html );
49 63
50 64 wp_die();
51 65 }
@@ -50,8 +64,40 @@
50 64 wp_die();
51 65 }
52 66
53 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 + /**
54 100 * Create a new field with ajax
55 101 */
56 102 public static function create() {
57 103 FrmAppHelper::permission_check( 'frm_edit_forms' );
@@ -56,16 +102,17 @@
56 102 public static function create() {
57 103 FrmAppHelper::permission_check( 'frm_edit_forms' );
58 104 check_ajax_referer( 'frm_ajax', 'nonce' );
59 105
60 - $field_type = FrmAppHelper::get_post_param( 'field_type', '', 'sanitize_text_field' );
61 - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
106 + $field_type = FrmAppHelper::get_post_param( 'field_type', '', 'sanitize_text_field' );
107 + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
108 + $field_options = FrmAppHelper::get_post_param( 'field_options', array(), 'wp_kses_post' );
62 109
63 110 do_action( 'frm_before_create_field', $field_type, $form_id );
64 111
65 - $field = self::include_new_field( $field_type, $form_id );
112 + $field = self::include_new_field( $field_type, $form_id, $field_options );
66 113
67 - // 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
68 115 do_action( 'frm_after_field_created', $field, $form_id );
69 116
70 117 wp_die();
71 118 }
@@ -73,29 +120,40 @@
73 120 /**
74 121 * Set up and create a new field
75 122 *
76 123 * @param string $field_type
77 - * @param integer $form_id
124 + * @param int $form_id
125 + * @param array $field_options
78 126 *
79 - * @return array|bool
127 + * @return array|false
80 128 */
81 - public static function include_new_field( $field_type, $form_id ) {
129 + public static function include_new_field( $field_type, $form_id, $field_options = array() ) {
82 130 $field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
131 +
132 + if ( $field_options ) {
133 + $field_values['field_options'] = array_merge( $field_values['field_options'], $field_options );
134 + }
135 +
136 + // When a new field is added to the form, flag it as draft and hide it from the front-end.
137 + $field_values['field_options']['draft'] = 1;
138 +
139 + /**
140 + * @param array $field_values
141 + */
83 142 $field_values = apply_filters( 'frm_before_field_created', $field_values );
143 + $field_id = FrmField::create( $field_values );
84 144
85 - $field_id = FrmField::create( $field_values );
86 -
87 145 if ( ! $field_id ) {
88 146 return false;
89 147 }
90 148
91 - $field = self::get_field_array_from_id( $field_id );
149 + $field = self::get_field_array_from_id( $field_id );
150 + $values = array();
92 151
93 - $values = array();
94 152 if ( FrmAppHelper::pro_is_installed() ) {
95 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' );
96 155
97 - $parent_form_id = FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'parent_form_id' );
98 156 if ( $parent_form_id ) {
99 157 $field['parent_form_id'] = $parent_form_id;
100 158 }
101 159 }
@@ -108,15 +166,14 @@
108 166 public static function duplicate() {
109 167 FrmAppHelper::permission_check( 'frm_edit_forms' );
110 168 check_ajax_referer( 'frm_ajax', 'nonce' );
111 169
112 - $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
113 - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
114 -
170 + $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
171 + $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
115 172 $new_field = FrmField::duplicate_single_field( $field_id, $form_id );
116 173
117 174 if ( is_array( $new_field ) && ! empty( $new_field['field_id'] ) ) {
118 - 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 );
119 176 }
120 177
121 178 wp_die();
122 179 }
@@ -129,9 +186,8 @@
129 186 * @return array
130 187 */
131 188 public static function get_field_array_from_id( $field_id ) {
132 189 $field = FrmField::getOne( $field_id );
133 -
134 190 return FrmFieldsHelper::setup_edit_vars( $field );
135 191 }
136 192
137 193 /**
@@ -136,11 +192,13 @@
136 192
137 193 /**
138 194 * @since 3.0
139 195 *
140 - * @param int|array|object $field_object
141 - * @param array $values
142 - * @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
143 201 */
144 202 public static function load_single_field( $field_object, $values, $form_id = 0 ) {
145 203 global $frm_vars;
146 204 $frm_vars['is_admin'] = true;
@@ -151,38 +209,63 @@
151 209 $field = $field_object;
152 210 $field_object = FrmField::getOne( $field['id'] );
153 211 }
154 212
155 - $field_obj = FrmFieldFactory::get_field_factory( $field_object );
156 - $display = self::display_field_options( array(), $field_obj );
213 + $field_obj = FrmFieldFactory::get_field_factory( $field_object );
214 + $display = self::display_field_options( array(), $field_obj );
215 + $ajax_loading = ! empty( $values['ajax_load'] );
216 + $ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ), true );
157 217
158 - $ajax_loading = isset( $values['ajax_load'] ) && $values['ajax_load'];
159 - $ajax_this_field = isset( $values['count'] ) && $values['count'] > 10 && ! in_array( $field_object->type, array( 'divider', 'end_divider' ) );
160 -
161 218 if ( $ajax_loading && $ajax_this_field ) {
162 219 $li_classes = self::get_classes_for_builder_field( array(), $display, $field_obj );
163 - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php' );
164 - } else {
165 - if ( ! isset( $field ) && is_object( $field_object ) ) {
166 - $field_object->parent_form_id = isset( $values['id'] ) ? $values['id'] : $field_object->form_id;
220 + include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php';
221 + return;
222 + }
167 223
168 - $field = FrmFieldsHelper::setup_edit_vars( $field_object );
169 - }
224 + if ( ! isset( $field ) && is_object( $field_object ) ) {
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 );
229 + $field = FrmFieldsHelper::setup_edit_vars( $field_object );
230 + }
170 231
171 - $li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj );
172 - $li_classes .= ' ui-state-default widgets-holder-wrap';
232 + /**
233 + * Filter for adding extra attributes to the field container.
234 + *
235 + * @since 6.23
236 + *
237 + * @param array $extra_field_attributes
238 + * @param array $field
239 + * @param array $display
240 + */
241 + $extra_field_attributes = apply_filters( 'frm_field_container_extra_attributes', array(), $field, $display );
173 242
174 - require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' );
175 - }
243 + $li_classes = self::get_classes_for_builder_field( $field, $display, $field_obj );
244 + $li_classes .= ' ui-state-default widgets-holder-wrap';
245 +
246 + require FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php';
176 247 }
177 248
178 249 /**
179 250 * @since 3.0
251 + *
252 + * @param array $field
253 + * @param array $display
254 + * @param FrmFieldType $field_info
255 + *
256 + * @return string
180 257 */
181 258 private static function get_classes_for_builder_field( $field, $display, $field_info ) {
182 259 $li_classes = $field_info->form_builder_classes( $display['type'] );
183 260 $li_classes .= ' frm_form_field frmstart ';
184 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 +
185 268 if ( isset( $field['classes'] ) ) {
186 269 $li_classes .= trim( $field['classes'] ) . ' ';
187 270 }
188 271
@@ -188,14 +271,45 @@
188 271
189 272 $li_classes .= 'frmend';
190 273
191 274 if ( $field ) {
192 - $li_classes = apply_filters( 'frm_build_field_class', $li_classes, $field );
275 + return apply_filters( 'frm_build_field_class', $li_classes, $field );
193 276 }
194 277
195 278 return $li_classes;
196 279 }
197 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 +
198 312 public static function destroy() {
199 313 FrmAppHelper::permission_check( 'frm_edit_forms' );
200 314 check_ajax_referer( 'frm_ajax', 'nonce' );
201 315
@@ -203,10 +317,11 @@
203 317 FrmField::destroy( $field_id );
204 318 wp_die();
205 319 }
206 320
207 - /* Field Options */
208 -
321 + /**
322 + * Field Options.
323 + */
209 324 public static function import_options() {
210 325 FrmAppHelper::permission_check( 'frm_edit_forms' );
211 326 check_ajax_referer( 'frm_ajax', 'nonce' );
212 327
@@ -213,27 +328,40 @@
213 328 if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
214 329 return;
215 330 }
216 331
217 - $field_id = FrmAppHelper::get_param( 'field_id', '', 'post', 'absint' );
218 - $field = FrmField::getOne( $field_id );
332 + $field_id = FrmAppHelper::get_param( 'field_id', '', 'post', 'absint' );
333 + $field = FrmField::getOne( $field_id );
334 + $bulk_edit_types = array( 'radio', 'checkbox', 'select' );
219 335
220 - if ( ! in_array( $field->type, array( 'radio', 'checkbox', 'select' ) ) ) {
336 + /**
337 + * Filter to add new fields that will support import_options/Bulk Edit Options.
338 + *
339 + * @since 6.8.4
340 + *
341 + * @param array $bulk_edit_types
342 + *
343 + * @return array
344 + */
345 + $bulk_edit_types = apply_filters( 'frm_bulk_edit_field_types', $bulk_edit_types );
346 +
347 + if ( ! in_array( $field->type, $bulk_edit_types, true ) ) {
221 348 return;
222 349 }
223 350
224 351 $field = FrmFieldsHelper::setup_edit_vars( $field );
225 - $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
226 - $opts = explode( "\n", rtrim( $opts, "\n" ) );
227 - $opts = array_map( 'trim', $opts );
228 352
229 - $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' );
230 - $field['separate_value'] = ( $separate === 'true' );
353 + $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
354 + $opts = explode( "\n", rtrim( $opts, "\n" ) );
355 + $opts = array_map( 'trim', $opts );
231 356
357 + $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' );
358 + $field['separate_value'] = $separate === 'true';
359 +
232 360 if ( $field['separate_value'] ) {
233 361 foreach ( $opts as $opt_key => $opt ) {
234 - if ( strpos( $opt, '|' ) !== false ) {
235 - $vals = explode( '|', $opt );
362 + if ( str_contains( $opt, '|' ) ) {
363 + $vals = explode( '|', $opt );
236 364 $opts[ $opt_key ] = array(
237 365 'label' => trim( $vals[0] ),
238 366 'value' => trim( $vals[1] ),
239 367 );
@@ -243,10 +371,11 @@
243 371 }
244 372 }
245 373
246 374 // Keep other options after bulk update.
247 - if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) {
375 + if ( ! empty( $field['field_options']['other'] ) ) {
248 376 $other_array = array();
377 +
249 378 foreach ( $field['options'] as $opt_key => $opt ) {
250 379 if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
251 380 $other_array[ $opt_key ] = $opt;
252 381 }
@@ -251,9 +380,10 @@
251 380 $other_array[ $opt_key ] = $opt;
252 381 }
253 382 unset( $opt_key, $opt );
254 383 }
255 - if ( ! empty( $other_array ) ) {
384 +
385 + if ( $other_array ) {
256 386 $opts = array_merge( $opts, $other_array );
257 387 }
258 388 }
259 389
@@ -267,8 +397,10 @@
267 397 /**
268 398 * @since 4.0
269 399 *
270 400 * @param array $atts - Includes field array, field_obj, display array, values array.
401 + *
402 + * @return void
271 403 */
272 404 public static function load_single_field_settings( $atts ) {
273 405 $field = $atts['field'];
274 406 $field_obj = $atts['field_obj'];
@@ -283,19 +415,19 @@
283 415 if ( ! isset( $field['read_only'] ) ) {
284 416 $field['read_only'] = false;
285 417 }
286 418
287 - $field_types = FrmFieldsHelper::get_field_types( $field['type'] );
288 - $pro_field_selection = FrmField::pro_field_selection();
289 - $all_field_types = array_merge( $pro_field_selection, FrmField::field_selection() );
290 - $disabled_fields = FrmAppHelper::pro_is_installed() ? array() : $pro_field_selection;
291 - $frm_settings = FrmAppHelper::get_settings();
419 + $field_selection_data = self::maybe_define_field_selection_data();
420 + $all_field_types = $field_selection_data->all_field_types;
421 + $disabled_fields = $field_selection_data->disabled_fields;
422 + $frm_settings = FrmAppHelper::get_settings();
423 + $field_types = FrmFieldTypeOptionData::get_field_types( $field['type'] );
292 424
293 425 if ( ! isset( $all_field_types[ $field['type'] ] ) ) {
294 426 // Add fallback for an add-on field type that has been deactivated.
295 427 $all_field_types[ $field['type'] ] = array(
296 428 'name' => ucfirst( $field['type'] ),
297 - 'icon' => 'frm_icon_font frm_pencil_icon',
429 + 'icon' => 'frmfont frm_pencil_icon',
298 430 );
299 431 } elseif ( ! is_array( $all_field_types[ $field['type'] ] ) ) {
300 432 // Fallback for fields added in a more basic way.
301 433 FrmFormsHelper::prepare_field_type( $all_field_types[ $field['type'] ] );
@@ -301,8 +433,9 @@
301 433 FrmFormsHelper::prepare_field_type( $all_field_types[ $field['type'] ] );
302 434 }
303 435
304 436 $type_name = $all_field_types[ $field['type'] ]['name'];
437 +
305 438 if ( $field['type'] === 'divider' && FrmField::is_option_true( $field, 'repeat' ) ) {
306 439 $type_name = $all_field_types['divider|repeat']['name'];
307 440 }
308 441
@@ -312,61 +445,148 @@
312 445
313 446 if ( $display['clear_on_focus'] && is_array( $field['placeholder'] ) ) {
314 447 $field['placeholder'] = implode( ', ', $field['placeholder'] );
315 448 }
316 - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php' );
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 +
521 + include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php';
317 522 }
318 523
319 524 /**
525 + * @since 6.9.1
526 + *
527 + * @return FrmFieldSelectionData
528 + */
529 + private static function maybe_define_field_selection_data() {
530 + if ( ! isset( self::$field_selection_data ) ) {
531 + self::$field_selection_data = new FrmFieldSelectionData();
532 + }
533 + return self::$field_selection_data;
534 + }
535 +
536 + /**
320 537 * Get the list of default value types that can be toggled in the builder.
321 538 *
322 539 * @since 4.0
540 + *
541 + * @param array $field
542 + * @param array $atts
543 + *
323 544 * @return array
324 545 */
325 546 private static function default_value_types( $field, $atts ) {
326 547 $types = array(
327 - 'default_value' => array(
548 + 'default_value' => array(
328 549 'class' => '',
329 - 'icon' => 'frm_icon_font frm_text2_icon',
330 - 'title' => __( 'Default Value (Text)', 'formidable' ),
550 + 'icon' => 'frmfont frm_text2_icon',
551 + 'title' => __( 'Default Value', 'formidable' ),
331 552 'data' => array(
332 553 'frmshow' => '#default-value-for-',
333 554 ),
334 555 ),
335 - 'calc' => array(
336 - 'class' => 'frm_show_upgrade frm_noallow',
337 - 'title' => __( 'Default Value (Calculation)', 'formidable' ),
338 - 'icon' => 'frm_icon_font frm_calculator_icon',
339 - 'data' => array(
556 + 'calc' => array(
557 + 'class' => 'frm_show_upgrade frm_noallow',
558 + 'title' => __( 'Calculate Value', 'formidable' ),
559 + 'icon' => 'frmfont frm_calculator_icon',
560 + 'data' => array(
340 561 'medium' => 'calculations',
341 562 'upgrade' => __( 'Calculator forms', 'formidable' ),
342 563 ),
564 + 'tooltip' => __( 'Automatically calculate the value of this field based on values from other fields.', 'formidable' ),
343 565 ),
344 566 'get_values_field' => array(
345 - 'class' => 'frm_show_upgrade frm_noallow',
346 - 'title' => __( 'Default Value (Lookup)', 'formidable' ),
347 - 'icon' => 'frm_icon_font frm_search_icon',
348 - 'data' => array(
567 + 'class' => 'frm_show_upgrade frm_noallow',
568 + 'title' => __( 'Lookup', 'formidable' ),
569 + 'icon' => 'frmfont frm_search_icon',
570 + 'data' => array(
349 571 'medium' => 'lookup',
350 572 'upgrade' => __( 'Lookup fields', 'formidable' ),
351 573 ),
574 + 'tooltip' => __( 'Dynamically retrieve the value of this field from a lookup field.', 'formidable' ),
352 575 ),
353 576 );
354 577
355 578 $types = apply_filters( 'frm_default_value_types', $types, $atts );
356 579
357 - if ( FrmAppHelper::pro_is_installed() && ! FrmAppHelper::meets_min_pro_version( '4.0' ) ) {
358 - // Prevent settings from showing in 2 spots.
359 - unset( $types['calc'], $types['get_values_field'] );
360 - }
361 -
362 580 // Set active class.
363 581 $settings = array_keys( $types );
364 582 $active = 'default_value';
365 583
366 - foreach ( $settings as $type ) {
367 - if ( ! empty( $field[ $type ] ) ) {
368 - $active = $type;
584 + if ( FrmAppHelper::pro_is_connected() ) {
585 + foreach ( $settings as $type ) {
586 + if ( ! empty( $field[ $type ] ) ) {
587 + $active = $type;
588 + }
369 589 }
370 590 }
371 591
372 592 $types[ $active ]['class'] .= ' current';
@@ -374,8 +594,13 @@
374 594
375 595 return $types;
376 596 }
377 597
598 + /**
599 + * @param string $type
600 + *
601 + * @return string
602 + */
378 603 public static function change_type( $type ) {
379 604 $type_switch = array(
380 605 'scale' => 'radio',
381 606 'star' => 'radio',
@@ -383,30 +608,32 @@
383 608 'rte' => 'textarea',
384 609 'website' => 'url',
385 610 'image' => 'url',
386 611 );
612 +
387 613 if ( isset( $type_switch[ $type ] ) ) {
388 614 $type = $type_switch[ $type ];
389 615 }
390 616
391 617 $pro_fields = FrmField::pro_field_selection();
392 - $types = array_keys( $pro_fields );
393 - if ( in_array( $type, $types ) ) {
394 - $type = 'text';
395 - }
618 + // We want to keep credit_card types as credit card types for Stripe Lite.
619 + // The credit_card key is set for backward compatibility.
620 + FrmField::remove_moved_field_types_from_pro( $pro_fields );
396 621
397 - return $type;
622 + return array_key_exists( $type, $pro_fields ) ? 'text' : $type;
398 623 }
399 624
400 625 /**
401 - * @param array $settings
402 - * @param object $field_info
626 + * @param array $settings
627 + * @param FrmFieldType|null $field_info
403 628 *
404 629 * @return array
405 630 */
406 631 public static function display_field_options( $settings, $field_info = null ) {
407 632 if ( $field_info ) {
408 - $settings = $field_info->display_field_settings();
633 + $settings = $field_info->display_field_settings();
634 +
635 + // Field appears to be protected but there is a __get function in FrmFieldType that makes this public.
409 636 $settings['field_data'] = $field_info->field;
410 637 }
411 638
412 639 return apply_filters( 'frm_display_field_options', $settings );
@@ -416,14 +643,32 @@
416 643 * Display the format option
417 644 *
418 645 * @since 3.0
419 646 *
420 - * @param array $field
647 + * @param array $field Field data.
648 + * @param bool $is_hidden Whether the format option should be hidden.
649 + *
650 + * @return void
421 651 */
422 - public static function show_format_option( $field ) {
423 - include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php' );
652 + public static function show_format_option( $field, $is_hidden = false ) {
653 + $attributes = array(
654 + 'class' => 'frm-has-modal',
655 + 'id' => 'frm-field-format-custom-' . $field['id'],
656 + );
657 +
658 + if ( $is_hidden ) {
659 + $attributes['class'] .= ' frm_hidden';
660 + }
661 +
662 + include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/value-format.php';
424 663 }
425 664
665 + /**
666 + * @param array $field
667 + * @param bool $echo
668 + *
669 + * @return string
670 + */
426 671 public static function input_html( $field, $echo = true ) {
427 672 $class = array();
428 673 self::add_input_classes( $field, $class );
429 674
@@ -438,8 +683,9 @@
438 683 FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
439 684
440 685 self::add_shortcodes_to_html( $field, $add_html );
441 686 self::add_pattern_attribute( $field, $add_html );
687 + self::add_currency_field_attributes( $field, $add_html );
442 688
443 689 $add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
444 690 $add_html = ' ' . implode( ' ', $add_html ) . ' ';
445 691
@@ -449,14 +695,20 @@
449 695
450 696 return $add_html;
451 697 }
452 698
699 + /**
700 + * @param array $field
701 + * @param array $class
702 + *
703 + * @return void
704 + */
453 705 private static function add_input_classes( $field, array &$class ) {
454 - if ( isset( $field['input_class'] ) && ! empty( $field['input_class'] ) ) {
706 + if ( ! empty( $field['input_class'] ) ) {
455 707 $class[] = $field['input_class'];
456 708 }
457 709
458 - if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) {
710 + if ( $field['type'] === 'hidden' || $field['type'] === 'user_id' ) {
459 711 return;
460 712 }
461 713
462 714 if ( isset( $field['size'] ) && $field['size'] > 0 ) {
@@ -463,8 +715,14 @@
463 715 $class[] = 'auto_width';
464 716 }
465 717 }
466 718
719 + /**
720 + * @param array $field
721 + * @param array $add_html
722 + *
723 + * @return void
724 + */
467 725 private static function add_html_size( $field, array &$add_html ) {
468 726 $size_fields = array(
469 727 'select',
470 728 'data',
@@ -473,9 +731,9 @@
473 731 'file',
474 732 'lookup',
475 733 );
476 734
477 - if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields ) ) {
735 + if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields, true ) ) {
478 736 return;
479 737 }
480 738
481 739 if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
@@ -492,10 +750,16 @@
492 750
493 751 self::add_html_cols( $field, $add_html );
494 752 }
495 753
754 + /**
755 + * @param array $field
756 + * @param array $add_html
757 + *
758 + * @return void
759 + */
496 760 private static function add_html_cols( $field, array &$add_html ) {
497 - if ( ! in_array( $field['type'], array( 'textarea', 'rte' ) ) ) {
761 + if ( ! in_array( $field['type'], array( 'textarea', 'rte' ), true ) ) {
498 762 return;
499 763 }
500 764
501 765 // Convert to cols for textareas.
@@ -505,9 +769,9 @@
505 769 'rem' => 0.444,
506 770 'em' => 0.544,
507 771 );
508 772
509 - // include "col" for valid html
773 + // Include "col" for valid html
510 774 $unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) );
511 775
512 776 if ( ! isset( $calc[ $unit ] ) ) {
513 777 return;
@@ -512,13 +776,18 @@
512 776 if ( ! isset( $calc[ $unit ] ) ) {
513 777 return;
514 778 }
515 779
516 - $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
517 -
780 + $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
518 781 $add_html['cols'] = 'cols="' . absint( $size ) . '"';
519 782 }
520 783
784 + /**
785 + * @param array $field
786 + * @param array $add_html
787 + *
788 + * @return void
789 + */
521 790 private static function add_html_length( $field, array &$add_html ) {
522 791 // Check for max setting and if this field accepts maxlength.
523 792 $fields = array(
524 793 'textarea',
@@ -526,9 +795,9 @@
526 795 'hidden',
527 796 'file',
528 797 );
529 798
530 - if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields ) ) {
799 + if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields, true ) ) {
531 800 return;
532 801 }
533 802
534 803 if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
@@ -538,9 +807,17 @@
538 807
539 808 $add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"';
540 809 }
541 810
811 + /**
812 + * @param array $field
813 + * @param array $add_html
814 + * @param array $class
815 + *
816 + * @return void
817 + */
542 818 private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
819 + // phpcs:ignore Universal.Operators.StrictComparisons
543 820 if ( $field['default_value'] != '' ) {
544 821 if ( is_array( $field['default_value'] ) ) {
545 822 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( json_encode( $field['default_value'] ) ) . '"';
546 823 } else {
@@ -548,26 +825,16 @@
548 825 }
549 826 }
550 827
551 828 $field['placeholder'] = self::prepare_placeholder( $field );
829 +
830 + // phpcs:ignore Universal.Operators.StrictComparisons
552 831 if ( $field['placeholder'] == '' || is_array( $field['placeholder'] ) ) {
553 - // don't include a json placeholder
832 + // Don't include a json placeholder
554 833 return;
555 834 }
556 835
557 - $frm_settings = FrmAppHelper::get_settings();
558 -
559 - if ( $frm_settings->use_html ) {
560 - self::add_placeholder_to_input( $field, $add_html );
561 - } else {
562 - self::add_frmval_to_input( $field, $add_html );
563 -
564 - $class[] = 'frm_toggle_default';
565 -
566 - if ( $field['value'] == $field['placeholder'] ) {
567 - $class[] = 'frm_default';
568 - }
569 - }
836 + self::add_placeholder_to_input( $field, $add_html );
570 837 }
571 838
572 839 /**
573 840 * Prepares field placeholder.
@@ -574,14 +841,13 @@
574 841 *
575 842 * @since 5.4 This doesn't call `FrmFieldsController::get_default_value_from_name()` anymore.
576 843 *
577 844 * @param array $field Field array.
845 + *
578 846 * @return string
579 847 */
580 848 private static function prepare_placeholder( $field ) {
581 - $placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
582 -
583 - return $placeholder;
849 + return $field['placeholder'] ?? '';
584 850 }
585 851
586 852 /**
587 853 * If the label position is "inside",
@@ -602,22 +868,39 @@
602 868 * Maybe add a blank placeholder option before any options
603 869 * in a dropdown.
604 870 *
605 871 * @since 4.04
872 + *
873 + * @param array|object $field
874 + *
606 875 * @return bool True if placeholder was added.
607 876 */
608 877 public static function add_placeholder_to_select( $field ) {
609 878 $placeholder = FrmField::get_option( $field, 'placeholder' );
610 - if ( empty( $placeholder ) ) {
879 +
880 + if ( ! $placeholder ) {
611 881 $placeholder = self::get_default_value_from_name( $field );
612 882 }
613 883
884 + $use_placeholder = $placeholder;
885 + $autocomplete = FrmField::get_option( $field, 'autocom' );
886 +
887 + if ( $autocomplete ) {
888 + $use_chosen = ! is_callable( 'FrmProAppHelper::use_chosen_js' ) || FrmProAppHelper::use_chosen_js();
889 +
890 + if ( $use_chosen ) {
891 + $use_placeholder = '';
892 + }
893 + }
894 +
614 895 if ( $placeholder !== '' ) {
615 - ?>
616 - <option value="">
617 - <?php echo esc_html( FrmField::get_option( $field, 'autocom' ) ? '' : $placeholder ); ?>
618 - </option>
619 - <?php
896 + $placeholder_attributes = array(
897 + 'class' => 'frm-select-placeholder',
898 + 'value' => '',
899 + 'data-placeholder' => 'true',
900 + );
901 +
902 + FrmHtmlHelper::echo_dropdown_option( $use_placeholder, false, $placeholder_attributes );
620 903 return true;
621 904 }
622 905
623 906 return false;
@@ -623,12 +906,14 @@
623 906 return false;
624 907 }
625 908
626 909 /**
627 - * Use HMTL5 placeholder with js fallback
910 + * Use HTML5 placeholder with js fallback.
628 911 *
629 912 * @param array $field
630 913 * @param array $add_html
914 + *
915 + * @return void
631 916 */
632 917 private static function add_placeholder_to_input( $field, &$add_html ) {
633 918 if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) {
634 919 $add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"';
@@ -634,9 +919,16 @@
634 919 $add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"';
635 920 }
636 921 }
637 922
923 + /**
924 + * @param array $field
925 + * @param array $add_html
926 + *
927 + * @return void
928 + */
638 929 private static function add_frmval_to_input( $field, &$add_html ) {
930 + // phpcs:ignore Universal.Operators.StrictComparisons
639 931 if ( $field['placeholder'] != '' ) {
640 932 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['placeholder'] ) . '"';
641 933
642 934 if ( 'select' === $field['type'] ) {
@@ -643,21 +935,30 @@
643 935 $add_html['data-frmplaceholder'] = 'data-frmplaceholder="' . esc_attr( $field['placeholder'] ) . '"';
644 936 }
645 937 }
646 938
939 + // phpcs:ignore Universal.Operators.StrictComparisons
647 940 if ( $field['default_value'] != '' ) {
648 941 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
649 942 }
650 943 }
651 944
945 + /**
946 + * @param array $field
947 + * @param array $add_html
948 + *
949 + * @return void
950 + */
652 951 private static function add_validation_messages( $field, array &$add_html ) {
653 - if ( FrmField::is_required( $field ) ) {
952 + $field_validation_messages_status = self::get_validation_data_attribute_visibility_info( $field );
953 +
954 + if ( FrmField::is_required( $field ) && ! empty( $field_validation_messages_status['data-reqmsg'] ) ) {
654 955 $required_message = FrmFieldsHelper::get_error_msg( $field, 'blank' );
655 956 $add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"';
656 957 self::maybe_add_html_required( $field, $add_html );
657 958 }
658 959
659 - if ( ! FrmField::is_option_empty( $field, 'invalid' ) ) {
960 + if ( ! FrmField::is_option_empty( $field, 'invalid' ) && ! empty( $field_validation_messages_status['data-invmsg'] ) ) {
660 961 $invalid_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
661 962 $add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"';
662 963 }
663 964
@@ -666,24 +967,63 @@
666 967 }
667 968 }
668 969
669 970 /**
971 + * Returns an array that contains field validation messages status.
972 + *
973 + * @since 6.9
974 + *
975 + * @param array|object $field
976 + *
977 + * @return array
978 + */
979 + private static function get_validation_data_attribute_visibility_info( $field ) {
980 + if ( FrmField::get_field_type( $field ) === 'hidden' ) {
981 + $field_validation_data_attributes = array(
982 + 'data-invmsg' => false,
983 + 'data-reqmsg' => false,
984 + );
985 + } else {
986 + $field_validation_data_attributes = array(
987 + 'data-invmsg' => true,
988 + 'data-reqmsg' => true,
989 + );
990 + }
991 +
992 + /**
993 + * Allows controlling which field validation messages would be included in the field html.
994 + *
995 + * @since 6.9
996 + *
997 + * @param array $field_validation_messages_status
998 + * @param array|object $field
999 + */
1000 + return apply_filters( 'frm_field_validation_include_data_attributes', $field_validation_data_attributes, $field );
1001 + }
1002 +
1003 + /**
670 1004 * @since 5.0.03
671 1005 *
672 1006 * @param array $field
673 1007 * @param array $add_html
1008 + *
1009 + * @return void
674 1010 */
675 1011 private static function maybe_add_error_html_for_js_validation( $field, array &$add_html ) {
676 1012 $form = self::get_form_for_js_validation( $field );
1013 +
677 1014 if ( false === $form ) {
678 1015 return;
679 1016 }
680 1017
681 1018 $error_body = self::pull_custom_error_body_from_custom_html( $form, $field );
682 - if ( false !== $error_body ) {
683 - $error_body = urlencode( $error_body );
684 - $add_html['data-error-html'] = 'data-error-html="' . esc_attr( $error_body ) . '"';
1019 +
1020 + if ( false === $error_body ) {
1021 + return;
685 1022 }
1023 +
1024 + $error_body = urlencode( $error_body );
1025 + $add_html['data-error-html'] = 'data-error-html="' . esc_attr( $error_body ) . '"';
686 1026 }
687 1027
688 1028 /**
689 1029 * @since 5.0.03
@@ -688,20 +1028,24 @@
688 1028 /**
689 1029 * @since 5.0.03
690 1030 *
691 1031 * @param array $field
692 - * @return stdClass|false false if there is no form object found with JS validation active.
1032 + *
1033 + * @return false|stdClass false if there is no form object found with JS validation active.
693 1034 */
694 1035 private static function get_form_for_js_validation( $field ) {
695 1036 global $frm_vars;
1037 +
696 1038 if ( ! empty( $frm_vars['js_validate_forms'] ) ) {
697 1039 if ( isset( $frm_vars['js_validate_forms'][ $field['form_id'] ] ) ) {
698 1040 return $frm_vars['js_validate_forms'][ $field['form_id'] ];
699 1041 }
1042 +
700 1043 if ( ! empty( $field['parent_form_id'] ) && isset( $frm_vars['js_validate_forms'][ $field['parent_form_id'] ] ) ) {
701 1044 return $frm_vars['js_validate_forms'][ $field['parent_form_id'] ];
702 1045 }
703 1046 }
1047 +
704 1048 return false;
705 1049 }
706 1050
707 1051 /**
@@ -707,9 +1051,10 @@
707 1051 /**
708 1052 * @param stdClass $form
709 1053 * @param array $field
710 1054 * @param array $errors
711 - * @return string|false
1055 + *
1056 + * @return false|string
712 1057 */
713 1058 public static function pull_custom_error_body_from_custom_html( $form, $field, $errors = array() ) {
714 1059 if ( empty( $field['custom_html'] ) ) {
715 1060 return false;
@@ -716,15 +1061,16 @@
716 1061 }
717 1062
718 1063 $custom_html = $field['custom_html'];
719 1064 $custom_html = apply_filters( 'frm_before_replace_shortcodes', $custom_html, $field, $errors, $form );
1065 + $start = strpos( $custom_html, '[if error]' );
720 1066
721 - $start = strpos( $custom_html, '[if error]' );
722 1067 if ( false === $start ) {
723 1068 return false;
724 1069 }
725 1070
726 1071 $end = strpos( $custom_html, '[/if error]' );
1072 +
727 1073 if ( false === $end || $end < $start ) {
728 1074 return false;
729 1075 }
730 1076
@@ -735,13 +1081,9 @@
735 1081 '<div class="frm_error">[error]</div>',
736 1082 '<div class="frm_error" role="alert">[error]</div>',
737 1083 );
738 1084
739 - if ( in_array( $error_body, $default_html, true ) ) {
740 - return false;
741 - }
742 -
743 - return $error_body;
1085 + return in_array( $error_body, $default_html, true ) ? false : $error_body;
744 1086 }
745 1087
746 1088 /**
747 1089 * If 'required' is added to a conditionally hidden field, the form won't
@@ -748,22 +1090,37 @@
748 1090 * submit in many browsers. Check to make sure the javascript to conditionally
749 1091 * remove it is present if needed.
750 1092 *
751 1093 * @since 3.06.01
1094 + *
752 1095 * @param array $field
753 1096 * @param array $add_html
1097 + *
1098 + * @return void
754 1099 */
755 1100 private static function maybe_add_html_required( $field, array &$add_html ) {
756 - if ( in_array( $field['type'], array( 'file', 'data', 'lookup' ), true ) ) {
1101 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
1102 + $excluded_field_types =
1103 + FrmField::is_radio( $field ) ||
1104 + FrmField::is_checkbox( $field ) ||
1105 + FrmField::is_field_type( $field, 'file' ) ||
1106 + FrmField::is_field_type( $field, 'nps' ) ||
1107 + FrmField::is_field_type( $field, 'scale' );
1108 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
1109 +
1110 + if ( $excluded_field_types ) {
757 1111 return;
758 1112 }
759 1113
760 - $include_html = FrmAppHelper::meets_min_pro_version( '3.06.01' );
761 - if ( $include_html ) {
762 - $add_html['aria-required'] = 'aria-required="true"';
763 - }
1114 + $add_html['aria-required'] = 'aria-required="true"';
764 1115 }
765 1116
1117 + /**
1118 + * @param array $field
1119 + * @param array $add_html
1120 + *
1121 + * @return void
1122 + */
766 1123 private static function add_shortcodes_to_html( $field, array &$add_html ) {
767 1124 if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) {
768 1125 return;
769 1126 }
@@ -772,13 +1129,25 @@
772 1129 unset( $field['shortcodes']['autocomplete'] );
773 1130 }
774 1131
775 1132 foreach ( $field['shortcodes'] as $k => $v ) {
776 - if ( 'opt' === $k ) {
1133 + if ( isset( $field['subfield_name'] ) && str_starts_with( $k, 'aria-invalid' ) ) {
1134 + $subfield_name = $field['subfield_name'];
1135 +
1136 + if ( ! isset( $field['shortcodes'][ 'aria-invalid-' . $subfield_name ] ) ) {
1137 + continue;
1138 + }
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.
1140 + $k = 'aria-invalid';
1141 + $v = $field['shortcodes'][ 'aria-invalid-' . $subfield_name ];
1142 + unset( $field['shortcodes'][ 'aria-invalid-' . $subfield_name ] );
1143 + }
1144 +
1145 + if ( 'opt' === $k || ! self::should_allow_input_attribute( $k ) ) {
777 1146 continue;
778 1147 }
779 1148
780 - if ( is_numeric( $k ) && strpos( $v, '=' ) ) {
1149 + if ( is_numeric( $k ) && str_contains( $v, '=' ) ) {
781 1150 $add_html[] = $v;
782 1151 } elseif ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
783 1152 $add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
784 1153 } else {
@@ -785,129 +1154,128 @@
785 1154 $add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"';
786 1155 }
787 1156
788 1157 unset( $k, $v );
1158 + }//end foreach
1159 + }
1160 +
1161 + /**
1162 + * Disallow possibly unsafe attributees (that trigger JavaScript) when unasfe HTML is not allowed.
1163 + *
1164 + * @since 6.11.2
1165 + *
1166 + * @param string $key The option key.
1167 + *
1168 + * @return bool
1169 + */
1170 + private static function should_allow_input_attribute( $key ) {
1171 + if ( ! FrmAppHelper::should_never_allow_unfiltered_html() ) {
1172 + return true;
789 1173 }
1174 + return FrmAppHelper::input_key_is_safe( $key );
790 1175 }
791 1176
792 1177 /**
793 - * Add pattern attribute
1178 + * Add pattern attribute.
794 1179 *
795 1180 * @since 3.0
796 1181 *
797 1182 * @param array $field
798 1183 * @param array $add_html
1184 + *
1185 + * @return void
799 1186 */
800 1187 private static function add_pattern_attribute( $field, array &$add_html ) {
801 - $has_format = FrmField::is_option_true_in_array( $field, 'format' );
1188 + $format_value = FrmField::get_option( $field, 'format' );
1189 + $has_format = $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value );
802 1190 $format_field = FrmField::is_field_type( $field, 'text' );
803 1191
804 - if ( $field['type'] == 'phone' || ( $has_format && $format_field ) ) {
805 - $frm_settings = FrmAppHelper::get_settings();
1192 + if ( $field['type'] !== 'phone' && ( ! $has_format || ! $format_field ) ) {
1193 + return;
1194 + }
806 1195
807 - if ( $frm_settings->use_html ) {
808 - $format = FrmEntryValidate::phone_format( $field );
809 - $format = substr( $format, 2, - 1 );
1196 + $format = FrmEntryValidate::phone_format( $field );
1197 + $format = substr( $format, 2, - 1 );
810 1198
811 - $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"';
812 - }
813 - }
1199 + $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"';
814 1200 }
815 1201
816 - public static function check_value( $opt, $opt_key, $field ) {
817 - if ( is_array( $opt ) ) {
818 - if ( FrmField::is_option_true( $field, 'separate_value' ) ) {
819 - $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
820 - } else {
821 - $opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt );
822 - }
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;
823 1216 }
824 1217
825 - return $opt;
826 - }
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 );
827 1227
828 - public static function check_label( $opt ) {
829 - if ( is_array( $opt ) ) {
830 - $opt = ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
1228 + $add_html['data-frmprice'] = 'data-frmprice="' . esc_attr( $price ) . '"';
831 1229 }
832 -
833 - return $opt;
834 1230 }
835 1231
836 1232 /**
837 - * @deprecated 4.0
1233 + * @param array $field
838 1234 */
839 - public static function update_ajax_option() {
840 - _deprecated_function( __METHOD__, '4.0' );
841 - FrmAppHelper::permission_check( 'frm_edit_forms' );
842 - check_ajax_referer( 'frm_ajax', 'nonce' );
843 -
844 - $field_id = FrmAppHelper::get_post_param( 'field', 0, 'absint' );
845 - if ( ! $field_id ) {
846 - wp_die();
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'];
847 1243 }
848 1244
849 - $field = FrmField::getOne( $field_id );
1245 + $field_obj = FrmFieldFactory::get_field_object( $field['id'] );
850 1246
851 - if ( isset( $_POST['separate_value'] ) ) {
852 - $new_val = FrmField::is_option_true( $field, 'separate_value' ) ? 0 : 1;
853 -
854 - $field->field_options['separate_value'] = $new_val;
855 - unset( $new_val );
1247 + if ( method_exists( $field_obj, 'get_posted_price' ) ) {
1248 + return $field_obj->get_posted_price( $value );
856 1249 }
857 1250
858 - FrmField::update(
859 - $field_id,
860 - array(
861 - 'field_options' => $field->field_options,
862 - 'form_id' => $field->form_id,
863 - )
864 - );
865 - wp_die();
1251 + return $value;
866 1252 }
867 1253
868 1254 /**
869 - * @deprecated 4.0
870 - */
871 - public static function import_choices() {
872 - _deprecated_function( __METHOD__, '4.0' );
873 - wp_die();
874 - }
875 -
876 - /**
877 - * Add Single Option or Other Option.
1255 + * @param mixed $opt
1256 + * @param mixed $opt_key
1257 + * @param array|object $field
878 1258 *
879 - * @deprecated 4.0 Moved to Pro for Other option only.
1259 + * @return mixed
880 1260 */
881 - public static function add_option() {
882 - _deprecated_function( __METHOD__, '4.0', 'FrmProFieldsController::add_other_option' );
883 - }
1261 + public static function check_value( $opt, $opt_key, $field ) {
1262 + if ( is_array( $opt ) ) {
1263 + if ( FrmField::is_option_true( $field, 'separate_value' ) ) {
1264 + $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt );
1265 + } else {
1266 + $opt = $opt['label'] ?? reset( $opt );
1267 + }
1268 + }
884 1269
885 - /**
886 - * @deprecated 4.0
887 - */
888 - public static function update_order() {
889 - FrmDeprecated::update_order();
1270 + return $opt;
890 1271 }
891 1272
892 1273 /**
893 - * @deprecated 3.0
894 - * @codeCoverageIgnore
895 - */
896 - public static function edit_name( $field = 'name', $id = '' ) {
897 - FrmDeprecated::edit_name( $field, $id );
898 - }
899 -
900 - /**
901 - * @deprecated 3.0
902 - * @codeCoverageIgnore
1274 + * @param mixed $opt
903 1275 *
904 - * @param int $field_id
905 - * @param array $values
906 - * @param int $form_id
907 - *
908 - * @return array
1276 + * @return mixed
909 1277 */
910 - public static function include_single_field( $field_id, $values, $form_id = 0 ) {
911 - return FrmDeprecated::include_single_field( $field_id, $values, $form_id );
1278 + public static function check_label( $opt ) {
1279 + return is_array( $opt ) ? ( $opt['label'] ?? reset( $opt ) ) : $opt;
912 1280 }
913 1281 }