Ansico-Contact-Form/ansico-contact-form/ansico-contact-form.php

163 lines
9.1 KiB
PHP
Raw Normal View History

2026-04-15 21:05:00 +00:00
<?php
/**
* Plugin Name: Ansico Contact Form
* Plugin URI: https://ansico.dk/Ansico/Ansico-Contact-Form
* Description: A lightweight, fully customizable contact form block for the Gutenberg editor with built-in math spam protection.
* Version: 1.0.0
* Author: Andreas Andersen (Ansico)
* Author URI: https://ansico.dk
* License: GPL-3.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: ansico-contact-form
* Requires at least: 5.8
* Tested up to: 6.9.4
*/
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Render function for the contact form frontend.
*/
function ansico_cf_v1_render_callback($attributes) {
// Attributes with defaults
$block_id = isset($attributes['blockId']) ? $attributes['blockId'] : uniqid('ansicocf_');
$recipient = !empty($attributes['recipientEmail']) ? $attributes['recipientEmail'] : get_option('admin_email');
$success_msg = isset($attributes['successMessage']) ? $attributes['successMessage'] : 'Message has been sent';
$spam_err_msg = isset($attributes['spamErrorMessage']) ? $attributes['spamErrorMessage'] : 'Wrong spam protection answer.';
$footer_msg = isset($attributes['footerMessage']) ? $attributes['footerMessage'] : 'Message has been sent on website';
$format = isset($attributes['emailFormat']) ? $attributes['emailFormat'] : 'text';
$label_name = isset($attributes['labelName']) ? $attributes['labelName'] : 'Name';
$label_email = isset($attributes['labelEmail']) ? $attributes['labelEmail'] : 'E-mail';
$label_subj = isset($attributes['labelSubject']) ? $attributes['labelSubject'] : 'Subject';
$label_msg = isset($attributes['labelMessage']) ? $attributes['labelMessage'] : 'Message';
$label_spam = isset($attributes['labelSpam']) ? $attributes['labelSpam'] : 'Spam check:';
$btn_text = isset($attributes['buttonText']) ? $attributes['buttonText'] : 'Send message';
$msg_html = '';
static $form_processed_once = false;
$posted_name = '';
$posted_email = '';
$posted_subject = '';
$posted_message = '';
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ansicocf_submit']) && isset($_POST['ansicocf_block_id']) && $_POST['ansicocf_block_id'] === $block_id) {
$posted_name = sanitize_text_field($_POST['ansicocf_name']);
$posted_email = sanitize_email($_POST['ansicocf_email']);
$posted_subject = sanitize_text_field($_POST['ansicocf_subject']);
$posted_message = sanitize_textarea_field($_POST['ansicocf_message']);
if (intval($_POST['ansicocf_spam_answer']) !== intval($_POST['ansicocf_spam_expected'])) {
$msg_html = '<p style="color:#d32f2f; font-weight:bold; padding:10px; border:1px solid #d32f2f; border-radius:4px;">' . esc_html($spam_err_msg) . '</p>';
} else {
if (!$form_processed_once) {
ansico_cf_v1_send_final_mail($posted_name, $posted_email, $posted_subject, $posted_message, $recipient, $format, $footer_msg, $label_name, $label_email, $label_subj, $label_msg);
$form_processed_once = true;
}
$msg_html = '<p style="color:#2e7d32; font-weight:bold; padding:10px; border:1px solid #2e7d32; border-radius:4px;">' . esc_html($success_msg) . '</p>';
$posted_name = $posted_email = $posted_subject = $posted_message = '';
}
}
$num1 = rand(1, 5);
$num2 = rand(1, 5);
$expected = $num1 + $num2;
ob_start();
?>
<div class="ansico-contact-form-wrapper" style="max-width:500px; margin-bottom: 25px;">
<?php echo $msg_html; ?>
<form method="post" style="font-family: inherit;">
<p style="margin-bottom: 15px;">
<label style="font-weight: 600; display: block; margin-bottom: 5px;"><?php echo esc_html($label_name); ?></label>
<input type="text" name="ansicocf_name" value="<?php echo esc_attr($posted_name); ?>" required style="width:100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box;">
</p>
<p style="margin-bottom: 15px;">
<label style="font-weight: 600; display: block; margin-bottom: 5px;"><?php echo esc_html($label_email); ?></label>
<input type="email" name="ansicocf_email" value="<?php echo esc_attr($posted_email); ?>" required style="width:100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box;">
</p>
<p style="margin-bottom: 15px;">
<label style="font-weight: 600; display: block; margin-bottom: 5px;"><?php echo esc_html($label_subj); ?></label>
<input type="text" name="ansicocf_subject" value="<?php echo esc_attr($posted_subject); ?>" required style="width:100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box;">
</p>
<p style="margin-bottom: 15px;">
<label style="font-weight: 600; display: block; margin-bottom: 5px;"><?php echo esc_html($label_msg); ?></label>
<textarea name="ansicocf_message" required style="width:100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; resize: none;" rows="5"><?php echo esc_textarea($posted_message); ?></textarea>
</p>
<p style="margin-bottom: 20px;">
<label style="font-weight: 600; display: block; margin-bottom: 5px;"><?php echo esc_html($label_spam); ?> <?php echo "$num1 + $num2 = ?"; ?></label>
<input type="number" name="ansicocf_spam_answer" required style="width:100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box;">
<input type="hidden" name="ansicocf_spam_expected" value="<?php echo $expected; ?>">
</p>
<p style="margin-bottom: 0;">
<input type="hidden" name="ansicocf_block_id" value="<?php echo esc_attr($block_id); ?>">
<input type="submit" name="ansicocf_submit" value="<?php echo esc_attr($btn_text); ?>" style="padding: 12px 24px; background: #007cba; color: #fff; font-weight: bold; border: none; border-radius: 4px; cursor: pointer;">
</p>
</form>
</div>
<?php
return ob_get_clean();
}
/**
* Sends the email with all fields and user email as sender.
*/
function ansico_cf_v1_send_final_mail($name, $email, $subject, $message, $to, $format, $footer_msg, $l_name, $l_email, $l_subj, $l_msg) {
$site_name = get_bloginfo('name');
// Headers: Set sender name and user email as "From" address
$headers = array('Content-Type: text/' . $format . '; charset=UTF-8');
$headers[] = 'Reply-To: ' . $name . ' <' . $email . '>';
$headers[] = 'From: ' . $name . ' <' . $email . '>';
if ($format === 'html') {
$body = "<strong>" . esc_html($l_name) . ":</strong> " . esc_html($name) . "<br>";
$body .= "<strong>" . esc_html($l_email) . ":</strong> " . esc_html($email) . "<br>";
$body .= "<strong>" . esc_html($l_subj) . ":</strong> " . esc_html($subject) . "<br><br>";
$body .= "<strong>" . esc_html($l_msg) . ":</strong><br>" . nl2br(esc_html($message)) . "<br><br>";
$body .= "<hr><em>" . esc_html($footer_msg) . " " . esc_html($site_name) . "</em>";
} else {
$body = $l_name . ": " . $name . "\n";
$body .= $l_email . ": " . $email . "\n";
$body .= $l_subj . ": " . $subject . "\n\n";
$body .= $l_msg . ":\n" . $message . "\n\n";
$body .= "---\n" . $footer_msg . " " . $site_name;
}
wp_mail($to, $subject, $body, $headers);
}
/**
* Register the Gutenberg block.
*/
function ansico_cf_v1_init() {
wp_register_script(
'ansico-cf-js',
plugins_url('block.js', __FILE__),
array('wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components'),
'1.0.0'
);
register_block_type('ansico/contact-form', array(
'editor_script' => 'ansico-cf-js',
'render_callback' => 'ansico_cf_v1_render_callback',
'attributes' => array(
'blockId' => array('type' => 'string', 'default' => ''),
'recipientEmail' => array('type' => 'string', 'default' => ''),
'successMessage' => array('type' => 'string', 'default' => 'Message has been sent'),
'spamErrorMessage' => array('type' => 'string', 'default' => 'Wrong spam protection answer.'),
'footerMessage' => array('type' => 'string', 'default' => 'Message has been sent on website'),
'emailFormat' => array('type' => 'string', 'default' => 'text'),
'labelName' => array('type' => 'string', 'default' => 'Name'),
'labelEmail' => array('type' => 'string', 'default' => 'E-mail'),
'labelSubject' => array('type' => 'string', 'default' => 'Subject'),
'labelMessage' => array('type' => 'string', 'default' => 'Message'),
'labelSpam' => array('type' => 'string', 'default' => 'Spam check:'),
'buttonText' => array('type' => 'string', 'default' => 'Send message'),
)
));
}
add_action('init', 'ansico_cf_v1_init');