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.
237 lines
6.4 KiB
PHP
237 lines
6.4 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/**
|
|
* Theme Compatibility Layer
|
|
*
|
|
* Provides the static helper interface the BD Bookshelf theme calls (book
|
|
* covers, pen names, ratings, retailer links, series info, ...), backed by the
|
|
* catalogue module.
|
|
*
|
|
* This class used to be declared as BD_BookShelfHelper, which collided with
|
|
* the library module's own BD_BookShelfHelper class. That collision was latent
|
|
* while the two were separate plugins -- it only fired when both were active --
|
|
* but became an unconditional "Cannot declare class" fatal error once they
|
|
* were merged into a single plugin. It now has its own name, and the
|
|
* class_alias() at the bottom keeps the legacy name working for the
|
|
* unmodified theme.
|
|
*
|
|
* @package Umbra_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Static helper API consumed by the theme.
|
|
*/
|
|
class Umbra_Theme_Compatibility {
|
|
|
|
/**
|
|
* Get book cover URL
|
|
*/
|
|
public static function get_cover_url($book_id) {
|
|
$book = self::get_book_from_post($book_id);
|
|
if ($book && !empty($book->cover_image)) {
|
|
return $book->cover_image;
|
|
}
|
|
|
|
// Fallback to post thumbnail
|
|
$thumb = get_the_post_thumbnail_url($book_id, 'book-cover');
|
|
return $thumb ? $thumb : '';
|
|
}
|
|
|
|
/**
|
|
* Get pen name (primary author)
|
|
*/
|
|
public static function get_pen_name($book_id) {
|
|
$book = self::get_book_from_post($book_id);
|
|
if ($book && !empty($book->authors)) {
|
|
foreach ($book->authors as $author) {
|
|
if ($author->is_primary) {
|
|
return $author->author_name;
|
|
}
|
|
}
|
|
// Return first author if no primary
|
|
return $book->authors[0]->author_name;
|
|
}
|
|
|
|
return get_option('bdm_author_name', '');
|
|
}
|
|
|
|
/**
|
|
* Get ratings
|
|
*/
|
|
public static function get_ratings($book_id) {
|
|
$book = self::get_book_from_post($book_id);
|
|
if ($book) {
|
|
return [
|
|
'amazon' => (float) $book->rating_amazon,
|
|
'goodreads' => (float) $book->rating_goodreads,
|
|
];
|
|
}
|
|
|
|
return ['amazon' => 0, 'goodreads' => 0];
|
|
}
|
|
|
|
/**
|
|
* Get retailer links
|
|
*/
|
|
public static function get_retailer_links($book_id, $format = 'ebook') {
|
|
$book = self::get_book_from_post($book_id);
|
|
if (!$book) {
|
|
return [];
|
|
}
|
|
|
|
$links = Umbra_Book_Manager::get_book_retail_links($book->id, $format);
|
|
$retailers = [];
|
|
|
|
foreach ($links as $link) {
|
|
$retailers[] = [
|
|
'url' => $link->url,
|
|
'name' => $link->site_name,
|
|
'retailer' => strtolower(str_replace(' ', '-', $link->site_name)),
|
|
];
|
|
}
|
|
|
|
return $retailers;
|
|
}
|
|
|
|
/**
|
|
* Get retailer links by language
|
|
*/
|
|
public static function get_retailer_links_by_language($book_id, $language = 'en', $format = 'ebook') {
|
|
return self::get_retailer_links($book_id, $format);
|
|
}
|
|
|
|
/**
|
|
* Get series book count
|
|
*/
|
|
public static function get_series_book_count($series_id) {
|
|
$books = Umbra_Book_Manager::get_books_by_series($series_id);
|
|
return count($books);
|
|
}
|
|
|
|
/**
|
|
* Get series info
|
|
*/
|
|
public static function get_series_info($series_id) {
|
|
$series = Umbra_Series::get_series_by_id($series_id);
|
|
if ($series) {
|
|
return [
|
|
'name' => $series->name,
|
|
'description' => $series->description,
|
|
];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get reviews
|
|
*/
|
|
public static function get_reviews($book_id) {
|
|
// Return empty for now - reviews would need a separate implementation
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Get pull quotes
|
|
*/
|
|
public static function get_pull_quotes($book_id) {
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Get preview excerpt
|
|
*/
|
|
public static function get_preview_excerpt($book_id) {
|
|
$book = self::get_book_from_post($book_id);
|
|
if ($book) {
|
|
return wp_trim_words($book->description, 50);
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Get available languages
|
|
*/
|
|
public static function get_available_languages($book_id) {
|
|
return ['en'];
|
|
}
|
|
|
|
/**
|
|
* Get awards
|
|
*/
|
|
public static function get_awards() {
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Display retailer links
|
|
*/
|
|
public static function display_retailer_links($book_id, $language = 'en', $format = 'ebook') {
|
|
$links = self::get_retailer_links($book_id, $format);
|
|
|
|
if (empty($links)) {
|
|
return;
|
|
}
|
|
|
|
echo '<div class="retailer-links">';
|
|
foreach ($links as $link) {
|
|
echo '<a href="' . esc_url($link['url']) . '" target="_blank" rel="nofollow noopener" class="retailer-link">';
|
|
echo esc_html($link['name']);
|
|
echo '</a>';
|
|
}
|
|
echo '</div>';
|
|
}
|
|
|
|
/**
|
|
* Get book from post ID
|
|
* Maps WordPress post to our database book
|
|
*/
|
|
private static function get_book_from_post($post_id) {
|
|
global $wpdb;
|
|
|
|
// Check if post_id is actually a book ID from our database
|
|
$book = Umbra_Book_Manager::get_book($post_id);
|
|
if ($book) {
|
|
return $book;
|
|
}
|
|
|
|
// Check if there's a book ID stored in post meta
|
|
$book_id = get_post_meta($post_id, '_bd_bookshelf_book_id', true);
|
|
|
|
if ($book_id) {
|
|
return Umbra_Book_Manager::get_book($book_id);
|
|
}
|
|
|
|
// Try to find by title
|
|
$post = get_post($post_id);
|
|
if ($post) {
|
|
$book = Umbra_Book_Manager::get_book_by_title($post->post_title);
|
|
if ($book) {
|
|
// Link post to book
|
|
update_post_meta($post_id, '_bd_bookshelf_book_id', $book->id);
|
|
return $book;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Legacy alias.
|
|
*
|
|
* The BD Bookshelf theme (unchanged, and out of scope for this consolidation)
|
|
* still calls BD_BookShelfHelper:: statically. Keep that working. Guarded
|
|
* because a theme or add-on may legitimately have declared the name already.
|
|
*/
|
|
if (!class_exists('BD_BookShelfHelper', false)) {
|
|
class_alias('Umbra_Theme_Compatibility', 'BD_BookShelfHelper');
|
|
}
|