Poprawki bezpieczeństwa formularza i panelu ustawień

- 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>
This commit is contained in:
2026-11-15 23:44:46 +01:00
co-authored by Claude Fable 5.2
parent f6b707a6f2
commit 0f7eddd6a0
7 changed files with 506 additions and 268 deletions
+210 -1
View File
@@ -1 +1,210 @@
form #website{ display:none; } /* ============================================================
inContact — cform.css v0.3
Dark, modern, photography-focused form styles
Prefix: .ic- (inContact)
============================================================ */
/* ---------- Tokeny ---------- */
:root {
--ic-bg: #ffffff;
--ic-surface: #f8f8f8;
--ic-border: #dde0e8;
--ic-border-focus: #2563eb;
--ic-text: #1a1d26;
--ic-text-muted: #8a909e;
--ic-accent: #2563eb;
--ic-accent-dark: #1a4fc4;
--ic-error: #dc2626;
--ic-success-bg: #f0fdf4;
--ic-success-txt: #166534;
--ic-error-bg: #fef2f2;
--ic-radius: 6px;
--ic-transition: 200ms ease;
--ic-font: inherit;
}
/* ---------- Wrapper ---------- */
.ic-wrap {
max-width: 680px;
margin: 0 auto;
padding: 40px 48px;
background: var(--ic-surface);
border: 1px solid var(--ic-border);
border-radius: 10px;
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.07);
}
/* ---------- Alerty ---------- */
.ic-alert {
padding: 14px 18px;
border-radius: var(--ic-radius);
margin-bottom: 28px;
font-size: 0.92rem;
line-height: 1.5;
}
.ic-alert--success {
background: var(--ic-success-bg);
color: var(--ic-success-txt);
border: 1px solid #1e5e38;
}
.ic-alert--error {
background: var(--ic-error-bg);
color: var(--ic-error);
border: 1px solid #5e1e1e;
}
/* ---------- Pola formularza ---------- */
.ic-field {
margin-bottom: 22px;
position: relative;
}
/* Honeypot — ukryte */
.ic-field--honey {
position: absolute;
left: -9999px;
width: 1px;
height: 1px;
overflow: hidden;
opacity: 0;
}
.ic-field label {
display: block;
margin-bottom: 7px;
font-size: 0.82rem;
font-weight: 600;
letter-spacing: 0.08em;
text-transform: uppercase;
color: var(--ic-text-muted);
}
.ic-field input[type="text"],
.ic-field input[type="email"],
.ic-field textarea {
display: block;
width: 100%;
padding: 12px 16px;
background: var(--ic-bg);
border: 1px solid var(--ic-border);
border-radius: var(--ic-radius);
color: var(--ic-text);
font-size: 1rem;
font-family: inherit;
line-height: 1.5;
transition:
border-color var(--ic-transition),
box-shadow var(--ic-transition);
box-sizing: border-box;
-webkit-appearance: none;
appearance: none;
}
.ic-field input::placeholder,
.ic-field textarea::placeholder {
color: var(--ic-text-muted);
opacity: 1;
}
.ic-field input:focus,
.ic-field textarea:focus {
outline: none;
border-color: var(--ic-border-focus);
box-shadow: 0 0 0 3px rgba(226, 176, 74, 0.15);
}
.ic-field textarea {
resize: vertical;
min-height: 130px;
}
/* ---------- Walidacja ---------- */
.ic-field__error {
display: none;
margin-top: 6px;
font-size: 0.8rem;
color: var(--ic-error);
}
.ic-field--invalid input,
.ic-field--invalid textarea {
border-color: var(--ic-error);
box-shadow: 0 0 0 3px rgba(224, 85, 85, 0.15);
}
.ic-field--invalid .ic-field__error {
display: block;
}
/* ---------- reCAPTCHA ---------- */
.ic-field--captcha {
margin-bottom: 26px;
}
/* reCAPTCHA ma własny iframe — filtrujemy tak żeby ciemne tło nie świeciło przez padding */
.ic-field--captcha .g-recaptcha {
display: inline-block;
border-radius: var(--ic-radius);
overflow: hidden;
}
/* ---------- Przycisk ---------- */
.ic-field--submit {
margin-top: 8px;
margin-bottom: 0;
}
#ic-submit {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 13px 32px;
background: var(--ic-accent);
color: #ffffff;
border: none;
border-radius: var(--ic-radius);
font-size: 0.95rem;
font-weight: 700;
letter-spacing: 0.04em;
cursor: pointer;
transition:
background var(--ic-transition),
transform var(--ic-transition),
box-shadow var(--ic-transition);
-webkit-appearance: none;
appearance: none;
}
#ic-submit svg {
flex-shrink: 0;
transition: transform var(--ic-transition);
}
#ic-submit:hover {
background: var(--ic-accent-dark);
box-shadow: 0 4px 20px rgba(37, 99, 235, 0.3);
transform: translateY(-1px);
}
#ic-submit:hover svg {
transform: translateX(2px) translateY(-2px);
}
#ic-submit:active {
transform: translateY(0);
box-shadow: none;
}
#ic-submit:focus-visible {
outline: 2px solid var(--ic-accent);
outline-offset: 3px;
}
/* ---------- Responsywność ---------- */
@media (max-width: 640px) {
.ic-wrap {
padding: 28px 20px;
}
}
+34 -68
View File
@@ -39,49 +39,35 @@ class AdminMenu
public function InContactSettings() public function InContactSettings()
{ {
$updated = isset($_POST["updated"]) ? $_POST["updated"] : ""; $nonce = isset($_POST["incontact-config"])
$incontactconfig = isset($_REQUEST["incontact-config"]) ? sanitize_text_field(wp_unslash($_POST["incontact-config"]))
? $_REQUEST["incontact-config"]
: ""; : "";
if ( if (
$updated === "true" || $_SERVER["REQUEST_METHOD"] === "POST" &&
wp_verify_nonce($incontactconfig, "incontact-update") current_user_can("manage_options") &&
wp_verify_nonce($nonce, "incontact-update")
) { ) {
$fields = [
update_option("incontact-site-key", $_POST["incontact-site-key"]); "incontact-site-key" => "sanitize_text_field",
"incontact-secret-key" => "sanitize_text_field",
"incontact-target-mail" => "sanitize_email",
"incontact-form-ph-name" => "sanitize_text_field",
"incontact-form-ph-email" => "sanitize_text_field",
"incontact-form-ph-message" => "sanitize_text_field",
"incontact-form-name" => "sanitize_text_field",
"incontact-form-email" => "sanitize_text_field",
"incontact-form-message" => "sanitize_text_field",
"incontact-form-snd-ok" => "sanitize_text_field",
];
foreach ($fields as $option => $sanitize) {
if (!isset($_POST[$option])) {
continue;
}
update_option( update_option(
"incontact-secret-key", $option,
$_POST["incontact-secret-key"], $sanitize(wp_unslash($_POST[$option])),
);
update_option(
"incontact-target-mail",
$_POST["incontact-target-mail"],
);
update_option(
"incontact-form-ph-name",
$_POST["incontact-form-ph-name"],
);
update_option(
"incontact-form-ph-email",
$_POST["incontact-form-ph-email"],
);
update_option(
"incontact-form-ph-message",
$_POST["incontact-form-ph-message"],
);
update_option("incontact-form-name", $_POST["incontact-form-name"]);
update_option(
"incontact-form-email",
$_POST["incontact-form-email"],
);
update_option(
"incontact-form-message",
$_POST["incontact-form-message"],
);
update_option(
"incontact-form-snd-ok",
$_POST["incontact-form-snd-ok"],
); );
}
?> ?>
<div class="updated"> <div class="updated">
@@ -108,9 +94,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-site-key" id="incontact-site-key" type="text" <td><input name="incontact-site-key" id="incontact-site-key" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-site-key")); ?>" class="regular-text"/></td>
"incontact-site-key",
); ?>" class="regular-text"/></td>
</tr> </tr>
<tr> <tr>
<th><label for="incontact-secret-key"><?php echo esc_html__( <th><label for="incontact-secret-key"><?php echo esc_html__(
@@ -118,9 +102,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-secret-key" id="incontact-secret-key" type="text" <td><input name="incontact-secret-key" id="incontact-secret-key" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-secret-key")); ?>" class="regular-text"/></td>
"incontact-secret-key",
); ?>" class="regular-text"/></td>
</tr> </tr>
<tr> <tr>
<th><label for="incontact-target-mail"><?php echo esc_html__( <th><label for="incontact-target-mail"><?php echo esc_html__(
@@ -128,9 +110,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-target-mail" id="incontact-target-mail" type="text" <td><input name="incontact-target-mail" id="incontact-target-mail" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-target-mail")); ?>" class="regular-text"/></td>
"incontact-target-mail",
); ?>" class="regular-text"/></td>
</tr> </tr>
<tr> <tr>
@@ -139,9 +119,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-form-ph-name" id="incontact-form-ph-name" type="text" <td><input name="incontact-form-ph-name" id="incontact-form-ph-name" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-form-ph-name")); ?>" class="regular-text"/></td>
"incontact-form-ph-name",
); ?>" class="regular-text"/></td>
</tr> </tr>
<tr> <tr>
<th><label for="incontact-form-ph-email"><?php echo esc_html__( <th><label for="incontact-form-ph-email"><?php echo esc_html__(
@@ -149,9 +127,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-form-ph-email" id="incontact-form-ph-email" type="text" <td><input name="incontact-form-ph-email" id="incontact-form-ph-email" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-form-ph-email")); ?>" class="regular-text"/></td>
"incontact-form-ph-email",
); ?>" class="regular-text"/></td>
</tr> </tr>
<tr> <tr>
<th><label for="incontact-form-ph-message"><?php echo esc_html__( <th><label for="incontact-form-ph-message"><?php echo esc_html__(
@@ -159,9 +135,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-form-ph-message" id="incontact-form-ph-message" type="text" <td><input name="incontact-form-ph-message" id="incontact-form-ph-message" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-form-ph-message")); ?>" class="regular-text"/></td>
"incontact-form-ph-message",
); ?>" class="regular-text"/></td>
</tr> </tr>
<tr> <tr>
@@ -170,9 +144,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-form-name" id="incontact-form-name" type="text" <td><input name="incontact-form-name" id="incontact-form-name" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-form-name")); ?>" class="regular-text"/></td>
"incontact-form-name",
); ?>" class="regular-text"/></td>
</tr> </tr>
<tr> <tr>
<th><label for="incontact-form-email"><?php echo esc_html__( <th><label for="incontact-form-email"><?php echo esc_html__(
@@ -180,9 +152,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-form-email" id="incontact-form-email" type="text" <td><input name="incontact-form-email" id="incontact-form-email" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-form-email")); ?>" class="regular-text"/></td>
"incontact-form-email",
); ?>" class="regular-text"/></td>
</tr> </tr>
<tr> <tr>
<th><label for="incontact-form-message"><?php echo esc_html__( <th><label for="incontact-form-message"><?php echo esc_html__(
@@ -190,9 +160,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-form-message" id="incontact-form-message" type="text" <td><input name="incontact-form-message" id="incontact-form-message" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-form-message")); ?>" class="regular-text"/></td>
"incontact-form-message",
); ?>" class="regular-text"/></td>
</tr> </tr>
<tr> <tr>
@@ -201,9 +169,7 @@ class AdminMenu
"in-contact", "in-contact",
); ?></label></th> ); ?></label></th>
<td><input name="incontact-form-snd-ok" id="incontact-form-snd-ok" type="text" <td><input name="incontact-form-snd-ok" id="incontact-form-snd-ok" type="text"
value="<?php echo get_option( value="<?php echo esc_attr(get_option("incontact-form-snd-ok")); ?>" class="regular-text"/></td>
"incontact-form-snd-ok",
); ?>" class="regular-text"/></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
+15 -9
View File
@@ -11,19 +11,25 @@ namespace inContact;
* @version 0.2 * @version 0.2
*/ */
class Enqueue { class Enqueue
{
use Singleton; use Singleton;
function __construct() { /*function __construct() {
add_action('wp_enqueue_scripts',[ $this, 'enqueueScripts']); add_action('wp_enqueue_scripts',[ $this, 'enqueueScripts']);
} }*/
public function enqueueScripts() { public function enqueueScripts()
{
global $pluginDirUrl, $pluginPath; global $pluginDirUrl, $pluginPath;
wp_register_style('ContactCss', $pluginDirUrl.'css/inContact.css', [], filemtime( $pluginPath . 'css/inContact.css'), 'all'); wp_register_style(
"ContactCss",
wp_enqueue_style('ContactCss'); $pluginDirUrl . "css/inContact.css",
[],
filemtime($pluginPath . "css/inContact.css"),
"all",
);
wp_enqueue_style("ContactCss");
} }
} }
+234 -185
View File
@@ -3,12 +3,12 @@
namespace inContact; namespace inContact;
/** /**
* Klasa Menus * Klasa Shortcode
* *
* @link https://krzysztof-turek.com * @link https://krzysztof-turek.com
* *
* @package tylkofotografia.pl * @package tylkofotografia.pl
* @version 0.2 * @version 0.3
*/ */
class Shortcode class Shortcode
{ {
@@ -29,29 +29,34 @@ class Shortcode
$em_name = null; $em_name = null;
$em_mail = null; $em_mail = null;
$em_message = null; $em_message = null;
$class = null;
$html = null; $html = null;
$error = null; $error = null;
$body = null; $body = null;
$telefon = null; $telefon = null;
$mailResult = 0; $mailResult = 0;
$canSend = false;
$sendFailed = false;
$form_submitted = $_SERVER["REQUEST_METHOD"] === "POST"; $form_submitted = $_SERVER["REQUEST_METHOD"] === "POST";
if (!empty($_POST["website"])) { global $pluginDirUrl;
die();
} // Honeypot — bot dostaje pozorny sukces, mail nie jest wysyłany
$isBot = !empty($_POST["website"]);
if ($form_submitted) { if ($form_submitted) {
$emmsg = isset($_POST["emmsg"]) ? $_POST["emmsg"] : ""; $emmsg = isset($_POST["emmsg"]) ? $_POST["emmsg"] : "";
// Walidacja pól
if (!empty($_POST["em_name"])) { if (!empty($_POST["em_name"])) {
$em_name = $_POST["em_name"]; $em_name = sanitize_text_field(wp_unslash($_POST["em_name"]));
} else { } else {
$error .= $error .=
esc_html__("Please enter your name", "in-contact") . esc_html__("Please enter your name", "in-contact") .
"<br />"; "<br />";
} }
if (!empty($_POST["em_mail"])) { if (!empty($_POST["em_mail"]) && is_email($_POST["em_mail"])) {
$em_mail = $_POST["em_mail"]; $em_mail = sanitize_email(wp_unslash($_POST["em_mail"]));
} else { } else {
$error .= $error .=
esc_html__( esc_html__(
@@ -60,41 +65,47 @@ class Shortcode
) . "<br />"; ) . "<br />";
} }
if (!empty($_POST["em_message"])) { if (!empty($_POST["em_message"])) {
$em_message = $_POST["em_message"]; $em_message = sanitize_textarea_field(
wp_unslash($_POST["em_message"]),
);
} else { } else {
$error .= $error .=
esc_html__("Please enter your message", "in-contact") . esc_html__("Please enter your message", "in-contact") .
"<br />"; "<br />";
} }
$targetEmail = // Budowanie emaila
"To: Tylkofotografia <" . // From z domeny strony (SPF/DMARC), adres nadawcy tylko w Reply-To
get_option("incontact-target-mail") . $siteHost = (string) wp_parse_url(home_url(), PHP_URL_HOST);
">"; if (strpos($siteHost, "www.") === 0) {
$mailResult = 0; $siteHost = substr($siteHost, 4);
$headers = "MIME-Version: 1.0" . "\r\n"; }
$headers .= $headerChars = ["\"", "<", ">", "\r", "\n"];
"Content-type: text/html; charset=" . $fromName = str_replace(
get_bloginfo("charset") . $headerChars,
"" . "",
"\r\n"; wp_specialchars_decode(get_bloginfo("name"), ENT_QUOTES),
$headers .= "From: " . $em_name . " <" . $em_mail . ">" . "\r\n"; );
$replyName = str_replace($headerChars, "", (string) $em_name);
$headers = [ $headers = [
"Content-Type: text/html; charset=UTF-8", "Content-Type: text/html; charset=UTF-8",
"From: " . $em_name . " <" . $em_mail . ">", "From: " . $fromName . " <wordpress@" . $siteHost . ">",
"Reply-To: " . $em_name . " <" . $em_mail . ">", "Reply-To: " . $replyName . " <" . $em_mail . ">",
]; ];
$targetEmail = get_option("incontact-target-mail");
$emailTitle = __("New message from contact form", "in-contact"); $emailTitle = __("New message from contact form", "in-contact");
$emailHeading = __("Contact details", "in-contact"); $emailHeading = __("Contact details", "in-contact");
$emailNameLabel = __("Name and surname", "in-contact"); $emailNameLabel = __("Name and surname", "in-contact");
$emailEmailLabel = __("Email", "in-contact"); $emailEmailLabel = __("Email", "in-contact");
$emailMessageHeading = __("Message content", "in-contact"); $emailMsgHeading = __("Message content", "in-contact");
$emailReplyLabel = __("Reply to message", "in-contact"); $emailReplyLabel = __("Reply to message", "in-contact");
$emailFooter = __( $emailFooter = __(
"This email was generated automatically by the website contact form.", "This email was generated automatically by the website contact form.",
"in-contact", "in-contact",
); );
$emailRights = __("All rights reserved.", "in-contact"); $emailRights = __("All rights reserved.", "in-contact");
$body = $body =
'<!DOCTYPE html> '<!DOCTYPE html>
<html lang="pl"> <html lang="pl">
@@ -104,70 +115,24 @@ class Shortcode
esc_html($emailTitle) . esc_html($emailTitle) .
'</title> '</title>
<style> <style>
body { body { font-family: Arial, sans-serif; background-color: #f5f5f5; margin: 0; padding: 0; }
font-family: Arial, sans-serif; .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); }
background-color: #f5f5f5; .header { background-color: #1a1a2e; color: #ffffff; padding: 20px; text-align: center; }
margin: 0; .header h1 { margin: 0; font-size: 20px; }
padding: 0; .content { padding: 24px; line-height: 1.6; }
} .content h2 { margin-top: 0; color: #1a1a2e; }
.container { .info { background-color: #f9f9f9; border-left: 4px solid #e2b04a; padding: 12px 16px; margin: 16px 0; border-radius: 0 4px 4px 0; }
width: 90%; .info h3 { margin: 0 0 8px; color: #1a1a2e; font-size: 14px; text-transform: uppercase; letter-spacing: .05em; }
max-width: 600px; .button { display: inline-block; padding: 10px 20px; margin-top: 20px; background-color: #e2b04a; color: #1a1a2e; text-decoration: none; border-radius: 4px; font-weight: bold; }
margin: 20px auto; .footer { background-color: #f1f1f1; text-align: center; padding: 12px; font-size: 12px; color: #777; }
background-color: #ffffff; @media (max-width: 600px) { .container { width: 95%; } }
border-radius: 8px;
overflow: hidden;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.header {
background-color: #3a7bd5;
color: #ffffff;
padding: 20px;
text-align: center;
}
.content {
padding: 20px;
line-height: 1.6;
}
.content h2 {
margin-top: 0;
}
.footer {
background-color: #f1f1f1;
text-align: center;
padding: 10px;
font-size: 12px;
color: #777777;
}
.button {
display: inline-block;
padding: 10px 20px;
margin-top: 20px;
background-color: #3a7bd5;
color: #ffffff;
text-decoration: none;
border-radius: 5px;
}
.info {
background-color: #f9f9f9;
border-left: 4px solid #3a7bd5;
padding: 10px;
margin: 10px 0;
}
@media (max-width: 600px) {
.container {
width: 95%;
}
}
</style> </style>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<div class="header"> <div class="header"><h1>' .
<h1>' .
esc_html($emailTitle) . esc_html($emailTitle) .
'</h1> '</h1></div>
</div>
<div class="content"> <div class="content">
<h2>' . <h2>' .
esc_html($emailHeading) . esc_html($emailHeading) .
@@ -178,15 +143,12 @@ class Shortcode
<p><strong>' . <p><strong>' .
esc_html($emailEmailLabel) . esc_html($emailEmailLabel) .
':</strong> {{EMAIL}}</p> ':</strong> {{EMAIL}}</p>
<div class="info"> <div class="info">
<h3>' . <h3>' .
esc_html($emailMessageHeading) . esc_html($emailMsgHeading) .
':</h3> '</h3>
<p>{{WIADOMOSC}}</p> <p>{{WIADOMOSC}}</p>
</div> </div>
<a href="mailto:{{EMAIL}}" class="button">' . <a href="mailto:{{EMAIL}}" class="button">' .
esc_html($emailReplyLabel) . esc_html($emailReplyLabel) .
'</a> '</a>
@@ -195,14 +157,16 @@ class Shortcode
' . ' .
esc_html($emailFooter) . esc_html($emailFooter) .
'<br> '<br>
© {{ROK}} Twoja Firma. ' . &copy; {{ROK}} ' .
esc_html(get_bloginfo("name")) .
". " .
esc_html($emailRights) . esc_html($emailRights) .
' '
</div> </div>
</div> </div>
</body> </body>
</html> </html>';
';
$body = str_replace( $body = str_replace(
[ [
"{{IMIE_NAZWISKO}}", "{{IMIE_NAZWISKO}}",
@@ -212,35 +176,44 @@ class Shortcode
"{{ROK}}", "{{ROK}}",
], ],
[ [
$em_name, esc_html($em_name),
$em_mail, esc_html($em_mail),
$telefon, esc_html($telefon),
str_replace(["\r\n", "\r", "\n"], "<br />", $em_message), str_replace(
["\r\n", "\r", "\n"],
"<br />",
esc_html($em_message),
),
date("Y"), date("Y"),
], ],
$body, $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"]) $gRecaptchaResponse = isset($_POST["g-recaptcha-response"])
? $_POST["g-recaptcha-response"] ? $_POST["g-recaptcha-response"]
: ""; : "";
$siteKey = get_option("incontact-site-key");
if (!empty($error)) { if ($siteKey != "") {
$html .=
'<div class="alert alert-success" role="alert">' .
$error .
"</div>";
} elseif ($emmsg == "true") {
$secret = get_option("incontact-secret-key"); $secret = get_option("incontact-secret-key");
$response = null;
if (get_option("incontact-site-key") != "") {
$reCaptcha = new \ReCaptcha\ReCaptcha($secret); $reCaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $reCaptcha $resp = $reCaptcha
->setExpectedHostname($_SERVER["SERVER_NAME"]) ->setExpectedHostname($_SERVER["SERVER_NAME"])
->setExpectedAction("submit")
->setScoreThreshold(0.5)
->verify($gRecaptchaResponse, $_SERVER["REMOTE_ADDR"]); ->verify($gRecaptchaResponse, $_SERVER["REMOTE_ADDR"]);
if ($resp->isSuccess()) { if ($resp->isSuccess()) {
$canSend = true; $canSend = true;
} else {
$errors = $resp->getErrorCodes();
} }
} else { } else {
$canSend = true; $canSend = true;
@@ -254,98 +227,174 @@ class Shortcode
$headers, $headers,
); );
} }
$sendFailed = !$mailResult;
} }
} }
wp_enqueue_style("ContactCss", plugins_url("/css/cform.css", __FILE__));
if (get_option("incontact-site-key") != "") {
$html .=
'<script src="https://www.google.com/recaptcha/api.js"></script>
<script>
$(document).ready(function(){window.setTimeout(function(){$(".alert").fadeTo(500,0).slideUp(500,function(){$(this).remove()})},2e3)}),$(document).ready(function(){"use strict";window.addEventListener("load",function(){var t=document.getElementsByClassName("needs-validation");Array.prototype.filter.call(t,function(t){t.addEventListener("submit",function(e){!1===t.checkValidity()&&(e.preventDefault(),e.stopPropagation()),t.classList.add("was-validated")},!1)})},!1)})();
</script>' . "\n";
}
$html .= '<div class="row justify-content-md-center"> // Enqueue styles & reCAPTCHA
<div class="col-12">'; 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) { if ($mailResult == 1) {
$html .= $html .=
'<div class="alert alert-success" role="alert">' . '<div class="ic-alert ic-alert--success">' .
get_option("incontact-form-snd-ok") . 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>"; "</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 .= $html .=
'<div class="jumbotron"><form action="" class="needs-validation" novalidate method="post" id="demo-form"> '<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response">';
<input type="hidden" name="emmsg" value="true" /> $html .= '<input type="hidden" name="emmsg" value="true" />';
<input type="text" id="website" name="website" />
<div class="jumbotron"> // Honeypot — ukryte pole
<div class="form-group"> $html .=
<label for="name" class="col-2 control-label">' . '<div class="ic-field ic-field--honey" aria-hidden="true">' .
get_option("incontact-form-name") . '<input type="text" id="website" name="website" tabindex="-1" autocomplete="off" />' .
'</label> "</div>";
<div class="col-12">
<input type="text" class="form-control" id="name" name="em_name" placeholder="' . // Pole: Nazwa
get_option("incontact-form-ph-name") . $html .=
'" value="" required> '<div class="ic-field">' .
<div class="invalid-feedback"> '<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") . esc_html__("Please enter your name!", "in-contact") .
' "</span>" .
</div> "</div>";
</div>
</div> // Pole: Email
<div class="form-group"> $html .=
<label for="email" class="col-2 control-label">' . '<div class="ic-field">' .
get_option("incontact-form-email") . '<label for="ic-email">' .
'</label> esc_html(get_option("incontact-form-email")) .
<div class="col-12"> "</label>" .
<input type="email" class="form-control" id="email" name="em_mail" placeholder="' . '<input type="email" id="ic-email" name="em_mail" value="' .
get_option("incontact-form-ph-email") . $valMail .
'" value="" required> '" placeholder="' .
<div class="invalid-feedback"> esc_attr(get_option("incontact-form-ph-email")) .
' . '" required />' .
'<span class="ic-field__error">' .
esc_html__("Please enter your email address!", "in-contact") . esc_html__("Please enter your email address!", "in-contact") .
' "</span>" .
</div> "</div>";
</div>
</div> // Pole: Wiadomość
<div class="form-group"> $html .=
<p class="col-2 control-label">' . '<div class="ic-field">' .
get_option("incontact-form-message") . '<label for="ic-message">' .
'</p> esc_html(get_option("incontact-form-message")) .
<div class="col-12"> "</label>" .
<textarea class="form-control" rows="4" name="em_message" placeholder="' . '<textarea id="ic-message" name="em_message" rows="5" placeholder="' .
get_option("incontact-form-ph-message") . esc_attr(get_option("incontact-form-ph-message")) .
'" required></textarea> '" required>' .
<div class="invalid-feedback"> $valMessage .
' . '</textarea>' .
'<span class="ic-field__error">' .
esc_html__("Please enter your message!", "in-contact") . esc_html__("Please enter your message!", "in-contact") .
' "</span>" .
</div> "</div>";
</div>
</div> // Przycisk
<div class="form-group"> $html .=
<div class="col-4"> '<div class="ic-field ic-field--submit">' .
<div class="g-recaptcha" data-sitekey="' . '<button type="submit" id="ic-submit">' .
get_option("incontact-site-key") . '<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>' .
'"></div> " " .
</div>
</div>
<div class="form-group">
<p></p>
<div class="col-4">
<button id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
<i class="fas fa-paper-plane"></i> ' .
esc_html__("Send", "in-contact") . esc_html__("Send", "in-contact") .
' "</button>" .
</button> "</div>";
</div>
</div> $html .= "</form>";
</div> $html .= "</div>"; // .ic-wrap
</form>
</div> // Inline JS — walidacja bez Bootstrap
</div></div>'; $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; return $html;
} }
Binary file not shown.
+4
View File
@@ -131,3 +131,7 @@ msgstr "Podaj swoją wiadomość!"
#: inc/classes/inContact/Shortcode.php #: inc/classes/inContact/Shortcode.php
msgid "Send" msgid "Send"
msgstr "Wyślij" msgstr "Wyślij"
#: inc/classes/inContact/Shortcode.php
msgid "The message could not be sent. Please try again."
msgstr "Nie udało się wysłać wiadomości. Spróbuj ponownie."
+4
View File
@@ -130,3 +130,7 @@ msgstr ""
#: inc/classes/inContact/Shortcode.php #: inc/classes/inContact/Shortcode.php
msgid "Send" msgid "Send"
msgstr "" msgstr ""
#: inc/classes/inContact/Shortcode.php
msgid "The message could not be sent. Please try again."
msgstr ""