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.
481 lines
14 KiB
PHP
481 lines
14 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/**
|
|
* Book Manager class
|
|
*
|
|
* @package Umbra_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Umbra_Book_Manager {
|
|
|
|
/**
|
|
* Get all books
|
|
*/
|
|
public static function get_books($args = []) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('books');
|
|
|
|
$defaults = [
|
|
'number' => 20,
|
|
'offset' => 0,
|
|
'orderby' => 'title',
|
|
'order' => 'ASC',
|
|
'search' => '',
|
|
];
|
|
|
|
$args = wp_parse_args($args, $defaults);
|
|
|
|
$where = '1=1';
|
|
$params = [];
|
|
|
|
if (!empty($args['search'])) {
|
|
$where .= ' AND title LIKE %s';
|
|
$params[] = '%' . $wpdb->esc_like($args['search']) . '%';
|
|
}
|
|
|
|
$order = strtoupper($args['order']) === 'DESC' ? 'DESC' : 'ASC';
|
|
$allowed_orders = ['title', 'created_at', 'updated_at', 'rating_amazon', 'rating_goodreads'];
|
|
$orderby = in_array($args['orderby'], $allowed_orders) ? $args['orderby'] : 'title';
|
|
|
|
$query = "SELECT * FROM {$table} WHERE {$where} ORDER BY {$orderby} {$order} LIMIT %d OFFSET %d";
|
|
$params[] = $args['number'];
|
|
$params[] = $args['offset'];
|
|
|
|
if (!empty($params)) {
|
|
$query = $wpdb->prepare($query, ...$params);
|
|
}
|
|
|
|
$books = $wpdb->get_results($query);
|
|
|
|
return $books;
|
|
}
|
|
|
|
/**
|
|
* Get book by ID
|
|
*/
|
|
public static function get_book($book_id) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('books');
|
|
|
|
$book = $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE id = %d",
|
|
$book_id
|
|
)
|
|
);
|
|
|
|
if ($book) {
|
|
$book->authors = self::get_book_authors($book_id);
|
|
$book->retail_links = self::get_book_retail_links($book_id);
|
|
$book->seo = Umbra_SEO::get_book_seo($book_id);
|
|
}
|
|
|
|
return $book;
|
|
}
|
|
|
|
/**
|
|
* Get book by title
|
|
*/
|
|
public static function get_book_by_title($title) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('books');
|
|
|
|
$book = $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE title = %s",
|
|
$title
|
|
)
|
|
);
|
|
|
|
return $book;
|
|
}
|
|
|
|
/**
|
|
* Get book by slug
|
|
*/
|
|
public static function get_book_by_slug($slug) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('books');
|
|
|
|
$book = $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE slug = %s",
|
|
$slug
|
|
)
|
|
);
|
|
|
|
if ($book) {
|
|
$book->authors = self::get_book_authors($book->id);
|
|
$book->retail_links = self::get_book_retail_links($book->id);
|
|
$book->seo = Umbra_SEO::get_book_seo($book->id);
|
|
}
|
|
|
|
return $book;
|
|
}
|
|
|
|
/**
|
|
* Create new book
|
|
*/
|
|
public static function create_book($data) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('books');
|
|
|
|
$slug = sanitize_title($data['slug'] ?? $data['title'] ?? '');
|
|
|
|
$insert_data = [
|
|
'title' => sanitize_text_field($data['title']),
|
|
'slug' => $slug,
|
|
'description' => wp_kses_post($data['description'] ?? ''),
|
|
'cover_image' => esc_url_raw($data['cover_image'] ?? ''),
|
|
'isbn_ebook' => self::sanitize_isbn($data['isbn_ebook'] ?? ''),
|
|
'isbn_paperback' => self::sanitize_isbn($data['isbn_paperback'] ?? ''),
|
|
'isbn_hardcover' => self::sanitize_isbn($data['isbn_hardcover'] ?? ''),
|
|
'isbn_audio' => self::sanitize_isbn($data['isbn_audio'] ?? ''),
|
|
'asin_ebook' => sanitize_text_field($data['asin_ebook'] ?? ''),
|
|
'asin_paperback' => sanitize_text_field($data['asin_paperback'] ?? ''),
|
|
'asin_hardcover' => sanitize_text_field($data['asin_hardcover'] ?? ''),
|
|
'asin_audio' => sanitize_text_field($data['asin_audio'] ?? ''),
|
|
'amazon_asin' => sanitize_text_field($data['amazon_asin'] ?? ''),
|
|
'goodreads_id' => sanitize_text_field($data['goodreads_id'] ?? ''),
|
|
'series_id' => absint($data['series_id'] ?? 0),
|
|
];
|
|
|
|
// Handle ratings
|
|
if (isset($data['rating_amazon'])) {
|
|
$insert_data['rating_amazon'] = (float) $data['rating_amazon'];
|
|
}
|
|
if (isset($data['rating_goodreads'])) {
|
|
$insert_data['rating_goodreads'] = (float) $data['rating_goodreads'];
|
|
}
|
|
|
|
$result = $wpdb->insert($table, $insert_data);
|
|
|
|
if ($result) {
|
|
$book_id = $wpdb->insert_id;
|
|
|
|
// Handle authors - new system uses author_ids
|
|
if (!empty($data['author_ids']) && is_array($data['author_ids'])) {
|
|
self::update_book_authors_by_id($book_id, $data['author_ids']);
|
|
} elseif (!empty($data['authors']) && is_array($data['authors'])) {
|
|
// Legacy support
|
|
self::update_book_authors($book_id, $data['authors']);
|
|
}
|
|
|
|
return $book_id;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Update book
|
|
*/
|
|
public static function update_book($book_id, $data) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('books');
|
|
|
|
$update_data = [];
|
|
|
|
if (isset($data['title'])) {
|
|
$update_data['title'] = sanitize_text_field($data['title']);
|
|
}
|
|
|
|
if (isset($data['description'])) {
|
|
$update_data['description'] = wp_kses_post($data['description']);
|
|
}
|
|
|
|
if (isset($data['cover_image'])) {
|
|
$update_data['cover_image'] = esc_url_raw($data['cover_image']);
|
|
}
|
|
|
|
if (isset($data['isbn_ebook'])) {
|
|
$update_data['isbn_ebook'] = self::sanitize_isbn($data['isbn_ebook']);
|
|
}
|
|
|
|
if (isset($data['isbn_paperback'])) {
|
|
$update_data['isbn_paperback'] = self::sanitize_isbn($data['isbn_paperback']);
|
|
}
|
|
|
|
if (isset($data['isbn_hardcover'])) {
|
|
$update_data['isbn_hardcover'] = self::sanitize_isbn($data['isbn_hardcover']);
|
|
}
|
|
|
|
if (isset($data['isbn_audio'])) {
|
|
$update_data['isbn_audio'] = self::sanitize_isbn($data['isbn_audio']);
|
|
}
|
|
|
|
if (isset($data['asin_ebook'])) {
|
|
$update_data['asin_ebook'] = sanitize_text_field($data['asin_ebook']);
|
|
}
|
|
|
|
if (isset($data['asin_paperback'])) {
|
|
$update_data['asin_paperback'] = sanitize_text_field($data['asin_paperback']);
|
|
}
|
|
|
|
if (isset($data['asin_hardcover'])) {
|
|
$update_data['asin_hardcover'] = sanitize_text_field($data['asin_hardcover']);
|
|
}
|
|
|
|
if (isset($data['asin_audio'])) {
|
|
$update_data['asin_audio'] = sanitize_text_field($data['asin_audio']);
|
|
}
|
|
|
|
if (isset($data['amazon_asin'])) {
|
|
$update_data['amazon_asin'] = sanitize_text_field($data['amazon_asin']);
|
|
}
|
|
|
|
if (isset($data['goodreads_id'])) {
|
|
$update_data['goodreads_id'] = sanitize_text_field($data['goodreads_id']);
|
|
}
|
|
|
|
if (isset($data['series_id'])) {
|
|
$update_data['series_id'] = absint($data['series_id']);
|
|
}
|
|
|
|
if (empty($update_data)) {
|
|
return false;
|
|
}
|
|
|
|
$result = $wpdb->update(
|
|
$table,
|
|
$update_data,
|
|
['id' => $book_id],
|
|
null,
|
|
['%d']
|
|
);
|
|
|
|
// Handle authors - new system uses author_ids
|
|
if (isset($data['author_ids']) && is_array($data['author_ids'])) {
|
|
self::update_book_authors_by_id($book_id, $data['author_ids']);
|
|
} elseif (isset($data['authors']) && is_array($data['authors'])) {
|
|
// Legacy support
|
|
self::update_book_authors($book_id, $data['authors']);
|
|
}
|
|
|
|
return $result !== false;
|
|
}
|
|
|
|
/**
|
|
* Delete book
|
|
*/
|
|
public static function delete_book($book_id) {
|
|
global $wpdb;
|
|
|
|
// Delete related data
|
|
$wpdb->delete(Umbra_Database::table('book_authors'), ['book_id' => $book_id], ['%d']);
|
|
$wpdb->delete(Umbra_Database::table('retail_links'), ['book_id' => $book_id], ['%d']);
|
|
$wpdb->delete(Umbra_Database::table('book_seo'), ['book_id' => $book_id], ['%d']);
|
|
|
|
// Delete book
|
|
$table = Umbra_Database::table('books');
|
|
$result = $wpdb->delete($table, ['id' => $book_id], ['%d']);
|
|
|
|
return $result !== false;
|
|
}
|
|
|
|
/**
|
|
* Get book authors
|
|
*/
|
|
public static function get_book_authors($book_id) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('book_authors');
|
|
|
|
$authors = $wpdb->get_results(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE book_id = %d ORDER BY sort_order ASC",
|
|
$book_id
|
|
)
|
|
);
|
|
|
|
return $authors;
|
|
}
|
|
|
|
/**
|
|
* Update book authors by author IDs
|
|
*/
|
|
public static function update_book_authors_by_id($book_id, $author_ids) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('book_authors');
|
|
$authors_table = Umbra_Database::table('authors');
|
|
|
|
// Delete existing authors
|
|
$wpdb->delete($table, ['book_id' => $book_id], ['%d']);
|
|
|
|
// Filter out zero values
|
|
$author_ids = array_filter(array_map('absint', $author_ids));
|
|
|
|
// Insert new authors
|
|
foreach ($author_ids as $index => $author_id) {
|
|
if ($author_id <= 0) continue;
|
|
|
|
// Get author details
|
|
$author = $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$authors_table} WHERE id = %d",
|
|
$author_id
|
|
)
|
|
);
|
|
|
|
if ($author) {
|
|
$wpdb->insert($table, [
|
|
'book_id' => $book_id,
|
|
'author_name' => $author->display_name,
|
|
'author_id' => $author_id,
|
|
'goodreads_author_id' => $author->goodreads_author_id,
|
|
'is_primary' => ($index === 0) ? 1 : 0,
|
|
'sort_order' => $index + 1,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update book authors (legacy)
|
|
*/
|
|
public static function update_book_authors($book_id, $authors) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('book_authors');
|
|
|
|
// Delete existing authors
|
|
$wpdb->delete($table, ['book_id' => $book_id], ['%d']);
|
|
|
|
// Insert new authors
|
|
foreach ($authors as $index => $author) {
|
|
$wpdb->insert($table, [
|
|
'book_id' => $book_id,
|
|
'author_name' => sanitize_text_field($author['name']),
|
|
'author_id' => absint($author['user_id'] ?? 0),
|
|
'goodreads_author_id' => sanitize_text_field($author['goodreads_id'] ?? ''),
|
|
'is_primary' => ($index === 0) ? 1 : 0,
|
|
'sort_order' => $index + 1,
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get book retail links
|
|
*/
|
|
public static function get_book_retail_links($book_id, $format = '') {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('retail_links');
|
|
$sites_table = Umbra_Database::table('retail_sites');
|
|
|
|
$where = 'rl.book_id = %d AND rs.is_active = 1';
|
|
$params = [$book_id];
|
|
|
|
if (!empty($format)) {
|
|
$where .= ' AND rl.format = %s';
|
|
$params[] = $format;
|
|
}
|
|
|
|
$query = $wpdb->prepare(
|
|
"SELECT rl.*, rs.name as site_name, rs.icon as site_icon, rs.affiliate_param, rs.affiliate_code
|
|
FROM {$table} rl
|
|
LEFT JOIN {$sites_table} rs ON rl.site_id = rs.id
|
|
WHERE {$where}
|
|
ORDER BY rs.name ASC",
|
|
...$params
|
|
);
|
|
|
|
$links = $wpdb->get_results($query);
|
|
|
|
return $links;
|
|
}
|
|
|
|
/**
|
|
* Get books by series
|
|
*/
|
|
public static function get_books_by_series($series_id) {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('books');
|
|
|
|
$books = $wpdb->get_results(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE series_id = %d ORDER BY created_at ASC",
|
|
$series_id
|
|
)
|
|
);
|
|
|
|
return $books;
|
|
}
|
|
|
|
/**
|
|
* Sanitize ISBN
|
|
*/
|
|
private static function sanitize_isbn($isbn) {
|
|
return preg_replace('/[^0-9X]/i', '', $isbn);
|
|
}
|
|
|
|
/**
|
|
* Get series list
|
|
*/
|
|
public static function get_series_list() {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('series');
|
|
|
|
return $wpdb->get_results("SELECT * FROM {$table} ORDER BY name ASC");
|
|
}
|
|
|
|
/**
|
|
* Count books
|
|
*/
|
|
public static function count_books($search = '') {
|
|
global $wpdb;
|
|
|
|
$table = Umbra_Database::table('books');
|
|
|
|
$where = '1=1';
|
|
$params = [];
|
|
|
|
if (!empty($search)) {
|
|
$where .= ' AND title LIKE %s';
|
|
$params[] = '%' . $wpdb->esc_like($search) . '%';
|
|
}
|
|
|
|
$query = "SELECT COUNT(*) FROM {$table} WHERE {$where}";
|
|
|
|
if (!empty($params)) {
|
|
$query = $wpdb->prepare($query, ...$params);
|
|
}
|
|
|
|
return $wpdb->get_var($query);
|
|
}
|
|
|
|
/**
|
|
* Get rating for book
|
|
*/
|
|
public static function get_rating($book_id) {
|
|
$book = self::get_book($book_id);
|
|
|
|
if (!$book) {
|
|
return [
|
|
'amazon' => 0,
|
|
'goodreads' => 0,
|
|
];
|
|
}
|
|
|
|
return [
|
|
'amazon' => (float) $book->rating_amazon,
|
|
'goodreads' => (float) $book->rating_goodreads,
|
|
];
|
|
}
|
|
}
|