Closes the three problems that blocked the consolidation and records the rest
as tasks.
Licence: GPLv2 or later (WordPress.org compatible)
LICENSE is now the full GPLv2 text and NOTICE is gone (it was Apache
specific). All 101 SPDX headers read GPL-2.0-or-later and every shipped
header says "GPLv2 or later".
This also fixes an inconsistency that predated the consolidation: the theme's
style.css declared GPLv2 while its PHP files carried Apache headers.
Old/ and archive/pre-umbra-releases/ keep their original Apache headers on
purpose -- they are frozen snapshots, not shipped code.
Schema: the catalogue schema is canonical for bd_books and bd_series
Adds Umbra_Schema_Migration. The catalogue and library modules previously
both created those two tables with different columns, so whichever plugin
activated first won and the other read and wrote against the wrong shape.
The migration detects a library-shaped table, RENAMES it aside (never drops
it), creates the canonical table and copies rows across:
- book IDs preserved, keeping bd_book_meta, bd_reviews, bd_ratings and
bd_review_shortlinks valid
- title/slug/description resolved from the linked post
- the library's single isbn/asin land on the ebook columns; amazon_asin is
also set so existing product links keep working
- series migrated first so books.series_id still resolves
- post linkage rewritten as _bd_bookshelf_book_id (preserved meta key)
- publish_date kept as _bd_bookshelf_publish_date (no catalogue column)
- bd_ratings goodreads/amazon folded into rating_goodreads/rating_amazon
- rows whose post is gone are skipped with a log line, not turned into
unusable books
- duplicate slugs de-duplicated, since catalogue slug is UNIQUE
It runs before dbDelta, and on admin_init as well as activation, because
plugin updates do not re-run activation hooks. Ordering matters: dbDelta adds
columns but never drops them, so pointed at a library-shaped table it would
graft on catalogue columns and leave a table satisfying neither schema.
Umbra_Bookshelf_Helper_Install no longer creates bd_books or bd_series.
Scraping: Goodreads only, one implementation
There were three Goodreads implementations and two Amazon ones; both library
"scrapers" were empty stubs attached to cron events that fired on schedule to
do nothing, and both real scraper classes were never called at all.
Umbra_Amazon_Scraper is deleted and the duplicate HTML parsing is gone from
Umbra_Rating_Sync, which now delegates to Umbra_Goodreads_Scraper. The two
library stubs and their cron registrations are removed. One schedule
survives: bd_bookshelf_sync_ratings, every 12 hours.
The rating_amazon column is retained (it holds data and is populated during
migration) and Amazon retail links are unaffected.
A new rating fetch failure keeps the last known value instead of writing zero.
The admin ratings notice no longer warns about Amazon scraping.
Umbra_SEO is now instantiated
It registers its hooks from a constructor and the bootstrap never constructed
it, so the class was dead code and no SEO output was produced. Its meta, Open
Graph, Twitter card and sitemap hooks now attach. Completing SEO for generated
book detail pages is docs/TASKS.md #2.
Assets that 404'd
- assets/images/default-cover.png now holds a real 512x815 cover
(WorkingTitleCover_512x815.png). The helper returned this path but the
repository contained no images at all.
- email/assets/js/admin.js is committed as an empty placeholder. It was
enqueued on every email admin page but had never been committed.
Tests
- tests/schema_migration_test.php: 42 checks against a real database, driven
through the sqlite3 CLI on a throwaway file. Covers shape detection, row
mapping, ID preservation, orphan handling, rating folding, idempotency and
slug collisions.
- tests/run_all.sh runs every suite.
- The declared version is now checked against the plugin header rather than a
literal, so a version bump cannot silently break the suite.
Docs
- KNOWN-ISSUES.md rewritten: three resolved, three remaining.
- TASKS.md added: email admin behaviour, book detail page SEO, batching the
rating sync, reclaiming legacy tables, reconciling the two book models,
retiring the Old/ archive.
- SPEC and ARCHITECTURE updated for schema ownership, the migration ordering
constraint, and the single scraper.
Version 1.1.0.
391 lines
15 KiB
PHP
391 lines
15 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/**
|
|
* Email Admin Class
|
|
*
|
|
* @package Umbra_Simple_Email
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Umbra_Simple_Email_Admin {
|
|
private static $instance = null;
|
|
|
|
/**
|
|
* Get instance
|
|
*
|
|
* @return Umbra_Simple_Email_Admin
|
|
*/
|
|
public static function instance() {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
private function __construct() {
|
|
add_action('admin_enqueue_scripts', array($this, 'enqueue_assets'));
|
|
}
|
|
|
|
/**
|
|
* Enqueue admin assets
|
|
*
|
|
* @param string $hook Current admin page.
|
|
*/
|
|
public function enqueue_assets($hook) {
|
|
// Matches the submenu slugs registered by Umbra_Simple_Email::add_menu_pages().
|
|
if (strpos($hook, 'umbra-bookshelf-subscribers') !== false
|
|
|| strpos($hook, 'umbra-bookshelf-newsletter') !== false
|
|
|| strpos($hook, 'umbra-bookshelf-email-settings') !== false) {
|
|
|
|
wp_enqueue_style(
|
|
'umbra-bookshelf-email-admin',
|
|
UMBRA_BOOKSHELF_URL . 'email/assets/css/admin.css',
|
|
array(),
|
|
UMBRA_BOOKSHELF_EMAIL_VERSION
|
|
);
|
|
|
|
wp_enqueue_script(
|
|
'umbra-bookshelf-email-admin',
|
|
UMBRA_BOOKSHELF_URL . 'email/assets/js/admin.js',
|
|
array('jquery'),
|
|
UMBRA_BOOKSHELF_EMAIL_VERSION,
|
|
true
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Subscribers page
|
|
*/
|
|
public static function subscribers_page() {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'bd_subscribers';
|
|
|
|
if (isset($_POST['bd_export_subscribers'])) {
|
|
check_admin_referer('bd_export_subscribers');
|
|
|
|
header('Content-Type: text/csv');
|
|
header('Content-Disposition: attachment; filename=subscribers-' . date('Y-m-d') . '.csv');
|
|
|
|
$subscribers = $wpdb->get_results(
|
|
"SELECT email, first_name, status, subscribed_at FROM {$table_name} WHERE status = 'subscribed'"
|
|
);
|
|
|
|
printf("Email,First Name,Status,Subscribed Date\n");
|
|
foreach ($subscribers as $sub) {
|
|
printf("%s,%s,%s,%s\n",
|
|
esc_html($sub->email),
|
|
esc_html($sub->first_name),
|
|
esc_html($sub->status),
|
|
esc_html($sub->subscribed_at)
|
|
);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
$subscribers = $wpdb->get_results("SELECT * FROM {$table_name} ORDER BY subscribed_at DESC");
|
|
$total = count($subscribers);
|
|
$subscribed = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name} WHERE status = 'subscribed'");
|
|
$unsubscribed = $wpdb->get_var("SELECT COUNT(*) FROM {$table_name} WHERE status = 'unsubscribed'");
|
|
|
|
echo '<div class="wrap">';
|
|
echo '<h1>' . esc_html__('Subscribers', 'bd-simpleemail') . '</h1>';
|
|
|
|
echo '<div class="bd-email-stats">';
|
|
printf('<div class="stat-box"><h2>%d</h2><p>%s</p></div>', esc_html($total), esc_html__('Total', 'bd-simpleemail'));
|
|
printf('<div class="stat-box"><h2>%d</h2><p>%s</p></div>', esc_html($subscribed), esc_html__('Active', 'bd-simpleemail'));
|
|
printf('<div class="stat-box"><h2>%d</h2><p>%s</p></div>', esc_html($unsubscribed), esc_html__('Unsubscribed', 'bd-simpleemail'));
|
|
echo '</div>';
|
|
|
|
echo '<form method="post">';
|
|
wp_nonce_field('bd_export_subscribers');
|
|
echo '<p><input type="submit" name="bd_export_subscribers" class="button" value="' . esc_attr__('Export CSV', 'bd-simpleemail') . '"></p>';
|
|
echo '</form>';
|
|
|
|
echo '<table class="widefat">';
|
|
echo '<thead><tr><th>ID</th><th>Email</th><th>First Name</th><th>Status</th><th>Subscribed</th><th>Unsubscribed</th></tr></thead>';
|
|
echo '<tbody>';
|
|
|
|
foreach ($subscribers as $sub) {
|
|
printf('<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',
|
|
esc_html($sub->id),
|
|
esc_html($sub->email),
|
|
esc_html($sub->first_name),
|
|
esc_html($sub->status),
|
|
esc_html($sub->subscribed_at),
|
|
$sub->unsubscribed_at ? esc_html($sub->unsubscribed_at) : '-'
|
|
);
|
|
}
|
|
|
|
echo '</tbody></table></div>';
|
|
}
|
|
|
|
/**
|
|
* New newsletter page
|
|
*/
|
|
public static function new_newsletter_page() {
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if (isset($_POST['bd_send_newsletter']) && check_admin_referer('bd_send_newsletter')) {
|
|
$subject = sanitize_text_field($_POST['subject'] ?? '');
|
|
$content = wp_kses_post($_POST['content'] ?? '');
|
|
$ai_generate = isset($_POST['ai_generate']);
|
|
$publish_blog = isset($_POST['publish_blog']);
|
|
|
|
if ($ai_generate && $subject) {
|
|
$prompt = sanitize_textarea_field($_POST['ai_prompt'] ?? '');
|
|
$content = self::generate_newsletter($subject, $prompt);
|
|
}
|
|
|
|
if ($content) {
|
|
$result = Umbra_Email_Sender::send_newsletter($subject, $content);
|
|
|
|
if ($result['success']) {
|
|
self::log_newsletter($subject, $content, $result['count']);
|
|
$success = sprintf(
|
|
esc_html__('Newsletter sent to %d subscribers!', 'bd-simpleemail'),
|
|
$result['count']
|
|
);
|
|
|
|
if ($publish_blog && $subject) {
|
|
self::publish_as_blog_post($subject, $content);
|
|
}
|
|
} else {
|
|
$error = $result['message'];
|
|
}
|
|
} else {
|
|
$error = esc_html__('Content is required', 'bd-simpleemail');
|
|
}
|
|
}
|
|
|
|
$settings = Umbra_Simple_Email::get_settings();
|
|
$has_ai = !empty($settings['gemini_api_key']);
|
|
|
|
echo '<div class="wrap">';
|
|
echo '<h1>' . esc_html__('New Newsletter', 'bd-simpleemail') . '</h1>';
|
|
|
|
if ($error) {
|
|
printf('<div class="error"><p>%s</p></div>', esc_html($error));
|
|
}
|
|
|
|
if ($success) {
|
|
printf('<div class="updated"><p>%s</p></div>', esc_html($success));
|
|
}
|
|
|
|
if (!$has_ai) {
|
|
echo '<div class="notice notice-warning"><p>' . esc_html__('Gemini API key not configured. AI generation disabled.', 'bd-simpleemail') . '</p></div>';
|
|
}
|
|
|
|
echo '<form method="post" id="newsletter-form">';
|
|
wp_nonce_field('bd_send_newsletter');
|
|
|
|
echo '<table class="form-table">';
|
|
echo '<tr><th>' . esc_html__('Subject', 'bd-simpleemail') . '</th>';
|
|
echo '<td><input type="text" name="subject" class="large-text" required></td></tr>';
|
|
|
|
if ($has_ai) {
|
|
echo '<tr><th>' . esc_html__('AI Generate', 'bd-simpleemail') . '</th>';
|
|
echo '<td><input type="checkbox" name="ai_generate" id="ai_generate" value="1"> ';
|
|
echo '<label for="ai_generate">' . esc_html__('Use Gemini AI to generate content', 'bd-simpleemail') . '</label></td></tr>';
|
|
|
|
echo '<tr class="ai-prompt-row" style="display:none;"><th>' . esc_html__('Prompt', 'bd-simpleemail') . '</th>';
|
|
echo '<td><textarea name="ai_prompt" rows="4" class="large-text" placeholder="' . esc_attr__('Describe what the newsletter should contain...', 'bd-simpleemail') . '"></textarea></td></tr>';
|
|
}
|
|
|
|
echo '<tr><th>' . esc_html__('Content', 'bd-simpleemail') . '</th>';
|
|
echo '<td><textarea name="content" rows="20" class="large-text" id="newsletter-content"></textarea></td></tr>';
|
|
|
|
echo '<tr><th>' . esc_html__('Options', 'bd-simpleemail') . '</th>';
|
|
echo '<td><input type="checkbox" name="publish_blog" id="publish_blog" value="1"> ';
|
|
echo '<label for="publish_blog">' . esc_html__('Also publish as blog post', 'bd-simpleemail') . '</label></td></tr>';
|
|
echo '</table>';
|
|
|
|
echo '<p><input type="submit" name="bd_send_newsletter" class="button button-primary" value="' . esc_attr__('Send Newsletter', 'bd-simpleemail') . '"></p>';
|
|
echo '</form>';
|
|
|
|
echo '<script>
|
|
jQuery(document).ready(function($) {
|
|
$("#ai_generate").on("change", function() {
|
|
$(".ai-prompt-row").toggle(this.checked);
|
|
});
|
|
});
|
|
</script>';
|
|
|
|
echo '</div>';
|
|
}
|
|
|
|
/**
|
|
* Settings page
|
|
*/
|
|
public static function settings_page() {
|
|
if (isset($_POST['bd_save_email_settings'])) {
|
|
check_admin_referer('bd_email_settings');
|
|
|
|
update_option('bdm_gmail_address', sanitize_email($_POST['gmail_address'] ?? ''));
|
|
update_option('bdm_sender_name', sanitize_text_field($_POST['sender_name'] ?? ''));
|
|
update_option('bdm_reply_to', sanitize_email($_POST['reply_to'] ?? ''));
|
|
|
|
if (!empty($_POST['gmail_password'])) {
|
|
update_option('bdm_gmail_password', Umbra_Email_Sender::encrypt_password($_POST['gmail_password']));
|
|
}
|
|
|
|
if (!empty($_POST['gemini_api_key'])) {
|
|
update_option('bdm_gemini_api_key', sanitize_text_field($_POST['gemini_api_key']));
|
|
}
|
|
|
|
echo '<div class="updated"><p>' . esc_html__('Settings saved.', 'bd-simpleemail') . '</p></div>';
|
|
}
|
|
|
|
$settings = Umbra_Simple_Email::get_settings();
|
|
$business = Umbra_Simple_Email::get_business_info();
|
|
|
|
echo '<div class="wrap">';
|
|
echo '<h1>' . esc_html__('BD SimpleEmail Settings', 'bd-simpleemail') . '</h1>';
|
|
|
|
echo '<form method="post">';
|
|
wp_nonce_field('bd_email_settings');
|
|
|
|
echo '<h2>' . esc_html__('Gmail Configuration', 'bd-simpleemail') . '</h2>';
|
|
echo '<p>' . esc_html__('Configure your Gmail account for sending newsletters via SMTP.', 'bd-simpleemail') . '</p>';
|
|
|
|
echo '<table class="form-table">';
|
|
echo '<tr><th>' . esc_html__('Gmail Address', 'bd-simpleemail') . '</th>';
|
|
printf('<td><input type="email" name="gmail_address" value="%s" class="regular-text"></td></tr>',
|
|
esc_attr($settings['gmail_address'])
|
|
);
|
|
|
|
echo '<tr><th>' . esc_html__('App Password', 'bd-simpleemail') . '</th>';
|
|
echo '<td><input type="password" name="gmail_password" class="regular-text" placeholder="' . esc_attr__('Leave blank to keep current', 'bd-simpleemail') . '">';
|
|
echo '<p class="description"><a href="https://support.google.com/accounts/answer/185833" target="_blank">' . esc_html__('Learn how to generate an app password', 'bd-simpleemail') . '</a></p></td></tr>';
|
|
|
|
echo '<tr><th>' . esc_html__('Sender Name', 'bd-simpleemail') . '</th>';
|
|
printf('<td><input type="text" name="sender_name" value="%s" class="regular-text"></td></tr>',
|
|
esc_attr($settings['sender_name'])
|
|
);
|
|
|
|
echo '<tr><th>' . esc_html__('Reply-To', 'bd-simpleemail') . '</th>';
|
|
printf('<td><input type="email" name="reply_to" value="%s" class="regular-text"></td></tr>',
|
|
esc_attr($settings['reply_to'])
|
|
);
|
|
echo '</table>';
|
|
|
|
echo '<h2>' . esc_html__('Gemini AI', 'bd-simpleemail') . '</h2>';
|
|
echo '<table class="form-table">';
|
|
echo '<tr><th>' . esc_html__('API Key', 'bd-simpleemail') . '</th>';
|
|
printf('<td><input type="text" name="gemini_api_key" value="%s" class="regular-text">',
|
|
esc_attr($settings['gemini_api_key'])
|
|
);
|
|
echo '<p class="description"><a href="https://makersuite.google.com/app/apikey" target="_blank">' . esc_html__('Get your API key', 'bd-simpleemail') . '</a></p></td></tr>';
|
|
echo '</table>';
|
|
|
|
echo '<h2>' . esc_html__('Business Information (CAN-SPAM)', 'bd-simpleemail') . '</h2>';
|
|
echo '<p>' . esc_html__('These fields are required for CAN-SPAM compliance.', 'bd-simpleemail') . '</p>';
|
|
echo '<table class="form-table">';
|
|
printf('<tr><th>' . esc_html__('Business Name', 'bd-simpleemail') . '</th><td>%s</td></tr>', esc_html($business['name']) ?: '-');
|
|
printf('<tr><th>' . esc_html__('Address', 'bd-simpleemail') . '</th><td>%s</td></tr>',
|
|
esc_html($business['address_1']) . '<br>' . esc_html($business['address_2']) . '<br>' . esc_html($business['city']) . '<br>' . esc_html($business['country'])
|
|
);
|
|
printf('<tr><th>' . esc_html__('Phone', 'bd-simpleemail') . '</th><td>%s</td></tr>', esc_html($business['phone']) ?: '-');
|
|
echo '</table>';
|
|
|
|
submit_button(esc_html__('Save Settings', 'bd-simpleemail'), 'primary', 'bd_save_email_settings');
|
|
echo '</form></div>';
|
|
}
|
|
|
|
/**
|
|
* Generate newsletter with Gemini AI
|
|
*
|
|
* @param string $subject Newsletter subject.
|
|
* @param string $prompt User prompt.
|
|
* @return string
|
|
*/
|
|
private static function generate_newsletter($subject, $prompt) {
|
|
$settings = Umbra_Simple_Email::get_settings();
|
|
|
|
if (empty($settings['gemini_api_key'])) {
|
|
return '';
|
|
}
|
|
|
|
$system_instruction = "You are a professional newsletter writer for an author. Write engaging, concise newsletter content in HTML format. Use <p> for paragraphs, <h2> for headings, and <ul> for lists. Keep it friendly but professional.";
|
|
|
|
$full_prompt = $system_instruction . "\n\nSubject: " . $subject . "\n\nAdditional instructions: " . $prompt;
|
|
|
|
$api_url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' . $settings['gemini_api_key'];
|
|
|
|
$body = array(
|
|
'contents' => array(
|
|
array(
|
|
'parts' => array(
|
|
array('text' => $full_prompt)
|
|
)
|
|
)
|
|
),
|
|
'generationConfig' => array(
|
|
'temperature' => 0.9,
|
|
'maxOutputTokens' => 2048,
|
|
),
|
|
);
|
|
|
|
$response = wp_remote_post($api_url, array(
|
|
'body' => json_encode($body),
|
|
'headers' => array('Content-Type' => 'application/json'),
|
|
));
|
|
|
|
if (is_wp_error($response)) {
|
|
return '';
|
|
}
|
|
|
|
$data = json_decode(wp_remote_retrieve_body($response), true);
|
|
|
|
if (isset($data['candidates'][0]['content']['parts'][0]['text'])) {
|
|
return $data['candidates'][0]['content']['parts'][0]['text'];
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Log newsletter send
|
|
*
|
|
* @param string $subject Subject.
|
|
* @param string $content Content.
|
|
* @param int $recipient_count Recipient count.
|
|
*/
|
|
private static function log_newsletter($subject, $content, $recipient_count) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'bd_newsletters';
|
|
|
|
$wpdb->insert($table_name, array(
|
|
'subject' => $subject,
|
|
'body_html' => $content,
|
|
'sent_at' => current_time('mysql'),
|
|
'recipient_count' => $recipient_count,
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Publish newsletter as blog post
|
|
*
|
|
* @param string $subject Subject.
|
|
* @param string $content Content.
|
|
* @return int Post ID.
|
|
*/
|
|
private static function publish_as_blog_post($subject, $content) {
|
|
return wp_insert_post(array(
|
|
'post_title' => $subject,
|
|
'post_content' => $content,
|
|
'post_status' => 'publish',
|
|
'post_type' => 'post',
|
|
));
|
|
}
|
|
}
|