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.
271 lines
6.9 KiB
PHP
271 lines
6.9 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/**
|
|
* Navigation Menu Hooks
|
|
*
|
|
* @package BD_Murphy
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Filter nav menu items to add dynamic submenus
|
|
*
|
|
* @param string $items Menu items HTML.
|
|
* @param stdClass $args Menu arguments.
|
|
* @return string
|
|
*/
|
|
function bdm_filter_nav_menu_items($items, $args) {
|
|
if ($args->theme_location !== 'primary') {
|
|
return $items;
|
|
}
|
|
|
|
// Get books for MyBooks submenu
|
|
$books = bdm_get_books_for_menu();
|
|
if (!empty($books)) {
|
|
$books_submenu = bdm_build_book_submenu($books);
|
|
|
|
// Find or create MyBooks menu item
|
|
if (preg_match('/(<li[^>]*>.*?My\s*Books.*?<\/li>)/s', $items, $matches)) {
|
|
$mybooks_item = $matches[1];
|
|
// Add submenu class to the li
|
|
$mybooks_item_with_sub = preg_replace(
|
|
'/(<li[^>]*class=["\'])/',
|
|
'$1menu-item-has-children ',
|
|
$mybooks_item
|
|
);
|
|
// Close current li and add submenu
|
|
$mybooks_item_with_sub = preg_replace(
|
|
'/<\/li>\s*$/',
|
|
$books_submenu . '</li>',
|
|
$mybooks_item_with_sub
|
|
);
|
|
$items = str_replace($mybooks_item, $mybooks_item_with_sub, $items);
|
|
}
|
|
}
|
|
|
|
// Get series for Series submenu
|
|
$series = bdm_get_series_for_menu();
|
|
if (!empty($series)) {
|
|
$series_submenu = bdm_build_series_submenu($series);
|
|
|
|
if (preg_match('/(<li[^>]*>.*?Series.*?<\/li>)/s', $items, $matches)) {
|
|
$series_item = $matches[1];
|
|
$series_item_with_sub = preg_replace(
|
|
'/(<li[^>]*class=["\'])/',
|
|
'$1menu-item-has-children ',
|
|
$series_item
|
|
);
|
|
$series_item_with_sub = preg_replace(
|
|
'/<\/li>\s*$/',
|
|
$series_submenu . '</li>',
|
|
$series_item_with_sub
|
|
);
|
|
$items = str_replace($series_item, $series_item_with_sub, $items);
|
|
}
|
|
}
|
|
|
|
// Get books with reviews for Reviews submenu
|
|
$review_books = bdm_get_books_with_reviews();
|
|
if (!empty($review_books)) {
|
|
$reviews_submenu = bdm_build_reviews_submenu($review_books);
|
|
|
|
if (preg_match('/(<li[^>]*>.*?Reviews.*?<\/li>)/s', $items, $matches)) {
|
|
$reviews_item = $matches[1];
|
|
$reviews_item_with_sub = preg_replace(
|
|
'/(<li[^>]*class=["\'])/',
|
|
'$1menu-item-has-children ',
|
|
$reviews_item
|
|
);
|
|
$reviews_item_with_sub = preg_replace(
|
|
'/<\/li>\s*$/',
|
|
$reviews_submenu . '</li>',
|
|
$reviews_item_with_sub
|
|
);
|
|
$items = str_replace($reviews_item, $reviews_item_with_sub, $items);
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
add_filter('wp_nav_menu_items', 'bdm_filter_nav_menu_items', 10, 2);
|
|
|
|
/**
|
|
* Get books for menu
|
|
*
|
|
* @return array
|
|
*/
|
|
function bdm_get_books_for_menu() {
|
|
$books = get_posts(array(
|
|
'post_type' => 'book',
|
|
'post_status' => 'publish',
|
|
'posts_per_page' => -1,
|
|
'orderby' => 'title',
|
|
'order' => 'ASC',
|
|
));
|
|
|
|
return $books;
|
|
}
|
|
|
|
/**
|
|
* Get series for menu
|
|
*
|
|
* @return array
|
|
*/
|
|
function bdm_get_series_for_menu() {
|
|
if (!taxonomy_exists('series')) {
|
|
return array();
|
|
}
|
|
|
|
$terms = get_terms(array(
|
|
'taxonomy' => 'series',
|
|
'hide_empty' => true,
|
|
'orderby' => 'name',
|
|
'order' => 'ASC',
|
|
));
|
|
|
|
return is_wp_error($terms) ? array() : $terms;
|
|
}
|
|
|
|
/**
|
|
* Get books with reviews
|
|
*
|
|
* @return array
|
|
*/
|
|
function bdm_get_books_with_reviews() {
|
|
$books = get_posts(array(
|
|
'post_type' => 'book',
|
|
'post_status' => 'publish',
|
|
'posts_per_page' => -1,
|
|
'orderby' => 'title',
|
|
'order' => 'ASC',
|
|
));
|
|
|
|
$review_books = array();
|
|
foreach ($books as $book) {
|
|
$goodreads = get_post_meta($book->ID, 'goodreads_url', true);
|
|
$amazon = get_post_meta($book->ID, 'amazon_review_url', true);
|
|
if (!empty($goodreads) || !empty($amazon)) {
|
|
$review_books[] = $book;
|
|
}
|
|
}
|
|
|
|
return $review_books;
|
|
}
|
|
|
|
/**
|
|
* Build book submenu HTML
|
|
*
|
|
* @param array $books Books.
|
|
* @return string
|
|
*/
|
|
function bdm_build_book_submenu($books) {
|
|
if (empty($books)) {
|
|
return '';
|
|
}
|
|
|
|
$submenu = "\n" . '<ul class="sub-menu">' . "\n";
|
|
|
|
foreach ($books as $book) {
|
|
$url = get_permalink($book->ID);
|
|
$title = $book->post_title;
|
|
$submenu .= '<li class="menu-item"><a href="' . esc_url($url) . '">' . esc_html($title) . '</a></li>' . "\n";
|
|
}
|
|
|
|
$submenu .= '</ul>';
|
|
|
|
return $submenu;
|
|
}
|
|
|
|
/**
|
|
* Build series submenu HTML
|
|
*
|
|
* @param array $series Series.
|
|
* @return string
|
|
*/
|
|
function bdm_build_series_submenu($series) {
|
|
if (empty($series)) {
|
|
return '';
|
|
}
|
|
|
|
$submenu = "\n" . '<ul class="sub-menu">' . "\n";
|
|
|
|
foreach ($series as $term) {
|
|
$url = get_term_link($term);
|
|
$submenu .= '<li class="menu-item"><a href="' . esc_url($url) . '">' . esc_html($term->name) . '</a></li>' . "\n";
|
|
}
|
|
|
|
$submenu .= '</ul>';
|
|
|
|
return $submenu;
|
|
}
|
|
|
|
/**
|
|
* Build reviews submenu HTML
|
|
*
|
|
* @param array $books Books with reviews.
|
|
* @return string
|
|
*/
|
|
function bdm_build_reviews_submenu($books) {
|
|
if (empty($books)) {
|
|
return '';
|
|
}
|
|
|
|
$submenu = "\n" . '<ul class="sub-menu">' . "\n";
|
|
|
|
foreach ($books as $book) {
|
|
$title = $book->post_title;
|
|
|
|
// Determine the review URL (prefer Goodreads if available)
|
|
$goodreads = get_post_meta($book->ID, 'goodreads_url', true);
|
|
$amazon = get_post_meta($book->ID, 'amazon_review_url', true);
|
|
$url = !empty($goodreads) ? $goodreads : $amazon;
|
|
|
|
if (!empty($url)) {
|
|
$submenu .= '<li class="menu-item"><a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($title) . '</a></li>' . "\n";
|
|
}
|
|
}
|
|
|
|
$submenu .= '</ul>';
|
|
|
|
return $submenu;
|
|
}
|
|
|
|
/**
|
|
* Check if series exist
|
|
*
|
|
* @return bool
|
|
*/
|
|
function bdm_series_exist() {
|
|
if (class_exists('BD_BookShelfHelper')) {
|
|
$terms = get_terms(array(
|
|
'taxonomy' => 'series',
|
|
'hide_empty' => true,
|
|
'fields' => 'count',
|
|
));
|
|
return $terms > 0;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Add series menu item class
|
|
*
|
|
* @param array $classes CSS classes.
|
|
* @param WP_Post $item Menu item.
|
|
* @param array $args Menu arguments.
|
|
* @param int $depth Item depth.
|
|
* @return array
|
|
*/
|
|
function bdm_nav_menu_css_class($classes, $item, $args, $depth) {
|
|
if ($item->url && false !== strpos($item->url, '/series/')) {
|
|
$classes[] = 'series-menu-item';
|
|
}
|
|
return $classes;
|
|
}
|
|
add_filter('nav_menu_css_class', 'bdm_nav_menu_css_class', 10, 4);
|