Browse Source

Improve contact form logic.

pull/13/head
Ned Zimmerman 7 years ago
parent
commit
b30bb8902e
No known key found for this signature in database
GPG Key ID: FF56334A013120CA
  1. 53
      app/controllers/App.php
  2. 59
      app/helpers.php
  3. 5
      app/setup.php

53
app/controllers/App.php

@ -65,57 +65,4 @@ class App extends Controller
return __('Contact Us', 'aldine');
}
public function contactFormResponse()
{
if (isset($_POST['submitted'])) {
$output = [];
$name = (isset($_POST['visitor_name'])) ? $_POST['visitor_name'] : false;
$email = (isset($_POST['visitor_email'])) ? $_POST['visitor_email'] : false;
$institution = (isset($_POST['visitor_institution'])) ? $_POST['visitor_institution'] : false;
$message = (isset($_POST['message'])) ? $_POST['message'] : false;
if (!$name) {
$output['message'] = __('Name is required.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'visitor_name';
} elseif (!$email) {
$output['message'] = __('Email is required.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'visitor_email';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$output['message'] = __('Email is invalid.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'visitor_email';
} elseif (!$institution) {
$output['message'] = __('Institution is required.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'visitor_institution';
} elseif (!$message) {
$output['message'] = __('Message is required.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'message';
} else {
$sent = wp_mail(
get_option('admin_email'),
__('Contact Form: ', 'aldine') . $name,
sprintf(
"From: %1\$s <%2\$s>\n%3\$s",
$name,
$email,
strip_tags($message)
),
"From: ${email}\r\nReply-To: ${email}\r\n"
);
if ($sent) {
$output['message'] = __('Your message was sent!', 'aldine');
$output['status'] = 'success';
} else {
$output['message'] = __('Your message could not be sent.', 'aldine');
$output['status'] = 'error';
}
}
return $output;
}
return false;
}
}

59
app/helpers.php

@ -144,3 +144,62 @@ function remove_hash($color)
{
return ltrim($color, '#');
}
/**
*
* Catch a contact form submission.
*
* @return false | array
*/
function contact_form_submission()
{
if (isset($_POST['submitted'])) {
$output = [];
$name = (isset($_POST['visitor_name'])) ? $_POST['visitor_name'] : false;
$email = (isset($_POST['visitor_email'])) ? $_POST['visitor_email'] : false;
$institution = (isset($_POST['visitor_institution'])) ? $_POST['visitor_institution'] : false;
$message = (isset($_POST['message'])) ? $_POST['message'] : false;
if (!$name) {
$output['message'] = __('Name is required.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'visitor_name';
} elseif (!$email) {
$output['message'] = __('Email is required.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'visitor_email';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$output['message'] = __('Email is invalid.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'visitor_email';
} elseif (!$institution) {
$output['message'] = __('Institution is required.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'visitor_institution';
} elseif (!$message) {
$output['message'] = __('Message is required.', 'aldine');
$output['status'] = 'error';
$output['field'] = 'message';
} else {
$sent = wp_mail(
get_option('admin_email'),
sprintf(__('Contact Form Submission from %s', 'aldine'), $name),
sprintf(
"From: %1\$s <%2\$s>\n%3\$s",
$name,
$email,
strip_tags($message)
),
"From: ${email}\r\nReply-To: ${email}\r\n"
);
if ($sent) {
$output['message'] = __('Your message was sent!', 'aldine');
$output['status'] = 'success';
} else {
$output['message'] = __('Your message could not be sent.', 'aldine');
$output['status'] = 'error';
}
}
return $output;
}
return false;
}

5
app/setup.php

@ -182,3 +182,8 @@ if ($header_text_color) {
}</style>
<?php }
});
add_action('wp_head', function () {
$response = contact_form_submission();
sage('blade')->share('contact_form_response', $response);
});

Loading…
Cancel
Save