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.
162 lines
5.2 KiB
PHP
162 lines
5.2 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/**
|
|
* Installation and Database Setup
|
|
*
|
|
* @package Umbra_Bookshelf_Helper
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Umbra_Bookshelf_Helper_Install {
|
|
/**
|
|
* Activate plugin
|
|
*/
|
|
public static function activate() {
|
|
self::create_tables();
|
|
self::create_roles();
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
/**
|
|
* Deactivate plugin
|
|
*/
|
|
public static function deactivate() {
|
|
self::remove_roles();
|
|
wp_clear_scheduled_hook('bd_bookshelf_goodreads_refresh');
|
|
wp_clear_scheduled_hook('bd_bookshelf_amazon_scrape');
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
/**
|
|
* Create database tables
|
|
*
|
|
* {prefix}bd_books and {prefix}bd_series are NOT created here. Those two
|
|
* table names are owned by the catalogue module (Umbra_Database), whose
|
|
* schema is canonical. This module previously created them with a
|
|
* different column set, so activating both plugins meant one silently
|
|
* created the table and the other read it against the wrong shape.
|
|
*
|
|
* Existing library-shaped installations are reconciled by
|
|
* Umbra_Schema_Migration, which runs before this method.
|
|
*
|
|
* @see \Umbra_Schema_Migration::maybe_migrate()
|
|
*/
|
|
public static function create_tables() {
|
|
global $wpdb;
|
|
$charset_collate = $wpdb->get_charset_collate();
|
|
|
|
$tables = array(
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_book_meta (
|
|
id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
book_id BIGINT(20) NOT NULL,
|
|
language_code VARCHAR(5) NOT NULL,
|
|
title VARCHAR(255),
|
|
blurb TEXT,
|
|
preview_excerpt TEXT,
|
|
pull_quotes JSON,
|
|
UNIQUE KEY book_language (book_id, language_code)
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_pen_names (
|
|
id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
pen_name VARCHAR(255) NOT NULL,
|
|
notes TEXT,
|
|
active TINYINT(1) DEFAULT 1,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_reviews (
|
|
id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
book_id BIGINT(20) NOT NULL,
|
|
comment_text TEXT,
|
|
synopsis TEXT,
|
|
source_name VARCHAR(255) NOT NULL,
|
|
source_url VARCHAR(500),
|
|
display_order INT(11) DEFAULT 0,
|
|
KEY book_id (book_id)
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_ratings (
|
|
id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
book_id BIGINT(20) NOT NULL,
|
|
source VARCHAR(20) NOT NULL,
|
|
rating DECIMAL(3,2),
|
|
review_count INT(11),
|
|
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE KEY book_source (book_id, source)
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_review_shortlinks (
|
|
id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
book_id BIGINT(20) NOT NULL,
|
|
site_code VARCHAR(5) NOT NULL,
|
|
slug VARCHAR(20) NOT NULL UNIQUE,
|
|
destination_url VARCHAR(500),
|
|
status VARCHAR(20) DEFAULT 'pending',
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
activated_at DATETIME
|
|
) $charset_collate;",
|
|
);
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
|
|
|
foreach ($tables as $table) {
|
|
dbDelta($table);
|
|
}
|
|
|
|
self::install_sample_data();
|
|
}
|
|
|
|
/**
|
|
* Install sample data
|
|
*/
|
|
private static function install_sample_data() {
|
|
global $wpdb;
|
|
|
|
// Add default pen name
|
|
$pen_names_table = $wpdb->prefix . 'bd_pen_names';
|
|
$exists = $wpdb->get_var("SELECT COUNT(*) FROM {$pen_names_table}");
|
|
|
|
if ($exists == 0) {
|
|
$wpdb->insert($pen_names_table, array(
|
|
'pen_name' => 'B.D. Murphy',
|
|
'notes' => 'Primary author name',
|
|
'active' => 1,
|
|
'created_at' => current_time('mysql'),
|
|
));
|
|
}
|
|
|
|
// Set default Goodreads ID
|
|
update_option('bdm_goodreads_author_id', '30469651');
|
|
}
|
|
|
|
/**
|
|
* Create roles
|
|
*/
|
|
private static function create_roles() {
|
|
add_role('book_manager', esc_html__('Book Manager', 'umbra-bookshelf'), array(
|
|
'read' => true,
|
|
'edit_books' => true,
|
|
'edit_others_books' => true,
|
|
'publish_books' => true,
|
|
'read_private_posts' => true,
|
|
'edit_posts' => true,
|
|
'edit_others_posts' => true,
|
|
'publish_posts' => true,
|
|
'upload_files' => true,
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Remove roles
|
|
*/
|
|
private static function remove_roles() {
|
|
remove_role('book_manager');
|
|
}
|
|
}
|