- wp_unslash dla pól formularza i opcji (koniec z \' w mailach) - panel: wymagany nonce i manage_options, sanityzacja, esc_attr - reCAPTCHA: oczekiwana akcja i próg wyniku 0.5 - From z domeny strony, adres nadawcy tylko w Reply-To - honeypot bez die(), walidacja JS nie jest omijana - komunikat o nieudanej wysyłce, zachowanie wpisanych pól - nowy wygląd formularza (klasy ic-*) i szablon maila Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
402 lines
14 KiB
PHP
402 lines
14 KiB
PHP
<?php
|
|
|
|
namespace inContact;
|
|
|
|
/**
|
|
* Klasa Shortcode
|
|
*
|
|
* @link https://krzysztof-turek.com
|
|
*
|
|
* @package tylkofotografia.pl
|
|
* @version 0.3
|
|
*/
|
|
class Shortcode
|
|
{
|
|
use Singleton;
|
|
|
|
function __construct()
|
|
{
|
|
$this->addShortcode();
|
|
}
|
|
|
|
protected function addShortcode(): void
|
|
{
|
|
add_shortcode("inContact", [$this, "inContact"]);
|
|
}
|
|
|
|
public function inContact($atts)
|
|
{
|
|
$em_name = null;
|
|
$em_mail = null;
|
|
$em_message = null;
|
|
$html = null;
|
|
$error = null;
|
|
$body = null;
|
|
$telefon = null;
|
|
$mailResult = 0;
|
|
$canSend = false;
|
|
$sendFailed = false;
|
|
|
|
$form_submitted = $_SERVER["REQUEST_METHOD"] === "POST";
|
|
|
|
global $pluginDirUrl;
|
|
|
|
// Honeypot — bot dostaje pozorny sukces, mail nie jest wysyłany
|
|
$isBot = !empty($_POST["website"]);
|
|
|
|
if ($form_submitted) {
|
|
$emmsg = isset($_POST["emmsg"]) ? $_POST["emmsg"] : "";
|
|
|
|
// Walidacja pól
|
|
if (!empty($_POST["em_name"])) {
|
|
$em_name = sanitize_text_field(wp_unslash($_POST["em_name"]));
|
|
} else {
|
|
$error .=
|
|
esc_html__("Please enter your name", "in-contact") .
|
|
"<br />";
|
|
}
|
|
if (!empty($_POST["em_mail"]) && is_email($_POST["em_mail"])) {
|
|
$em_mail = sanitize_email(wp_unslash($_POST["em_mail"]));
|
|
} else {
|
|
$error .=
|
|
esc_html__(
|
|
"Please enter your email address",
|
|
"in-contact",
|
|
) . "<br />";
|
|
}
|
|
if (!empty($_POST["em_message"])) {
|
|
$em_message = sanitize_textarea_field(
|
|
wp_unslash($_POST["em_message"]),
|
|
);
|
|
} else {
|
|
$error .=
|
|
esc_html__("Please enter your message", "in-contact") .
|
|
"<br />";
|
|
}
|
|
|
|
// Budowanie emaila
|
|
// From z domeny strony (SPF/DMARC), adres nadawcy tylko w Reply-To
|
|
$siteHost = (string) wp_parse_url(home_url(), PHP_URL_HOST);
|
|
if (strpos($siteHost, "www.") === 0) {
|
|
$siteHost = substr($siteHost, 4);
|
|
}
|
|
$headerChars = ["\"", "<", ">", "\r", "\n"];
|
|
$fromName = str_replace(
|
|
$headerChars,
|
|
"",
|
|
wp_specialchars_decode(get_bloginfo("name"), ENT_QUOTES),
|
|
);
|
|
$replyName = str_replace($headerChars, "", (string) $em_name);
|
|
$headers = [
|
|
"Content-Type: text/html; charset=UTF-8",
|
|
"From: " . $fromName . " <wordpress@" . $siteHost . ">",
|
|
"Reply-To: " . $replyName . " <" . $em_mail . ">",
|
|
];
|
|
|
|
$targetEmail = get_option("incontact-target-mail");
|
|
$emailTitle = __("New message from contact form", "in-contact");
|
|
$emailHeading = __("Contact details", "in-contact");
|
|
$emailNameLabel = __("Name and surname", "in-contact");
|
|
$emailEmailLabel = __("Email", "in-contact");
|
|
$emailMsgHeading = __("Message content", "in-contact");
|
|
$emailReplyLabel = __("Reply to message", "in-contact");
|
|
$emailFooter = __(
|
|
"This email was generated automatically by the website contact form.",
|
|
"in-contact",
|
|
);
|
|
$emailRights = __("All rights reserved.", "in-contact");
|
|
|
|
$body =
|
|
'<!DOCTYPE html>
|
|
<html lang="pl">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>' .
|
|
esc_html($emailTitle) .
|
|
'</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; background-color: #f5f5f5; margin: 0; padding: 0; }
|
|
.container { width: 90%; max-width: 600px; margin: 20px auto; background-color: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 0 10px rgba(0,0,0,.1); }
|
|
.header { background-color: #1a1a2e; color: #ffffff; padding: 20px; text-align: center; }
|
|
.header h1 { margin: 0; font-size: 20px; }
|
|
.content { padding: 24px; line-height: 1.6; }
|
|
.content h2 { margin-top: 0; color: #1a1a2e; }
|
|
.info { background-color: #f9f9f9; border-left: 4px solid #e2b04a; padding: 12px 16px; margin: 16px 0; border-radius: 0 4px 4px 0; }
|
|
.info h3 { margin: 0 0 8px; color: #1a1a2e; font-size: 14px; text-transform: uppercase; letter-spacing: .05em; }
|
|
.button { display: inline-block; padding: 10px 20px; margin-top: 20px; background-color: #e2b04a; color: #1a1a2e; text-decoration: none; border-radius: 4px; font-weight: bold; }
|
|
.footer { background-color: #f1f1f1; text-align: center; padding: 12px; font-size: 12px; color: #777; }
|
|
@media (max-width: 600px) { .container { width: 95%; } }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="header"><h1>' .
|
|
esc_html($emailTitle) .
|
|
'</h1></div>
|
|
<div class="content">
|
|
<h2>' .
|
|
esc_html($emailHeading) .
|
|
'</h2>
|
|
<p><strong>' .
|
|
esc_html($emailNameLabel) .
|
|
':</strong> {{IMIE_NAZWISKO}}</p>
|
|
<p><strong>' .
|
|
esc_html($emailEmailLabel) .
|
|
':</strong> {{EMAIL}}</p>
|
|
<div class="info">
|
|
<h3>' .
|
|
esc_html($emailMsgHeading) .
|
|
'</h3>
|
|
<p>{{WIADOMOSC}}</p>
|
|
</div>
|
|
<a href="mailto:{{EMAIL}}" class="button">' .
|
|
esc_html($emailReplyLabel) .
|
|
'</a>
|
|
</div>
|
|
<div class="footer">
|
|
' .
|
|
esc_html($emailFooter) .
|
|
'<br>
|
|
© {{ROK}} ' .
|
|
esc_html(get_bloginfo("name")) .
|
|
". " .
|
|
esc_html($emailRights) .
|
|
'
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>';
|
|
|
|
$body = str_replace(
|
|
[
|
|
"{{IMIE_NAZWISKO}}",
|
|
"{{EMAIL}}",
|
|
"{{TELEFON}}",
|
|
"{{WIADOMOSC}}",
|
|
"{{ROK}}",
|
|
],
|
|
[
|
|
esc_html($em_name),
|
|
esc_html($em_mail),
|
|
esc_html($telefon),
|
|
str_replace(
|
|
["\r\n", "\r", "\n"],
|
|
"<br />",
|
|
esc_html($em_message),
|
|
),
|
|
date("Y"),
|
|
],
|
|
$body,
|
|
);
|
|
|
|
// Obsługa błędów walidacji
|
|
if (!empty($error)) {
|
|
$html .=
|
|
'<div class="ic-alert ic-alert--error">' .
|
|
$error .
|
|
"</div>";
|
|
} elseif ($isBot) {
|
|
$mailResult = 1;
|
|
} elseif ($emmsg === "true") {
|
|
// reCAPTCHA
|
|
$gRecaptchaResponse = isset($_POST["g-recaptcha-response"])
|
|
? $_POST["g-recaptcha-response"]
|
|
: "";
|
|
$siteKey = get_option("incontact-site-key");
|
|
|
|
if ($siteKey != "") {
|
|
$secret = get_option("incontact-secret-key");
|
|
$reCaptcha = new \ReCaptcha\ReCaptcha($secret);
|
|
$resp = $reCaptcha
|
|
->setExpectedHostname($_SERVER["SERVER_NAME"])
|
|
->setExpectedAction("submit")
|
|
->setScoreThreshold(0.5)
|
|
->verify($gRecaptchaResponse, $_SERVER["REMOTE_ADDR"]);
|
|
if ($resp->isSuccess()) {
|
|
$canSend = true;
|
|
}
|
|
} else {
|
|
$canSend = true;
|
|
}
|
|
|
|
if ($canSend) {
|
|
$mailResult = wp_mail(
|
|
$targetEmail,
|
|
$em_name,
|
|
$body,
|
|
$headers,
|
|
);
|
|
}
|
|
$sendFailed = !$mailResult;
|
|
}
|
|
}
|
|
|
|
// Enqueue styles & reCAPTCHA
|
|
wp_enqueue_style(
|
|
"inContactCss",
|
|
$pluginDirUrl . "css/inContact.css?v=12",
|
|
[],
|
|
"0.3",
|
|
);
|
|
|
|
$siteKey = get_option("incontact-site-key");
|
|
if ($siteKey != "") {
|
|
wp_enqueue_script(
|
|
"recaptcha",
|
|
"https://www.google.com/recaptcha/api.js?render=" . $siteKey,
|
|
[],
|
|
null,
|
|
true,
|
|
);
|
|
|
|
wp_add_inline_script(
|
|
"recaptcha",
|
|
"
|
|
document.getElementById('ic-form').addEventListener('submit', function(e) {
|
|
if (e.defaultPrevented) return;
|
|
var token = document.getElementById('g-recaptcha-response').value;
|
|
if (!token) {
|
|
e.preventDefault();
|
|
grecaptcha.ready(function() {
|
|
grecaptcha.execute('" .
|
|
esc_js($siteKey) .
|
|
"', {action: 'submit'})
|
|
.then(function(token) {
|
|
document.getElementById('g-recaptcha-response').value = token;
|
|
document.getElementById('ic-form').submit();
|
|
});
|
|
});
|
|
}
|
|
});
|
|
",
|
|
);
|
|
}
|
|
|
|
// Budowanie HTML formularza
|
|
$html .= '<div class="ic-wrap">';
|
|
|
|
if ($mailResult == 1) {
|
|
$html .=
|
|
'<div class="ic-alert ic-alert--success">' .
|
|
esc_html(get_option("incontact-form-snd-ok")) .
|
|
"</div>";
|
|
} elseif ($sendFailed) {
|
|
$html .=
|
|
'<div class="ic-alert ic-alert--error">' .
|
|
esc_html__(
|
|
"The message could not be sent. Please try again.",
|
|
"in-contact",
|
|
) .
|
|
"</div>";
|
|
}
|
|
|
|
// Po nieudanej próbie pola zachowują wpisane wartości
|
|
$keep = $form_submitted && $mailResult != 1;
|
|
$valName = $keep ? esc_attr((string) $em_name) : "";
|
|
$valMail = $keep ? esc_attr((string) $em_mail) : "";
|
|
$valMessage = $keep ? esc_textarea((string) $em_message) : "";
|
|
|
|
$html .= '<form action="" method="post" id="ic-form" novalidate>';
|
|
$html .=
|
|
'<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">';
|
|
$html .= '<input type="hidden" name="emmsg" value="true" />';
|
|
|
|
// Honeypot — ukryte pole
|
|
$html .=
|
|
'<div class="ic-field ic-field--honey" aria-hidden="true">' .
|
|
'<input type="text" id="website" name="website" tabindex="-1" autocomplete="off" />' .
|
|
"</div>";
|
|
|
|
// Pole: Nazwa
|
|
$html .=
|
|
'<div class="ic-field">' .
|
|
'<label for="ic-name">' .
|
|
esc_html(get_option("incontact-form-name")) .
|
|
"</label>" .
|
|
'<input type="text" id="ic-name" name="em_name" value="' .
|
|
$valName .
|
|
'" placeholder="' .
|
|
esc_attr(get_option("incontact-form-ph-name")) .
|
|
'" required />' .
|
|
'<span class="ic-field__error">' .
|
|
esc_html__("Please enter your name!", "in-contact") .
|
|
"</span>" .
|
|
"</div>";
|
|
|
|
// Pole: Email
|
|
$html .=
|
|
'<div class="ic-field">' .
|
|
'<label for="ic-email">' .
|
|
esc_html(get_option("incontact-form-email")) .
|
|
"</label>" .
|
|
'<input type="email" id="ic-email" name="em_mail" value="' .
|
|
$valMail .
|
|
'" placeholder="' .
|
|
esc_attr(get_option("incontact-form-ph-email")) .
|
|
'" required />' .
|
|
'<span class="ic-field__error">' .
|
|
esc_html__("Please enter your email address!", "in-contact") .
|
|
"</span>" .
|
|
"</div>";
|
|
|
|
// Pole: Wiadomość
|
|
$html .=
|
|
'<div class="ic-field">' .
|
|
'<label for="ic-message">' .
|
|
esc_html(get_option("incontact-form-message")) .
|
|
"</label>" .
|
|
'<textarea id="ic-message" name="em_message" rows="5" placeholder="' .
|
|
esc_attr(get_option("incontact-form-ph-message")) .
|
|
'" required>' .
|
|
$valMessage .
|
|
'</textarea>' .
|
|
'<span class="ic-field__error">' .
|
|
esc_html__("Please enter your message!", "in-contact") .
|
|
"</span>" .
|
|
"</div>";
|
|
|
|
// Przycisk
|
|
$html .=
|
|
'<div class="ic-field ic-field--submit">' .
|
|
'<button type="submit" id="ic-submit">' .
|
|
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="22" y1="2" x2="11" y2="13"></line><polygon points="22 2 15 22 11 13 2 9 22 2"></polygon></svg>' .
|
|
" " .
|
|
esc_html__("Send", "in-contact") .
|
|
"</button>" .
|
|
"</div>";
|
|
|
|
$html .= "</form>";
|
|
$html .= "</div>"; // .ic-wrap
|
|
|
|
// Inline JS — walidacja bez Bootstrap
|
|
$html .= "<script>
|
|
(function(){
|
|
var form = document.getElementById('ic-form');
|
|
if (!form) return;
|
|
form.addEventListener('submit', function(e) {
|
|
var valid = true;
|
|
form.querySelectorAll('[required]').forEach(function(field) {
|
|
var wrapper = field.closest('.ic-field');
|
|
if (!field.value.trim()) {
|
|
wrapper.classList.add('ic-field--invalid');
|
|
valid = false;
|
|
} else {
|
|
wrapper.classList.remove('ic-field--invalid');
|
|
}
|
|
});
|
|
if (!valid) e.preventDefault();
|
|
});
|
|
form.querySelectorAll('[required]').forEach(function(field) {
|
|
field.addEventListener('input', function() {
|
|
if (field.value.trim()) {
|
|
field.closest('.ic-field').classList.remove('ic-field--invalid');
|
|
}
|
|
});
|
|
});
|
|
})();
|
|
</script>";
|
|
|
|
return $html;
|
|
}
|
|
}
|