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.
217 lines
6.3 KiB
PHP
217 lines
6.3 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Rating Sync Engine class
|
|
*
|
|
* @package Umbra_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Umbra_Rating_Sync {
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct() {
|
|
// Schedule cron job
|
|
add_action('bd_bookshelf_sync_ratings', [$this, 'sync_all_ratings']);
|
|
|
|
// Register cron interval
|
|
add_filter('cron_schedules', [$this, 'add_cron_interval']);
|
|
|
|
// Schedule on plugin activation
|
|
if (!wp_next_scheduled('bd_bookshelf_sync_ratings')) {
|
|
wp_schedule_event(time(), 'bd_bookshelf_twelve_hours', 'bd_bookshelf_sync_ratings');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add custom cron interval
|
|
*/
|
|
public function add_cron_interval($schedules) {
|
|
$schedules['bd_bookshelf_twelve_hours'] = [
|
|
'interval' => 43200, // 12 hours in seconds
|
|
'display' => esc_html__('Every 12 Hours', 'umbra-bookshelf'),
|
|
];
|
|
|
|
return $schedules;
|
|
}
|
|
|
|
/**
|
|
* Sync all book ratings
|
|
*/
|
|
public function sync_all_ratings() {
|
|
$books = Umbra_Book_Manager::get_books(['number' => 999]);
|
|
|
|
$synced = 0;
|
|
$failed = 0;
|
|
|
|
foreach ($books as $book) {
|
|
$result = $this->sync_book_rating($book->id);
|
|
|
|
if ($result) {
|
|
$synced++;
|
|
} else {
|
|
$failed++;
|
|
}
|
|
|
|
// Delay between requests to avoid rate limiting
|
|
sleep(2);
|
|
}
|
|
|
|
// Log results
|
|
update_option('bd_bookshelf_last_sync', [
|
|
'time' => current_time('mysql'),
|
|
'synced' => $synced,
|
|
'failed' => $failed,
|
|
'total' => count($books),
|
|
]);
|
|
|
|
return ['synced' => $synced, 'failed' => $failed];
|
|
}
|
|
|
|
/**
|
|
* Sync rating for a single book
|
|
*/
|
|
public function sync_book_rating($book_id) {
|
|
global $wpdb;
|
|
|
|
$book = Umbra_Book_Manager::get_book($book_id);
|
|
if (!$book) {
|
|
return false;
|
|
}
|
|
|
|
$amazon_rating = 0;
|
|
$goodreads_rating = 0;
|
|
|
|
// Sync Amazon rating
|
|
if (!empty($book->amazon_asin) || !empty($book->isbn_ebook)) {
|
|
$isbn = $book->amazon_asin ?: $book->isbn_ebook;
|
|
$amazon_rating = $this->fetch_amazon_rating($isbn);
|
|
|
|
// Keep last rating if fetch fails
|
|
if ($amazon_rating === false) {
|
|
$amazon_rating = $book->rating_amazon;
|
|
}
|
|
}
|
|
|
|
// Sync Goodreads rating
|
|
if (!empty($book->goodreads_id)) {
|
|
$goodreads_rating = $this->fetch_goodreads_rating($book->goodreads_id);
|
|
|
|
// Keep last rating if fetch fails
|
|
if ($goodreads_rating === false) {
|
|
$goodreads_rating = $book->rating_goodreads;
|
|
}
|
|
}
|
|
|
|
// Update book ratings
|
|
$table = Umbra_Database::table('books');
|
|
$wpdb->update(
|
|
$table,
|
|
[
|
|
'rating_amazon' => $amazon_rating,
|
|
'rating_goodreads' => $goodreads_rating,
|
|
'last_sync' => current_time('mysql'),
|
|
],
|
|
['id' => $book_id],
|
|
['%f', '%f', '%s'],
|
|
['%d']
|
|
);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Fetch Amazon rating via web scraping
|
|
*/
|
|
private function fetch_amazon_rating($isbn) {
|
|
$url = "https://www.amazon.com/dp/{$isbn}";
|
|
|
|
$response = wp_remote_get($url, [
|
|
'timeout' => 15,
|
|
'headers' => [
|
|
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
],
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
error_log('BD Bookshelf: Amazon fetch error - ' . $response->get_error_message());
|
|
return false;
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
// Parse rating from HTML
|
|
// Amazon uses data-hook="review-star-rating" or similar
|
|
if (preg_match('/data-hook="review-star-rating"[^>]*>.*?(\d+\.?\d*)\s*out of\s*5/si', $body, $matches)) {
|
|
return (float) $matches[1];
|
|
}
|
|
|
|
// Alternative pattern
|
|
if (preg_match('/a-icon-alt[^>]*>(\d+\.?\d*)\s*out of\s*\d+ stars/si', $body, $matches)) {
|
|
return (float) $matches[1];
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Fetch Goodreads rating via web scraping
|
|
*/
|
|
private function fetch_goodreads_rating($goodreads_id) {
|
|
$url = "https://www.goodreads.com/book/show/{$goodreads_id}";
|
|
|
|
$response = wp_remote_get($url, [
|
|
'timeout' => 15,
|
|
'headers' => [
|
|
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
],
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
error_log('BD Bookshelf: Goodreads fetch error - ' . $response->get_error_message());
|
|
return false;
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
// Parse rating from HTML
|
|
// Goodreads uses ratingValue in meta or specific class
|
|
if (preg_match('/ratingValue[^>]*>(\d+\.?\d*)</si', $body, $matches)) {
|
|
return (float) $matches[1];
|
|
}
|
|
|
|
// Alternative pattern
|
|
if (preg_match('/class="RatingStatistics__rating"[^>]*>(\d+\.?\d*)</si', $body, $matches)) {
|
|
return (float) $matches[1];
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Manual sync trigger
|
|
*/
|
|
public static function manual_sync() {
|
|
$sync = new self();
|
|
return $sync->sync_all_ratings();
|
|
}
|
|
|
|
/**
|
|
* Get last sync info
|
|
*/
|
|
public static function get_last_sync_info() {
|
|
return get_option('bd_bookshelf_last_sync', []);
|
|
}
|
|
}
|