-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact-page.php
169 lines (137 loc) · 6.65 KB
/
contact-page.php
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
<?php
session_start();
if (!isset($_SESSION["visits"]))
$_SESSION["visits"] = 0;
?>
<?php get_header(); ?>
<div class="content-column"><?php debug_location("Contact-Page"); ?>
<noscript>
<meta http-equiv="refresh" content="0;URL='/'" />
</noscript>
<article class="post image-container has-thumbnail">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<?php
$agr_options = get_option("agr_options");
$site_key = $agr_options["site_key"];
$secret_key = $agr_options["secret_key"];
$data_token = md5( uniqid( rand(), true ) );
// Process the form upon submission
if (isset($_POST["contact_submit"])) {
// Get the form data
$name = sanitize_text_field($_POST["contact_name"]);
$email = sanitize_email($_POST["contact_email"]);
$message = sanitize_textarea_field($_POST["contact_message"]);
// Validate the form data
$errors = [];
if (empty($name)) {
$errors[] = "Please enter your name.";
}
if (!is_email($email)) {
$errors[] = "Please enter a valid email address.";
}
if (empty($message)) {
$errors[] = "Please enter a message.";
}
// If there are no errors, send the email
if (empty($errors)) {
// echo "Visits: ";
// echo $_SESSION["visits"];
if ($_SESSION["visits"] > 1){?>
<p class="contact-success align-center">Your message was already sent!</p><?php
} else {
// Send the email
$_SESSION["visits"] = 100;
if (null !== get_bloginfo("name")){
$site_title = get_bloginfo("name");
}
$mail_options = get_option("wp_mail_smtp");
if (isset($mail_options["from_name"])) {
$mail_from_name = $mail_options["from_name"];
} else {
$mail_from_name = "";
}
if (isset($mail_options["from_email"])) {
$mail_from_email = $mail_options["from_email"];
} else {
$mail_from_email = "[email protected]";
}
// Set the email headers
$headers = [
"From: " . $mail_from_name . " <" . $mail_from_email . ">",
"Reply-To: " . $name . " <" . $email . ">",
];
$body = "Name: " . $name . "\n";
$body .= "eMail: " . $email . "\n";
$body .= "Message: " . $message;
wp_mail(
get_option("admin_email"),
$site_title . " - Contact info",
$body,
$headers
);?>
<!-- // Show a success message -->
<p class="contact-success align-center">Your message has been sent. Thank you!</p><?php
}
} else {
$_SESSION["visits"] = 0;
echo '<p class="contact-success align-center">All fields required (Not sent).</p>';
// Show the form with the error messages
echo '<div class="contact-form align-center">';
echo '<form id="form-contact" class="wpforms-validate wpforms-form wpforms-ajax-form" data-formid="86" method="post" enctype="multipart/form-data" action="/contact-me/" data-token="' . $data_token .'">';
echo '<p><label for="contact_name">Name:</label></br><input type="text" id="contact_name" name="contact_name" value="' . esc_attr($name) . '"></p>';
echo '<p><label for="contact_email">Email:</label></br><input type="email" id="contact_email" name="contact_email" value="' . esc_attr($email) . '"></p>';
echo '<p><label for="contact_message">Message:</label></br><textarea id="contact_message" name="contact_message">' . esc_textarea($message) . "</textarea></p>";
?>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="<?php echo $site_key; ?>"></div>
<input type="submit" value="Send Message" name="contact_submit" >
</form><?php
echo "</form>";
echo "</div>";
}
} else {
$_SESSION["visits"] = 0;
if (have_posts()) {
the_content();
}
// Show the form
echo '<div class="contact-form align-center">';
echo '<form id="form-contact" class="wpforms-validate wpforms-form wpforms-ajax-form" data-formid="86" method="post" enctype="multipart/form-data" action="/contact-me/" data-token="' . $data_token .'">';
echo '<p><label for="contact_name">Name:</label></br><input type="text" id="contact_name" name="contact_name"></p>';
echo '<p><label for="contact_email">Email:</label></br><input type="email" id="contact_email" name="contact_email"></p>';
echo '<p><label for="contact_message">Message:</label></br><textarea id="contact_message" name="contact_message"></textarea></p>';
?>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="<?php echo $site_key; ?>"></div>
<input type="submit" value="Send Message" name="contact_submit">
</form><?php
echo "</form>";
echo "</div>";
?>
<script>
var form = document.getElementById("form-contact");
form.onsubmit = function() {
var response = grecaptcha.getResponse();
if(response.length == 0) {
alert("Please complete the reCAPTCHA");
return false;
} else {
// Add the reCAPTCHA response to the form data
var formData = new FormData(form);
formData.append("g-recaptcha-response", response);
// Submit the form
var xhr = new XMLHttpRequest();
xhr.open("POST", form.action);
xhr.send(formData);
}
};
</script><?php
}
?>
</article>
</div>
<?php
get_sidebar();
get_footer();
?>