-
Notifications
You must be signed in to change notification settings - Fork 17
/
visual-form-builder.php
1940 lines (1664 loc) · 89.1 KB
/
visual-form-builder.php
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
/*
Plugin Name: Visual Form Builder
Description: Dynamically build forms using a simple interface. Forms include jQuery validation, a basic logic-based verification system, and entry tracking.
Author: Matthew Muro
Author URI: http://matthewmuro.com
Version: 2.6.9
*/
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Set to true to load uncompressed and unminified scripts and stylesheets
define( 'VFB_SCRIPT_DEBUG', false );
// Instantiate new class
$visual_form_builder = new Visual_Form_Builder();
// Visual Form Builder class
class Visual_Form_Builder{
/**
* The DB version. Used for SQL install and upgrades.
*
* Should only be changed when needing to change SQL
* structure or custom capabilities.
*
* @since 1.0
* @var string
* @access protected
*/
protected $vfb_db_version = '2.6.7';
/**
* Flag used to add scripts to front-end only once
*
* @since 1.0
* @var string
* @access protected
*/
protected $add_scripts = false;
/**
* An array of countries to be used throughout plugin
*
* @since 1.0
* @var array
* @access public
*/
public $countries = array( "", "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic", "Chad", "Chile", "China", "Colombi", "Comoros", "Congo (Brazzaville)", "Congo", "Costa Rica", "Cote d\'Ivoire", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor (Timor Timur)", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Fiji", "Finland", "France", "Gabon", "Gambia, The", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepa", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", "Romania", "Russia", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia and Montenegro", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States of America", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe" );
/**
* Constructor. Register core filters and actions.
*
* @access public
*/
public function __construct(){
global $wpdb;
// Setup global database table names
$this->field_table_name = $wpdb->prefix . 'visual_form_builder_fields';
$this->form_table_name = $wpdb->prefix . 'visual_form_builder_forms';
$this->entries_table_name = $wpdb->prefix . 'visual_form_builder_entries';
// Add suffix to load dev files
$this->load_dev_files = ( defined( 'VFB_SCRIPT_DEBUG' ) && VFB_SCRIPT_DEBUG ) ? '.dev' : '';
// Make sure we are in the admin before proceeding.
if ( is_admin() ) {
// Build options and settings pages.
add_action( 'admin_menu', array( &$this, 'add_admin' ) );
add_action( 'admin_init', array( &$this, 'save' ) );
add_action( 'wp_ajax_visual_form_builder_process_sort', array( &$this, 'process_sort_callback' ) );
add_action( 'wp_ajax_visual_form_builder_create_field', array( &$this, 'create_field_callback' ) );
add_action( 'wp_ajax_visual_form_builder_delete_field', array( &$this, 'delete_field_callback' ) );
add_action( 'wp_ajax_visual_form_builder_form_settings', array( &$this, 'form_settings_callback' ) );
add_action( 'wp_ajax_visual_form_builder_media_button', array( &$this, 'display_media_button' ) );
add_action( 'load-toplevel_page_visual-form-builder', array( &$this, 'help' ) );
// Adds additional media button to insert form shortcode
add_action( 'media_buttons', array( &$this, 'add_media_button' ), 999 );
// Load the includes files
add_action( 'load-visual-form-builder_page_vfb-entries', array( &$this, 'includes' ) );
// Adds a Screen Options tab to the Entries screen
add_filter( 'set-screen-option', array( &$this, 'save_screen_options' ), 10, 3 );
add_filter( 'load-toplevel_page_visual-form-builder', array( &$this, 'screen_options' ) );
add_filter( 'load-visual-form-builder_page_vfb-entries', array( &$this, 'screen_options' ) );
// Add meta boxes to the form builder admin page
add_action( 'load-toplevel_page_visual-form-builder', array( &$this, 'add_meta_boxes' ) );
// Adds a Settings link to the Plugins page
add_filter( 'plugin_action_links', array( &$this, 'plugin_action_links' ), 10, 2 );
// Check the db version and run SQL install, if needed
add_action( 'plugins_loaded', array( &$this, 'update_db_check' ) );
// Load the jQuery and CSS we need if we're on our plugin page
$current_pages = array( 'toplevel_page_visual-form-builder', 'visual-form-builder_page_vfb-add-new', 'visual-form-builder_page_vfb-entries', 'visual-form-builder_page_vfb-export' );
foreach ( $current_pages as $page ) {
add_action( "load-$page", array( &$this, 'admin_scripts' ) );
}
// Display update messages
add_action( 'admin_notices', array( &$this, 'admin_notices' ) );
}
// Load i18n
add_action( 'plugins_loaded', array( &$this, 'languages' ) );
add_shortcode( 'vfb', array( &$this, 'form_code' ) );
add_action( 'init', array( &$this, 'email' ), 10 );
add_action( 'init', array( &$this, 'confirmation' ), 12 );
// Add CSS to the front-end
add_action( 'wp_enqueue_scripts', array( &$this, 'css' ) );
}
/**
* Load localization file
*
* @since 2.7
*/
public function languages() {
load_plugin_textdomain( 'visual-form-builder', false , 'visual-form-builder/languages' );
}
/**
* Adds extra include files
*
* @since 1.2
*/
public function includes(){
global $entries_list, $entries_detail;
// Load the Entries List class
require_once( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'includes/class-entries-list.php' );
$entries_list = new VisualFormBuilder_Entries_List();
// Load the Entries Details class
require_once( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'includes/class-entries-detail.php' );
$entries_detail = new VisualFormBuilder_Entries_Detail();
}
/**
* Add Settings link to Plugins page
*
* @since 1.8
* @return $links array Links to add to plugin name
*/
public function plugin_action_links( $links, $file ) {
if ( $file == plugin_basename( __FILE__ ) )
$links[] = '<a href="admin.php?page=visual-form-builder">' . __( 'Settings' , 'visual-form-builder') . '</a>';
return $links;
}
/**
* Adds the media button image
*
* @since 2.3
*/
public function add_media_button(){
if ( current_user_can( 'manage_options' ) )
echo '<a href="' . add_query_arg( array( 'action' => 'visual_form_builder_media_button', 'width' => '450' ), admin_url( 'admin-ajax.php' ) ) . '" class="thickbox" title="Add Visual Form Builder form"><img width="18" height="18" src="' . plugins_url( 'visual-form-builder/images/vfb_icon.png' ) . '" alt="Add Visual Form Builder form" /></a>';
}
/**
* Display the additional media button
*
* Used for inserting the form shortcode with desired form ID
*
* @since 2.3
*/
public function display_media_button(){
global $wpdb;
// Sanitize the sql orderby
$order = sanitize_sql_orderby( 'form_id ASC' );
// Build our forms as an object
$forms = $wpdb->get_results( "SELECT form_id, form_title FROM $this->form_table_name ORDER BY $order" );
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$( '#add_vfb_form' ).submit(function(e){
e.preventDefault();
window.send_to_editor( '[vfb id=' + $( '#vfb_forms' ).val() + ']' );
window.tb_remove();
});
});
</script>
<div id="vfb_form">
<form id="add_vfb_form" class="media-upload-form type-form validate">
<h3 class="media-title">Insert Visual Form Builder Form</h3>
<p>Select a form below to insert into any Post or Page.</p>
<select id="vfb_forms" name="vfb_forms">
<?php foreach( $forms as $form ) : ?>
<option value="<?php echo $form->form_id; ?>"><?php echo $form->form_title; ?></option>
<?php endforeach; ?>
</select>
<p><input type="submit" class="button-primary" value="Insert Form" /></p>
</form>
</div>
<?php
die(1);
}
/**
* Register contextual help. This is for the Help tab dropdown
*
* @since 1.0
*/
public function help(){
$screen = get_current_screen();
$screen->add_help_tab( array(
'id' => 'vfb-help-tab-getting-started',
'title' => 'Getting Started',
'content' => '<ul>
<li>Click on the + tab, give your form a name and click Create Form.</li>
<li>Select form fields from the box on the left and click a field to add it to your form.</li>
<li>Edit the information for each form field by clicking on the down arrow.</li>
<li>Drag and drop the elements to put them in order.</li>
<li>Click Save Form to save your changes.</li>
</ul>'
) );
$screen->add_help_tab( array(
'id' => 'vfb-help-tab-item-config',
'title' => 'Form Item Configuration',
'content' => "<ul>
<li><em>Name</em> will change the display name of your form input.</li>
<li><em>Description</em> will be displayed below the associated input.</li>
<li><em>Validation</em> allows you to select from several of jQuery's Form Validation methods for text inputs. For more about the types of validation, read the <em>Validation</em> section below.</li>
<li><em>Required</em> is either Yes or No. Selecting 'Yes' will make the associated input a required field and the form will not submit until the user fills this field out correctly.</li>
<li><em>Options</em> will only be active for Radio and Checkboxes. This field contols how many options are available for the associated input.</li>
<li><em>Size</em> controls the width of Text, Textarea, Select, and Date Picker input fields. The default is set to Medium but if you need a longer text input, select Large.</li>
<li><em>CSS Classes</em> allow you to add custom CSS to a field. This option allows you to fine tune the look of the form.</li>
</ul>"
) );
$screen->add_help_tab( array(
'id' => 'vfb-help-tab-validation',
'title' => 'Validation',
'content' => "<p>Visual Form Builder uses the <a href='http://docs.jquery.com/Plugins/Validation/Validator'>jQuery Form Validation plugin</a> to perform clientside form validation.</p>
<ul>
<li><em>Email</em>: makes the element require a valid email.</li>
<li><em>URL</em>: makes the element require a valid url.</li>
<li><em>Date</em>: makes the element require a date. <a href='http://docs.jquery.com/Plugins/Validation/Methods/date'>Refer to documentation for various accepted formats</a>.
<li><em>Number</em>: makes the element require a decimal number.</li>
<li><em>Digits</em>: makes the element require digits only.</li>
<li><em>Phone</em>: makes the element require a US or International phone number. Most formats are accepted.</li>
<li><em>Time</em>: choose either 12- or 24-hour time format (NOTE: only available with the Time field).</li>
</ul>"
) );
$screen->add_help_tab( array(
'id' => 'vfb-help-tab-confirmation',
'title' => 'Confirmation',
'content' => "<p>Each form allows you to customize the confirmation by selecing either a Text Message, a WordPress Page, or to Redirect to a URL.</p>
<ul>
<li><em>Text</em> allows you to enter a custom formatted message that will be displayed on the page after your form is submitted. HTML is allowed here.</li>
<li><em>Page</em> displays a dropdown of all WordPress Pages you have created. Select one to redirect the user to that page after your form is submitted.</li>
<li><em>Redirect</em> will only accept URLs and can be used to send the user to a different site completely, if you choose.</li>
</ul>"
) );
$screen->add_help_tab( array(
'id' => 'vfb-help-tab-notification',
'title' => 'Notification',
'content' => "<p>Send a customized notification email to the user when the form has been successfully submitted.</p>
<ul>
<li><em>Sender Name</em>: the name that will be displayed on the email.</li>
<li><em>Sender Email</em>: the email that will be used as the Reply To email.</li>
<li><em>Send To</em>: the email where the notification will be sent. This must be a required text field with email validation.</li>
<li><em>Subject</em>: the subject of the email.</li>
<li><em>Message</em>: additional text that can be displayed in the body of the email. HTML tags are allowed.</li>
<li><em>Include a Copy of the User's Entry</em>: appends a copy of the user's submitted entry to the notification email.</li>
</ul>"
) );
$screen->add_help_tab( array(
'id' => 'vfb-help-tab-tips',
'title' => 'Tips',
'content' => "<ul>
<li>Fieldsets, a way to group form fields, are an essential piece of this plugin's HTML. As such, at least one fieldset is required and must be first in the order. Subsequent fieldsets may be placed wherever you would like to start your next grouping of fields.</li>
<li>Security verification is automatically included on very form. It's a simple logic question and should keep out most, if not all, spam bots.</li>
<li>There is a hidden spam field, known as a honey pot, that should also help deter potential abusers of your form.</li>
<li>Nesting is allowed underneath fieldsets and sections. Sections can be nested underneath fieldsets. Nesting is not required, however, it does make reorganizing easier.</li>
</ul>"
) );
}
/**
* Adds the Screen Options tab to the Entries screen
*
* @since 1.0
*/
public function screen_options(){
$screen = get_current_screen();
switch( $screen->id ) {
case 'visual-form-builder_page_vfb-entries' :
add_screen_option( 'per_page', array(
'label' => __( 'Entries per page', 'visual-form-builder' ),
'default' => 20,
'option' => 'vfb_entries_per_page'
) );
break;
case 'toplevel_page_visual-form-builder' :
if ( !isset( $_REQUEST['form'] ) )
break;
add_screen_option( 'layout_columns', array(
'max' => 2,
'default' => 2
) );
break;
}
}
/**
* Saves the Screen Options
*
* @since 1.0
*/
public function save_screen_options( $status, $option, $value ){
if ( $option == 'vfb_entries_per_page' )
return $value;
}
/**
* Add meta boxes to form builder screen
*
* @since 1.8
*/
public function add_meta_boxes() {
global $current_screen;
if ( $current_screen->id == 'toplevel_page_visual-form-builder' && isset( $_REQUEST['form'] ) ) {
add_meta_box( 'vfb_form_items_meta_box', __( 'Form Items', 'visual-form-builder' ), array( &$this, 'meta_box_form_items' ), 'toplevel_page_visual-form-builder', 'side', 'high' );
add_meta_box( 'vfb_form_media_button_tip', __( 'Display Forms', 'visual-form-builder' ), array( &$this, 'meta_box_display_forms' ), 'toplevel_page_visual-form-builder', 'side', 'low' );
}
}
/**
* Output for Form Items meta box
*
* @since 1.8
*/
public function meta_box_form_items() {
?>
<div class="taxonomydiv">
<p><strong><?php _e( 'Click' , 'visual-form-builder'); ?></strong> <?php _e( 'to Add a Field' , 'visual-form-builder'); ?> <img id="add-to-form" alt="" src="<?php echo admin_url( '/images/wpspin_light.gif' ); ?>" class="waiting spinner" /></p>
<ul class="posttype-tabs add-menu-item-tabs" id="vfb-field-tabs">
<li class="tabs"><a href="#standard-fields" class="nav-tab-link vfb-field-types"><?php _e( 'Standard' , 'visual-form-builder'); ?></a></li>
</ul>
<div id="standard-fields" class="tabs-panel tabs-panel-active">
<ul class="vfb-fields-col-1">
<li><a href="#" class="vfb-draggable-form-items" id="form-element-fieldset">Fieldset</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-text"><b></b>Text</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-checkbox"><b></b>Checkbox</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-select"><b></b>Select</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-datepicker"><b></b>Date</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-url"><b></b>URL</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-digits"><b></b>Number</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-phone"><b></b>Phone</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-file"><b></b>File Upload</a></li>
</ul>
<ul class="vfb-fields-col-2">
<li><a href="#" class="vfb-draggable-form-items" id="form-element-section"><b></b>Section</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-textarea"><b></b>Textarea</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-radio"><b></b>Radio</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-address"><b></b>Address</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-email"><b></b>Email</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-currency"><b></b>Currency</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-time"><b></b>Time</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-html"><b></b>HTML</a></li>
<li><a href="#" class="vfb-draggable-form-items" id="form-element-instructions"><b></b>Instructions</a></li>
</ul>
<div class="clear"></div>
</div> <!-- #standard-fields -->
</div> <!-- .taxonomydiv -->
<div class="clear"></div>
<?php
}
/**
* Output for the Display Forms meta box
*
* @since 1.8
*/
public function meta_box_display_forms() {
?>
<p><?php _e( 'Add forms to your Posts or Pages by locating the icon shown below in the area above your post/page editor.', 'visual-form-builder' ); ?><br>
<img src="<?php echo plugins_url( 'visual-form-builder/images/media-button-help.png' ); ?>">
</p>
<?php
}
/**
* Check database version and run SQL install, if needed
*
* @since 2.1
*/
public function update_db_check() {
// Add a database version to help with upgrades and run SQL install
if ( !get_option( 'vfb_db_version' ) ) {
update_option( 'vfb_db_version', $this->vfb_db_version );
$this->install_db();
}
// If database version doesn't match, update and run SQL install
if ( version_compare( get_option( 'vfb_db_version' ), $this->vfb_db_version, '<' ) ) {
update_option( 'vfb_db_version', $this->vfb_db_version );
$this->install_db();
}
}
/**
* Install database tables
*
* @since 1.0
*/
static function install_db() {
global $wpdb;
$field_table_name = $wpdb->prefix . 'visual_form_builder_fields';
$form_table_name = $wpdb->prefix . 'visual_form_builder_forms';
$entries_table_name = $wpdb->prefix . 'visual_form_builder_entries';
// Explicitly set the character set and collation when creating the tables
$charset = ( defined( 'DB_CHARSET' && '' !== DB_CHARSET ) ) ? DB_CHARSET : 'utf8';
$collate = ( defined( 'DB_COLLATE' && '' !== DB_COLLATE ) ) ? DB_COLLATE : 'utf8_general_ci';
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$field_sql = "CREATE TABLE $field_table_name (
field_id BIGINT(20) NOT NULL AUTO_INCREMENT,
form_id BIGINT(20) NOT NULL,
field_key VARCHAR(255) NOT NULL,
field_type VARCHAR(25) NOT NULL,
field_options TEXT,
field_description TEXT,
field_name TEXT NOT NULL,
field_sequence BIGINT(20) DEFAULT '0',
field_parent BIGINT(20) DEFAULT '0',
field_validation VARCHAR(25),
field_required VARCHAR(25),
field_size VARCHAR(25) DEFAULT 'medium',
field_css VARCHAR(255),
field_layout VARCHAR(255),
field_default TEXT,
PRIMARY KEY (field_id)
) DEFAULT CHARACTER SET $charset COLLATE $collate;";
$form_sql = "CREATE TABLE $form_table_name (
form_id BIGINT(20) NOT NULL AUTO_INCREMENT,
form_key TINYTEXT NOT NULL,
form_title TEXT NOT NULL,
form_email_subject TEXT,
form_email_to TEXT,
form_email_from VARCHAR(255),
form_email_from_name VARCHAR(255),
form_email_from_override VARCHAR(255),
form_email_from_name_override VARCHAR(255),
form_success_type VARCHAR(25) DEFAULT 'text',
form_success_message TEXT,
form_notification_setting VARCHAR(25),
form_notification_email_name VARCHAR(255),
form_notification_email_from VARCHAR(255),
form_notification_email VARCHAR(25),
form_notification_subject VARCHAR(255),
form_notification_message TEXT,
form_notification_entry VARCHAR(25),
form_label_alignment VARCHAR(25),
PRIMARY KEY (form_id)
) DEFAULT CHARACTER SET $charset COLLATE $collate;";
$entries_sql = "CREATE TABLE $entries_table_name (
entries_id BIGINT(20) NOT NULL AUTO_INCREMENT,
form_id BIGINT(20) NOT NULL,
data LONGTEXT NOT NULL,
subject TEXT,
sender_name VARCHAR(255),
sender_email VARCHAR(255),
emails_to TEXT,
date_submitted DATETIME,
ip_address VARCHAR(25),
PRIMARY KEY (entries_id)
) DEFAULT CHARACTER SET $charset COLLATE $collate;";
// Create or Update database tables
dbDelta( $field_sql );
dbDelta( $form_sql );
dbDelta( $entries_sql );
}
/**
* Queue plugin scripts for sorting form fields
*
* @since 1.0
*/
public function admin_scripts() {
wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_script( 'postbox' );
wp_enqueue_script( 'jquery-form-validation', plugins_url( '/js/jquery.validate.min.js', __FILE__ ), array( 'jquery' ), '1.9.0', true );
wp_enqueue_script( 'form-elements-add', plugins_url( "visual-form-builder/js/visual-form-builder$this->load_dev_files.js" ) , array( 'jquery', 'jquery-form-validation' ), '', true );
wp_enqueue_script( 'nested-sortable', plugins_url( 'visual-form-builder/js/jquery.ui.nestedSortable.js' ) , array( 'jquery', 'jquery-ui-sortable' ), '', true );
wp_enqueue_style( 'visual-form-builder-style', plugins_url( "visual-form-builder/css/visual-form-builder-admin.css" ) );
}
/**
* Queue form validation scripts
*
* @since 1.0
*/
public function scripts() {
// Make sure scripts are only added once via shortcode
$this->add_scripts = true;
wp_enqueue_script( 'jquery-form-validation', plugins_url( '/js/jquery.validate.min.js', __FILE__ ), array( 'jquery' ), '1.9.0', true );
wp_enqueue_script( 'jquery-ui-datepicker' );
wp_enqueue_script( 'visual-form-builder-validation', plugins_url( "visual-form-builder/js/visual-form-builder-validate.js" ) , array( 'jquery', 'jquery-form-validation' ), '', true );
wp_enqueue_script( 'visual-form-builder-metadata', plugins_url( 'visual-form-builder/js/jquery.metadata.js' ) , array( 'jquery', 'jquery-form-validation' ), '', true );
}
/**
* Add form CSS to wp_head
*
* @since 1.0
*/
public function css() {
wp_register_style( 'vfb-jqueryui-css', apply_filters( 'vfb-date-picker-css', plugins_url( '/css/smoothness/jquery-ui-1.9.2.min.css', __FILE__ ) ) );
wp_register_style( 'visual-form-builder-css', apply_filters( 'visual-form-builder-css', plugins_url( '/css/visual-form-builder.css', __FILE__ ) ) );
wp_register_script( 'visual-form-builder-quicktags', plugins_url( '/js/js_quicktags.js', __FILE__ ) );
wp_enqueue_style( 'visual-form-builder-css' );
wp_enqueue_style( 'vfb-jqueryui-css' );
wp_enqueue_script( 'visual-form-builder-quicktags' );
}
/**
* Actions to save, update, and delete forms/form fields
*
*
* @since 1.0
*/
public function save() {
global $wpdb;
if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], array( 'visual-form-builder', 'vfb-add-new' ) ) && isset( $_REQUEST['action'] ) ) {
switch ( $_REQUEST['action'] ) {
case 'create_form' :
$form_key = sanitize_title( $_REQUEST['form_title'] );
$form_title = esc_html( $_REQUEST['form_title'] );
$form_from_name = esc_html( $_REQUEST['form_email_from_name'] );
$form_subject = esc_html( $_REQUEST['form_email_subject'] );
$form_from = esc_html( $_REQUEST['form_email_from'] );
$form_to = serialize( array_map( 'esc_html', $_REQUEST['form_email_to'] ) );
check_admin_referer( 'create_form' );
$newdata = array(
'form_key' => $form_key,
'form_title' => $form_title,
'form_email_from_name' => $form_from_name,
'form_email_subject' => $form_subject,
'form_email_from' => $form_from,
'form_email_to' => $form_to
);
// Create the form
$wpdb->insert( $this->form_table_name, $newdata );
// Get form ID to add our first field
$new_form_selected = $wpdb->insert_id;
// Setup the initial fieldset
$initial_fieldset = array(
'form_id' => $wpdb->insert_id,
'field_key' => 'fieldset',
'field_type' => 'fieldset',
'field_name' => 'Fieldset',
'field_sequence' => 0
);
// Add the first fieldset to get things started
$wpdb->insert( $this->field_table_name, $initial_fieldset );
$verification_fieldset = array(
'form_id' => $new_form_selected,
'field_key' => 'verification',
'field_type' => 'verification',
'field_name' => 'Verification',
'field_description' => '(This is for preventing spam)',
'field_sequence' => 1
);
// Insert the submit field
$wpdb->insert( $this->field_table_name, $verification_fieldset );
$verify_fieldset_parent_id = $wpdb->insert_id;
$secret = array(
'form_id' => $new_form_selected,
'field_key' => 'secret',
'field_type' => 'secret',
'field_name' => 'Please enter any two digits with no spaces (Example: 12)',
'field_size' => 'medium',
'field_required' => 'yes',
'field_parent' => $verify_fieldset_parent_id,
'field_sequence' => 2
);
// Insert the submit field
$wpdb->insert( $this->field_table_name, $secret );
// Make the submit last in the sequence
$submit = array(
'form_id' => $new_form_selected,
'field_key' => 'submit',
'field_type' => 'submit',
'field_name' => 'Submit',
'field_parent' => $verify_fieldset_parent_id,
'field_sequence' => 3
);
// Insert the submit field
$wpdb->insert( $this->field_table_name, $submit );
// Redirect to keep the URL clean (use AJAX in the future?)
wp_redirect( 'admin.php?page=visual-form-builder&form=' . $new_form_selected );
exit();
break;
case 'update_form' :
$form_id = absint( $_REQUEST['form_id'] );
$form_key = sanitize_title( $_REQUEST['form_title'], $form_id );
$form_title = esc_html( $_REQUEST['form_title'] );
$form_subject = esc_html( $_REQUEST['form_email_subject'] );
$form_to = serialize( array_map( 'sanitize_email', $_REQUEST['form_email_to'] ) );
$form_from = esc_html( sanitize_email( $_REQUEST['form_email_from'] ) );
$form_from_name = esc_html( $_REQUEST['form_email_from_name'] );
$form_from_override = esc_html( $_REQUEST['form_email_from_override'] );
$form_from_name_override = esc_html( $_REQUEST['form_email_from_name_override'] );
$form_success_type = esc_html( $_REQUEST['form_success_type'] );
$form_notification_setting = isset( $_REQUEST['form_notification_setting'] ) ? esc_html( $_REQUEST['form_notification_setting'] ) : '';
$form_notification_email_name = isset( $_REQUEST['form_notification_email_name'] ) ? esc_html( $_REQUEST['form_notification_email_name'] ) : '';
$form_notification_email_from = isset( $_REQUEST['form_notification_email_from'] ) ? sanitize_email( $_REQUEST['form_notification_email_from'] ) : '';
$form_notification_email = isset( $_REQUEST['form_notification_email'] ) ? esc_html( $_REQUEST['form_notification_email'] ) : '';
$form_notification_subject = isset( $_REQUEST['form_notification_subject'] ) ? esc_html( $_REQUEST['form_notification_subject'] ) : '';
$form_notification_message = isset( $_REQUEST['form_notification_message'] ) ? wp_richedit_pre( $_REQUEST['form_notification_message'] ) : '';
$form_notification_entry = isset( $_REQUEST['form_notification_entry'] ) ? esc_html( $_REQUEST['form_notification_entry'] ) : '';
$form_label_alignment = esc_html( $_REQUEST['form_label_alignment'] );
// Add confirmation based on which type was selected
switch ( $form_success_type ) {
case 'text' :
$form_success_message = wp_richedit_pre( $_REQUEST['form_success_message_text'] );
break;
case 'page' :
$form_success_message = esc_html( $_REQUEST['form_success_message_page'] );
break;
case 'redirect' :
$form_success_message = esc_html( $_REQUEST['form_success_message_redirect'] );
break;
}
check_admin_referer( 'update_form-' . $form_id );
$newdata = array(
'form_key' => $form_key,
'form_title' => $form_title,
'form_email_subject' => $form_subject,
'form_email_to' => $form_to,
'form_email_from' => $form_from,
'form_email_from_name' => $form_from_name,
'form_email_from_override' => $form_from_override,
'form_email_from_name_override' => $form_from_name_override,
'form_success_type' => $form_success_type,
'form_success_message' => $form_success_message,
'form_notification_setting' => $form_notification_setting,
'form_notification_email_name' => $form_notification_email_name,
'form_notification_email_from' => $form_notification_email_from,
'form_notification_email' => $form_notification_email,
'form_notification_subject' => $form_notification_subject,
'form_notification_message' => $form_notification_message,
'form_notification_entry' => $form_notification_entry,
'form_label_alignment' => $form_label_alignment
);
$where = array( 'form_id' => $form_id );
// Update form details
$wpdb->update( $this->form_table_name, $newdata, $where );
// Initialize field sequence
$field_sequence = 0;
// Loop through each field and update all at once
if ( !empty( $_REQUEST['field_id'] ) ) {
foreach ( $_REQUEST['field_id'] as $id ) {
$field_name = ( isset( $_REQUEST['field_name-' . $id] ) ) ? esc_html( $_REQUEST['field_name-' . $id] ) : '';
$field_key = sanitize_title( $field_name, $id );
$field_desc = ( isset( $_REQUEST['field_description-' . $id] ) ) ? esc_html( $_REQUEST['field_description-' . $id] ) : '';
$field_options = ( isset( $_REQUEST['field_options-' . $id] ) ) ? serialize( array_map( 'esc_html', $_REQUEST['field_options-' . $id] ) ) : '';
$field_validation = ( isset( $_REQUEST['field_validation-' . $id] ) ) ? esc_html( $_REQUEST['field_validation-' . $id] ) : '';
$field_required = ( isset( $_REQUEST['field_required-' . $id] ) ) ? esc_html( $_REQUEST['field_required-' . $id] ) : '';
$field_size = ( isset( $_REQUEST['field_size-' . $id] ) ) ? esc_html( $_REQUEST['field_size-' . $id] ) : '';
$field_css = ( isset( $_REQUEST['field_css-' . $id] ) ) ? esc_html( $_REQUEST['field_css-' . $id] ) : '';
$field_layout = ( isset( $_REQUEST['field_layout-' . $id] ) ) ? esc_html( $_REQUEST['field_layout-' . $id] ) : '';
$field_default = ( isset( $_REQUEST['field_default-' . $id] ) ) ? esc_html( $_REQUEST['field_default-' . $id] ) : '';
$field_data = array(
'field_key' => $field_key,
'field_name' => $field_name,
'field_description' => $field_desc,
'field_options' => $field_options,
'field_validation' => $field_validation,
'field_required' => $field_required,
'field_size' => $field_size,
'field_css' => $field_css,
'field_layout' => $field_layout,
'field_sequence' => $field_sequence,
'field_default' => $field_default
);
$where = array(
'form_id' => $_REQUEST['form_id'],
'field_id' => $id
);
// Update all fields
$wpdb->update( $this->field_table_name, $field_data, $where );
$field_sequence++;
}
// Check if a submit field type exists for backwards compatibility upgrades
$is_verification = $wpdb->get_var( $wpdb->prepare( "SELECT field_id FROM $this->field_table_name WHERE field_type = 'verification' AND form_id = %d", $form_id ) );
$is_secret = $wpdb->get_var( $wpdb->prepare( "SELECT field_id FROM $this->field_table_name WHERE field_type = 'secret' AND form_id = %d", $form_id ) );
$is_submit = $wpdb->get_var( $wpdb->prepare( "SELECT field_id FROM $this->field_table_name WHERE field_type = 'submit' AND form_id = %d", $form_id ) );
// Decrement sequence
$field_sequence--;
$verification_id = '';
// If this form doesn't have a verification field, add one
if ( $is_verification == NULL ) {
// Adjust the sequence
$verification_fieldset = array(
'form_id' => $form_id,
'field_key' => 'verification',
'field_type' => 'verification',
'field_name' => 'Verification',
'field_sequence' => $field_sequence
);
// Insert the verification fieldset
$wpdb->insert( $this->field_table_name, $verification_fieldset );
$verification_id = $wpdb->insert_id;
}
// If the verification field was inserted, use that ID as a parent otherwise set no parent
$verify_fieldset_parent_id = ( $verification_id !== false ) ? $verification_id : 0;
// If this form doesn't have a secret field, add one
if ( $is_secret == NULL ) {
// Adjust the sequence
$secret = array(
'form_id' => $form_id,
'field_key' => 'secret',
'field_type' => 'secret',
'field_name' => 'Please enter any two digits with no spaces (Example: 12)',
'field_size' => 'medium',
'field_required' => 'yes',
'field_parent' => $verify_fieldset_parent_id,
'field_sequence' => ++$field_sequence
);
// Insert the submit field
$wpdb->insert( $this->field_table_name, $secret );
}
// If this form doesn't have a submit field, add one
if ( $is_submit == NULL ) {
// Make the submit last in the sequence
$submit = array(
'form_id' => $form_id,
'field_key' => 'submit',
'field_type' => 'submit',
'field_name' => 'Submit',
'field_parent' => $verify_fieldset_parent_id,
'field_sequence' => ++$field_sequence
);
// Insert the submit field
$wpdb->insert( $this->field_table_name, $submit );
}
else {
// Only update the Submit's parent ID if the Verification field is new
$data = ( $is_verification == NULL ) ? array( 'field_parent' => $verify_fieldset_parent_id, 'field_sequence' => ++$field_sequence ) : array( 'field_sequence' => $field_sequence );
$where = array(
'form_id' => $form_id,
'field_id' => $is_submit
);
// Update the submit field
$wpdb->update( $this->field_table_name, $data, $where );
}
}
break;
case 'delete_form' :
$id = absint( $_REQUEST['form'] );
check_admin_referer( 'delete-form-' . $id );
// Delete form and all fields
$wpdb->query( $wpdb->prepare( "DELETE FROM $this->form_table_name WHERE form_id = %d", $id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM $this->field_table_name WHERE form_id = %d", $id ) );
// Redirect to keep the URL clean (use AJAX in the future?)
wp_redirect( add_query_arg( 'action', 'deleted', 'admin.php?page=visual-form-builder' ) );
exit();
break;
case 'copy_form' :
$id = absint( $_REQUEST['form'] );
check_admin_referer( 'copy-form-' . $id );
// Get all fields and data for the request form
$fields = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $this->field_table_name WHERE form_id = %d", $id ) );
$forms = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $this->form_table_name WHERE form_id = %d", $id ) );
$override = $wpdb->get_var( $wpdb->prepare( "SELECT form_email_from_override, form_email_from_name_override, form_notification_email FROM $this->form_table_name WHERE form_id = %d", $id ) );
$from_name = $wpdb->get_var( null, 1 );
$notify = $wpdb->get_var( null, 2 );
// Copy this form and force the initial title to denote a copy
foreach ( $forms as $form ) {
$data = array(
'form_key' => sanitize_title( $form->form_key . ' copy' ),
'form_title' => $form->form_title . ' Copy',
'form_email_subject' => $form->form_email_subject,
'form_email_to' => $form->form_email_to,
'form_email_from' => $form->form_email_from,
'form_email_from_name' => $form->form_email_from_name,
'form_email_from_override' => $form->form_email_from_override,
'form_email_from_name_override' => $form->form_email_from_name_override,
'form_success_type' => $form->form_success_type,
'form_success_message' => $form->form_success_message,
'form_notification_setting' => $form->form_notification_setting,
'form_notification_email_name' => $form->form_notification_email_name,
'form_notification_email_from' => $form->form_notification_email_from,
'form_notification_email' => $form->form_notification_email,
'form_notification_subject' => $form->form_notification_subject,
'form_notification_message' => $form->form_notification_message,
'form_notification_entry' => $form->form_notification_entry,
'form_label_alignment' => $form->form_label_alignment
);
$wpdb->insert( $this->form_table_name, $data );
}
// Get form ID to add our first field
$new_form_selected = $wpdb->insert_id;
// Copy each field and data
foreach ( $fields as $field ) {
$data = array(
'form_id' => $new_form_selected,
'field_key' => $field->field_key,
'field_type' => $field->field_type,
'field_name' => $field->field_name,
'field_description' => $field->field_description,
'field_options' => $field->field_options,
'field_sequence' => $field->field_sequence,
'field_validation' => $field->field_validation,
'field_required' => $field->field_required,
'field_size' => $field->field_size,
'field_css' => $field->field_css,
'field_layout' => $field->field_layout,
'field_parent' => $field->field_parent
);
$wpdb->insert( $this->field_table_name, $data );
// If a parent field, save the old ID and the new ID to update new parent ID
if ( in_array( $field->field_type, array( 'fieldset', 'section', 'verification' ) ) )
$parents[ $field->field_id ] = $wpdb->insert_id;
if ( $override == $field->field_id )
$wpdb->update( $this->form_table_name, array( 'form_email_from_override' => $wpdb->insert_id ), array( 'form_id' => $new_form_selected ) );
if ( $from_name == $field->field_id )
$wpdb->update( $this->form_table_name, array( 'form_email_from_name_override' => $wpdb->insert_id ), array( 'form_id' => $new_form_selected ) );
if ( $notify == $field->field_id )
$wpdb->update( $this->form_table_name, array( 'form_notification_email' => $wpdb->insert_id ), array( 'form_id' => $new_form_selected ) );
}
// Loop through our parents and update them to their new IDs
foreach ( $parents as $k => $v ) {
$wpdb->update( $this->field_table_name, array( 'field_parent' => $v ), array( 'form_id' => $new_form_selected, 'field_parent' => $k ) );
}
break;
}
}
}
/**
* The jQuery field sorting callback
*
* @since 1.0
*/
public function process_sort_callback() {
global $wpdb;
$data = array();
foreach ( $_REQUEST['order'] as $k ) {
if ( 'root' !== $k['item_id'] ) {
$data[] = array(
'field_id' => $k['item_id'],
'parent' => $k['parent_id']
);
}
}
foreach ( $data as $k => $v ) {
// Update each field with it's new sequence and parent ID
$wpdb->update( $this->field_table_name, array( 'field_sequence' => $k, 'field_parent' => $v['parent'] ), array( 'field_id' => $v['field_id'] ) );
}
die(1);
}
/**
* The jQuery create field callback
*
* @since 1.9
*/
public function create_field_callback() {
global $wpdb;
$data = array();
$field_options = $field_validation = '';
foreach ( $_REQUEST['data'] as $k ) {
$data[ $k['name'] ] = $k['value'];
}
if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'toplevel_page_visual-form-builder' && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'visual_form_builder_create_field' ) {
$form_id = absint( $data['form_id'] );
$field_key = sanitize_title( $_REQUEST['field_type'] );
$field_name = esc_html( $_REQUEST['field_type'] );
$field_type = strtolower( sanitize_title( $_REQUEST['field_type'] ) );
// Set defaults for validation