Files
Bookshelf/bd-bookshelf/inc/ajax-handlers.php
2026-10-18 19:01:01 -07:00

107 lines
3.6 KiB
PHP

<?php
// Copyright 2026 Umbra Tools (umbratools.com)
// SPDX-License-Identifier: Apache-2.0
/**
* AJAX Handlers
*
* @package BD_Murphy
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Handle contact form submission
*/
function bdm_ajax_contact_submit() {
// Verify nonce
if (!isset($_POST['bdm_contact_nonce']) || !wp_verify_nonce($_POST['bdm_contact_nonce'], 'bdm_contact')) {
wp_send_json_error(array('message' => esc_html__('Security verification failed.', 'bd-bookshelf')));
}
// Check honeypot
if (!empty($_POST['website'])) {
// Silent fail for bots
wp_send_json_success(array('message' => esc_html__('Thank you for your message.', 'bd-bookshelf')));
}
// Sanitize inputs
$name = sanitize_text_field($_POST['name'] ?? '');
$email = sanitize_email($_POST['email'] ?? '');
$subject = sanitize_text_field($_POST['subject'] ?? '');
$message = sanitize_textarea_field($_POST['message'] ?? '');
// Validate
if (empty($name) || empty($email) || empty($subject) || empty($message)) {
wp_send_json_error(array('message' => esc_html__('All fields are required.', 'bd-bookshelf')));
}
if (!is_email($email)) {
wp_send_json_error(array('message' => esc_html__('Please enter a valid email address.', 'bd-bookshelf')));
}
// Email headers
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'Reply-To: ' . $name . ' <' . $email . '>',
);
// Site admin email
$to = get_option('admin_email');
// Send email
$sent = wp_mail(
$to,
sprintf('[Contact] %s', $subject),
nl2br($message),
$headers
);
if ($sent) {
wp_send_json_success(array('message' => esc_html__('Thank you for your message. We will get back to you soon.', 'bd-bookshelf')));
} else {
wp_send_json_error(array('message' => esc_html__('There was an error sending your message. Please try again.', 'bd-bookshelf')));
}
}
add_action('wp_ajax_bdm_contact_submit', 'bdm_ajax_contact_submit');
add_action('wp_ajax_nopriv_bdm_contact_submit', 'bdm_ajax_contact_submit');
/**
* Handle signup form submission (AJAX)
*/
function bdm_ajax_signup_submit() {
// Verify nonce
if (!isset($_POST['bdm_signup_nonce']) || !wp_verify_nonce($_POST['bdm_signup_nonce'], 'bdm_signup')) {
wp_send_json_error(array('message' => esc_html__('Security verification failed.', 'bd-bookshelf')));
}
$email = sanitize_email($_POST['email'] ?? '');
$first_name = sanitize_text_field($_POST['first_name'] ?? '');
$opt_in = isset($_POST['opt_in']) ? (bool) $_POST['opt_in'] : false;
// Validate
if (empty($email) || !is_email($email)) {
wp_send_json_error(array('message' => esc_html__('Please enter a valid email address.', 'bd-bookshelf')));
}
if (!$opt_in) {
wp_send_json_error(array('message' => esc_html__('Please confirm your subscription.', 'bd-bookshelf')));
}
// Check if already subscribed
if (function_exists('BD_SimpleEmail')) {
$result = BD_SimpleEmail::subscribe($email, $first_name);
if ($result) {
wp_send_json_success(array('message' => esc_html__('Thank you for subscribing!', 'bd-bookshelf')));
} else {
wp_send_json_error(array('message' => esc_html__('You are already subscribed or cannot be added.', 'bd-bookshelf')));
}
}
wp_send_json_error(array('message' => esc_html__('Newsletter plugin is not active.', 'bd-bookshelf')));
}
add_action('wp_ajax_bdm_signup_ajax', 'bdm_ajax_signup_submit');
add_action('wp_ajax_nopriv_bdm_signup_ajax', 'bdm_ajax_signup_submit');