/** * Return an instance of this class. * * @since 1.0.0 * * @return object A single instance of this class. */ public static function get_instance() { // If the single instance hasn't been set, set it now. if (null == self::$instance) { self::$instance = new self(); } return self::$instance; }
public function ps_forms_validate_data($inputs = null, $ajax = true) { //These are temporary. Need to be settings $validation_error_message = "Sorry, some fields have yet to be filled in correctly."; $error_message = "Oops, there was an error saving this form."; $success_message = "Thank you for making an enquiry with us. We will be in touch very soon."; $too_soon_message = "You've already submitted, please wait a minute before resubmitting."; //Validation messages. //We should have defaults, that can be overwritten //in settings. For sure! $validation_messages = self::get_default_validation_messages(); global $wpdb; //Get everything coming our way from AJAX, if we're ajaxed if ($inputs == null) { $inputs = $_GET['inputs']; } $form_name = $inputs['ps-form-name']; //Get validation rules for this form $validation_rules = self::get_validation_rules($form_name); //Get rid of the submit button and form name inputs unset($inputs['ps-submit']); unset($inputs['ps-form-name']); //Remove our prefixes $temp = array(); foreach ($inputs as $key => $input) { $key = str_replace('ps-', '', $key); $temp[$key] = $input; } $inputs = $temp; //Validation Time //First we set an empty array to store errors in //The key is the field name from the form, //and the value will be validation text for returning to the view $errors = array(); //Set a flag for if the form's valid or not. $valid = true; //The form name data variable hasn't been set, so //let's return an error if (!$form_name) { $data['message'] = $error_message; $data['errors'] = $errors; $valid = false; if ($ajax == true) { echo json_encode($data); exit; } else { return $data; } } //Let's do a flood check $ip = $_SERVER['REMOTE_ADDR']; $last_submit = $wpdb->get_row("SELECT time AS latest_time FROM {$wpdb->prefix}ps_form_data WHERE ip='{$ip}' AND time > NOW() - INTERVAL 1 MINUTE"); if ($last_submit) { $data['message'] = $too_soon_message; $data['errors'] = $errors; $valid = false; if ($ajax == true) { echo json_encode($data); exit; } else { return $data; } } //Loop through all fields submitted foreach ($inputs as $name => $value) { //Check if there's a validation rule for the current field if (array_key_exists($name, $validation_rules)) { //Loop all validation rules for this name. //There might be multiple. foreach ($validation_rules[$name] as $rule) { //create our function name for checking $func = 'ps_forms_validate_' . $rule['rule']; //Just in case, let's check that this validation function exists //As we're screwing about with variable function names here if (method_exists(Ps_forms, $func)) { //Validation returned false, so set errors //And set the valid flag to false. if (!self::$func($value)) { $valid = false; if (!($errors['ps-' . $name] = $rule['message'])) { $errors['ps-' . $name] = $validation_messages[$rule['rule']]; } } } } } } //The form did not pass validation. Return the errors. if ($valid == false) { $data = array('errors' => $errors, 'message' => $validation_error_message); if ($ajax == true) { echo json_encode($data); exit; } else { return $data; } } //Submission! Get the max value from the db of submit ids //So we can increment it and add a new one $submit_id = $wpdb->get_var("SELECT max(submit_id) FROM {$wpdb->prefix}ps_form_data"); if (!$submit_id) { $submit_id = 0; } $submit_id++; //Build the beginning of a query $query = "INSERT INTO " . $wpdb->prefix . "ps_form_data (name,time, value, submit_id,form_name,ip) VALUES"; //Loop the fields and add to the MySQL and template as we go $i = 1; $table_data = ''; $alt = false; foreach ($inputs as $name => $value) { //Generate email data $table_data .= '<tr class="data-table-row'; if ($alt) { $table_data .= '-alt'; $alt = false; } else { $alt = true; } $table_data .= '"><td width="30%">' . $name . '</td><td width="70%">' . $value . '</td></tr>'; $query .= $wpdb->prepare("(%s,now(),%s,%d,%s,%s)", $name, $value, $submit_id, $form_name, $ip); if ($i != sizeof($inputs)) { $query .= ','; } else { $query .= ';'; } $i++; } require_once plugin_dir_path(__FILE__) . 'class-ps-forms-admin.php'; $settings = Ps_forms_admin::get_form_settings($form_name); //Set some defaults $html_file = dirname(__FILE__) . '/email-templates/default.html'; $css_file = dirname(__FILE__) . '/email-templates/default.css'; $image_file = ''; $email_address = get_option('admin_email'); //If settings have been set, let's pick the css and html files if ($settings !== null) { if ($settings->html_file) { $html_file = $settings->html_file; } if ($settings->css_file) { $css_file = $settings->css_file; } if ($settings->logo_id) { $image_file = wp_get_attachment_image($settings->logo_id); } if ($settings->email_address) { $email_address = $settings->email_address; } } //Let's search the html for our marked up template links and replace with our table of //form submissions. $html = file_get_contents($html_file); $css = file_get_contents($css_file); $html = str_replace('{{FORM_DATA_ROWS}}', $table_data, $html); $html = str_replace('{{SITENAME}}', get_bloginfo('name'), $html); $html = str_replace('{{FORMNAME}}', $form_name, $html); $html = str_replace('{{LOGO}}', $image_file, $html); //We'll use the csstoinlinestyles class to sort out the string require_once plugin_dir_path(__FILE__) . '/vendor/CssToInlineStyles.php'; $email_html = new CssToInlineStyles($html, $css); $email_html = $email_html->convert(); //With our magnificently created table data, we can insert this into the database and send as an email. $wpdb->query($query); //Send email $headers[] = "Content-type: text/html"; wp_mail($email_address, 'Form has been submitted', $email_html, $headers); $data = array('message' => $success_message); if ($ajax == true) { echo json_encode($data); } else { return $data; } exit; }
<?php } ?> </select> </form> <?php } ?> <a style="margin:20px 0;" class="button-primary" href="<?php echo admin_url('?page=ps-form-entries&form_name=' . stripslashes($_GET['form_name']) . '&csv=1'); ?> ">Download CSV</a> <form id="ps-entries-form-main" method="post"> <?php Ps_forms_admin::get_entries_table($current_form_name); ?> </form> <!-- TODO: Provide markup for your options page here. --> </div>
* Represents the view for the single entry page * * This includes the header, options, and other information that should provide * The User Interface to the end user. * * @package Plugin_Name * @author Your Name <*****@*****.**> * @license GPL-2.0+ * @link http://example.com * @copyright 2013 Your Name or Company Name */ ?> <div class="wrap"> <?php $data = Ps_forms_admin::get_entry($_GET['submit_id']); ?> <h2>Submission details for <?php echo $data['form_name']; ?> <a href="<?php echo admin_url('admin.php?page=ps-form-entries&form_name=' . urlencode($data['form_name'])); ?> " class="add-new-h2">Back</a></h2> <h5>Submitted on <?php echo $data['submitted']; ?> </h5>
<tr> <td class="label"> <h3>Submission form settings</h3> <p>Here you can set some form settings.</p> </td> <td></td> </tr> <tr> <td class="label"> <h3>Pick an email template</h3> <p>If you put css & html files in a folder called 'email-templates' in your root template folder you can use them here.</p> <?php $files = Ps_forms_admin::get_custom_email_templates(); ?> </td> <td></td> </tr> <tr> <td class="label"> <label for="html_file">HTML</h3> <select name="html_file"> <option value="" name="default">Default</option> <?php foreach ($files['html'] as $file) { ?> <option <?php if ($file == $settings->html_file) {