Merge BD Bookshelf (0.1.0), BD BookShelfHelper (2.0.0) and BD SimpleEmail
(2.0.0) into a single plugin released as Umbra Bookshelf 1.0.0, with the
BD Bookshelf theme left as a separate, unrenamed artifact.
Modules
includes/books/ catalogue, ISBN/ASIN, series, retail, ratings, shortcodes,
SEO, GDPR, scrapers, admin, CSV/post import
includes/library/ book post type + series taxonomy, pen names, data entry,
shortlinks, reviews, migrate
includes/email/ subscribers, newsletters, composition, sending
Renamed: plugin headers, text domains (all three -> umbra-bookshelf), classes
(BD_*/BDM_* -> Umbra_*), constants (-> UMBRA_BOOKSHELF_*), asset handles, JS
globals and admin slugs.
Deliberately not renamed -- persisted contract with live installs and with
published post content: table names, option keys, post meta keys, transients,
nonce actions, cron hooks, AJAX actions, shortcodes ([bd-book], [bd-books],
[bdm_signup]), post type `book`, taxonomy `series`, cookies and CSS classes.
BD_BookShelfHelper survives only as a class_alias for the unmodified theme.
Collisions resolved
* class-umbra-theme-compatibility.php declared BD_BookShelfHelper, the same
name as the library module's helper. Latent while the plugins were
separate (only fired when both were active), but an unconditional fatal
once merged. Renamed to Umbra_Theme_Compatibility with a class_alias.
* All three modules called add_menu_page. One top-level menu is now
registered by the bootstrap at admin_menu priority 9.
* BD Bookshelf and BookShelfHelper both claimed bd-bookshelf-settings, leaving
one Settings screen unreachable -> umbra-bookshelf-settings and
umbra-bookshelf-library-settings.
* Catalogue and library both enqueued bd-bookshelf-admin; the second enqueue
was silently discarded -> unique handles per module.
* Umbra_Admin and Umbra_Migration both registered bd_bookshelf_import_csv, so
one handler silently replaced the other. The import page now lives only in
Umbra_Admin (the variant wired to the admin JS); Umbra_Migration keeps only
the preview/run endpoints it uniquely provides.
* Modules were constructed from an init callback, so their own init
registrations raced the hook being dispatched. Now constructed on
plugins_loaded behind a re-entrancy guard.
Also
* Missing assets created: assets/images/default-cover.png (referenced but
never committed, so every cover-less book 404'd) and email/assets/js/
admin.js (enqueued on every email admin page but never committed).
* Library module's strpos($hook, 'bd-bookshelf') asset check matched every
catalogue and email screen once slugs shared the umbra-bookshelf- prefix;
now matched against its own slug list.
* Consolidated uninstall deduplicates the double drop of bd_books/bd_series.
* Specs aligned to the new name; pre-merge specs retained under
spec/archive/ with provenance notes.
* Tests added: tests/verify_structure.py (static) and tests/smoke_runtime.php
(WordPress-stub runtime boot, front-end and admin contexts).
Deferred and recorded in docs/KNOWN-ISSUES.md: the catalogue and library
modules still create bd_books and bd_series with two incompatible schemas
(pre-existing, now unconditional), and Goodreads/Amazon scraping is still
implemented twice on shared cron hooks.
366 lines
12 KiB
PHP
366 lines
12 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Main SimpleEmail Plugin Class
|
|
*
|
|
* @package Umbra_Simple_Email
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Umbra_Simple_Email {
|
|
private static $instance = null;
|
|
|
|
/**
|
|
* Get instance
|
|
*
|
|
* @return Umbra_Simple_Email
|
|
*/
|
|
public static function instance() {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
private function __construct() {
|
|
$this->init_hooks();
|
|
}
|
|
|
|
/**
|
|
* Initialize 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'));
|
|
}
|
|
|
|
/**
|
|
* Add admin menu pages
|
|
*/
|
|
public function add_menu_pages() {
|
|
// The top-level "Umbra Bookshelf" menu is registered once by the plugin
|
|
// bootstrap (Umbra_Bookshelf::register_admin_menu); this module only
|
|
// contributes submenus beneath it.
|
|
add_submenu_page(
|
|
'umbra-bookshelf',
|
|
esc_html__('Subscribers', 'umbra-bookshelf'),
|
|
esc_html__('Subscribers', 'umbra-bookshelf'),
|
|
'manage_options',
|
|
'umbra-bookshelf-subscribers',
|
|
array('Umbra_Simple_Email_Admin', 'subscribers_page')
|
|
);
|
|
|
|
add_submenu_page(
|
|
'umbra-bookshelf',
|
|
esc_html__('New Newsletter', 'umbra-bookshelf'),
|
|
esc_html__('New Newsletter', 'umbra-bookshelf'),
|
|
'manage_options',
|
|
'umbra-bookshelf-newsletter',
|
|
array('Umbra_Simple_Email_Admin', 'new_newsletter_page')
|
|
);
|
|
|
|
add_submenu_page(
|
|
'umbra-bookshelf',
|
|
esc_html__('Email Settings', 'umbra-bookshelf'),
|
|
esc_html__('Email Settings', 'umbra-bookshelf'),
|
|
'manage_options',
|
|
'umbra-bookshelf-email-settings',
|
|
array('Umbra_Simple_Email_Admin', 'settings_page')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Handle signup form submission
|
|
*/
|
|
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']);
|
|
|
|
if (!$email) {
|
|
wp_safe_redirect(add_query_arg('error', 'invalid', home_url('/signup/')));
|
|
exit;
|
|
}
|
|
|
|
$first_name = sanitize_text_field($_POST['first_name'] ?? '');
|
|
|
|
$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;
|
|
}
|
|
|
|
/**
|
|
* Handle unsubscribe form submission
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Handle AJAX signup
|
|
*/
|
|
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' => esc_html__('Invalid email address', 'umbra-bookshelf')));
|
|
}
|
|
|
|
$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' => esc_html__('Please confirm your subscription', 'umbra-bookshelf')));
|
|
}
|
|
|
|
$result = self::subscribe($email, $first_name);
|
|
|
|
if ($result) {
|
|
wp_send_json_success(array('message' => esc_html__('Thank you for subscribing!', 'umbra-bookshelf')));
|
|
} else {
|
|
wp_send_json_error(array('message' => esc_html__('You are already subscribed or cannot be added', 'umbra-bookshelf')));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Subscribe a new email
|
|
*
|
|
* @param string $email Email address.
|
|
* @param string $first_name First name.
|
|
* @return bool
|
|
*/
|
|
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 !== false;
|
|
}
|
|
|
|
/**
|
|
* Unsubscribe an email
|
|
*
|
|
* @param string $email Email address.
|
|
* @return bool
|
|
*/
|
|
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)
|
|
) !== false;
|
|
}
|
|
|
|
/**
|
|
* Get subscribers
|
|
*
|
|
* @param string $status Status filter.
|
|
* @return array
|
|
*/
|
|
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
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Get subscriber count
|
|
*
|
|
* @param string $status Status filter.
|
|
* @return int
|
|
*/
|
|
public static function get_subscriber_count($status = 'subscribed') {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'bd_subscribers';
|
|
|
|
return (int) $wpdb->get_var($wpdb->prepare(
|
|
"SELECT COUNT(*) FROM {$table_name} WHERE status = %s",
|
|
$status
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Get plugin settings
|
|
*
|
|
* @return array
|
|
*/
|
|
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', ''),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get business info from theme options
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function get_business_info() {
|
|
$options = get_option('bdm_theme_options', array());
|
|
|
|
return array(
|
|
'name' => $options['business_name'] ?? '',
|
|
'address_1' => $options['business_address_1'] ?? '',
|
|
'address_2' => $options['business_address_2'] ?? '',
|
|
'city' => $options['business_city'] ?? '',
|
|
'country' => $options['business_country'] ?? '',
|
|
'phone' => $options['business_phone'] ?? '',
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Generate unsubscribe token
|
|
*
|
|
* @param string $email Email address.
|
|
* @return string
|
|
*/
|
|
public static function generate_unsubscribe_token($email) {
|
|
$secret_key = defined('AUTH_KEY') ? AUTH_KEY : 'bdm_unsubscribe_secret';
|
|
return hash_hmac('sha256', $email, $secret_key);
|
|
}
|
|
|
|
/**
|
|
* Verify unsubscribe token
|
|
*
|
|
* @param string $email Email address.
|
|
* @param string $token Token to verify.
|
|
* @return bool
|
|
*/
|
|
public static function verify_unsubscribe_token($email, $token) {
|
|
return hash_equals(self::generate_unsubscribe_token($email), $token);
|
|
}
|
|
|
|
/**
|
|
* Signup shortcode
|
|
*
|
|
* @param array $atts Shortcode attributes.
|
|
* @return string
|
|
*/
|
|
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', 'umbra-bookshelf'); ?> *</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', 'umbra-bookshelf'); ?></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.', 'umbra-bookshelf'); ?></label>
|
|
</p>
|
|
<p class="form-submit">
|
|
<button type="submit" class="submit-btn"><?php esc_html_e('Subscribe', 'umbra-bookshelf'); ?></button>
|
|
</p>
|
|
<div class="form-response" style="display:none;"></div>
|
|
</form>
|
|
<script>
|
|
(function() {
|
|
'use strict';
|
|
var form = document.getElementById('bdm-signup-form');
|
|
if (!form) return;
|
|
|
|
form.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
var formData = new FormData(form);
|
|
formData.append('action', 'bdm_ajax_signup');
|
|
var response = form.querySelector('.form-response');
|
|
|
|
fetch('<?php echo admin_url('admin-ajax.php'); ?>', {
|
|
method: 'POST',
|
|
body: formData,
|
|
credentials: 'same-origin'
|
|
})
|
|
.then(function(res) { return res.json(); })
|
|
.then(function(data) {
|
|
if (data.success) {
|
|
response.innerHTML = '<p class="success">' + data.data.message + '</p>';
|
|
form.reset();
|
|
} else {
|
|
response.innerHTML = '<p class="error">' + data.data.message + '</p>';
|
|
}
|
|
response.style.display = 'block';
|
|
})
|
|
.catch(function() {
|
|
response.innerHTML = '<p class="error"><?php esc_html_e('An error occurred. Please try again.', 'umbra-bookshelf'); ?></p>';
|
|
response.style.display = 'block';
|
|
});
|
|
});
|
|
})();
|
|
</script>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
}
|