-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathFrmFormsHelper.php
More file actions
2233 lines (1940 loc) · 60.5 KB
/
FrmFormsHelper.php
File metadata and controls
2233 lines (1940 loc) · 60.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}
class FrmFormsHelper {
/**
* Store and re-use field type data for the insert_opt_html function (to avoid multiple calls to FrmField::all_field_selection).
*
* @since 6.10
*
* @var array|null
*/
private static $field_type_data_for_insert_opt_html;
/**
* @since 2.2.10
*
* @return string
*/
public static function form_error_class() {
return apply_filters( 'frm_form_error_class', 'frm_error_style' );
}
/**
* @param string $key
* @param false|object $form
*
* @return string
*/
public static function get_direct_link( $key, $form = false ) {
$target_url = esc_url( admin_url( 'admin-ajax.php?action=frm_forms_preview&form=' . $key ) );
$target_url = apply_filters( 'frm_direct_link', $target_url, $key, $form );
return $target_url;
}
/**
* @param string $field_name
* @param int|string $field_value
* @param array $args
*
* @return void
*/
public static function forms_dropdown( $field_name, $field_value = '', $args = array() ) {
$defaults = array(
'blank' => true,
'field_id' => false,
'onchange' => false,
'exclude' => false,
'class' => '',
'inc_children' => 'exclude',
);
$args = wp_parse_args( $args, $defaults );
if ( ! $args['field_id'] ) {
$args['field_id'] = $field_name;
}
$query = array();
if ( $args['exclude'] ) {
$query['id !'] = $args['exclude'];
}
$where = apply_filters( 'frm_forms_dropdown', $query, $field_name );
$forms = FrmForm::get_published_forms( $where, 999, $args['inc_children'] );
$add_html = array();
self::add_html_attr( $args['onchange'], 'onchange', $add_html );
self::add_html_attr( $args['class'], 'class', $add_html );
?>
<select name="<?php echo esc_attr( $field_name ); ?>"
id="<?php echo esc_attr( $args['field_id'] ); ?>"
<?php echo wp_strip_all_tags( implode( ' ', $add_html ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<?php if ( $args['blank'] ) { ?>
<option value=""><?php echo $args['blank'] == 1 ? ' ' : '- ' . esc_attr( $args['blank'] ) . ' -'; ?></option>
<?php } ?>
<?php foreach ( $forms as $form ) { ?>
<option value="<?php echo esc_attr( $form->id ); ?>" <?php selected( $field_value, $form->id ); ?>>
<?php echo esc_html( '' === $form->name ? self::get_no_title_text() : FrmAppHelper::truncate( $form->name, 50 ) . ( $form->parent_form_id ? __( ' (child)', 'formidable' ) : '' ) ); ?>
</option>
<?php } ?>
</select>
<?php
}
/**
* @since 2.0.6
*
* @param string $class
* @param string $param
* @param array $add_html
*
* @return void
*/
public static function add_html_attr( $class, $param, &$add_html ) {
if ( ! empty( $class ) ) {
$add_html[ $param ] = sanitize_title( $param ) . '="' . esc_attr( trim( sanitize_text_field( $class ) ) ) . '"';
}
}
/**
* @param false|object|string $selected - The label for the placeholder, or the form object.
*
* @return void
*/
public static function form_switcher( $selected = false ) {
$where = apply_filters( 'frm_forms_dropdown', array(), '' );
$forms = FrmForm::get_published_forms( $where );
$args = array(
'id' => 0,
'form' => 0,
);
if ( isset( $_GET['id'] ) && ! isset( $_GET['form'] ) ) {
unset( $args['form'] );
} elseif ( isset( $_GET['form'] ) && ! isset( $_GET['id'] ) ) {
unset( $args['id'] );
}
$frm_action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' );
if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) && in_array( $frm_action, array( 'edit', 'show', 'destroy', 'destroy_all' ), true ) ) {
$args['frm_action'] = 'list';
$args['form'] = 0;
} elseif ( FrmAppHelper::is_admin_page( 'formidable' ) && in_array( $frm_action, array( 'new', 'duplicate' ), true ) ) {
$args['frm_action'] = 'edit';
} elseif ( FrmAppHelper::is_style_editor_page() ) {
// Avoid passing style into form switcher on style page.
unset( $args['id'] );
$query_args = array(
'page' => 'formidable-styles',
);
if ( $frm_action ) {
$query_args['frm_action'] = $frm_action;
}
$base = add_query_arg( $query_args, admin_url( 'admin.php' ) );
} elseif ( isset( $_GET['post'] ) ) {
$args['form'] = 0;
$base = admin_url( 'edit.php?post_type=frm_display' );
}//end if
$form_id = 0;
if ( is_object( $selected ) ) {
$form_id = $selected->id;
$selected = $selected->name;
}
$name = $selected === false ? __( 'Switch Form', 'formidable' ) : $selected;
$name = '' === $name || is_null( $name ) ? self::get_no_title_text() : strip_tags( $name );
$truncated_name = FrmAppHelper::truncate( $name, 25 );
if ( count( $forms ) < 2 ) {
?>
<div id="frm_bs_dropdown">
<h1>
<span class="frm_bstooltip" title="<?php echo esc_attr( $truncated_name === $name ? '' : $name ); ?>" data-placement="right">
<?php echo esc_html( $name ); ?>
</span>
</h1>
</div>
<?php
return;
}
?>
<div id="frm_bs_dropdown" class="dropdown <?php echo esc_attr( is_rtl() ? 'dropdown-menu-right' : 'dropdown-menu-left' ); ?>">
<a href="#" id="frm-navbarDrop" class="frm-dropdown-toggle" data-toggle="dropdown">
<h1>
<span class="frm_bstooltip" title="<?php echo esc_attr( $truncated_name === $name ? '' : $name ); ?>" data-placement="right">
<?php echo esc_html( $name ); ?>
</span>
<?php FrmAppHelper::icon_by_class( 'frmfont frm_arrowdown6_icon', array( 'aria-hidden' => 'true' ) ); ?>
</h1>
</a>
<ul class="frm-dropdown-menu frm-on-top frm-inline-modal frm_code_list frm-full-hover" role="menu" aria-labelledby="frm-navbarDrop">
<?php if ( count( $forms ) > 8 ) { ?>
<li class="frm-with-search">
<?php
FrmAppHelper::show_search_box(
array(
'input_id' => 'dropform',
'placeholder' => __( 'Search Forms', 'formidable' ),
'tosearch' => 'frm-dropdown-form',
// Specify a value to avoid the $_REQUEST['s'] default value.
'value' => '',
)
);
?>
</li>
<?php } ?>
<?php
foreach ( $forms as $form ) {
if ( $form->id === $form_id ) {
// Don't include the selected form in the switcher since it does nothing.
continue;
}
if ( isset( $args['id'] ) ) {
$args['id'] = $form->id;
}
if ( isset( $args['form'] ) ) {
$args['form'] = $form->id;
}
$url = isset( $base ) ? add_query_arg( $args, $base ) : add_query_arg( $args );
$form_name = empty( $form->name ) ? self::get_no_title_text() : $form->name;
?>
<li class="frm-dropdown-form">
<a href="<?php echo esc_url( $url ); ?>" tabindex="-1" class="frm-justify-between">
<?php echo esc_html( $form_name ); ?>
<span>
<?php
printf(
/* translators: %d: Form ID */
esc_html__( '(ID %d)', 'formidable' ),
esc_attr( $form->id )
);
?>
</span>
<span class="frm_hidden"><?php echo esc_html( $form->form_key ); ?></span>
</a>
</li>
<?php
unset( $form );
}//end foreach
?>
</ul>
</div>
<?php
}
/**
* @param string $col
* @param string $sort_col
* @param string $sort_dir
*
* @return void
*/
public static function get_sortable_classes( $col, $sort_col, $sort_dir ) {
echo $sort_col == $col ? 'sorted' : 'sortable';
echo $sort_col == $col && $sort_dir === 'desc' ? ' asc' : ' desc';
}
/**
* @since 3.0
*
* @param array|string $field_type
*
* @return string
*/
public static function get_field_link_name( $field_type ) {
if ( is_array( $field_type ) ) {
$field_label = $field_type['name'];
} else {
$field_label = $field_type;
}
return $field_label;
}
/**
* @since 3.0
*
* @param array|string $field_type
*
* @return string
*/
public static function get_field_link_icon( $field_type ) {
if ( is_array( $field_type ) && isset( $field_type['icon'] ) ) {
$icon = $field_type['icon'];
} else {
$icon = 'frm_icon_font frm_pencil_icon';
}
return $icon;
}
/**
* Get the invalid form error message
*
* @since 2.02.07
*
* @param array $args
*
* @return string
*/
public static function get_invalid_error_message( $args ) {
$settings_args = $args;
if ( isset( $args['form'] ) ) {
$settings_args['current_form'] = $args['form']->id;
}
$frm_settings = FrmAppHelper::get_settings( $settings_args );
$invalid_msg = do_shortcode( $frm_settings->invalid_msg );
return apply_filters( 'frm_invalid_error_message', $invalid_msg, $args );
}
/**
* @param array $atts {
* The success message details.
*
* @type string $message
* @type stdClass $form
* @type int $entry_id
* @type string $class
* }
*
* @return string
*/
public static function get_success_message( $atts ) {
$message = apply_filters( 'frm_content', $atts['message'], $atts['form'], $atts['entry_id'] );
// Only autop if the message includes line breaks.
$autop = strpos( $message, "\n" ) !== false;
/**
* Filters whether to autop the success message.
* This is false by default if the message does not include line breaks.
*
* @since 6.23
*
* @param bool $autop
* @param string $message
* @param object $form
*/
$autop = (bool) apply_filters( 'frm_wpautop_success_message', $autop, $message, $atts['form'] );
if ( $autop ) {
$message = FrmAppHelper::use_wpautop( $message );
}
$message = do_shortcode( $message );
$message = '<div class="' . esc_attr( $atts['class'] ) . '" role="status">' . $message . '</div>';
return $message;
}
/**
* Used when a form is created
*
* @param array $values
*
* @return array
*/
public static function setup_new_vars( $values = array() ) {
global $wpdb;
if ( ! empty( $values ) ) {
$post_values = $values;
} else {
$values = array();
$post_values = ! empty( $_POST ) ? $_POST : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
$defaults = array(
'name' => '',
'description' => '',
);
foreach ( $defaults as $var => $default ) {
if ( ! isset( $values[ $var ] ) ) {
$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
}
}
$values['description'] = FrmAppHelper::use_wpautop( $values['description'] );
$defaults = array(
'form_id' => '',
'logged_in' => '',
'editable' => '',
'is_template' => 0,
'status' => 'published',
'parent_form_id' => 0,
);
foreach ( $defaults as $var => $default ) {
if ( ! isset( $values[ $var ] ) ) {
$values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
}
}
unset( $defaults );
if ( ! isset( $values['form_key'] ) ) {
$values['form_key'] = $post_values && isset( $post_values['form_key'] ) ? $post_values['form_key'] : FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_forms', 'form_key' );
}
$values = self::fill_default_opts( $values, false, $post_values );
$values['custom_style'] = FrmAppHelper::custom_style_value( $post_values );
return apply_filters( 'frm_setup_new_form_vars', $values );
}
/**
* Used when editing a form
*
* @param array $values
* @param object $record
* @param array $post_values
*
* @return array
*/
public static function setup_edit_vars( $values, $record, $post_values = array() ) {
if ( empty( $post_values ) ) {
$post_values = wp_unslash( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
$values['form_key'] = $post_values['form_key'] ?? $record->form_key;
$values['is_template'] = $post_values['is_template'] ?? $record->is_template;
$values['status'] = $record->status;
$values = self::fill_default_opts( $values, $record, $post_values );
return apply_filters( 'frm_setup_edit_form_vars', $values );
}
/**
* @param array $values
* @param false|object $record
* @param array|false $post_values
*
* @return array
*/
public static function fill_default_opts( $values, $record, $post_values ) {
$defaults = self::get_default_opts();
foreach ( $defaults as $var => $default ) {
if ( is_array( $default ) ) {
if ( ! isset( $values[ $var ] ) ) {
$values[ $var ] = $record && isset( $record->options[ $var ] ) ? $record->options[ $var ] : array();
}
foreach ( $default as $k => $v ) {
$values[ $var ][ $k ] = $post_values && isset( $post_values[ $var ][ $k ] ) ? $post_values[ $var ][ $k ] : ( $record && isset( $record->options[ $var ] ) && isset( $record->options[ $var ][ $k ] ) ? $record->options[ $var ][ $k ] : $v );
if ( is_array( $v ) ) {
foreach ( $v as $k1 => $v1 ) {
$values[ $var ][ $k ][ $k1 ] = $post_values && isset( $post_values[ $var ][ $k ][ $k1 ] ) ? $post_values[ $var ][ $k ][ $k1 ] : ( $record && isset( $record->options[ $var ] ) && isset( $record->options[ $var ][ $k ] ) && isset( $record->options[ $var ][ $k ][ $k1 ] ) ? $record->options[ $var ][ $k ][ $k1 ] : $v1 );
unset( $k1, $v1 );
}
}
unset( $k, $v );
}
} else {
$values[ $var ] = $post_values && isset( $post_values['options'][ $var ] ) ? $post_values['options'][ $var ] : ( $record && isset( $record->options[ $var ] ) ? $record->options[ $var ] : $default );
}
unset( $var, $default );
}//end foreach
return $values;
}
/**
* @return array
*/
public static function get_default_opts() {
$frm_settings = FrmAppHelper::get_settings();
return array(
'submit_value' => $frm_settings->submit_value,
'success_action' => 'message',
'success_msg' => $frm_settings->success_msg,
'show_form' => 0,
'akismet' => '',
'stopforumspam' => 0,
'antispam' => 0,
'no_save' => 0,
'ajax_load' => 0,
'js_validate' => 0,
'form_class' => '',
'custom_style' => 1,
'before_html' => self::get_default_html( 'before' ),
'after_html' => '',
'submit_html' => self::get_default_html( 'submit' ),
'show_title' => 0,
'show_description' => 0,
'ajax_submit' => 0,
);
}
/**
* @since 2.0.6
*
* @param array $options
* @param array $values
*
* @return void
*/
public static function fill_form_options( &$options, $values ) {
$defaults = self::get_default_opts();
foreach ( $defaults as $var => $default ) {
$options[ $var ] = $values['options'][ $var ] ?? $default;
unset( $var, $default );
}
}
/**
* @param string $loc
*
* @return string
*/
public static function get_default_html( $loc ) {
if ( $loc === 'submit' ) {
$draft_link = self::get_draft_link();
$start_over = self::get_start_over_shortcode();
$default_html = <<<SUBMIT_HTML
<div class="frm_submit frm_flex">
<button class="frm_button_submit" type="submit" [button_action]>[button_label]</button>
[if back_button]<button type="submit" name="frm_prev_page" formnovalidate="formnovalidate" class="frm_prev_page" [back_hook]>[back_label]</button>[/if back_button]
$draft_link
$start_over
</div>
SUBMIT_HTML;
} elseif ( $loc === 'before' ) {
$default_html = <<<BEFORE_HTML
<legend class="frm_screen_reader">[form_name]</legend>
[if form_name]<h3 class="frm_form_title">[form_name]</h3>[/if form_name]
[if form_description]<div class="frm_description">[form_description]</div>[/if form_description]
BEFORE_HTML;
} else {
$default_html = '';
}
return $default_html;
}
/**
* @return string
*/
public static function get_draft_link() {
$link = '[if save_draft]<button class="frm_save_draft" [draft_hook]>[draft_label]</button>[/if save_draft]';
return $link;
}
/**
* Gets start over button shortcode.
*
* @since 5.4
*
* @return string
*/
public static function get_start_over_shortcode() {
return '[if start_over]<a href="#" tabindex="0" class="frm_start_over" [start_over_hook]>[start_over_label]</a>[/if start_over]';
}
/**
* @param string $html
* @param object $form
* @param string $submit
* @param object|null $form_action
* @param array $values
*
* @return void
*/
public static function get_custom_submit( $html, $form, $submit, $form_action, $values ) {
$button = self::replace_shortcodes( $html, $form, $submit, $form_action, $values );
if ( ! strpos( $button, '[button_action]' ) ) {
echo FrmAppHelper::maybe_kses( $button ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
/**
* @since 5.0.06
*/
$button = apply_filters( 'frm_submit_button_html', $button, compact( 'form' ) );
if ( FrmAppHelper::should_never_allow_unfiltered_html() ) {
$button = FrmAppHelper::kses_submit_button( $button );
}
$button_parts = explode( '[button_action]', $button );
$classes = apply_filters( 'frm_submit_button_class', array(), $form );
if ( ! empty( $classes ) ) {
$classes = implode( ' ', $classes );
$button_class = 'frm_button_submit';
if ( preg_match( '/\bclass="[^"]*?\b' . preg_quote( $button_class, '/' ) . '\b[^"]*?"/', $button_parts[0] ) ) {
$button_parts[0] = str_replace( $button_class, $button_class . ' ' . esc_attr( $classes ), $button_parts[0] );
} else {
$button_parts[0] .= ' class="' . esc_attr( $classes ) . '"';
}
}
echo $button_parts[0]; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
do_action( 'frm_submit_button_action', $form, $form_action );
echo $button_parts[1]; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* @since 4.0
*
* @return array
*/
public static function html_shortcodes() {
$codes = array(
'id' => array(
'label' => __( 'Field ID', 'formidable' ),
'class' => 'show_field_custom_html',
),
'key' => array(
'label' => __( 'Field Key', 'formidable' ),
'class' => 'show_field_custom_html',
),
'field_name' => array(
'label' => __( 'Field Name', 'formidable' ),
'class' => 'show_field_custom_html',
),
'description' => array(
'label' => __( 'Field Description', 'formidable' ),
'class' => 'show_field_custom_html',
),
'label_position' => array(
'label' => __( 'Label Position', 'formidable' ),
'class' => 'show_field_custom_html',
),
'required_label' => array(
'label' => __( 'Required Label', 'formidable' ),
'class' => 'show_field_custom_html',
),
'input' => array(
'label' => __( 'Input Field', 'formidable' ),
'class' => 'show_field_custom_html',
),
'input opt=1' => array(
'label' => __( 'Single Option', 'formidable' ),
'title' => __( 'Show a single radio or checkbox option by replacing 1 with the order of the option', 'formidable' ),
'class' => 'show_field_custom_html',
),
'input label=0' => array(
'label' => __( 'Hide Option Label', 'formidable' ),
'class' => 'show_field_custom_html',
),
'required_class' => array(
'label' => __( 'Required Class', 'formidable' ),
'title' => __( 'Add class name if field is required', 'formidable' ),
'class' => 'show_field_custom_html',
),
'error_class' => array(
'label' => __( 'Error Class', 'formidable' ),
'title' => __( 'Add class name if field has an error on form submit', 'formidable' ),
'class' => 'show_field_custom_html',
),
'form_name' => array(
'label' => __( 'Form Name', 'formidable' ),
'class' => 'show_before_html show_after_html',
),
'form_description' => array(
'label' => __( 'Form Description', 'formidable' ),
'class' => 'show_before_html show_after_html',
),
'form_key' => array(
'label' => __( 'Form Key', 'formidable' ),
'class' => 'show_before_html show_after_html',
),
'deletelink' => array(
'label' => __( 'Delete Entry Link', 'formidable' ),
'class' => 'show_before_html show_after_html',
),
'button_label' => array(
'label' => __( 'Button Label', 'formidable' ),
'class' => 'show_submit_html',
),
'button_action' => array(
'label' => __( 'Button Hook', 'formidable' ),
'class' => 'show_submit_html',
),
);
/**
* @since 4.0
*/
return apply_filters( 'frm_html_codes', $codes );
}
/**
* @since 4.0
*
* @param array $args
*
* @return void
*/
public static function insert_opt_html( $args ) {
$class = $args['class'] ?? '';
$fields = self::get_field_type_data_for_insert_opt_html();
$field = $fields[ $args['type'] ] ?? array();
self::prepare_field_type( $field );
if ( ! isset( $field['icon'] ) ) {
$field['icon'] = 'frmfont frm_pencil_icon';
}
$possible_email_field = FrmFieldFactory::field_has_property( $args['type'], 'holds_email_values' );
if ( $possible_email_field ) {
$class .= ' show_frm_not_email_to';
}
if ( 'url' === $args['type'] ) {
$class .= ' frm_insert_url';
}
$truncated_name = FrmAppHelper::truncate( $args['name'], 60 );
if ( isset( $field['icon'] ) ) {
$icon = FrmAppHelper::icon_by_class(
$field['icon'],
array(
'aria-hidden' => 'true',
'echo' => false,
)
);
} else {
$icon = '';
}
?>
<li class="<?php echo esc_attr( $class ); ?>">
<a href="javascript:void(0)" class="frmids frm_insert_code" data-code="<?php echo esc_attr( $args['id'] ); ?>">
<?php
echo FrmAppHelper::kses_icon( $icon ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
printf(
'<span>%s</span>',
esc_html( $truncated_name )
);
?>
<span>[<?php echo esc_attr( $args['id_label'] ?? $args['id'] ); ?>]</span>
</a>
<a href="javascript:void(0)" class="frmkeys frm_insert_code frm_hidden" data-code="<?php echo esc_attr( $args['key'] ); ?>">
<?php
echo FrmAppHelper::kses_icon( $icon ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
printf(
'<span>%s</span>',
esc_html( $truncated_name )
);
?>
<span>[<?php echo esc_attr( FrmAppHelper::truncate( $args['key_label'] ?? $args['key'], 7 ) ); ?>]</span>
</a>
</li>
<?php
}
/**
* Store and re-use field selection data for use when outputting shortcodes options in shortcode pop up.
* This significantly improves performance by avoiding repeat calls to FrmField::all_field_selection.
*
* @since 6.10
*
* @return array
*/
private static function get_field_type_data_for_insert_opt_html() {
if ( ! isset( self::$field_type_data_for_insert_opt_html ) ) {
self::$field_type_data_for_insert_opt_html = FrmField::all_field_selection();
}
return self::$field_type_data_for_insert_opt_html;
}
/**
* @since 4.0
*
* @param array $args
*
* @return void
*/
public static function insert_code_html( $args ) {
$defaults = array(
'class' => '',
'code' => '',
'label' => '',
'title' => '',
);
$args = array_merge( $defaults, $args );
$has_tooltip = ! empty( $args['title'] );
?>
<li class="<?php echo esc_attr( $args['class'] ); ?>">
<a href="javascript:void(0)" class="frm_insert_code <?php echo $has_tooltip ? 'frm_help' : ''; ?>"
<?php echo $has_tooltip ? 'title="' . esc_attr( $args['title'] ) . '"' : ''; ?>
data-code="<?php echo esc_attr( $args['code'] ); ?>">
<?php echo esc_attr( FrmAppHelper::truncate( $args['label'], 60 ) ); ?>
<span>
[<?php echo esc_attr( FrmAppHelper::truncate( $args['code'], 10 ) ); ?>]
</span>
</a>
</li>
<?php
}
/**
* Some field types in add-ons may have been added with only
* a field type and name.
*
* @since 4.0
*
* @param array|string $field
*
* @return void
*/
public static function prepare_field_type( &$field ) {
if ( ! is_array( $field ) ) {
$field = array(
'name' => $field,
'icon' => 'frm_icon_font frm_pencil_icon',
);
}
}
/**
* Automatically add end section fields if they don't exist (2.0 migration)
*
* @since 2.0
*
* @param object $form
* @param array $fields
* @param bool $reset_fields
*
* @return void
*/
public static function auto_add_end_section_fields( $form, $fields, &$reset_fields ) {
if ( empty( $fields ) ) {
return;
}
$end_section_values = apply_filters( 'frm_before_field_created', FrmFieldsHelper::setup_new_vars( 'end_divider', $form->id ) );
$open = false;
$prev_order = false;
$add_order = 0;
$last_field = false;
foreach ( $fields as $field ) {
if ( $prev_order === $field->field_order ) {
++$add_order;
}
if ( $add_order ) {
$reset_fields = true;
$field->field_order = $field->field_order + $add_order;
FrmField::update( $field->id, array( 'field_order' => $field->field_order ) );
}
switch ( $field->type ) {
case 'divider':
// Create an end section if open.
self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' );
// Mark it open for the next end section.
$open = true;
break;
case 'break':
self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $field, 'move' );
break;
case 'end_divider':
if ( ! $open ) {
// The section isn't open, so this is an extra field that needs to be removed.
FrmField::destroy( $field->id );
$reset_fields = true;
}
// There is already an end section here, so there is no need to create one.
$open = false;
}//end switch
$prev_order = $field->field_order;
$last_field = $field;
unset( $field );
}//end foreach
self::maybe_create_end_section( $open, $reset_fields, $add_order, $end_section_values, $last_field );
}
/**
* Create end section field if it doesn't exist. This is for migration from < 2.0
* Fix any ordering that may be messed up
*
* @param bool $open
* @param bool $reset_fields
* @param int $add_order
* @param array $end_section_values
* @param object $field
* @param string $move
*
* @return void
*/
public static function maybe_create_end_section( &$open, &$reset_fields, &$add_order, $end_section_values, $field, $move = 'no' ) {
if ( ! $open ) {
return;
}
$end_section_values['field_order'] = $field->field_order + 1;
FrmField::create( $end_section_values );
if ( $move === 'move' ) {
// bump the order of current field unless we're at the end of the form
FrmField::update( $field->id, array( 'field_order' => $field->field_order + 2 ) );
}
$add_order += 2;
$open = false;
$reset_fields = true;
}
/**
* @param string $html
* @param object $form
* @param bool $title
* @param bool $description
* @param array $values
*
* @return string
*/
public static function replace_shortcodes( $html, $form, $title = false, $description = false, $values = array() ) {
$codes = array(
'form_name' => $title,
'form_description' => $description,
'entry_key' => true,
);
foreach ( $codes as $code => $show ) {
if ( $code === 'form_name' ) {
$replace_with = $form->name;
} elseif ( $code === 'form_description' ) {
$replace_with = FrmAppHelper::use_wpautop( $form->description );
} elseif ( $code === 'entry_key' && ! empty( $_GET ) && isset( $_GET['entry'] ) ) {
$replace_with = FrmAppHelper::simple_get( 'entry' );
} else {
$replace_with = '';
}
FrmShortcodeHelper::remove_inline_conditions( ( FrmAppHelper::is_true( $show ) && $replace_with != '' ), $code, $replace_with, $html );
}
// Replace [form_key].
$html = str_replace( '[form_key]', $form->form_key, $html );
// Replace [frmurl].
$html = str_replace( '[frmurl]', FrmFieldsHelper::dynamic_default_values( 'frmurl' ), $html );
if ( strpos( $html, '[button_label]' ) ) {
add_filter( 'frm_submit_button', 'FrmFormsHelper::submit_button_label', 1 );
$submit_label = apply_filters( 'frm_submit_button', $title, $form );
$submit_label = esc_attr( do_shortcode( $submit_label ) );
$html = str_replace( '[button_label]', $submit_label, $html );
}
$html = apply_filters( 'frm_form_replace_shortcodes', $html, $form, $values );
if ( strpos( $html, '[if back_button]' ) ) {
$html = preg_replace( '/(\[if\s+back_button\])(.*?)(\[\/if\s+back_button\])/mis', '', $html );
}
if ( strpos( $html, '[if save_draft]' ) ) {
$html = preg_replace( '/(\[if\s+save_draft\])(.*?)(\[\/if\s+save_draft\])/mis', '', $html );
}
if ( strpos( $html, '[if start_over]' ) ) {
$html = preg_replace( '/(\[if\s+start_over\])(.*?)(\[\/if\s+start_over\])/mis', '', $html );
}
if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
$html = do_shortcode( $html );
}
return $html;
}
/**
* @param string $submit
*
* @return string
*/
public static function submit_button_label( $submit ) {
if ( ! $submit ) {
$frm_settings = FrmAppHelper::get_settings();
$submit = $frm_settings->submit_value;
}
return $submit;
}
/**
* If the Formidable styling isn't being loaded,
* use inline styling to hide the element