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") . "
"; } 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", ) . "
"; } 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") . "
"; } // 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 . " ", "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 = ' ' . esc_html($emailTitle) . '

' . esc_html($emailTitle) . '

' . esc_html($emailHeading) . '

' . esc_html($emailNameLabel) . ': {{IMIE_NAZWISKO}}

' . esc_html($emailEmailLabel) . ': {{EMAIL}}

' . esc_html($emailMsgHeading) . '

{{WIADOMOSC}}

' . esc_html($emailReplyLabel) . '
'; $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"], "
", esc_html($em_message), ), date("Y"), ], $body, ); // Obsługa błędów walidacji if (!empty($error)) { $html .= '
' . $error . "
"; } 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 .= '
'; if ($mailResult == 1) { $html .= '
' . esc_html(get_option("incontact-form-snd-ok")) . "
"; } elseif ($sendFailed) { $html .= '
' . esc_html__( "The message could not be sent. Please try again.", "in-contact", ) . "
"; } // 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 .= '
'; $html .= ''; $html .= ''; // Honeypot — ukryte pole $html .= '"; // Pole: Nazwa $html .= '
' . '" . '' . '' . esc_html__("Please enter your name!", "in-contact") . "" . "
"; // Pole: Email $html .= '
' . '" . '' . '' . esc_html__("Please enter your email address!", "in-contact") . "" . "
"; // Pole: Wiadomość $html .= '
' . '" . '' . '' . esc_html__("Please enter your message!", "in-contact") . "" . "
"; // Przycisk $html .= '
' . '" . "
"; $html .= "
"; $html .= "
"; // .ic-wrap // Inline JS — walidacja bez Bootstrap $html .= ""; return $html; } }