282 lines
9.8 KiB
PHP
282 lines
9.8 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Main SimpleEmail Plugin Class
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class BD_SimpleEmail {
|
|
private static $instance = null;
|
|
|
|
public static function instance() {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
private function __construct() {
|
|
$this->init_hooks();
|
|
}
|
|
|
|
private function init_hooks() {
|
|
add_action('admin_menu', array($this, 'add_menu_pages'));
|
|
add_action('admin_post_bdm_signup_submit', array($this, 'handle_signup'));
|
|
add_action('admin_post_bdm_unsubscribe_submit', array($this, 'handle_unsubscribe'));
|
|
add_action('wp_ajax_bdm_ajax_signup', array($this, 'ajax_signup'));
|
|
add_action('wp_ajax_nopriv_bdm_ajax_signup', array($this, 'ajax_signup'));
|
|
|
|
add_shortcode('bdm_signup', array($this, 'signup_shortcode'));
|
|
}
|
|
|
|
public function add_menu_pages() {
|
|
add_menu_page(
|
|
__('BD SimpleEmail', 'bd-simpleemail'),
|
|
__('BD SimpleEmail', 'bd-simpleemail'),
|
|
'manage_options',
|
|
'bd-simpleemail',
|
|
'',
|
|
'dashicons-email',
|
|
32
|
|
);
|
|
|
|
add_submenu_page(
|
|
'bd-simpleemail',
|
|
__('Subscribers', 'bd-simpleemail'),
|
|
__('Subscribers', 'bd-simpleemail'),
|
|
'manage_options',
|
|
'bd-email-subscribers',
|
|
array('BD_SimpleEmail_Admin', 'subscribers_page')
|
|
);
|
|
|
|
add_submenu_page(
|
|
'bd-simpleemail',
|
|
__('New Newsletter', 'bd-simpleemail'),
|
|
__('New Newsletter', 'bd-simpleemail'),
|
|
'manage_options',
|
|
'bd-email-new',
|
|
array('BD_SimpleEmail_Admin', 'new_newsletter_page')
|
|
);
|
|
|
|
add_submenu_page(
|
|
'bd-simpleemail',
|
|
__('Settings', 'bd-simpleemail'),
|
|
__('Settings', 'bd-simpleemail'),
|
|
'manage_options',
|
|
'bd-email-settings',
|
|
array('BD_SimpleEmail_Admin', 'settings_page')
|
|
);
|
|
}
|
|
|
|
public function handle_signup() {
|
|
if (!isset($_POST['bdm_signup_nonce']) || !wp_verify_nonce($_POST['bdm_signup_nonce'], 'bdm_signup')) {
|
|
wp_safe_redirect(add_query_arg('error', 'invalid', home_url('/signup/')));
|
|
exit;
|
|
}
|
|
|
|
$email = sanitize_email($_POST['email']);
|
|
$first_name = sanitize_text_field($_POST['first_name']);
|
|
|
|
if (!$email) {
|
|
wp_safe_redirect(add_query_arg('error', 'invalid', home_url('/signup/')));
|
|
exit;
|
|
}
|
|
|
|
$result = self::subscribe($email, $first_name);
|
|
|
|
if ($result) {
|
|
wp_safe_redirect(add_query_arg('success', '1', home_url('/signup/')));
|
|
} else {
|
|
wp_safe_redirect(add_query_arg('error', 'exists', home_url('/signup/')));
|
|
}
|
|
exit;
|
|
}
|
|
|
|
public function handle_unsubscribe() {
|
|
if (!isset($_POST['bdm_unsubscribe_nonce']) || !wp_verify_nonce($_POST['bdm_unsubscribe_nonce'], 'bdm_unsubscribe')) {
|
|
wp_safe_redirect(add_query_arg('error', 'invalid', home_url('/unsubscribe/')));
|
|
exit;
|
|
}
|
|
|
|
$email = sanitize_email($_POST['email']);
|
|
|
|
if ($email) {
|
|
self::unsubscribe($email);
|
|
}
|
|
|
|
wp_safe_redirect(add_query_arg('success', '1', home_url('/unsubscribe/')));
|
|
exit;
|
|
}
|
|
|
|
public function ajax_signup() {
|
|
check_ajax_referer('bdm_ajax_signup');
|
|
|
|
$email = sanitize_email($_POST['email']);
|
|
|
|
if (!$email || !is_email($email)) {
|
|
wp_send_json_error(array('message' => 'Invalid email address'));
|
|
}
|
|
|
|
$first_name = isset($_POST['first_name']) ? sanitize_text_field($_POST['first_name']) : '';
|
|
$opt_in = isset($_POST['opt_in']) && $_POST['opt_in'];
|
|
|
|
if (!$opt_in) {
|
|
wp_send_json_error(array('message' => 'Please confirm your subscription'));
|
|
}
|
|
|
|
$result = self::subscribe($email, $first_name);
|
|
|
|
if ($result) {
|
|
wp_send_json_success(array('message' => 'Thank you for subscribing!'));
|
|
} else {
|
|
wp_send_json_error(array('message' => 'You are already subscribed or cannot be added'));
|
|
}
|
|
}
|
|
|
|
public static function subscribe($email, $first_name = '') {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'bd_subscribers';
|
|
|
|
$exists = $wpdb->get_var($wpdb->prepare(
|
|
"SELECT id FROM {$table_name} WHERE email = %s",
|
|
$email
|
|
));
|
|
|
|
if ($exists) {
|
|
return false;
|
|
}
|
|
|
|
$result = $wpdb->insert($table_name, array(
|
|
'email' => $email,
|
|
'first_name' => $first_name,
|
|
'status' => 'subscribed',
|
|
'subscribed_at' => current_time('mysql'),
|
|
'ip_address' => $_SERVER['REMOTE_ADDR'],
|
|
));
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function unsubscribe($email) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'bd_subscribers';
|
|
|
|
return $wpdb->update($table_name, array(
|
|
'status' => 'unsubscribed',
|
|
'unsubscribed_at' => current_time('mysql'),
|
|
), array('email' => $email));
|
|
}
|
|
|
|
public static function get_subscribers($status = 'subscribed') {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'bd_subscribers';
|
|
|
|
return $wpdb->get_results($wpdb->prepare(
|
|
"SELECT * FROM {$table_name} WHERE status = %s ORDER BY subscribed_at DESC",
|
|
$status
|
|
));
|
|
}
|
|
|
|
public static function get_subscriber_count($status = 'subscribed') {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'bd_subscribers';
|
|
|
|
return $wpdb->get_var($wpdb->prepare(
|
|
"SELECT COUNT(*) FROM {$table_name} WHERE status = %s",
|
|
$status
|
|
));
|
|
}
|
|
|
|
public static function get_settings() {
|
|
return array(
|
|
'gmail_address' => get_option('bdm_gmail_address', ''),
|
|
'gmail_password' => get_option('bdm_gmail_password', ''),
|
|
'sender_name' => get_option('bdm_sender_name', get_bloginfo('name')),
|
|
'reply_to' => get_option('bdm_reply_to', ''),
|
|
'gemini_api_key' => get_option('bdm_gemini_api_key', ''),
|
|
);
|
|
}
|
|
|
|
public static function get_business_info() {
|
|
$options = get_option('bdm_theme_options', array());
|
|
|
|
return array(
|
|
'name' => isset($options['business_name']) ? $options['business_name'] : '',
|
|
'address_1' => isset($options['business_address_1']) ? $options['business_address_1'] : '',
|
|
'address_2' => isset($options['business_address_2']) ? $options['business_address_2'] : '',
|
|
'city' => isset($options['business_city']) ? $options['business_city'] : '',
|
|
'country' => isset($options['business_country']) ? $options['business_country'] : '',
|
|
'phone' => isset($options['business_phone']) ? $options['business_phone'] : '',
|
|
);
|
|
}
|
|
|
|
public static function generate_unsubscribe_token($email) {
|
|
$secret_key = defined('AUTH_KEY') ? AUTH_KEY : 'bdm_unsubscribe_secret';
|
|
|
|
return hash_hmac('sha256', $email, $secret_key);
|
|
}
|
|
|
|
public static function verify_unsubscribe_token($email, $token) {
|
|
return hash_equals(self::generate_unsubscribe_token($email), $token);
|
|
}
|
|
|
|
public function signup_shortcode($atts = array()) {
|
|
$atts = shortcode_atts(array(), $atts);
|
|
|
|
ob_start();
|
|
?>
|
|
<form id="bdm-signup-form" class="bdm-signup-form">
|
|
<?php wp_nonce_field('bdm_ajax_signup', 'bdm_signup_nonce'); ?>
|
|
<p class="form-field">
|
|
<label for="bdm-email"><?php esc_html_e('Email Address', 'bd-simpleemail'); ?> *</label>
|
|
<input type="email" id="bdm-email" name="email" required>
|
|
</p>
|
|
<p class="form-field">
|
|
<label for="bdm-first-name"><?php esc_html_e('First Name', 'bd-simpleemail'); ?></label>
|
|
<input type="text" id="bdm-first-name" name="first_name">
|
|
</p>
|
|
<p class="form-field checkbox-field">
|
|
<input type="checkbox" id="bdm-opt-in" name="opt_in" value="1" required>
|
|
<label for="bdm-opt-in"><?php esc_html_e('I agree to receive email updates.', 'bd-simpleemail'); ?></label>
|
|
</p>
|
|
<p class="form-submit">
|
|
<button type="submit" class="submit-btn"><?php esc_html_e('Subscribe', 'bd-simpleemail'); ?></button>
|
|
</p>
|
|
<div class="form-response" style="display:none;"></div>
|
|
</form>
|
|
<script>
|
|
jQuery(document).ready(function($) {
|
|
$('#bdm-signup-form').on('submit', function(e) {
|
|
e.preventDefault();
|
|
var form = $(this);
|
|
var response = form.find('.form-response');
|
|
|
|
$.ajax({
|
|
type: 'POST',
|
|
url: '<?php echo admin_url('admin-ajax.php'); ?>',
|
|
data: form.serialize() + '&action=bdm_ajax_signup',
|
|
success: function(response) {
|
|
if (response.success) {
|
|
response.html('<p class="success">' + response.data.message + '</p>').show();
|
|
form[0].reset();
|
|
} else {
|
|
response.html('<p class="error">' + response.data.message + '</p>').show();
|
|
}
|
|
},
|
|
error: function() {
|
|
response.html('<p class="error"><?php esc_html_e('An error occurred. Please try again.', 'bd-simpleemail'); ?></p>').show();
|
|
}
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
}
|