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;
 }
Exemple #2
0
						<input type="submit" class="ps-forms-button" name="submit" value="Save Options">
					</div>
				
				</td>
			</tr>

		</tbody>
	</table>

	<?php 
if ($current_form_name) {
    ?>

		<?php 
    $settings = Ps_forms_admin::get_form_settings($current_form_name);
    ?>

		<table class="widefat" id="ps_submission_settings">
			<tbody>



				<tr>
					<td class="label">
						<h3>Submission form settings</h3>
						<p>Here you can set some form settings.</p>
					</td>
					<td></td>

				</tr>