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';
/* 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_menu', array( &$this, 'save' ) );
add_action( 'wp_ajax_visual_form_builder_process_sort', array( &$this, 'visual_form_builder_process_sort_callback' ) );
add_action( 'admin_init', array( &$this, 'add_visual_form_builder_contextual_help' ) );
add_action( 'admin_init', array( &$this, 'export_entries' ) );
/* Load the includes files */
add_action( 'plugins_loaded', array( &$this, 'includes' ) );
/* Adds a Screen Options tab to the Entries screen */
add_action( 'admin_init', array( &$this, 'save_screen_options' ) );
add_filter( 'screen_settings', array( &$this, 'add_visual_form_builder_screen_options' ) );
/* Adds a Settings link to the Plugins page */
add_filter( 'plugin_action_links', array( &$this, 'visual_form_builder_plugin_action_links' ), 10, 2 );
/* Load the nav-menu CSS if we're on our plugin page */
if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'visual-form-builder' )
wp_admin_css( 'nav-menu' );
/* 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 ( get_option( 'vfb_db_version' ) != $this->vfb_db_version ) {
update_option( 'vfb_db_version', $this->vfb_db_version );
$this->install_db();
}
/* Load the jQuery and CSS we need if we're on our plugin page */
add_action( 'load-settings_page_visual-form-builder', array( &$this, 'form_admin_scripts' ) );
add_action( 'load-settings_page_visual-form-builder', array( &$this, 'form_admin_css' ) );
}
add_shortcode( 'vfb', array( &$this, 'form_code' ) );
add_action( 'init', array( &$this, 'email' ), 10 );
add_action( 'init', array( &$this, 'confirmation' ), 12 );
/* Add jQuery and CSS to the front-end */
add_action( 'wp_head', array( &$this, 'form_css' ) );
add_action( 'template_redirect', array( &$this, 'form_validation' ) );
}
/**
* Adds extra include files
*
* @since 1.2
*/
public function includes(){
/* Load the Entries List class */
require_once( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'class-entries-list.php' );
/* Load the Entries Details class */
require_once( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'class-entries-detail.php' );
}
/**
* Register contextual help. This is for the Help tab dropdown
*
* @since 1.0
*/
public function add_visual_form_builder_contextual_help(){
$text = "
Getting Started
Click on the + tab, give your form a name and click Create Form.
Select form fields from the box on the left and click a field to add it to your form.
Edit the information for each form field by clicking on the down arrow.
Drag and drop the elements to put them in order.
Click Save Form to save your changes.
Form Item Configuration
Name will change the display name of your form input.
Description will be displayed below the associated input.
Validation allows you to select from several of jQuery's Form Validation methods for text inputs. For more about the types of validation, read the Validation section below.
Required 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.
Options will only be active for Radio and Checkboxes. This field contols how many options are available for the associated input. Multiple options must be separated by commas (ex: Option 1, Option 2, Option 3).
Size 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.
Number: makes the element require a decimal number.
Digits: makes the element require digits only.
Phone: makes the element require a US or International phone number. Most formats are accepted.
Time: choose either 12- or 24-hour time format (NOTE: only available with the Time field).
Confirmation
Each form allows you to customize the confirmation by selecing either a Text Message, a WordPress Page, or to Redirect to a URL.
Text allows you to enter a custom formatted message that will be displayed on the page after your form is submitted. HTML is allowed here.
Page displays a dropdown of all WordPress Pages you have created. Select one to redirect the user to that page after your form is submitted.
Redirect will only accept URLs and can be used to send the user to a different site completely, if you choose.
Tips
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.
Security verification is automatically included on very form. It's a simple logic question and should keep out most, if not all, spam bots.
There is a hidden spam field, known as a honey pot, that should also help deter potential abusers of your form.
";
add_contextual_help( 'settings_page_visual-form-builder', $text );
}
/**
* Adds the Screen Options tab to the Entries screen
*
* @since 1.2
*/
public function add_visual_form_builder_screen_options($current){
global $current_screen;
$options = get_option( 'visual-form-builder-screen-options' );
if ( $current_screen->id == 'settings_page_visual-form-builder' && isset( $_REQUEST['view'] ) && in_array( $_REQUEST['view'], array( 'entries' ) ) ){
$current = '
Show on screen
';
}
return $current;
}
/**
* Saves the Screen Options
*
* @since 1.2
*/
public function save_screen_options(){
$options = get_option( 'visual-form-builder-screen-options' );
/* Default is 20 per page */
$defaults = array(
'per_page' => 20
);
/* If the option doesn't exist, add it with defaults */
if ( !$options )
update_option( 'visual-form-builder-screen-options', $defaults );
/* If the user has saved the Screen Options, update */
if ( isset( $_REQUEST['visual-form-builder-screen-options-apply'] ) && in_array( $_REQUEST['visual-form-builder-screen-options-apply'], array( 'Apply', 'apply' ) ) ) {
$per_page = absint( $_REQUEST['visual-form-builder-screen-options']['per_page'] );
$updated_options = array(
'per_page' => $per_page
);
update_option( 'visual-form-builder-screen-options', $updated_options );
}
}
/**
* Runs the export_entries function in the class-entries-list.php file
*
* @since 1.4
*/
public function export_entries() {
$entries = new VisualFormBuilder_Entries_List();
/* If exporting all, don't pass the IDs */
if ( 'export-all' === $entries->current_action() )
$entries->export_entries();
/* If exporting selected, pick up the ID array and pass them */
elseif ( 'export-selected' === $entries->current_action() ) {
$entry_id = ( is_array( $_REQUEST['entry'] ) ) ? $_REQUEST['entry'] : array( $_REQUEST['entry'] );
$entries->export_entries( $entry_id );
}
}
/**
* 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 VARCHAR(255) NOT NULL,
field_sequence TINYINT DEFAULT '0',
field_validation VARCHAR(25),
field_required VARCHAR(25),
field_size VARCHAR(25),
UNIQUE 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 VARCHAR(255),
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,
UNIQUE 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 TEXT NOT NULL,
subject TEXT,
sender_name VARCHAR(255),
sender_email VARCHAR(25),
emails_to VARCHAR(255),
date_submitted VARCHAR(25),
ip_address VARCHAR(25),
UNIQUE 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 CSS for admin styles
*
* @since 1.0
*/
public function form_admin_css(){
wp_enqueue_style( 'visual-form-builder-style', plugins_url( 'visual-form-builder' ) . '/css/visual-form-builder-admin.css' );
}
/**
* Queue plugin scripts for sorting form fields
*
* @since 1.0
*/
public function form_admin_scripts() {
wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_script( 'jquery-form-validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'form-elements-add', plugins_url( 'visual-form-builder' ) . '/js/visual-form-builder.js' , array( 'jquery', 'jquery-form-validation' ), '', true );
}
/**
* Queue form validation scripts
*
* @since 1.0
*/
public function form_validation(){
wp_enqueue_script( 'jquery-form-validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'jquery-ui-core ', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js', array( 'jquery' ), '', true );
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-quicktags', plugins_url( 'visual-form-builder' ) . '/js/js_quicktags.js' );
}
/**
* Add form CSS to wp_head
*
* @since 1.0
*/
public function form_css(){
echo apply_filters( 'visual-form-builder-css', '' );
echo apply_filters( 'vfb-date-picker-css', '' );
}
/**
* Add Settings link to Plugins page
*
* @since 1.8
* @return $links array Links to add to plugin name
*/
public function visual_form_builder_plugin_action_links($links, $file){
if ( $file == plugin_basename(__FILE__) )
$links[] = ''.__('Settings').'';
return $links;
}
/**
* Add options page to Settings menu
*
*
* @since 1.0
* @uses add_options_page() Creates a menu item under the Settings menu.
*/
public function add_admin() {
add_options_page( __('Visual Form Builder', 'visual-form-builder'), __('Visual Form Builder', 'visual-form-builder'), 'create_users', 'visual-form-builder', array( &$this, 'admin' ) );
}
/**
* Actions to save, update, and delete forms/form fields
*
*
* @since 1.0
*/
public function save() {
global $wpdb;
if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] == 'visual-form-builder' && isset( $_REQUEST['action'] ) ) {
switch ( $_REQUEST['action'] ) {
case 'create_form' :
$form_id = absint( $_REQUEST['form_id'] );
$form_key = sanitize_title( $_REQUEST['form_title'] );
$form_title = esc_html( $_REQUEST['form_title'] );
check_admin_referer( 'create_form-' . $form_id );
$newdata = array(
'form_key' => $form_key,
'form_title' => $form_title
);
/* Set message to display */
$this->message = '
';
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 ) );
/* Set message to display */
$this->message = '
This form has been deleted.
';
/* Redirect to keep the URL clean (use AJAX in the future?) */
wp_redirect( 'options-general.php?page=visual-form-builder' );
exit();
break;
case 'delete_field' :
$form_id = absint( $_REQUEST['form'] );
$field_id = absint( $_REQUEST['field'] );
check_admin_referer( 'delete-field-' . $form_id );
/* Delete the field */
$wpdb->query( $wpdb->prepare( "DELETE FROM $this->field_table_name WHERE field_id = %d", $field_id ) );
/* Set message to display */
$this->message = '
The field has been deleted.
';
/* Redirect to keep the URL clean (use AJAX in the future?) */
wp_redirect( 'options-general.php?page=visual-form-builder&form=' . $form_id );
exit();
break;
case 'create_field' :
$form_id = absint( $_REQUEST['form_id'] );
$field_key = sanitize_title( $_REQUEST['field_name'] );
$field_name = esc_html( $_REQUEST['field_type'] );
$field_type = strtolower( sanitize_title( $_REQUEST['field_type'] ) );
/* Set defaults for validation */
switch ( $field_type ) {
case 'email' :
case 'url' :
case 'phone' :
$field_validation = $field_type;
break;
case 'currency' :
$field_validation = 'number';
break;
case 'number' :
$field_validation = 'digits';
break;
case 'time' :
$field_validation = 'time-12';
break;
}
check_admin_referer( 'create-field-' . $form_id );
/* Get the last row's sequence */
$sequence_last_row = $wpdb->get_row( "SELECT field_sequence FROM $this->field_table_name WHERE form_id = $form_id ORDER BY field_sequence DESC LIMIT 1" );
/* If it's not the first for this form, add 1 */
$field_sequence = ( !empty( $sequence_last_row ) ) ? $sequence_last_row->field_sequence + 1 : 0;
$newdata = array(
'form_id' => absint( $_REQUEST['form_id'] ),
'field_key' => $field_key,
'field_name' => $field_name,
'field_type' => $field_type,
'field_sequence' => $field_sequence,
'field_validation' => $field_validation
);
/* Create the field */
$wpdb->insert( $this->field_table_name, $newdata );
break;
}
}
}
/**
* The jQuery field sorting callback
*
* @since 1.0
*/
public function visual_form_builder_process_sort_callback() {
global $wpdb;
/* Get the order of the fields as make an array */
$order = explode( ',', $_REQUEST['order'] );
foreach ( $order as $k => $v ) {
/* Find the digits from each field */
preg_match( '/(\d+)/', $v, $matches );
/* Update each field with it's new sequence */
$wpdb->update( $this->field_table_name, array( 'field_sequence' => $k ), array( 'field_id' => $matches[0] ) );
}
die(1);
}
/**
* Builds the options settings page
*
* @since 1.0
*/
public function admin() {
global $wpdb;
/* Set variables depending on which tab is selected */
$form_nav_selected_id = ( isset( $_REQUEST['form'] ) ) ? $_REQUEST['form'] : '0';
$action = ( isset( $_REQUEST['form'] ) && $_REQUEST['form'] !== '0' ) ? 'update_form' : 'create_form';
$details_meta = ( isset( $_REQUEST['details'] ) ) ? $_REQUEST['details'] : 'email';
/* Query to get all forms */
$order = sanitize_sql_orderby( 'form_id DESC' );
$query = "SELECT * FROM $this->form_table_name ORDER BY $order";
/* Build our forms as an object */
$forms = $wpdb->get_results( $query );
/* Loop through each form and assign a form id, if any */
foreach ( $forms as $form ) {
$form_id = ( $form_nav_selected_id == $form->form_id ) ? $form->form_id : '';
/* If we are on a form, set the form name for the shortcode box */
if ( $form_nav_selected_id == $form->form_id )
$form_name = stripslashes( $form->form_title );
}
?>
';
}
}
}
}
/* Setup our entries data */
$entry = array(
'form_id' => $form_id,
'data' => serialize( $fields ),
'subject' => $form_subject,
'sender_name' => $form_from_name,
'sender_email' => $form_from,
'emails_to' => serialize( $form_to ),
'date_submitted' => date_i18n( 'Y-m-d G:i:s' ),
'ip_address' => $_SERVER['REMOTE_ADDR']
);
/* Insert this data into the entries table */
$wpdb->insert( $this->entries_table_name, $entry );
/* Close out the content */
$message .= '
';
/* Set headers to send an HTML email */
$headers = "MIME-Version: 1.0\n".
"From: " . $form_from_name . " <" . $form_from . ">\n" .
"Content-Type: text/html; charset=\"" . get_settings( 'blog_charset' ) . "\"\n";
/* Send the mail */
foreach ( $form_to as $email ) {
$mail_sent = wp_mail( $email, esc_html( $form_subject ), $message, $headers, $attachments );
}
elseif ( isset( $_REQUEST['visual-form-builder-submit'] ) ) :
/* If any of the security checks fail, provide some user feedback */
if ( $_REQUEST['vfb-spam'] !== '' || !is_numeric( $_REQUEST['vfb-secret'] ) || strlen( $_REQUEST['vfb-secret'] ) !== 2 )
wp_die( 'Ooops! Looks like you have failed the security validation for this form. Please go back and try again.' );
endif;
}
}
/* On plugin activation, install the databases and add/update the DB version */
register_activation_hook( __FILE__, array( 'Visual_Form_Builder', 'install_db' ) );
?>