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.
299 lines
8.9 KiB
PHP
299 lines
8.9 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Retail Sites management class
|
|
*
|
|
* @package Umbra_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Umbra_Retail_Sites {
|
|
|
|
/**
|
|
* Get all retail sites
|
|
*/
|
|
public static function get_sites($active_only = true) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('retail_sites');
|
|
|
|
$where = $active_only ? 'is_active = 1' : '1=1';
|
|
|
|
$sites = $wpdb->get_results(
|
|
"SELECT * FROM {$table} WHERE {$where} ORDER BY is_custom ASC, name ASC"
|
|
);
|
|
|
|
return $sites;
|
|
}
|
|
|
|
/**
|
|
* Get site by ID
|
|
*/
|
|
public static function get_site($site_id) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('retail_sites');
|
|
|
|
return $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE id = %d",
|
|
$site_id
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create new retail site
|
|
*/
|
|
public static function create_site($data) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('retail_sites');
|
|
|
|
$result = $wpdb->insert($table, [
|
|
'name' => sanitize_text_field($data['name']),
|
|
'url_pattern' => esc_url_raw($data['url_pattern']),
|
|
'icon' => sanitize_text_field($data['icon'] ?? ''),
|
|
'affiliate_param' => sanitize_text_field($data['affiliate_param'] ?? ''),
|
|
'affiliate_code' => sanitize_text_field($data['affiliate_code'] ?? ''),
|
|
'is_custom' => 1,
|
|
'is_active' => 1,
|
|
]);
|
|
|
|
return $result ? $wpdb->insert_id : false;
|
|
}
|
|
|
|
/**
|
|
* Update retail site
|
|
*/
|
|
public static function update_site($site_id, $data) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('retail_sites');
|
|
|
|
$update_data = [];
|
|
|
|
if (isset($data['name'])) {
|
|
$update_data['name'] = sanitize_text_field($data['name']);
|
|
}
|
|
|
|
if (isset($data['url_pattern'])) {
|
|
$update_data['url_pattern'] = esc_url_raw($data['url_pattern']);
|
|
}
|
|
|
|
if (isset($data['icon'])) {
|
|
$update_data['icon'] = sanitize_text_field($data['icon']);
|
|
}
|
|
|
|
if (isset($data['affiliate_param'])) {
|
|
$update_data['affiliate_param'] = sanitize_text_field($data['affiliate_param']);
|
|
}
|
|
|
|
if (isset($data['affiliate_code'])) {
|
|
$update_data['affiliate_code'] = sanitize_text_field($data['affiliate_code']);
|
|
}
|
|
|
|
if (isset($data['is_active'])) {
|
|
$update_data['is_active'] = absint($data['is_active']);
|
|
}
|
|
|
|
if (empty($update_data)) {
|
|
return false;
|
|
}
|
|
|
|
$result = $wpdb->update(
|
|
$table,
|
|
$update_data,
|
|
['id' => $site_id],
|
|
null,
|
|
['%d']
|
|
);
|
|
|
|
return $result !== false;
|
|
}
|
|
|
|
/**
|
|
* Delete retail site
|
|
*/
|
|
public static function delete_site($site_id) {
|
|
global $wpdb;
|
|
|
|
// Only allow deleting custom sites
|
|
$site = self::get_site($site_id);
|
|
if ($site && $site->is_custom) {
|
|
$wpdb->delete(Umbra_Database::table('retail_links'), ['site_id' => $site_id], ['%d']);
|
|
return $wpdb->delete(Umbra_Database::table('retail_sites'), ['id' => $site_id], ['%d']) !== false;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Generate links for a book
|
|
*/
|
|
public static function generate_links_for_book($book_id) {
|
|
global $wpdb;
|
|
|
|
$book = Umbra_Book_Manager::get_book($book_id);
|
|
if (!$book) {
|
|
return false;
|
|
}
|
|
|
|
$sites = self::get_sites(true);
|
|
$links_table = Umbra_Database::table('retail_links');
|
|
|
|
// Delete existing links
|
|
$wpdb->delete($links_table, ['book_id' => $book_id], ['%d']);
|
|
|
|
// Format to ISBN mapping
|
|
$formats = [
|
|
'ebook' => $book->isbn_ebook,
|
|
'paperback' => $book->isbn_paperback,
|
|
'hardcover' => $book->isbn_hardcover,
|
|
'audio' => $book->isbn_audio,
|
|
];
|
|
|
|
// Use Amazon ASIN for Amazon if available
|
|
$amazon_asin = $book->amazon_asin ?: '';
|
|
|
|
foreach ($formats as $format => $isbn) {
|
|
if (empty($isbn)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($sites as $site) {
|
|
$site_isbn = $isbn;
|
|
|
|
// Special handling for Amazon - needs ISBN-10 for /dp/ path
|
|
if ($site->name === 'Amazon') {
|
|
// Use ASIN if available (it's already ISBN-10 format)
|
|
if (!empty($amazon_asin)) {
|
|
$site_isbn = $amazon_asin;
|
|
$url = str_replace('{ISBN}', $site_isbn, $site->url_pattern);
|
|
} else {
|
|
// Try to convert ISBN-13 to ISBN-10
|
|
$isbn10 = self::convert_isbn13_to_isbn10($isbn);
|
|
if ($isbn10) {
|
|
$url = str_replace('{ISBN}', $isbn10, $site->url_pattern);
|
|
} else {
|
|
// Use search fallback for ISBNs that can't convert (979 prefix)
|
|
$url = 'https://www.amazon.com/s?k=' . urlencode($isbn) . '&i=stripbooks';
|
|
}
|
|
}
|
|
|
|
// Add affiliate code if available
|
|
if (!empty($site->affiliate_param) && !empty($site->affiliate_code)) {
|
|
$separator = strpos($url, '?') !== false ? '&' : '?';
|
|
$url .= $separator . $site->affiliate_param . '=' . $site->affiliate_code;
|
|
}
|
|
|
|
$wpdb->insert($links_table, [
|
|
'book_id' => $book_id,
|
|
'site_id' => $site->id,
|
|
'format' => $format,
|
|
'url' => $url,
|
|
'is_custom' => 0,
|
|
]);
|
|
continue;
|
|
}
|
|
|
|
$url = str_replace('{ISBN}', $site_isbn, $site->url_pattern);
|
|
|
|
// Add affiliate code if available
|
|
if (!empty($site->affiliate_param) && !empty($site->affiliate_code)) {
|
|
$separator = strpos($url, '?') !== false ? '&' : '?';
|
|
$url .= $separator . $site->affiliate_param . '=' . $site->affiliate_code;
|
|
}
|
|
|
|
$wpdb->insert($links_table, [
|
|
'book_id' => $book_id,
|
|
'site_id' => $site->id,
|
|
'format' => $format,
|
|
'url' => $url,
|
|
'is_custom' => 0,
|
|
]);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Convert ISBN-13 to ISBN-10
|
|
*
|
|
* @param string $isbn13 ISBN-13 to convert.
|
|
* @return string|false ISBN-10 on success, false on failure.
|
|
*/
|
|
private static function convert_isbn13_to_isbn10($isbn13) {
|
|
// Clean the ISBN
|
|
$isbn13 = preg_replace('/[^0-9X]/i', '', $isbn13);
|
|
|
|
// Must be 13 digits
|
|
if (strlen($isbn13) !== 13) {
|
|
return false;
|
|
}
|
|
|
|
// Must start with 978 (Bookland prefix)
|
|
if (substr($isbn13, 0, 3) !== '978') {
|
|
return false;
|
|
}
|
|
|
|
// Take the first 9 digits (drop 978 prefix and last check digit)
|
|
$isbn10 = substr($isbn13, 3, 9);
|
|
|
|
// Calculate check digit
|
|
$sum = 0;
|
|
for ($i = 0; $i < 9; $i++) {
|
|
$sum += (int)$isbn10[$i] * (10 - $i);
|
|
}
|
|
$remainder = $sum % 11;
|
|
$check_digit = (11 - $remainder) % 11;
|
|
|
|
// Convert check digit to ISBN-10 format (10 = X)
|
|
$check_digit = ($check_digit === 10) ? 'X' : (string)$check_digit;
|
|
|
|
return $isbn10 . $check_digit;
|
|
}
|
|
|
|
/**
|
|
* Update custom link
|
|
*/
|
|
public static function update_link($link_id, $url) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('retail_links');
|
|
|
|
return $wpdb->update(
|
|
$table,
|
|
['url' => esc_url_raw($url), 'is_custom' => 1],
|
|
['id' => $link_id],
|
|
['%s', '%d'],
|
|
['%d']
|
|
) !== false;
|
|
}
|
|
|
|
/**
|
|
* Create custom link
|
|
*/
|
|
public static function create_custom_link($book_id, $site_id, $format, $url) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('retail_links');
|
|
|
|
$result = $wpdb->insert($table, [
|
|
'book_id' => $book_id,
|
|
'site_id' => $site_id,
|
|
'format' => $format,
|
|
'url' => esc_url_raw($url),
|
|
'is_custom' => 1,
|
|
]);
|
|
|
|
return $result ? $wpdb->insert_id : false;
|
|
}
|
|
}
|