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
<?php
namespace hji\AgentRoster\models;
class TestimonialsModel
{
private $submittedData = array();
private $formErrors = array();
private $testimonial_inserted = false;
public function processPostData()
{
$this->validate_post();
if (empty($this->formErrors))
{
return $this->insert_post($this->submittedData);
}
}
public function getErrors()
{
return $this->formErrors;
}
private function validate_post()
{
$required_fields = array('client_name', 'client_email', 'client_location', 'client_feedback', 'client_math');
$errors = array();
$values = array();
foreach ($_POST as $k => $v)
{
if (in_array($k, $required_fields) && empty($_POST[$k]))
{
$errors[$k] = '<span class="not-valid">This field can not be empty.</span>';
}
$values[$k] = wp_filter_nohtml_kses($v);
}
if (isset($values['input_field_731']) && !empty($values['input_field_731']))
{
$errors['honeypot'] = 'Failed!';
}
$this->submittedData = $values;
$this->formErrors = $errors;
}
private function insert_post($values)
{
if (isset($values['agent_id']))
{
$agent_id = stripslashes($values['agent_id']);
}
$name = stripslashes($values['client_name']);
$email = stripslashes($values['client_email']);
$location = stripslashes($values['client_location']);
$feedback = stripslashes($values['client_feedback']);
$postData = array();
$postData['post_title'] = $name . ' - ' . $location;
$postData['post_content'] = $feedback;
$postData['post_status'] = 'pending';
$postData['post_author'] = 1;
$postData['post_type'] = 'testimonial';
$send_to = (isset($values['send_to']) && !empty($values['send_to'])) ? esc_attr($values['send_to']) : false;
$newPost = wp_insert_post($postData);
if ($newPost)
{
if (isset($agent_id))
{
update_post_meta($newPost, '_cmb_agent_id', $agent_id);
}
$this->testimonial_inserted = true;
$this->notify_admin($send_to);
}
return $newPost;
}
private function notify_admin($send_to = false)
{
$admin_email = get_bloginfo('admin_email');
$siteurl = get_bloginfo('url');
$headers[] = 'From: ' . $siteurl . ' <' . $admin_email . '>';
if (!$send_to)
{
$send_to = $admin_email;
}
else
{
$headers[] = 'BCc: Admin <' . $admin_email . '>';
}
$subject = $siteurl . ': New Testimonial!';
$message = "There is a new testimonial waiting for your review at " . $siteurl . ".\n\r";
$message .= 'Name: ' . esc_attr($_POST['client_name']) . "\n\r";
$message .= 'Email: ' . esc_attr($_POST['client_email']) . "\n\r";
$message .= 'Moving from: ' . esc_attr($_POST['client_location']) . "\n\r";
$message .= "Testimonial\n\r" . esc_attr($_POST['client_feedback']) . "\n\r";
$message .= "Referring URL: http://" . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"] . "\r\n";
wp_mail($send_to, $subject, $message, $headers);
}
static function get_testimonials(array $args = array())
{
$params['post_type'] = 'testimonial';
$params['posts_per_page'] = -1;
$params = array_merge($params, $args);
$testimonials = new \WP_Query($params);
if ($testimonials->have_posts())
{
$output = '<h2>Testimonials</h2>';
while ($testimonials->have_posts())
{
$testimonials->the_post();
$output .= '<h3>';
$output .= get_the_title();
$output .= '</h3>';
$output .= '<p>';
$output .= get_the_content();
$output .= '</p>';
}
}
if (isset($output))
{
return $output;
}
return;
}
static function get_agent_testimonials($agent_id)
{
$params['meta_query'] = array(
array(
'key' => '_cmb_agent_id',
'value' => $agent_id,
)
);
return self::get_testimonials($params);
}
}