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