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.
267 lines
10 KiB
PHP
267 lines
10 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Migration Manager class
|
|
*
|
|
* @package Umbra_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Umbra_Migration {
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* The "Import" admin page itself is registered once by Umbra_Admin
|
|
* (slug: umbra-bookshelf-import), which is the variant wired to
|
|
* admin/js/umbra-bookshelf-admin.js. This class only supplies the
|
|
* two-phase preview/run AJAX endpoints that page has no equivalent for.
|
|
*
|
|
* Note: the bd_bookshelf_import_csv action is intentionally NOT
|
|
* registered here -- Umbra_Admin already owns that action. Registering
|
|
* it twice silently overwrote one of the two handlers.
|
|
*/
|
|
public function __construct() {
|
|
// AJAX handlers
|
|
add_action('wp_ajax_bd_bookshelf_preview_import', [$this, 'ajax_import_posts']);
|
|
add_action('wp_ajax_bd_bookshelf_run_import', [$this, 'ajax_run_import_posts']);
|
|
}
|
|
|
|
/**
|
|
* Render import page
|
|
*/
|
|
public function render_import_page() {
|
|
if (!current_user_can('manage_options')) {
|
|
wp_die(__('You do not have sufficient permissions to access this page.', 'umbra-bookshelf'));
|
|
}
|
|
|
|
?>
|
|
<div class="wrap">
|
|
<h1><?php esc_html_e('Import Books', 'umbra-bookshelf'); ?></h1>
|
|
|
|
<div class="bd-bookshelf-import-section">
|
|
<h2><?php esc_html_e('Import from WordPress Posts', 'umbra-bookshelf'); ?></h2>
|
|
<p><?php esc_html_e('Import books from existing WordPress posts or custom post types.', 'umbra-bookshelf'); ?></p>
|
|
|
|
<form id="bd-bookshelf-import-posts-form">
|
|
<?php wp_nonce_field('bd_bookshelf_import_posts', 'nonce'); ?>
|
|
|
|
<table class="form-table">
|
|
<tr>
|
|
<th><label for="post_type"><?php esc_html_e('Post Type', 'umbra-bookshelf'); ?></label></th>
|
|
<td>
|
|
<select name="post_type" id="post_type">
|
|
<option value="post"><?php esc_html_e('Posts', 'umbra-bookshelf'); ?></option>
|
|
<option value="page"><?php esc_html_e('Pages', 'umbra-bookshelf'); ?></option>
|
|
<option value="book"><?php esc_html_e('Books (if available)', 'umbra-bookshelf'); ?></option>
|
|
</select>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<th><label for="limit"><?php esc_html_e('Limit', 'umbra-bookshelf'); ?></label></th>
|
|
<td>
|
|
<input type="number" name="limit" id="limit" value="100" min="1" max="1000">
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p class="submit">
|
|
<button type="submit" class="button button-primary"><?php esc_html_e('Preview Import', 'umbra-bookshelf'); ?></button>
|
|
</p>
|
|
</form>
|
|
|
|
<div id="bd-bookshelf-import-preview" style="display: none;">
|
|
<h3><?php esc_html_e('Preview', 'umbra-bookshelf'); ?></h3>
|
|
<div id="bd-bookshelf-import-results"></div>
|
|
<p class="submit">
|
|
<button type="button" id="bd-bookshelf-run-import" class="button button-primary"><?php esc_html_e('Run Import', 'umbra-bookshelf'); ?></button>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="bd-bookshelf-import-section">
|
|
<h2><?php esc_html_e('Import from CSV', 'umbra-bookshelf'); ?></h2>
|
|
<p><?php esc_html_e('Import books from a CSV file.', 'umbra-bookshelf'); ?></p>
|
|
|
|
<form id="bd-bookshelf-import-csv-form" enctype="multipart/form-data">
|
|
<?php wp_nonce_field('bd_bookshelf_import_csv', 'nonce'); ?>
|
|
|
|
<table class="form-table">
|
|
<tr>
|
|
<th><label for="csv_file"><?php esc_html_e('CSV File', 'umbra-bookshelf'); ?></label></th>
|
|
<td>
|
|
<input type="file" name="csv_file" id="csv_file" accept=".csv">
|
|
<p class="description"><?php esc_html_e('Required columns: title. Optional: description, isbn_ebook, isbn_paperback, isbn_hardcover, isbn_audio, amazon_asin, goodreads_id.', 'umbra-bookshelf'); ?></p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p class="submit">
|
|
<button type="submit" class="button button-primary"><?php esc_html_e('Import CSV', 'umbra-bookshelf'); ?></button>
|
|
</p>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* AJAX handler for previewing post import
|
|
*/
|
|
public function ajax_import_posts() {
|
|
check_ajax_referer('bd_bookshelf_import_posts', 'nonce');
|
|
|
|
if (!current_user_can('manage_options')) {
|
|
wp_send_json_error(['message' => 'Insufficient permissions']);
|
|
}
|
|
|
|
$post_type = sanitize_text_field($_POST['post_type'] ?? 'post');
|
|
$limit = absint($_POST['limit'] ?? 100);
|
|
|
|
$posts = get_posts([
|
|
'post_type' => $post_type,
|
|
'post_status' => 'publish',
|
|
'numberposts' => $limit,
|
|
]);
|
|
|
|
$preview = [];
|
|
foreach ($posts as $post) {
|
|
$preview[] = [
|
|
'id' => $post->ID,
|
|
'title' => $post->post_title,
|
|
'date' => $post->post_date,
|
|
'content' => wp_trim_words($post->post_content, 20),
|
|
];
|
|
}
|
|
|
|
wp_send_json_success(['posts' => $preview, 'count' => count($preview)]);
|
|
}
|
|
|
|
/**
|
|
* AJAX handler for running post import
|
|
*/
|
|
public function ajax_run_import_posts() {
|
|
check_ajax_referer('bd_bookshelf_import_posts', 'nonce');
|
|
|
|
if (!current_user_can('manage_options')) {
|
|
wp_send_json_error(['message' => 'Insufficient permissions']);
|
|
}
|
|
|
|
$post_type = sanitize_text_field($_POST['post_type'] ?? 'post');
|
|
$limit = absint($_POST['limit'] ?? 100);
|
|
|
|
$posts = get_posts([
|
|
'post_type' => $post_type,
|
|
'post_status' => 'publish',
|
|
'numberposts' => $limit,
|
|
]);
|
|
|
|
$imported = 0;
|
|
$skipped = 0;
|
|
|
|
foreach ($posts as $post) {
|
|
// Check if already imported
|
|
$existing = Umbra_Book_Manager::get_book_by_title($post->post_title);
|
|
if ($existing) {
|
|
$skipped++;
|
|
continue;
|
|
}
|
|
|
|
// Import book
|
|
$book_id = Umbra_Book_Manager::create_book([
|
|
'title' => $post->post_title,
|
|
'description' => $post->post_content,
|
|
]);
|
|
|
|
if ($book_id) {
|
|
// Link to WordPress post
|
|
update_post_meta($post->ID, '_bd_bookshelf_book_id', $book_id);
|
|
$imported++;
|
|
}
|
|
}
|
|
|
|
wp_send_json_success([
|
|
'message' => sprintf('Imported %d books, skipped %d', $imported, $skipped),
|
|
'imported' => $imported,
|
|
'skipped' => $skipped,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* AJAX handler for CSV import
|
|
*/
|
|
public function ajax_import_csv() {
|
|
check_ajax_referer('bd_bookshelf_import_csv', 'nonce');
|
|
|
|
if (!current_user_can('manage_options')) {
|
|
wp_send_json_error(['message' => 'Insufficient permissions']);
|
|
}
|
|
|
|
if (empty($_FILES['csv_file']['tmp_name'])) {
|
|
wp_send_json_error(['message' => 'No file uploaded']);
|
|
}
|
|
|
|
$file = $_FILES['csv_file']['tmp_name'];
|
|
$handle = fopen($file, 'r');
|
|
|
|
if (!$handle) {
|
|
wp_send_json_error(['message' => 'Unable to open file']);
|
|
}
|
|
|
|
// Get header row
|
|
$header = fgetcsv($handle);
|
|
|
|
$imported = 0;
|
|
$skipped = 0;
|
|
$errors = [];
|
|
|
|
while (($row = fgetcsv($handle)) !== false) {
|
|
$data = array_combine($header, $row);
|
|
|
|
if (empty($data['title'])) {
|
|
$errors[] = 'Row ' . ($imported + $skipped + 1) . ': Missing title';
|
|
continue;
|
|
}
|
|
|
|
// Check if already imported
|
|
$existing = Umbra_Book_Manager::get_book_by_title($data['title']);
|
|
if ($existing) {
|
|
$skipped++;
|
|
continue;
|
|
}
|
|
|
|
// Create book
|
|
$book_id = Umbra_Book_Manager::create_book([
|
|
'title' => $data['title'],
|
|
'description' => $data['description'] ?? '',
|
|
'isbn_ebook' => $data['isbn_ebook'] ?? '',
|
|
'isbn_paperback' => $data['isbn_paperback'] ?? '',
|
|
'isbn_hardcover' => $data['isbn_hardcover'] ?? '',
|
|
'isbn_audio' => $data['isbn_audio'] ?? '',
|
|
'amazon_asin' => $data['amazon_asin'] ?? '',
|
|
'goodreads_id' => $data['goodreads_id'] ?? '',
|
|
]);
|
|
|
|
if ($book_id) {
|
|
$imported++;
|
|
} else {
|
|
$errors[] = 'Row ' . ($imported + $skipped + 1) . ': Failed to create book';
|
|
}
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
wp_send_json_success([
|
|
'message' => sprintf('Imported %d books, skipped %d, %d errors', $imported, $skipped, count($errors)),
|
|
'imported' => $imported,
|
|
'skipped' => $skipped,
|
|
'errors' => $errors,
|
|
]);
|
|
}
|
|
}
|