PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 1.07.11
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v1.07.11
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 +512 -1262 6.351.07.11 View file →
@@ -1,1281 +1,531 @@
1 1 <?php
2 -if ( ! defined( 'ABSPATH' ) ) {
3 - die( 'You are not allowed to call this page directly.' );
4 -}
2 +/**
3 + * @package Formidable
4 + */
5 5
6 -class FrmFieldsController {
6 +if(!defined('ABSPATH')) die('You are not allowed to call this page directly.');
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;
8 +if(class_exists('FrmFieldsController'))
9 + return;
10 +
11 +class FrmFieldsController{
12 + public static function load_hooks(){
13 + add_action('wp_ajax_frm_load_field', 'FrmFieldsController::load_field');
14 + add_action('wp_ajax_frm_insert_field', 'FrmFieldsController::create');
15 + add_action('wp_ajax_frm_field_name_in_place_edit', 'FrmFieldsController::edit_name');
16 + add_action('wp_ajax_frm_field_desc_in_place_edit', 'FrmFieldsController::edit_description');
17 + add_action('wp_ajax_frm_update_ajax_option', 'FrmFieldsController::update_ajax_option');
18 + add_action('wp_ajax_frm_duplicate_field', 'FrmFieldsController::duplicate');
19 + add_action('wp_ajax_frm_delete_field', 'FrmFieldsController::destroy');
20 + add_action('wp_ajax_frm_add_field_option', 'FrmFieldsController::add_option');
21 + add_action('wp_ajax_frm_field_option_ipe', 'FrmFieldsController::edit_option');
22 + add_action('wp_ajax_frm_delete_field_option', 'FrmFieldsController::delete_option');
23 + add_action('wp_ajax_frm_import_choices', 'FrmFieldsController::import_choices');
24 + add_action('wp_ajax_frm_import_options', 'FrmFieldsController::import_options');
25 + add_action('wp_ajax_frm_update_field_order', 'FrmFieldsController::update_order');
26 + add_filter('frm_field_type', 'FrmFieldsController::change_type');
27 + add_filter('frm_display_field_options', 'FrmFieldsController::display_field_options');
28 + add_action('frm_field_input_html', 'FrmFieldsController::input_html');
29 + add_filter('frm_field_value_saved', 'FrmFieldsController::check_value', 50, 3);
30 + add_filter('frm_field_label_seen', 'FrmFieldsController::check_label', 10, 3);
31 + }
32 +
33 + public static function load_field(){
34 + $fields = $_POST['field'];
35 + if ( empty($fields) ) {
36 + die();
37 + }
38 +
39 + $_GET['page'] = 'formidable';
40 + $fields = stripslashes_deep($fields);
41 +
42 + $ajax = true;
43 + $values = array();
44 + $path = FrmAppHelper::plugin_path();
45 + $field_html = array();
46 +
47 + foreach ( $fields as $field ) {
48 + $field = htmlspecialchars_decode(nl2br($field));
49 + $field = json_decode($field, true);
50 +
51 + $field_id = $field['id'];
52 +
53 + if ( !isset($field['value']) ) {
54 + $field['value'] = '';
55 + }
56 +
57 + $field_name = "item_meta[$field_id]";
58 +
59 + ob_start();
60 + include($path .'/classes/views/frm-forms/add_field.php');
61 + $field_html[$field_id] = ob_get_contents();
62 + ob_end_clean();
63 + }
64 +
65 + unset($path);
66 +
67 + echo json_encode($field_html);
68 +
69 + die();
70 + }
71 +
72 + public static function create(){
73 + $field_data = $_POST['field'];
74 + $form_id = $_POST['form_id'];
75 + $values = array();
76 + if(class_exists('FrmProForm'))
77 + $values['post_type'] = FrmProFormsHelper::post_type($form_id);
78 +
79 + $field_values = apply_filters('frm_before_field_created', FrmFieldsHelper::setup_new_vars($field_data, $form_id));
80 +
81 + $frm_field = new FrmField();
82 + $field_id = $frm_field->create( $field_values );
83 +
84 + if ($field_id){
85 + $field = FrmFieldsHelper::setup_edit_vars($frm_field->getOne($field_id));
86 + $field_name = "item_meta[$field_id]";
87 + $id = $form_id;
88 + require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/add_field.php');
89 + }
90 + die();
91 + }
92 +
93 + public static function edit_name($field = 'name', $id = '') {
94 + if ( empty($field) ) {
95 + $field = 'name';
96 + }
97 +
98 + if ( empty($id) ) {
99 + $id = str_replace('field_label_', '', $_POST['element_id']);
100 + }
101 +
102 + $value = trim($_POST['update_value']);
103 + if ( trim(strip_tags($value)) == '' ) {
104 + // set blank value if there is no content
105 + $value = '';
106 + }
107 +
108 + $frm_field = new FrmField();
109 + $form = $frm_field->update($id, array($field => $value));
110 + echo stripslashes($value);
111 + die();
112 + }
113 +
14 114
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 - */
24 - public static function load_field() {
25 - FrmAppHelper::permission_check( 'frm_edit_forms' );
26 - check_ajax_referer( 'frm_ajax', 'nonce' );
115 + public static function edit_description(){
116 + $id = str_replace('field_description_', '', $_POST['element_id']);
117 + self::edit_name('description', $id);
118 + }
119 +
120 + public static function update_ajax_option(){
121 + $frm_field = new FrmField();
122 + $field = $frm_field->getOne($_POST['field']);
123 + foreach ( array('clear_on_focus', 'separate_value', 'default_blank') as $val ) {
124 + if ( isset($_POST[$val]) ) {
125 + $new_val = $_POST[$val];
126 + if ( $val == 'separate_value' ) {
127 + $new_val = (isset($field->field_options[$val]) && $field->field_options[$val]) ? 0 : 1;
128 + }
129 +
130 + $field->field_options[$val] = $new_val;
131 + unset($new_val);
132 + }
133 + unset($val);
134 + }
27 135
28 - $field_ids = FrmAppHelper::get_post_param( 'field_ids', array(), 'absint' );
29 - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
136 + $frm_field->update($_POST['field'], array('field_options' => $field->field_options));
137 + die();
138 + }
139 +
140 + public static function duplicate(){
141 + global $wpdb;
142 +
143 + $frm_field = new FrmField();
144 + $copy_field = $frm_field->getOne($_POST['field_id']);
145 + if (!$copy_field) return;
146 +
147 + $values = array();
148 + $values['field_key'] = FrmAppHelper::get_unique_key('', $wpdb->prefix . 'frm_fields', 'field_key');
149 + $values['options'] = maybe_serialize($copy_field->options);
150 + $values['default_value'] = maybe_serialize($copy_field->default_value);
151 + $values['form_id'] = $copy_field->form_id;
152 + foreach (array('name', 'description', 'type', 'field_options', 'required') as $col)
153 + $values[$col] = $copy_field->{$col};
154 + $field_count = FrmAppHelper::getRecordCount(array('form_id' => $copy_field->form_id), $wpdb->prefix . 'frm_fields');
155 + $values['field_order'] = $field_count + 1;
156 +
157 + $field_id = $frm_field->create($values);
158 +
159 + if ($field_id){
160 + $field = FrmFieldsHelper::setup_edit_vars($frm_field->getOne($field_id));
161 + $field_name = "item_meta[$field_id]";
162 + $id = $field['form_id'];
163 + if($field['type'] == 'html')
164 + $field['stop_filter'] = true;
165 + require(FrmAppHelper::plugin_path() .'/classes/views/frm-forms/add_field.php');
166 + }
167 + die();
168 + }
169 +
170 + public static function destroy(){
171 + $frm_field = new FrmField();
172 + $field_id = $frm_field->destroy($_POST['field_id']);
173 + die();
174 + }
30 175
31 - if ( ! $form_id || ! is_array( $field_ids ) || ! $field_ids ) {
32 - wp_die();
33 - }
176 + /* Field Options */
177 + public static function add_option(){
178 + $frm_field = new FrmField();
34 179
35 - $_GET['page'] = 'formidable';
36 - $fields = self::get_builder_fields_by_id( $form_id );
37 - $values = array(
38 - 'id' => $form_id,
39 - 'doing_ajax' => true,
40 - );
41 - $field_html = array();
180 + $id = $_POST['field_id'];
181 + $field = $frm_field->getOne($id);
182 + $options = maybe_unserialize($field->options);
183 + if(!empty($options))
184 + $last = max(array_keys($options));
185 + else
186 + $last = 0;
187 +
188 + $opt_key = $last + 1;
189 + $first_opt = reset($options);
190 + $next_opt = count($options);
191 + if($first_opt != '')
192 + $next_opt++;
193 + $opt = __('Option', 'formidable') .' '. $next_opt;
194 + unset($next_opt);
195 +
196 + $field_val = $opt;
197 + $options[$opt_key] = $opt;
198 + $frm_field->update($id, array('options' => maybe_serialize($options)));
199 + $checked = '';
42 200
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.
46 - continue;
47 - }
201 + $field_data = $frm_field->getOne($id);
202 + $field_data->field_options = maybe_unserialize($field_data->field_options);
203 + $field = array();
204 + $field['type'] = $field_data->type;
205 + $field['id'] = $id;
206 + $field['separate_value'] = isset($field_data->field_options['separate_value']) ? $field_data->field_options['separate_value'] : 0;
207 + $field_name = "item_meta[$id]";
208 +
209 + require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/single-option.php');
210 + die();
211 + }
48 212
49 - $field = $fields[ $field_id ];
213 + public static function edit_option(){
214 + $ids = explode('-', $_POST['element_id']);
215 + $id = str_replace('field_', '', $ids[0]);
216 + if ( strpos($_POST['element_id'], 'key_') ) {
217 + $id = str_replace('key_', '', $id);
218 + $new_value = trim($_POST['update_value']);
219 + } else {
220 + $new_label = trim($_POST['update_value']);
221 + }
222 +
223 + $frm_field = new FrmField();
224 + $field = $frm_field->getOne($id);
225 + $options = maybe_unserialize($field->options);
226 + $this_opt = (array) $options[$ids[1]];
227 +
228 + $label = isset($this_opt['label']) ? $this_opt['label'] : reset($this_opt);
229 + if ( isset($this_opt['value']) ) {
230 + $value = $this_opt['value'];
231 + }
232 +
233 + if ( !isset($new_label) ) {
234 + $new_label = $label;
235 + }
236 +
237 + if ( isset($new_value) || isset($value) ) {
238 + $update_value = isset($new_value) ? $new_value : $value;
239 + }
240 +
241 + if ( isset($update_value) && $update_value != $new_label ) {
242 + $options[$ids[1]] = array('value' => $update_value, 'label' => $new_label);
243 + } else {
244 + $options[$ids[1]] = trim($_POST['update_value']);
245 + }
246 +
247 + $frm_field->update($id, array('options' => maybe_serialize($options)));
248 + echo (trim($_POST['update_value']) == '') ? __('(Blank)', 'formidable') : stripslashes($_POST['update_value']);
249 + die();
250 + }
50 251
51 - ob_start();
52 - self::load_single_field( $field, $values );
252 + public static function delete_option(){
253 + $frm_field = new FrmField();
254 + $field = $frm_field->getOne($_POST['field_id']);
255 + $options = maybe_unserialize($field->options);
256 + unset($options[$_POST['opt_key']]);
257 + $frm_field->update($_POST['field_id'], array('options' => maybe_serialize($options)));
258 + die();
259 + }
260 +
261 + public static function import_choices(){
262 + if ( !current_user_can('frm_edit_forms') ) {
263 + return;
264 + }
265 +
266 + $field_id = $_REQUEST['field_id'];
267 +
268 + global $current_screen, $hook_suffix;
53 269
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
270 + // Catch plugins that include admin-header.php before admin.php completes.
271 + if ( empty( $current_screen ) && function_exists('set_current_screen') ) {
272 + $hook_suffix = '';
273 + set_current_screen();
274 + }
275 +
276 + if ( function_exists('register_admin_color_schemes') ) {
277 + register_admin_color_schemes();
278 + }
279 +
280 + $hook_suffix = $admin_body_class = '';
281 +
282 + if ( get_user_setting('mfold') == 'f' )
283 + $admin_body_class .= ' folded';
61 284
62 - echo json_encode( $field_html );
285 + if ( function_exists('is_admin_bar_showing') && is_admin_bar_showing() ) {
286 + $admin_body_class .= ' admin-bar';
287 + }
63 288
64 - wp_die();
65 - }
289 + if ( is_rtl() )
290 + $admin_body_class .= ' rtl';
66 291
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 );
292 + $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
293 + $prepop = array();
294 + $prepop[__('Countries', 'formidable')] = FrmAppHelper::get_countries();
295 +
296 + $states = FrmAppHelper::get_us_states();
297 + $state_abv = array_keys($states);
298 + sort($state_abv);
299 + $prepop[__('U.S. State Abbreviations', 'formidable')] = $state_abv;
300 + $states = array_values($states);
301 + sort($states);
302 + $prepop[__('U.S. States', 'formidable')] = $states;
303 + unset($state_abv);
304 + unset($states);
305 +
306 + $prepop[__('Age', 'formidable')] = array(
307 + __('Under 18', 'formidable'), __('18-24', 'formidable'), __('25-34', 'formidable'),
308 + __('35-44', 'formidable'), __('45-54', 'formidable'), __('55-64', 'formidable'),
309 + __('65 or Above', 'formidable'), __('Prefer Not to Answer', 'formidable')
310 + );
311 +
312 + $prepop[__('Satisfaction', 'formidable')] = array(
313 + __('Very Satisfied', 'formidable'), __('Satisfied', 'formidable'), __('Neutral', 'formidable'),
314 + __('Unsatisfied', 'formidable'), __('Very Unsatisfied', 'formidable'), __('N/A', 'formidable')
315 + );
78 316
79 - if ( ! $form ) {
80 - return array();
81 - }
317 + $prepop[__('Importance', 'formidable')] = array(
318 + __('Very Important', 'formidable'), __('Important', 'formidable'), __('Neutral', 'formidable'),
319 + __('Somewhat Important', 'formidable'), __('Not at all Important', 'formidable'), __('N/A', 'formidable')
320 + );
321 +
322 + $prepop[__('Agreement', 'formidable')] = array(
323 + __('Strongly Agree', 'formidable'), __('Agree', 'formidable'), __('Neutral', 'formidable'),
324 + __('Disagree', 'formidable'), __('Strongly Disagree', 'formidable'), __('N/A', 'formidable')
325 + );
326 +
327 + $prepop = apply_filters('frm_bulk_field_choices', $prepop);
328 +
329 + $frm_field = new FrmField();
330 + $field = $frm_field->getOne($field_id);
331 +
332 + include(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/import_choices.php');
333 + die();
334 + }
335 +
336 + public static function import_options(){
337 + if(!is_admin() or !current_user_can('frm_edit_forms'))
338 + return;
339 +
340 + extract(stripslashes_deep($_POST));
341 +
342 + $frm_field = new FrmField();
343 + $field = $frm_field->getOne($field_id);
344 +
345 + if(!in_array($field->type, array('radio', 'checkbox', 'select')))
346 + return;
347 +
348 + $field = FrmFieldsHelper::setup_edit_vars($field);
349 + $opts = explode("\n", rtrim($opts, "\n"));
350 + if ( $field['separate_value'] ) {
351 + foreach ( $opts as $opt_key => $opt ) {
352 + if ( strpos($opt, '|') !== false ) {
353 + $vals = explode('|', $opt);
354 + if ( $vals[0] != $vals[1] ) {
355 + $opts[$opt_key] = array('label' => trim($vals[0]), 'value' => trim($vals[1]));
356 + }
357 + unset($vals);
358 + }
359 + unset($opt_key);
360 + unset($opt);
361 + }
362 + }
363 +
364 + $frm_field->update($field_id, array('options' => maybe_serialize($opts)));
365 +
366 + $field['options'] = $opts;
367 + $field_name = $field['name'];
368 +
369 + if ( $field['type'] == 'radio' || $field['type'] == 'checkbox' ) {
370 + require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/radio.php');
371 + } else {
372 + foreach ( $field['options'] as $opt_key => $opt ) {
373 + $field_val = apply_filters('frm_field_value_saved', $opt, $opt_key, $field);
374 + $opt = apply_filters('frm_field_label_seen', $opt, $opt_key, $field);
375 + require(FrmAppHelper::plugin_path() .'/classes/views/frm-fields/single-option.php');
376 + }
377 + }
378 +
379 + die();
380 + }
82 381
83 - $fields = FrmField::get_all_for_form( $form_id );
382 + public static function update_order(){
383 + if(isset($_POST) and isset($_POST['frm_field_id'])){
384 + $frm_field = new FrmField();
385 +
386 + foreach ($_POST['frm_field_id'] as $position => $item)
387 + $frm_field->update($item, array('field_order' => $position));
388 + }
389 + die();
390 + }
391 +
392 + public static function change_type($type){
393 + global $frm_vars;
84 394
85 - /** This filter is documented in classes/controllers/FrmFormsController.php */
86 - $fields = apply_filters( 'frm_fields_in_form_builder', $fields, compact( 'form' ) );
395 + if ($frm_vars['pro_is_installed']) return $type;
396 +
397 + if($type == 'scale' || $type == '10radio')
398 + $type = 'radio';
399 + else if($type == 'rte')
400 + $type = 'textarea';
401 +
402 + $frm_field_selection = FrmFieldsHelper::field_selection();
403 + $types = array_keys($frm_field_selection);
404 + if (!in_array($type, $types) && $type != 'captcha')
405 + $type = 'text';
87 406
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 - /**
100 - * Create a new field with ajax
101 - */
102 - public static function create() {
103 - FrmAppHelper::permission_check( 'frm_edit_forms' );
104 - check_ajax_referer( 'frm_ajax', 'nonce' );
105 -
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' );
109 -
110 - do_action( 'frm_before_create_field', $field_type, $form_id );
111 -
112 - $field = self::include_new_field( $field_type, $form_id, $field_options );
113 -
114 - // This hook will allow for multiple fields to be added at once
115 - do_action( 'frm_after_field_created', $field, $form_id );
116 -
117 - wp_die();
118 - }
119 -
120 - /**
121 - * Set up and create a new field
122 - *
123 - * @param string $field_type
124 - * @param int $form_id
125 - * @param array $field_options
126 - *
127 - * @return array|false
128 - */
129 - public static function include_new_field( $field_type, $form_id, $field_options = array() ) {
130 - $field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
131 -
132 - if ( $field_options ) {
133 - $field_values['field_options'] = array_merge( $field_values['field_options'], $field_options );
134 - }
135 -
136 - // When a new field is added to the form, flag it as draft and hide it from the front-end.
137 - $field_values['field_options']['draft'] = 1;
138 -
139 - /**
140 - * @param array $field_values
141 - */
142 - $field_values = apply_filters( 'frm_before_field_created', $field_values );
143 - $field_id = FrmField::create( $field_values );
144 -
145 - if ( ! $field_id ) {
146 - return false;
147 - }
148 -
149 - $field = self::get_field_array_from_id( $field_id );
150 - $values = array();
151 -
152 - if ( FrmAppHelper::pro_is_installed() ) {
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' );
155 -
156 - if ( $parent_form_id ) {
157 - $field['parent_form_id'] = $parent_form_id;
158 - }
159 - }
160 -
161 - self::load_single_field( $field, $values, $form_id );
162 -
163 - return $field;
164 - }
165 -
166 - public static function duplicate() {
167 - FrmAppHelper::permission_check( 'frm_edit_forms' );
168 - check_ajax_referer( 'frm_ajax', 'nonce' );
169 -
170 - $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
171 - $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
172 - $new_field = FrmField::duplicate_single_field( $field_id, $form_id );
173 -
174 - if ( is_array( $new_field ) && ! empty( $new_field['field_id'] ) ) {
175 - self::load_single_field( $new_field['field_id'], $new_field['values'], $form_id );
176 - }
177 -
178 - wp_die();
179 - }
180 -
181 - /**
182 - * @since 3.0
183 - *
184 - * @param int $field_id
185 - *
186 - * @return array
187 - */
188 - public static function get_field_array_from_id( $field_id ) {
189 - $field = FrmField::getOne( $field_id );
190 - return FrmFieldsHelper::setup_edit_vars( $field );
191 - }
192 -
193 - /**
194 - * @since 3.0
195 - *
196 - * @param array|int|object|string $field_object
197 - * @param array $values
198 - * @param int $form_id
199 - *
200 - * @return void
201 - */
202 - public static function load_single_field( $field_object, $values, $form_id = 0 ) {
203 - global $frm_vars;
204 - $frm_vars['is_admin'] = true;
205 -
206 - if ( is_numeric( $field_object ) ) {
207 - $field_object = FrmField::getOne( $field_object );
208 - } elseif ( is_array( $field_object ) ) {
209 - $field = $field_object;
210 - $field_object = FrmField::getOne( $field['id'] );
211 - }
212 -
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 );
217 -
218 - if ( $ajax_loading && $ajax_this_field ) {
219 - $li_classes = self::get_classes_for_builder_field( array(), $display, $field_obj );
220 - include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/ajax-field-placeholder.php';
221 - return;
222 - }
223 -
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 - }
231 -
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 );
242 -
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';
247 - }
248 -
249 - /**
250 - * @since 3.0
251 - *
252 - * @param array $field
253 - * @param array $display
254 - * @param FrmFieldType $field_info
255 - *
256 - * @return string
257 - */
258 - private static function get_classes_for_builder_field( $field, $display, $field_info ) {
259 - $li_classes = $field_info->form_builder_classes( $display['type'] );
260 - $li_classes .= ' frm_form_field frmstart ';
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 -
268 - if ( isset( $field['classes'] ) ) {
269 - $li_classes .= trim( $field['classes'] ) . ' ';
270 - }
271 -
272 - $li_classes .= 'frmend';
273 -
274 - if ( $field ) {
275 - return apply_filters( 'frm_build_field_class', $li_classes, $field );
276 - }
277 -
278 - return $li_classes;
279 - }
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 -
312 - public static function destroy() {
313 - FrmAppHelper::permission_check( 'frm_edit_forms' );
314 - check_ajax_referer( 'frm_ajax', 'nonce' );
315 -
316 - $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
317 - FrmField::destroy( $field_id );
318 - wp_die();
319 - }
320 -
321 - /**
322 - * Field Options.
323 - */
324 - public static function import_options() {
325 - FrmAppHelper::permission_check( 'frm_edit_forms' );
326 - check_ajax_referer( 'frm_ajax', 'nonce' );
327 -
328 - if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
329 - return;
330 - }
331 -
332 - $field_id = FrmAppHelper::get_param( 'field_id', '', 'post', 'absint' );
333 - $field = FrmField::getOne( $field_id );
334 - $bulk_edit_types = array( 'radio', 'checkbox', 'select' );
335 -
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 ) ) {
348 - return;
349 - }
350 -
351 - $field = FrmFieldsHelper::setup_edit_vars( $field );
352 -
353 - $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
354 - $opts = explode( "\n", rtrim( $opts, "\n" ) );
355 - $opts = array_map( 'trim', $opts );
356 -
357 - $separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' );
358 - $field['separate_value'] = $separate === 'true';
359 -
360 - if ( $field['separate_value'] ) {
361 - foreach ( $opts as $opt_key => $opt ) {
362 - if ( str_contains( $opt, '|' ) ) {
363 - $vals = explode( '|', $opt );
364 - $opts[ $opt_key ] = array(
365 - 'label' => trim( $vals[0] ),
366 - 'value' => trim( $vals[1] ),
367 - );
368 - unset( $vals );
369 - }
370 - unset( $opt_key, $opt );
371 - }
372 - }
373 -
374 - // Keep other options after bulk update.
375 - if ( ! empty( $field['field_options']['other'] ) ) {
376 - $other_array = array();
377 -
378 - foreach ( $field['options'] as $opt_key => $opt ) {
379 - if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
380 - $other_array[ $opt_key ] = $opt;
381 - }
382 - unset( $opt_key, $opt );
383 - }
384 -
385 - if ( $other_array ) {
386 - $opts = array_merge( $opts, $other_array );
387 - }
388 - }
389 -
390 - $field['options'] = $opts;
391 -
392 - FrmFieldsHelper::show_single_option( $field );
393 -
394 - wp_die();
395 - }
396 -
397 - /**
398 - * @since 4.0
399 - *
400 - * @param array $atts - Includes field array, field_obj, display array, values array.
401 - *
402 - * @return void
403 - */
404 - public static function load_single_field_settings( $atts ) {
405 - $field = $atts['field'];
406 - $field_obj = $atts['field_obj'];
407 - $values = $atts['values'];
408 - $display = $atts['display'];
409 - unset( $atts );
410 -
411 - if ( ! isset( $field['unique'] ) ) {
412 - $field['unique'] = false;
413 - }
414 -
415 - if ( ! isset( $field['read_only'] ) ) {
416 - $field['read_only'] = false;
417 - }
418 -
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'] );
424 -
425 - if ( ! isset( $all_field_types[ $field['type'] ] ) ) {
426 - // Add fallback for an add-on field type that has been deactivated.
427 - $all_field_types[ $field['type'] ] = array(
428 - 'name' => ucfirst( $field['type'] ),
429 - 'icon' => 'frmfont frm_pencil_icon',
430 - );
431 - } elseif ( ! is_array( $all_field_types[ $field['type'] ] ) ) {
432 - // Fallback for fields added in a more basic way.
433 - FrmFormsHelper::prepare_field_type( $all_field_types[ $field['type'] ] );
434 - }
435 -
436 - $type_name = $all_field_types[ $field['type'] ]['name'];
437 -
438 - if ( $field['type'] === 'divider' && FrmField::is_option_true( $field, 'repeat' ) ) {
439 - $type_name = $all_field_types['divider|repeat']['name'];
440 - }
441 -
442 - if ( $display['default'] ) {
443 - $default_value_types = self::default_value_types( $field, compact( 'display' ) );
444 - }
445 -
446 - if ( $display['clear_on_focus'] && is_array( $field['placeholder'] ) ) {
447 - $field['placeholder'] = implode( ', ', $field['placeholder'] );
448 - }
449 -
450 - $pro_is_installed = FrmAppHelper::pro_is_installed();
451 -
452 - $unique_values_label_atts = array(
453 - 'for' => 'frm_uniq_field_' . $field['id'],
454 - 'class' => 'frm_help frm-mb-0',
455 - 'title' => __(
456 - 'Unique: Do not allow the same response multiple times. For example, if one user enters \'Joe\', then no one else will be allowed to enter the same name.',
457 - 'formidable'
458 - ),
459 - 'data-trigger' => 'hover',
460 - );
461 -
462 - $read_only_label_atts = array(
463 - 'for' => 'frm_read_only_field_' . $field['id'],
464 - 'class' => 'frm_help frm-mb-0',
465 - 'title' => __( 'Read Only: Show this field but do not allow the field value to be edited from the front-end.', 'formidable' ),
466 - 'data-trigger' => 'hover',
467 - );
468 -
469 - if ( ! $pro_is_installed ) {
470 - $visibility_upsell_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
471 - array(
472 - 'id' => 'field_options_admin_only_' . $field['id'],
473 - 'readonly' => '1',
474 - ),
475 - 'field_visibility',
476 - __( 'Visibility options', 'formidable' ),
477 - '/field-options/#kb-visibility'
478 - );
479 -
480 - $autocomplete_upsell_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
481 - array( 'id' => 'field_options_autocomplete_' . $field['id'] ),
482 - 'autocomplete',
483 - __( 'Autocomplete options', 'formidable' ),
484 - '/email-address/#kb-autocomplete-attribute'
485 - );
486 -
487 - $before_after_content_upsell_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
488 - array(
489 - 'type' => 'text',
490 - 'readonly' => '1',
491 - ),
492 - 'before_after_contents',
493 - __( 'Before and after field contents', 'formidable' ),
494 - '/field-options/#kb-before-after-input'
495 - );
496 -
497 - $show_upsell_for_unique_value = in_array(
498 - $field['type'],
499 - array( 'checkbox', 'email', 'name', 'number', 'phone', 'radio', 'text', 'textarea', 'url' ),
500 - true
501 - );
502 - $show_upsell_for_read_only = in_array( $field['type'], array( 'address', 'email', 'hidden', 'number', 'phone', 'radio', 'text', 'textarea', 'url' ), true );
503 - $show_upsell_for_before_after_contents = in_array( $field['type'], array( 'email', 'number', 'phone', 'quantity', 'select', 'tag', 'text', 'total', 'url' ), true );
504 - $show_upsell_for_autocomplete = in_array( $field['type'], array( 'text', 'email', 'number' ), true );
505 - $show_upsell_for_visibility = $field['type'] !== 'hidden';
506 -
507 - $unique_values_label_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
508 - $unique_values_label_atts,
509 - 'unique_values',
510 - __( 'Unique Values', 'formidable' ),
511 - '/field-options/#kb-unique'
512 - );
513 - $read_only_label_atts = FrmSettingsUpsellHelper::add_upgrade_modal_atts(
514 - $read_only_label_atts,
515 - 'read_only',
516 - __( 'Read Only Values', 'formidable' ),
517 - '/field-options/#kb-read-only'
518 - );
519 - }//end if
520 -
521 - include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/settings.php';
522 - }
523 -
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 - /**
537 - * Get the list of default value types that can be toggled in the builder.
538 - *
539 - * @since 4.0
540 - *
541 - * @param array $field
542 - * @param array $atts
543 - *
544 - * @return array
545 - */
546 - private static function default_value_types( $field, $atts ) {
547 - $types = array(
548 - 'default_value' => array(
549 - 'class' => '',
550 - 'icon' => 'frmfont frm_text2_icon',
551 - 'title' => __( 'Default Value', 'formidable' ),
552 - 'data' => array(
553 - 'frmshow' => '#default-value-for-',
554 - ),
555 - ),
556 - 'calc' => array(
557 - 'class' => 'frm_show_upgrade frm_noallow',
558 - 'title' => __( 'Calculate Value', 'formidable' ),
559 - 'icon' => 'frmfont frm_calculator_icon',
560 - 'data' => array(
561 - 'medium' => 'calculations',
562 - 'upgrade' => __( 'Calculator forms', 'formidable' ),
563 - ),
564 - 'tooltip' => __( 'Automatically calculate the value of this field based on values from other fields.', 'formidable' ),
565 - ),
566 - 'get_values_field' => array(
567 - 'class' => 'frm_show_upgrade frm_noallow',
568 - 'title' => __( 'Lookup', 'formidable' ),
569 - 'icon' => 'frmfont frm_search_icon',
570 - 'data' => array(
571 - 'medium' => 'lookup',
572 - 'upgrade' => __( 'Lookup fields', 'formidable' ),
573 - ),
574 - 'tooltip' => __( 'Dynamically retrieve the value of this field from a lookup field.', 'formidable' ),
575 - ),
576 - );
577 -
578 - $types = apply_filters( 'frm_default_value_types', $types, $atts );
579 -
580 - // Set active class.
581 - $settings = array_keys( $types );
582 - $active = 'default_value';
583 -
584 - if ( FrmAppHelper::pro_is_connected() ) {
585 - foreach ( $settings as $type ) {
586 - if ( ! empty( $field[ $type ] ) ) {
587 - $active = $type;
588 - }
589 - }
590 - }
591 -
592 - $types[ $active ]['class'] .= ' current';
593 - $types[ $active ]['current'] = true;
594 -
595 - return $types;
596 - }
597 -
598 - /**
599 - * @param string $type
600 - *
601 - * @return string
602 - */
603 - public static function change_type( $type ) {
604 - $type_switch = array(
605 - 'scale' => 'radio',
606 - 'star' => 'radio',
607 - '10radio' => 'radio',
608 - 'rte' => 'textarea',
609 - 'website' => 'url',
610 - 'image' => 'url',
611 - );
612 -
613 - if ( isset( $type_switch[ $type ] ) ) {
614 - $type = $type_switch[ $type ];
615 - }
616 -
617 - $pro_fields = FrmField::pro_field_selection();
618 - // We want to keep credit_card types as credit card types for Stripe Lite.
619 - // The credit_card key is set for backward compatibility.
620 - FrmField::remove_moved_field_types_from_pro( $pro_fields );
621 -
622 - return array_key_exists( $type, $pro_fields ) ? 'text' : $type;
623 - }
624 -
625 - /**
626 - * @param array $settings
627 - * @param FrmFieldType|null $field_info
628 - *
629 - * @return array
630 - */
631 - public static function display_field_options( $settings, $field_info = null ) {
632 - if ( $field_info ) {
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.
636 - $settings['field_data'] = $field_info->field;
637 - }
638 -
639 - return apply_filters( 'frm_display_field_options', $settings );
640 - }
641 -
642 - /**
643 - * Display the format option
644 - *
645 - * @since 3.0
646 - *
647 - * @param array $field Field data.
648 - * @param bool $is_hidden Whether the format option should be hidden.
649 - *
650 - * @return void
651 - */
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';
663 - }
664 -
665 - /**
666 - * @param array $field
667 - * @param bool $echo
668 - *
669 - * @return string
670 - */
671 - public static function input_html( $field, $echo = true ) {
672 - $class = array();
673 - self::add_input_classes( $field, $class );
674 -
675 - $add_html = array();
676 - self::add_html_size( $field, $add_html );
677 - self::add_html_length( $field, $add_html );
678 - self::add_html_placeholder( $field, $add_html, $class );
679 - self::add_validation_messages( $field, $add_html );
680 -
681 - $class = apply_filters( 'frm_field_classes', implode( ' ', $class ), $field );
682 -
683 - FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
684 -
685 - self::add_shortcodes_to_html( $field, $add_html );
686 - self::add_pattern_attribute( $field, $add_html );
687 - self::add_currency_field_attributes( $field, $add_html );
688 -
689 - $add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
690 - $add_html = ' ' . implode( ' ', $add_html ) . ' ';
691 -
692 - if ( $echo ) {
693 - echo $add_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
694 - }
695 -
696 - return $add_html;
697 - }
698 -
699 - /**
700 - * @param array $field
701 - * @param array $class
702 - *
703 - * @return void
704 - */
705 - private static function add_input_classes( $field, array &$class ) {
706 - if ( ! empty( $field['input_class'] ) ) {
707 - $class[] = $field['input_class'];
708 - }
709 -
710 - if ( $field['type'] === 'hidden' || $field['type'] === 'user_id' ) {
711 - return;
712 - }
713 -
714 - if ( isset( $field['size'] ) && $field['size'] > 0 ) {
715 - $class[] = 'auto_width';
716 - }
717 - }
718 -
719 - /**
720 - * @param array $field
721 - * @param array $add_html
722 - *
723 - * @return void
724 - */
725 - private static function add_html_size( $field, array &$add_html ) {
726 - $size_fields = array(
727 - 'select',
728 - 'data',
729 - 'time',
730 - 'hidden',
731 - 'file',
732 - 'lookup',
733 - );
734 -
735 - if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], $size_fields, true ) ) {
736 - return;
737 - }
738 -
739 - if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
740 - return;
741 - }
742 -
743 - if ( is_numeric( $field['size'] ) ) {
744 - $field['size'] .= 'px';
745 - }
746 -
747 - $important = apply_filters( 'frm_use_important_width', 1, $field );
748 - // Note: This inline styling must stay since we cannot realistically set a class for every possible field size.
749 - $add_html['style'] = 'style="width:' . esc_attr( $field['size'] ) . ( $important ? ' !important' : '' ) . '"';
750 -
751 - self::add_html_cols( $field, $add_html );
752 - }
753 -
754 - /**
755 - * @param array $field
756 - * @param array $add_html
757 - *
758 - * @return void
759 - */
760 - private static function add_html_cols( $field, array &$add_html ) {
761 - if ( ! in_array( $field['type'], array( 'textarea', 'rte' ), true ) ) {
762 - return;
763 - }
764 -
765 - // Convert to cols for textareas.
766 - $calc = array(
767 - '' => 9,
768 - 'px' => 9,
769 - 'rem' => 0.444,
770 - 'em' => 0.544,
771 - );
772 -
773 - // Include "col" for valid html
774 - $unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) );
775 -
776 - if ( ! isset( $calc[ $unit ] ) ) {
777 - return;
778 - }
779 -
780 - $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
781 - $add_html['cols'] = 'cols="' . absint( $size ) . '"';
782 - }
783 -
784 - /**
785 - * @param array $field
786 - * @param array $add_html
787 - *
788 - * @return void
789 - */
790 - private static function add_html_length( $field, array &$add_html ) {
791 - // Check for max setting and if this field accepts maxlength.
792 - $fields = array(
793 - 'textarea',
794 - 'rte',
795 - 'hidden',
796 - 'file',
797 - );
798 -
799 - if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], $fields, true ) ) {
800 - return;
801 - }
802 -
803 - if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
804 - // Don't load on form builder page.
805 - return;
806 - }
807 -
808 - $add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"';
809 - }
810 -
811 - /**
812 - * @param array $field
813 - * @param array $add_html
814 - * @param array $class
815 - *
816 - * @return void
817 - */
818 - private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
819 - // phpcs:ignore Universal.Operators.StrictComparisons
820 - if ( $field['default_value'] != '' ) {
821 - if ( is_array( $field['default_value'] ) ) {
822 - $add_html['data-frmval'] = 'data-frmval="' . esc_attr( json_encode( $field['default_value'] ) ) . '"';
823 - } else {
824 - self::add_frmval_to_input( $field, $add_html );
825 - }
826 - }
827 -
828 - $field['placeholder'] = self::prepare_placeholder( $field );
829 -
830 - // phpcs:ignore Universal.Operators.StrictComparisons
831 - if ( $field['placeholder'] == '' || is_array( $field['placeholder'] ) ) {
832 - // Don't include a json placeholder
833 - return;
834 - }
835 -
836 - self::add_placeholder_to_input( $field, $add_html );
837 - }
838 -
839 - /**
840 - * Prepares field placeholder.
841 - *
842 - * @since 5.4 This doesn't call `FrmFieldsController::get_default_value_from_name()` anymore.
843 - *
844 - * @param array $field Field array.
845 - *
846 - * @return string
847 - */
848 - private static function prepare_placeholder( $field ) {
849 - return $field['placeholder'] ?? '';
850 - }
851 -
852 - /**
853 - * If the label position is "inside",
854 - * get the label to use as the placeholder
855 - *
856 - * @since 2.05
857 - * @since 5.4 Remove the logic code for "inside" label position.
858 - *
859 - * @param array $field
860 - *
861 - * @return string
862 - */
863 - public static function get_default_value_from_name( $field ) {
864 - return '';
865 - }
866 -
867 - /**
868 - * Maybe add a blank placeholder option before any options
869 - * in a dropdown.
870 - *
871 - * @since 4.04
872 - *
873 - * @param array|object $field
874 - *
875 - * @return bool True if placeholder was added.
876 - */
877 - public static function add_placeholder_to_select( $field ) {
878 - $placeholder = FrmField::get_option( $field, 'placeholder' );
879 -
880 - if ( ! $placeholder ) {
881 - $placeholder = self::get_default_value_from_name( $field );
882 - }
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 -
895 - if ( $placeholder !== '' ) {
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 );
903 - return true;
904 - }
905 -
906 - return false;
907 - }
908 -
909 - /**
910 - * Use HTML5 placeholder with js fallback.
911 - *
912 - * @param array $field
913 - * @param array $add_html
914 - *
915 - * @return void
916 - */
917 - private static function add_placeholder_to_input( $field, &$add_html ) {
918 - if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) {
919 - $add_html['placeholder'] = 'placeholder="' . esc_attr( $field['placeholder'] ) . '"';
920 - }
921 - }
922 -
923 - /**
924 - * @param array $field
925 - * @param array $add_html
926 - *
927 - * @return void
928 - */
929 - private static function add_frmval_to_input( $field, &$add_html ) {
930 - // phpcs:ignore Universal.Operators.StrictComparisons
931 - if ( $field['placeholder'] != '' ) {
932 - $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['placeholder'] ) . '"';
933 -
934 - if ( 'select' === $field['type'] ) {
935 - $add_html['data-frmplaceholder'] = 'data-frmplaceholder="' . esc_attr( $field['placeholder'] ) . '"';
936 - }
937 - }
938 -
939 - // phpcs:ignore Universal.Operators.StrictComparisons
940 - if ( $field['default_value'] != '' ) {
941 - $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
942 - }
943 - }
944 -
945 - /**
946 - * @param array $field
947 - * @param array $add_html
948 - *
949 - * @return void
950 - */
951 - private static function add_validation_messages( $field, array &$add_html ) {
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'] ) ) {
955 - $required_message = FrmFieldsHelper::get_error_msg( $field, 'blank' );
956 - $add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"';
957 - self::maybe_add_html_required( $field, $add_html );
958 - }
959 -
960 - if ( ! FrmField::is_option_empty( $field, 'invalid' ) && ! empty( $field_validation_messages_status['data-invmsg'] ) ) {
961 - $invalid_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
962 - $add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"';
963 - }
964 -
965 - if ( ! empty( $add_html['data-reqmsg'] ) || ! empty( $add_html['data-invmsg'] ) ) {
966 - self::maybe_add_error_html_for_js_validation( $field, $add_html );
967 - }
968 - }
969 -
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 - /**
1004 - * @since 5.0.03
1005 - *
1006 - * @param array $field
1007 - * @param array $add_html
1008 - *
1009 - * @return void
1010 - */
1011 - private static function maybe_add_error_html_for_js_validation( $field, array &$add_html ) {
1012 - $form = self::get_form_for_js_validation( $field );
1013 -
1014 - if ( false === $form ) {
1015 - return;
1016 - }
1017 -
1018 - $error_body = self::pull_custom_error_body_from_custom_html( $form, $field );
1019 -
1020 - if ( false === $error_body ) {
1021 - return;
1022 - }
1023 -
1024 - $error_body = urlencode( $error_body );
1025 - $add_html['data-error-html'] = 'data-error-html="' . esc_attr( $error_body ) . '"';
1026 - }
1027 -
1028 - /**
1029 - * @since 5.0.03
1030 - *
1031 - * @param array $field
1032 - *
1033 - * @return false|stdClass false if there is no form object found with JS validation active.
1034 - */
1035 - private static function get_form_for_js_validation( $field ) {
1036 - global $frm_vars;
1037 -
1038 - if ( ! empty( $frm_vars['js_validate_forms'] ) ) {
1039 - if ( isset( $frm_vars['js_validate_forms'][ $field['form_id'] ] ) ) {
1040 - return $frm_vars['js_validate_forms'][ $field['form_id'] ];
1041 - }
1042 -
1043 - if ( ! empty( $field['parent_form_id'] ) && isset( $frm_vars['js_validate_forms'][ $field['parent_form_id'] ] ) ) {
1044 - return $frm_vars['js_validate_forms'][ $field['parent_form_id'] ];
1045 - }
1046 - }
1047 -
1048 - return false;
1049 - }
1050 -
1051 - /**
1052 - * @param stdClass $form
1053 - * @param array $field
1054 - * @param array $errors
1055 - *
1056 - * @return false|string
1057 - */
1058 - public static function pull_custom_error_body_from_custom_html( $form, $field, $errors = array() ) {
1059 - if ( empty( $field['custom_html'] ) ) {
1060 - return false;
1061 - }
1062 -
1063 - $custom_html = $field['custom_html'];
1064 - $custom_html = apply_filters( 'frm_before_replace_shortcodes', $custom_html, $field, $errors, $form );
1065 - $start = strpos( $custom_html, '[if error]' );
1066 -
1067 - if ( false === $start ) {
1068 - return false;
1069 - }
1070 -
1071 - $end = strpos( $custom_html, '[/if error]' );
1072 -
1073 - if ( false === $end || $end < $start ) {
1074 - return false;
1075 - }
1076 -
1077 - $error_body = substr( $custom_html, $start + 10, $end - $start - 10 );
1078 - $default_html = array(
1079 - '<div class="frm_error" id="frm_error_field_[key]">[error]</div>',
1080 - '<div class="frm_error" role="alert" id="frm_error_field_[key]">[error]</div>',
1081 - '<div class="frm_error">[error]</div>',
1082 - '<div class="frm_error" role="alert">[error]</div>',
1083 - );
1084 -
1085 - return in_array( $error_body, $default_html, true ) ? false : $error_body;
1086 - }
1087 -
1088 - /**
1089 - * If 'required' is added to a conditionally hidden field, the form won't
1090 - * submit in many browsers. Check to make sure the javascript to conditionally
1091 - * remove it is present if needed.
1092 - *
1093 - * @since 3.06.01
1094 - *
1095 - * @param array $field
1096 - * @param array $add_html
1097 - *
1098 - * @return void
1099 - */
1100 - private static function maybe_add_html_required( $field, array &$add_html ) {
1101 - // phpcs:disable Generic.WhiteSpace.ScopeIndent
1102 - $excluded_field_types =
1103 - FrmField::is_radio( $field ) ||
1104 - FrmField::is_checkbox( $field ) ||
1105 - FrmField::is_field_type( $field, 'file' ) ||
1106 - FrmField::is_field_type( $field, 'nps' ) ||
1107 - FrmField::is_field_type( $field, 'scale' );
1108 - // phpcs:enable Generic.WhiteSpace.ScopeIndent
1109 -
1110 - if ( $excluded_field_types ) {
1111 - return;
1112 - }
1113 -
1114 - $add_html['aria-required'] = 'aria-required="true"';
1115 - }
1116 -
1117 - /**
1118 - * @param array $field
1119 - * @param array $add_html
1120 - *
1121 - * @return void
1122 - */
1123 - private static function add_shortcodes_to_html( $field, array &$add_html ) {
1124 - if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) {
1125 - return;
1126 - }
1127 -
1128 - if ( ! empty( $field['autocomplete'] ) ) {
1129 - unset( $field['shortcodes']['autocomplete'] );
1130 - }
1131 -
1132 - foreach ( $field['shortcodes'] as $k => $v ) {
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 ) ) {
1146 - continue;
1147 - }
1148 -
1149 - if ( is_numeric( $k ) && str_contains( $v, '=' ) ) {
1150 - $add_html[] = $v;
1151 - } elseif ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
1152 - $add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
1153 - } else {
1154 - $add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"';
1155 - }
1156 -
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;
1173 - }
1174 - return FrmAppHelper::input_key_is_safe( $key );
1175 - }
1176 -
1177 - /**
1178 - * Add pattern attribute.
1179 - *
1180 - * @since 3.0
1181 - *
1182 - * @param array $field
1183 - * @param array $add_html
1184 - *
1185 - * @return void
1186 - */
1187 - private static function add_pattern_attribute( $field, array &$add_html ) {
1188 - $format_value = FrmField::get_option( $field, 'format' );
1189 - $has_format = $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value );
1190 - $format_field = FrmField::is_field_type( $field, 'text' );
1191 -
1192 - if ( $field['type'] !== 'phone' && ( ! $has_format || ! $format_field ) ) {
1193 - return;
1194 - }
1195 -
1196 - $format = FrmEntryValidate::phone_format( $field );
1197 - $format = substr( $format, 2, - 1 );
1198 -
1199 - $add_html['pattern'] = 'pattern="' . esc_attr( $format ) . '"';
1200 - }
1201 -
1202 - /**
1203 - * Add product attributes to fields in multi-paged forms.
1204 - *
1205 - * @param array $field
1206 - * @param array $add_html
1207 - *
1208 - * @return void
1209 - */
1210 - private static function add_currency_field_attributes( $field, &$add_html ) {
1211 - $type = $field['original_type'] ?? $field['type'];
1212 - $is_product_field = in_array( $type, array( 'total', 'quantity', 'product' ), true );
1213 -
1214 - if ( ! $is_product_field ) {
1215 - return;
1216 - }
1217 -
1218 - if ( $type === 'total' ) {
1219 - $add_html['data-frmtotal'] = 'data-frmtotal';
1220 - } elseif ( $type === 'quantity' ) {
1221 - $product_field = FrmField::get_option( $field, 'product_field' );
1222 - $add_html['data-frmproduct'] = 'data-frmproduct="' . esc_attr( json_encode( $product_field ) ) . '"';
1223 - } elseif ( $type === 'product' && 'hidden' === $field['type'] ) {
1224 - // We want to do this only for fields that are hidden because it's
1225 - // not their page, hence the check : 'hidden' === $field['type'].
1226 - $price = empty( $field['value'] ) ? 0 : self::get_product_price( $field );
1227 -
1228 - $add_html['data-frmprice'] = 'data-frmprice="' . esc_attr( $price ) . '"';
1229 - }
1230 - }
1231 -
1232 - /**
1233 - * @param array $field
1234 - */
1235 - private static function get_product_price( $field ) {
1236 - if ( is_array( $field['value'] ) ) {
1237 - // '' is unlikely though, let's just do it to prevent warnings
1238 - $value = isset( $field['opt_key'] ) && isset( $field['value'][ $field['opt_key'] ] )
1239 - ? $field['value'][ $field['opt_key'] ]
1240 - : '';
1241 - } else {
1242 - $value = $field['value'];
1243 - }
1244 -
1245 - $field_obj = FrmFieldFactory::get_field_object( $field['id'] );
1246 -
1247 - if ( method_exists( $field_obj, 'get_posted_price' ) ) {
1248 - return $field_obj->get_posted_price( $value );
1249 - }
1250 -
1251 - return $value;
1252 - }
1253 -
1254 - /**
1255 - * @param mixed $opt
1256 - * @param mixed $opt_key
1257 - * @param array|object $field
1258 - *
1259 - * @return mixed
1260 - */
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 - }
1269 -
1270 - return $opt;
1271 - }
1272 -
1273 - /**
1274 - * @param mixed $opt
1275 - *
1276 - * @return mixed
1277 - */
1278 - public static function check_label( $opt ) {
1279 - return is_array( $opt ) ? ( $opt['label'] ?? reset( $opt ) ) : $opt;
1280 - }
407 + return $type;
408 + }
409 +
410 + public static function display_field_options($display){
411 + switch($display['type']){
412 + case 'captcha':
413 + $display['required'] = false;
414 + $display['invalid'] = true;
415 + $display['default_blank'] = false;
416 + break;
417 + case 'radio':
418 + $display['default_blank'] = false;
419 + break;
420 + case 'text':
421 + case 'textarea':
422 + $display['size'] = true;
423 + $display['clear_on_focus'] = true;
424 + break;
425 + case 'select':
426 + $display['size'] = true;
427 + break;
428 + }
429 +
430 + return $display;
431 + }
432 +
433 + public static function input_html($field, $echo=true){
434 + global $frm_settings, $frm_vars;
435 +
436 + $class = ''; //$field['type'];
437 +
438 + if ( is_admin() && (!isset($frm_vars['preview']) || !$frm_vars['preview']) && !in_array($field['type'], array('scale', 'radio', 'checkbox', 'data')) ) {
439 + $class .= 'dyn_default_value';
440 + }
441 +
442 + $add_html = '';
443 +
444 + if(isset($field['size']) and $field['size'] > 0){
445 + if(!in_array($field['type'], array('textarea', 'select', 'data', 'time', 'hidden')))
446 + $add_html .= ' size="'. $field['size'] .'"';
447 + $class .= " auto_width";
448 + }
449 +
450 + if ( isset($field['max']) && !in_array($field['type'], array('textarea', 'rte', 'hidden')) && !empty($field['max']) && (!is_admin() || !isset($_GET) || !isset($_GET['page']) || $_GET['page'] != 'formidable') ) {
451 + $add_html .= ' maxlength="'. $field['max'] .'"';
452 + }
453 +
454 + if(!is_admin() or defined('DOING_AJAX') or !isset($_GET) or !isset($_GET['page']) or $_GET['page'] == 'formidable-entries'){
455 + /*if(isset($field['required']) and $field['required']){
456 + $action = isset($_REQUEST['frm_action']) ? 'frm_action' : 'action';
457 + $action = FrmAppHelper::get_param($action);
458 +
459 + //if($field['type'] != 'checkbox')
460 + // $add_html .= ' required="required"';
461 +
462 + if($field['type'] == 'file' and $action == 'edit'){
463 + //don't add the required class if this is a file upload when editing
464 + }else{
465 + $class .= " required";
466 + }
467 + unset($action);
468 + }*/
469 +
470 + if(isset($field['clear_on_focus']) and $field['clear_on_focus'] and !empty($field['default_value'])){
471 +
472 + if($frm_settings->use_html and !in_array($field['type'], array('select', 'radio', 'checkbox', 'hidden'))){
473 + $add_html .= ' placeholder="'. esc_attr($field['default_value']) .'"';
474 + FrmAppHelper::load_scripts('jquery-placeholder');
475 + }else if(!$frm_settings->use_html){
476 + $val = str_replace(array("\r\n", "\n"), '\r', addslashes(str_replace('&#039;', "'", esc_attr($field['default_value']))));
477 + $add_html .= ' onfocus="frmClearDefault('."'". $val ."'". ',this)" onblur="frmReplaceDefault('."'". $val ."'". ',this)"';
478 + if($field['value'] == $field['default_value'])
479 + $class .= ' frm_default';
480 + }
481 + }
482 + }
483 +
484 + if(isset($field['input_class']) and !empty($field['input_class']))
485 + $class .= ' '. $field['input_class'];
486 +
487 + $class = apply_filters('frm_field_classes', $class, $field);
488 +
489 + if(!empty($class))
490 + $add_html .= ' class="'. trim($class) .'"';
491 +
492 + if ( isset($field['shortcodes']) && !empty($field['shortcodes']) ) {
493 + foreach ( $field['shortcodes'] as $k => $v ) {
494 + if ( 'opt' === $k || (!is_numeric($k) && strpos($add_html, " $k=")) ) {
495 + continue;
496 + }
497 +
498 + if ( is_numeric($k) && strpos($v, '=') ) {
499 + $add_html .= ' '. $v;
500 + } else {
501 + $add_html .= ' '. $k .'="'. $v .'"';
502 + }
503 +
504 + unset($k, $v);
505 + }
506 + }
507 +
508 + if($echo)
509 + echo $add_html;
510 +
511 + return $add_html;
512 + }
513 +
514 + public static function check_value($opt, $opt_key, $field){
515 + if(is_array($opt)){
516 + if(isset($field['separate_value']) and $field['separate_value']){
517 + $opt = isset($opt['value']) ? $opt['value'] : (isset($opt['label']) ? $opt['label'] : reset($opt));
518 + }else{
519 + $opt = (isset($opt['label']) ? $opt['label'] : reset($opt));
520 + }
521 + }
522 + return $opt;
523 + }
524 +
525 + public static function check_label($opt, $opt_key, $field){
526 + if(is_array($opt))
527 + $opt = (isset($opt['label']) ? $opt['label'] : reset($opt));
528 +
529 + return $opt;
530 + }
1281 531 }