PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 2.05.09
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v2.05.09
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
formidable / classes / controllers / FrmFieldsController.php

FrmFieldsController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 2.05.09, at classes/controllers/FrmFieldsController.php

660 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class FrmFieldsController {
4
5 public static function load_field() {
6 FrmAppHelper::permission_check( 'frm_edit_forms' );
7 check_ajax_referer( 'frm_ajax', 'nonce' );
8
9 $fields = $_POST['field'];
10 if ( empty( $fields ) ) {
11 wp_die();
12 }
13
14 $_GET['page'] = 'formidable';
15 $fields = stripslashes_deep( $fields );
16
17 $ajax = true;
18 $values = array( 'id' => FrmAppHelper::get_post_param( 'form_id', '', 'absint' ) );
19 $path = FrmAppHelper::plugin_path();
20 $field_html = array();
21
22 foreach ( $fields as $field ) {
23 $field = htmlspecialchars_decode( nl2br( $field ) );
24 $field = json_decode( $field, true );
25 if ( ! isset( $field['id'] ) ) {
26 // this field may have already been loaded
27 continue;
28 }
29
30 $field_id = absint( $field['id'] );
31
32 if ( ! isset( $field['value'] ) ) {
33 $field['value'] = '';
34 }
35
36 $field_name = 'item_meta[' . $field_id . ']';
37 $html_id = FrmFieldsHelper::get_html_id( $field );
38
39 ob_start();
40 include( $path . '/classes/views/frm-forms/add_field.php' );
41 $field_html[ $field_id ] = ob_get_contents();
42 ob_end_clean();
43 }
44
45 unset( $path );
46
47 echo json_encode( $field_html );
48
49 wp_die();
50 }
51
52 /**
53 * Create a new field with ajax
54 */
55 public static function create() {
56 FrmAppHelper::permission_check( 'frm_edit_forms' );
57 check_ajax_referer( 'frm_ajax', 'nonce' );
58
59 $field_type = FrmAppHelper::get_post_param( 'field_type', '', 'sanitize_text_field' );
60 $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
61
62 $field = self::include_new_field( $field_type, $form_id );
63
64 // this hook will allow for multiple fields to be added at once
65 do_action( 'frm_after_field_created', $field, $form_id );
66
67 wp_die();
68 }
69
70 /**
71 * Set up and create a new field
72 *
73 * @param string $field_type
74 * @param integer $form_id
75 * @return array|bool
76 */
77 public static function include_new_field( $field_type, $form_id ) {
78 $values = array();
79 if ( FrmAppHelper::pro_is_installed() ) {
80 $values['post_type'] = FrmProFormsHelper::post_type( $form_id );
81 }
82
83 $field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
84 $field_values = apply_filters( 'frm_before_field_created', $field_values );
85
86 $field_id = FrmField::create( $field_values );
87
88 if ( ! $field_id ) {
89 return false;
90 }
91
92 $field = self::include_single_field( $field_id, $values, $form_id );
93
94 return $field;
95 }
96
97 public static function edit_name( $field = 'name', $id = '' ) {
98 FrmAppHelper::permission_check( 'frm_edit_forms' );
99 check_ajax_referer( 'frm_ajax', 'nonce' );
100
101 if ( empty( $field ) ) {
102 $field = 'name';
103 }
104
105 if ( empty( $id ) ) {
106 $id = FrmAppHelper::get_post_param( 'element_id', '', 'sanitize_title' );
107 $id = str_replace( 'field_label_', '', $id );
108 }
109
110 $value = FrmAppHelper::get_post_param( 'update_value', '', 'wp_kses_post' );
111 $value = trim( $value );
112 if ( trim( strip_tags( $value ) ) === '' ) {
113 // set blank value if there is no content
114 $value = '';
115 }
116
117 FrmField::update( $id, array( $field => $value ) );
118
119 do_action( 'frm_after_update_field_' . $field, compact( 'id', 'value' ) );
120
121 echo stripslashes( wp_kses_post( $value ) );
122 wp_die();
123 }
124
125 public static function update_ajax_option() {
126 FrmAppHelper::permission_check( 'frm_edit_forms' );
127 check_ajax_referer( 'frm_ajax', 'nonce' );
128
129 $field_id = FrmAppHelper::get_post_param( 'field', 0, 'absint' );
130 if ( ! $field_id ) {
131 wp_die();
132 }
133
134 $field = FrmField::getOne( $field_id );
135
136 if ( isset( $_POST['separate_value'] ) ) {
137 $new_val = FrmField::is_option_true( $field, 'separate_value' ) ? 0 : 1;
138 $field->field_options['separate_value'] = $new_val;
139 unset( $new_val );
140 }
141
142 FrmField::update( $field_id, array(
143 'field_options' => $field->field_options,
144 'form_id' => $field->form_id,
145 ) );
146 wp_die();
147 }
148
149 public static function duplicate() {
150 FrmAppHelper::permission_check( 'frm_edit_forms' );
151 check_ajax_referer( 'frm_ajax', 'nonce' );
152
153 global $wpdb;
154
155 $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
156 $form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
157
158 $copy_field = FrmField::getOne( $field_id );
159 if ( ! $copy_field ) {
160 wp_die();
161 }
162
163 do_action( 'frm_duplicate_field', $copy_field, $form_id );
164 do_action( 'frm_duplicate_field_' . $copy_field->type, $copy_field, $form_id );
165
166 $values = array();
167 FrmFieldsHelper::fill_field( $values, $copy_field, $copy_field->form_id );
168 $values = apply_filters( 'frm_prepare_single_field_for_duplication', $values );
169
170 $field_id = FrmField::create( $values );
171 if ( ! $field_id ) {
172 wp_die();
173 }
174
175 self::include_single_field( $field_id, $values );
176
177 wp_die();
178 }
179
180 /**
181 * Load a single field in the form builder along with all needed variables
182 */
183 public static function include_single_field( $field_id, $values, $form_id = 0 ) {
184 $field = FrmFieldsHelper::setup_edit_vars( FrmField::getOne( $field_id ) );
185 $field_name = 'item_meta[' . $field_id . ']';
186 $html_id = FrmFieldsHelper::get_html_id( $field );
187 $id = $form_id ? $form_id : $field['form_id'];
188 if ( $field['type'] == 'html' ) {
189 $field['stop_filter'] = true;
190 }
191
192 require( FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php' );
193
194 return $field;
195 }
196
197 public static function destroy() {
198 FrmAppHelper::permission_check( 'frm_edit_forms' );
199 check_ajax_referer( 'frm_ajax', 'nonce' );
200
201 $field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
202 FrmField::destroy( $field_id );
203 wp_die();
204 }
205
206 /* Field Options */
207
208 //Add Single Option or Other Option
209 public static function add_option() {
210 FrmAppHelper::permission_check( 'frm_edit_forms' );
211 check_ajax_referer( 'frm_ajax', 'nonce' );
212
213 $id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
214 $opt_type = FrmAppHelper::get_post_param( 'opt_type', '', 'sanitize_text_field' );
215 $opt_key = FrmAppHelper::get_post_param( 'opt_key', 0, 'absint' );
216
217 $field = FrmField::getOne( $id );
218
219 if ( 'other' == $opt_type ) {
220 $opt = __( 'Other', 'formidable' );
221 $other_val = '';
222 $opt_key = 'other_' . $opt_key;
223 } else {
224 $opt = __( 'New Option', 'formidable' );
225 }
226 $field_val = $opt;
227
228 $field_data = $field;
229 $field = (array) $field;
230 $field['separate_value'] = isset( $field_data->field_options['separate_value'] ) ? $field_data->field_options['separate_value'] : 0;
231 unset( $field_data );
232
233 $field_name = 'item_meta[' . $id . ']';
234 $html_id = FrmFieldsHelper::get_html_id( $field );
235 $checked = '';
236
237 if ( 'other' == $opt_type ) {
238 include( FrmAppHelper::plugin_path() . '/pro/classes/views/frmpro-fields/other-option.php' );
239 } else {
240 require( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/single-option.php' );
241 }
242 wp_die();
243 }
244
245 public static function edit_option() {
246 _deprecated_function( __FUNCTION__, '2.3' );
247 }
248
249 public static function delete_option() {
250 _deprecated_function( __FUNCTION__, '2.3' );
251 }
252
253 public static function import_choices() {
254 FrmAppHelper::permission_check( 'frm_edit_forms', 'hide' );
255
256 $field_id = absint( $_REQUEST['field_id'] );
257
258 global $current_screen, $hook_suffix;
259
260 // Catch plugins that include admin-header.php before admin.php completes.
261 if ( empty( $current_screen ) && function_exists( 'set_current_screen' ) ) {
262 $hook_suffix = '';
263 set_current_screen();
264 }
265
266 if ( function_exists( 'register_admin_color_schemes' ) ) {
267 register_admin_color_schemes();
268 }
269
270 $hook_suffix = '';
271 $admin_body_class = '';
272
273 if ( get_user_setting( 'mfold' ) == 'f' ) {
274 $admin_body_class .= ' folded';
275 }
276
277 if ( function_exists( 'is_admin_bar_showing' ) && is_admin_bar_showing() ) {
278 $admin_body_class .= ' admin-bar';
279 }
280
281 if ( is_rtl() ) {
282 $admin_body_class .= ' rtl';
283 }
284
285 $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
286 $prepop = array();
287 FrmFieldsHelper::get_bulk_prefilled_opts( $prepop );
288
289 $field = FrmField::getOne( $field_id );
290
291 wp_enqueue_script( 'utils' );
292 wp_enqueue_style( 'formidable-admin', FrmAppHelper::plugin_url() . '/css/frm_admin.css' );
293 FrmAppHelper::load_admin_wide_js();
294
295 include( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/import_choices.php' );
296 wp_die();
297 }
298
299 public static function import_options() {
300 FrmAppHelper::permission_check( 'frm_edit_forms' );
301 check_ajax_referer( 'frm_ajax', 'nonce' );
302
303 if ( ! is_admin() || ! current_user_can( 'frm_edit_forms' ) ) {
304 return;
305 }
306
307 $field_id = absint( $_POST['field_id'] );
308 $field = FrmField::getOne( $field_id );
309
310 if ( ! in_array( $field->type, array( 'radio', 'checkbox', 'select' ) ) ) {
311 return;
312 }
313
314 $field = FrmFieldsHelper::setup_edit_vars( $field );
315 $opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
316 $opts = explode( "\n", rtrim( $opts, "\n" ) );
317 $opts = array_map( 'trim', $opts );
318
319 if ( $field['separate_value'] ) {
320 foreach ( $opts as $opt_key => $opt ) {
321 if ( strpos( $opt, '|' ) !== false ) {
322 $vals = explode( '|', $opt );
323 if ( $vals[0] != $vals[1] ) {
324 $opts[ $opt_key ] = array(
325 'label' => trim( $vals[0] ),
326 'value' => trim( $vals[1] ),
327 );
328 }
329 unset( $vals );
330 }
331 unset( $opt_key, $opt );
332 }
333 }
334
335 //Keep other options after bulk update
336 if ( isset( $field['field_options']['other'] ) && $field['field_options']['other'] == true ) {
337 $other_array = array();
338 foreach ( $field['options'] as $opt_key => $opt ) {
339 if ( FrmFieldsHelper::is_other_opt( $opt_key ) ) {
340 $other_array[ $opt_key ] = $opt;
341 }
342 unset( $opt_key, $opt );
343 }
344 if ( ! empty( $other_array ) ) {
345 $opts = array_merge( $opts, $other_array );
346 }
347 }
348
349 $field['options'] = $opts;
350
351 if ( $field['type'] == 'radio' || $field['type'] == 'checkbox' ) {
352 $field_name = 'item_meta[' . $field['id'] . ']';
353
354 // Get html_id which will be used in single-option.php
355 $html_id = FrmFieldsHelper::get_html_id( $field );
356
357 require( FrmAppHelper::plugin_path() . '/classes/views/frm-fields/radio.php' );
358 } else {
359 FrmFieldsHelper::show_single_option( $field );
360 }
361
362 wp_die();
363 }
364
365 public static function update_order() {
366 FrmAppHelper::permission_check( 'frm_edit_forms' );
367 check_ajax_referer( 'frm_ajax', 'nonce' );
368
369 $fields = FrmAppHelper::get_post_param( 'frm_field_id' );
370 foreach ( (array) $fields as $position => $item ) {
371 FrmField::update( absint( $item ), array( 'field_order' => absint( $position ) ) );
372 }
373 wp_die();
374 }
375
376 public static function change_type( $type ) {
377 $type_switch = array(
378 'scale' => 'radio',
379 '10radio' => 'radio',
380 'rte' => 'textarea',
381 'website' => 'url',
382 );
383 if ( isset( $type_switch[ $type ] ) ) {
384 $type = $type_switch[ $type ];
385 }
386
387 $pro_fields = FrmField::pro_field_selection();
388 $types = array_keys( $pro_fields );
389 if ( in_array( $type, $types ) ) {
390 $type = 'text';
391 }
392
393 return $type;
394 }
395
396 public static function display_field_options( $display ) {
397 switch ( $display['type'] ) {
398 case 'captcha':
399 $display['required'] = false;
400 $display['invalid'] = true;
401 $display['default_blank'] = false;
402 $display['captcha_size'] = true;
403 break;
404 case 'radio':
405 $display['default_blank'] = false;
406 break;
407 case 'text':
408 case 'textarea':
409 $display['size'] = true;
410 $display['clear_on_focus'] = true;
411 break;
412 case 'select':
413 $display['size'] = true;
414 break;
415 case 'url':
416 case 'website':
417 case 'email':
418 $display['size'] = true;
419 $display['clear_on_focus'] = true;
420 $display['invalid'] = true;
421 }
422
423 return $display;
424 }
425
426 public static function input_html( $field, $echo = true ) {
427 $class = array(); //$field['type'];
428 self::add_input_classes( $field, $class );
429
430 $add_html = array();
431 self::add_html_size( $field, $add_html );
432 self::add_html_length( $field, $add_html );
433 self::add_html_placeholder( $field, $add_html, $class );
434 self::add_validation_messages( $field, $add_html );
435
436 $class = apply_filters( 'frm_field_classes', implode( ' ', $class ), $field );
437
438 FrmFormsHelper::add_html_attr( $class, 'class', $add_html );
439
440 self::add_shortcodes_to_html( $field, $add_html );
441
442 $add_html = apply_filters( 'frm_field_extra_html', $add_html, $field );
443 $add_html = ' ' . implode( ' ', $add_html ) . ' ';
444
445 if ( $echo ) {
446 echo $add_html;
447 }
448
449 return $add_html;
450 }
451
452 private static function add_input_classes( $field, array &$class ) {
453 if ( isset( $field['input_class'] ) && ! empty( $field['input_class'] ) ) {
454 $class[] = $field['input_class'];
455 }
456
457 if ( $field['type'] == 'hidden' || $field['type'] == 'user_id' ) {
458 return;
459 }
460
461 if ( isset( $field['size'] ) && $field['size'] > 0 ) {
462 $class[] = 'auto_width';
463 }
464 }
465
466 private static function add_html_size( $field, array &$add_html ) {
467 if ( ! isset( $field['size'] ) || $field['size'] <= 0 || in_array( $field['type'], array( 'select', 'data', 'time', 'hidden', 'file', 'lookup' ) ) ) {
468 return;
469 }
470
471 if ( FrmAppHelper::is_admin_page('formidable' ) ) {
472 return;
473 }
474
475 if ( is_numeric( $field['size'] ) ) {
476 $field['size'] .= 'px';
477 }
478
479 $important = apply_filters( 'frm_use_important_width', 1, $field );
480 // Note: This inline styling must stay since we cannot realistically set a class for every possible field size
481 $add_html['style'] = 'style="width:' . esc_attr( $field['size'] ) . ( $important ? ' !important' : '' ) . '"';
482
483 self::add_html_cols( $field, $add_html );
484 }
485
486 private static function add_html_cols( $field, array &$add_html ) {
487 if ( ! in_array( $field['type'], array( 'textarea', 'rte' ) ) ) {
488 return;
489 }
490
491 // convert to cols for textareas
492 $calc = array(
493 '' => 9,
494 'px' => 9,
495 'rem' => 0.444,
496 'em' => 0.544,
497 );
498
499 // include "col" for valid html
500 $unit = trim( preg_replace( '/[0-9]+/', '', $field['size'] ) );
501
502 if ( ! isset( $calc[ $unit ] ) ) {
503 return;
504 }
505
506 $size = (float) str_replace( $unit, '', $field['size'] ) / $calc[ $unit ];
507
508 $add_html['cols'] = 'cols="' . absint( $size ) . '"';
509 }
510
511 private static function add_html_length( $field, array &$add_html ) {
512 // check for max setting and if this field accepts maxlength
513 if ( FrmField::is_option_empty( $field, 'max' ) || in_array( $field['type'], array( 'textarea', 'rte', 'hidden', 'file' ) ) ) {
514 return;
515 }
516
517 if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
518 // don't load on form builder page
519 return;
520 }
521
522 $add_html['maxlength'] = 'maxlength="' . esc_attr( $field['max'] ) . '"';
523 }
524
525 private static function add_html_placeholder( $field, array &$add_html, array &$class ) {
526 if ( FrmAppHelper::is_admin_page( 'formidable' ) ) {
527 return;
528 }
529
530 if ( $field['default_value'] != '' && ! FrmField::is_option_true( $field, 'clear_on_focus' ) ) {
531 if ( is_array( $field['default_value'] ) ) {
532 $field['default_value'] = json_encode( $field['default_value'] );
533 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
534 } else {
535 self::add_frmval_to_input( $field, $add_html );
536 }
537 $field['default_value'] = '';
538 }
539
540 $field['default_value'] = self::prepare_default_value( $field );
541 if ( $field['default_value'] == '' || is_array( $field['default_value'] ) ) {
542 // don't include a json placeholder
543 return;
544 }
545
546 $frm_settings = FrmAppHelper::get_settings();
547
548 if ( $frm_settings->use_html ) {
549 self::add_placeholder_to_input( $field, $add_html );
550 } else {
551 self::add_frmval_to_input( $field, $add_html );
552
553 $class[] = 'frm_toggle_default';
554
555 if ( $field['value'] == $field['default_value'] ) {
556 $class[] = 'frm_default';
557 }
558 }
559 }
560
561 private static function prepare_default_value( $field ) {
562 $is_placeholder_field = FrmFieldsHelper::is_placeholder_field_type( $field['type'] );
563 $is_combo_field = in_array( $field['type'], array( 'address', 'credit_card' ) );
564
565 $default_value = $field['default_value'];
566 if ( empty( $default_value ) ) {
567 if ( $is_placeholder_field && ! $is_combo_field ) {
568 $default_value = self::get_default_value_from_name( $field );
569 }
570 }
571
572 return $default_value;
573 }
574
575 /**
576 * If the label position is "inside",
577 * get the label to use as the placeholder
578 *
579 * @since 2.05
580 */
581 public static function get_default_value_from_name( $field ) {
582 $position = FrmField::get_option( $field, 'label' );
583 if ( $position == 'inside' ) {
584 $default_value = $field['name'];
585 } else {
586 $default_value = '';
587 }
588 return $default_value;
589 }
590
591 /**
592 * use HMTL5 placeholder with js fallback
593 */
594 private static function add_placeholder_to_input( $field, &$add_html ) {
595 if ( FrmFieldsHelper::is_placeholder_field_type( $field['type'] ) ) {
596 $add_html['placeholder'] = 'placeholder="' . esc_attr( $field['default_value'] ) . '"';
597 wp_enqueue_script( 'jquery-placeholder' );
598 }
599 }
600
601 private static function add_frmval_to_input( $field, &$add_html ) {
602 if ( $field['default_value'] != '' ) {
603 $add_html['data-frmval'] = 'data-frmval="' . esc_attr( $field['default_value'] ) . '"';
604 }
605 }
606
607 private static function add_validation_messages( $field, array &$add_html ) {
608 if ( FrmField::is_required( $field ) ) {
609 $required_message = FrmFieldsHelper::get_error_msg( $field, 'blank' );
610 $add_html['data-reqmsg'] = 'data-reqmsg="' . esc_attr( $required_message ) . '"';
611 }
612
613 if ( ! FrmField::is_option_empty( $field, 'invalid' ) ) {
614 $invalid_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' );
615 $add_html['data-invmsg'] = 'data-invmsg="' . esc_attr( $invalid_message ) . '"';
616 }
617 }
618
619 private static function add_shortcodes_to_html( $field, array &$add_html ) {
620 if ( FrmField::is_option_empty( $field, 'shortcodes' ) ) {
621 return;
622 }
623
624 foreach ( $field['shortcodes'] as $k => $v ) {
625 if ( 'opt' === $k ) {
626 continue;
627 }
628
629 if ( is_numeric( $k ) && strpos( $v, '=' ) ) {
630 $add_html[] = $v;
631 } else if ( ! empty( $k ) && isset( $add_html[ $k ] ) ) {
632 $add_html[ $k ] = str_replace( $k . '="', $k . '="' . $v, $add_html[ $k ] );
633 } else {
634 $add_html[ $k ] = $k . '="' . esc_attr( $v ) . '"';
635 }
636
637 unset( $k, $v );
638 }
639 }
640
641 public static function check_value( $opt, $opt_key, $field ) {
642 if ( is_array( $opt ) ) {
643 if ( FrmField::is_option_true( $field, 'separate_value' ) ) {
644 $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
645 } else {
646 $opt = isset( $opt['label'] ) ? $opt['label'] : reset( $opt );
647 }
648 }
649 return $opt;
650 }
651
652 public static function check_label( $opt ) {
653 if ( is_array( $opt ) ) {
654 $opt = ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
655 }
656
657 return $opt;
658 }
659 }
660