Files
Bookshelf/umbra-bookshelf/includes/email/class-umbra-email-sender.php
bdm dec732931c Consolidate three plugins into Umbra Bookshelf
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.
2026-10-18 19:55:36 -07:00

211 lines
6.5 KiB
PHP

<?php
// Copyright 2026 Umbra Tools (umbratools.com)
// SPDX-License-Identifier: Apache-2.0
/**
* Email Sender Class
*
* @package Umbra_Simple_Email
*/
if (!defined('ABSPATH')) {
exit;
}
class Umbra_Email_Sender {
/**
* Send newsletter to all subscribers
*
* @param string $subject Newsletter subject.
* @param string $content Newsletter HTML content.
* @return array
*/
public static function send_newsletter($subject, $content) {
$settings = Umbra_Simple_Email::get_settings();
if (empty($settings['gmail_address']) || empty($settings['gmail_password'])) {
return array(
'success' => false,
'message' => esc_html__('Gmail not configured', 'umbra-bookshelf'),
);
}
$subscribers = Umbra_Simple_Email::get_subscribers();
if (empty($subscribers)) {
return array(
'success' => false,
'message' => esc_html__('No subscribers found', 'umbra-bookshelf'),
);
}
$business = Umbra_Simple_Email::get_business_info();
$html = self::build_email_html($subject, $content, $business);
$sent = 0;
foreach ($subscribers as $subscriber) {
if ($subscriber->status === 'subscribed') {
$result = self::send_single_email(
$subscriber->email,
$subject,
$html,
$subscriber->first_name
);
if ($result) {
$sent++;
}
}
}
return array(
'success' => true,
'count' => $sent,
);
}
/**
* Send single email
*
* @param string $to Recipient email.
* @param string $subject Email subject.
* @param string $html Email HTML.
* @param string $first_name First name.
* @return bool
*/
private static function send_single_email($to, $subject, $html, $first_name = '') {
$settings = Umbra_Simple_Email::get_settings();
// Replace placeholder with actual unsubscribe link
$token = Umbra_Simple_Email::generate_unsubscribe_token($to);
$unsubscribe_url = add_query_arg(array(
'email' => rawurlencode($to),
'token' => $token,
), home_url('/unsubscribe/'));
$html = str_replace('{{unsubscribe_url}}', esc_url($unsubscribe_url), $html);
$html = str_replace('{{subscriber_email}}', esc_html($to), $html);
if ($first_name) {
$html = str_replace('{{first_name}}', esc_html($first_name), $html);
} else {
$html = str_replace('{{first_name}}', '', $html);
}
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . $settings['sender_name'] . ' <' . $settings['gmail_address'] . '>',
);
if (!empty($settings['reply_to'])) {
$headers[] = 'Reply-To: ' . $settings['reply_to'];
}
$headers[] = 'MIME-Version: 1.0';
return wp_mail($to, $subject, $html, $headers);
}
/**
* Build email HTML
*
* @param string $subject Subject.
* @param string $content Content.
* @param array $business Business info.
* @return string
*/
private static function build_email_html($subject, $content, $business) {
$site_url = get_bloginfo('url');
$site_name = get_bloginfo('name');
$address_html = '';
if ($business['name']) {
$address_html = '<p>' . esc_html($business['name']) . '<br>';
if ($business['address_1']) {
$address_html .= esc_html($business['address_1']) . '<br>';
}
if ($business['city']) {
$address_html .= esc_html($business['city']);
if ($business['country']) {
$address_html .= ', ' . esc_html($business['country']);
}
}
$address_html .= '</p>';
}
return '<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>' . esc_html($subject) . '</title>
<style>
body { margin: 0; padding: 0; font-family: Georgia, serif; font-size: 16px; line-height: 1.6; color: #2C2C2C; background-color: #F5F0E8; }
.email-container { max-width: 600px; margin: 0 auto; background-color: #ffffff; }
.email-header { background-color: #1B3A6B; padding: 30px; text-align: center; }
.email-header h1 { color: #C8A96E; margin: 0; font-size: 28px; }
.email-body { padding: 30px; }
.email-footer { background-color: #EDE4D0; padding: 20px 30px; text-align: center; font-size: 12px; color: #555555; }
a { color: #2E6DA4; }
.unsubscribe-link { color: #888888; text-decoration: underline; }
h2 { color: #1B3A6B; }
p { margin: 0 0 15px; }
</style>
</head>
<body>
<div class="email-container">
<div class="email-header">
<h1>' . esc_html($site_name) . '</h1>
</div>
<div class="email-body">
' . $content . '
</div>
<div class="email-footer">
<p>
<a href="' . esc_url($site_url) . '">' . esc_html($site_url) . '</a>
</p>
' . $address_html . '
<p>
<a href="{{unsubscribe_url}}" class="unsubscribe-link">Unsubscribe</a> from this mailing list.
</p>
<p>You received this email because you signed up for updates from ' . esc_html($site_name) . '.</p>
</div>
</div>
</body>
</html>';
}
/**
* Encrypt password
*
* @param string $password Plain password.
* @return string
*/
public static function encrypt_password($password) {
$key = defined('AUTH_KEY') ? AUTH_KEY : 'bdm_email_secret';
$iv = openssl_random_pseudo_bytes(16);
$encrypted = openssl_encrypt($password, 'AES-256-CBC', $key, 0, $iv);
return base64_encode($iv . $encrypted);
}
/**
* Decrypt password
*
* @param string $encrypted Encrypted password.
* @return string
*/
public static function decrypt_password($encrypted) {
$data = base64_decode($encrypted);
$key = defined('AUTH_KEY') ? AUTH_KEY : 'bdm_email_secret';
$iv = substr($data, 0, 16);
$ciphertext = substr($data, 16);
return openssl_decrypt($ciphertext, 'AES-256-CBC', $key, 0, $iv);
}
}