Files
Bookshelf/umbra-bookshelf/includes/books/class-umbra-theme-compatibility.php
bdm dec732931c Consolidate three plugins into Umbra Bookshelf
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.
2026-10-18 19:55:36 -07:00

237 lines
6.4 KiB
PHP

<?php
// Copyright 2026 Umbra Tools (umbratools.com)
// SPDX-License-Identifier: Apache-2.0
/**
* 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');
}