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