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.
326 lines
11 KiB
PHP
326 lines
11 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Goodreads Scraper class
|
|
*
|
|
* Fetches author and book data from Goodreads
|
|
*
|
|
* @package Umbra_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class Umbra_Goodreads_Scraper {
|
|
|
|
private $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';
|
|
|
|
/**
|
|
* Get author info from Goodreads
|
|
*/
|
|
public function get_author_info($author_id) {
|
|
$url = "https://www.goodreads.com/author/show/{$author_id}";
|
|
|
|
$response = wp_remote_get($url, [
|
|
'timeout' => 30,
|
|
'headers' => [
|
|
'User-Agent' => $this->user_agent,
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
],
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
return ['error' => $response->get_error_message()];
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
return $this->parse_author_page($body, $author_id);
|
|
}
|
|
|
|
/**
|
|
* Get books from Goodreads author page
|
|
*/
|
|
public function get_author_books($author_id) {
|
|
$url = "https://www.goodreads.com/author/list/{$author_id}";
|
|
|
|
$response = wp_remote_get($url, [
|
|
'timeout' => 30,
|
|
'headers' => [
|
|
'User-Agent' => $this->user_agent,
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
],
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
return ['error' => $response->get_error_message()];
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
return $this->parse_books_list($body);
|
|
}
|
|
|
|
/**
|
|
* Get full book details from individual book page
|
|
*/
|
|
public function get_book_details($goodreads_id) {
|
|
$url = "https://www.goodreads.com/book/show/{$goodreads_id}";
|
|
|
|
$response = wp_remote_get($url, [
|
|
'timeout' => 30,
|
|
'headers' => [
|
|
'User-Agent' => $this->user_agent,
|
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
'Accept-Language' => 'en-US,en;q=0.5',
|
|
],
|
|
]);
|
|
|
|
if (is_wp_error($response)) {
|
|
return ['error' => $response->get_error_message()];
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
|
|
return $this->parse_book_details($body, $goodreads_id);
|
|
}
|
|
|
|
/**
|
|
* Parse author page HTML
|
|
*/
|
|
private function parse_author_page($html, $author_id) {
|
|
$author = [
|
|
'id' => $author_id,
|
|
'name' => '',
|
|
'photo' => '',
|
|
'bio' => '',
|
|
'website' => '',
|
|
'genre' => '',
|
|
'books_count' => 0,
|
|
'average_rating' => 0,
|
|
];
|
|
|
|
// Extract author name
|
|
if (preg_match('/<h1[^>]*class="[^"]*authorName[^"]*"[^>]*>(.*?)<\/h1>/si', $html, $matches)) {
|
|
$author['name'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract photo
|
|
if (preg_match('/<img[^>]*class="[^"]*authorImage[^"]*"[^>]*src="([^"]+)"/si', $html, $matches)) {
|
|
$author['photo'] = $matches[1];
|
|
}
|
|
|
|
// Extract bio
|
|
if (preg_match('/<div[^>]*class="[^"]*authorAbout[^"]*"[^>]*>.*?<div[^>]*class="[^"]*aboutRight[^"]*"[^>]*>(.*?)<\/div>/si', $html, $matches)) {
|
|
$author['bio'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract website
|
|
if (preg_match('/<a[^>]*class="[^"]*authorLink[^"]*"[^>]*href="([^"]+)"/si', $html, $matches)) {
|
|
$author['website'] = $matches[1];
|
|
}
|
|
|
|
// Extract books count
|
|
if (preg_match('/(\d+)\s*books?/si', $html, $matches)) {
|
|
$author['books_count'] = absint($matches[1]);
|
|
}
|
|
|
|
return $author;
|
|
}
|
|
|
|
/**
|
|
* Parse books list HTML
|
|
*/
|
|
private function parse_books_list($html) {
|
|
$books = [];
|
|
|
|
// Match book rows
|
|
if (preg_match_all('/<tr[^>]*itemtype="http:\/\/schema.org\/Book"[^>]*>(.*?)<\/tr>/si', $html, $matches)) {
|
|
foreach ($matches[1] as $row) {
|
|
$book = $this->parse_book_row($row);
|
|
if ($book) {
|
|
$books[] = $book;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $books;
|
|
}
|
|
|
|
/**
|
|
* Parse individual book row
|
|
*/
|
|
private function parse_book_row($html) {
|
|
$book = [
|
|
'title' => '',
|
|
'author' => '',
|
|
'isbn' => '',
|
|
'isbn13' => '',
|
|
'rating' => 0,
|
|
'ratings_count' => 0,
|
|
'reviews_count' => 0,
|
|
'cover_image' => '',
|
|
'description' => '',
|
|
'published' => '',
|
|
'goodreads_id' => '',
|
|
'goodreads_url' => '',
|
|
];
|
|
|
|
// Extract title from bookTitle span (role=heading)
|
|
if (preg_match('/<span[^>]*itemprop=["\']name["\'][^>]*role=["\']heading["\'][^>]*>(.*?)<\/span>/si', $html, $matches)) {
|
|
$book['title'] = trim(strip_tags($matches[1]));
|
|
} elseif (preg_match('/<a[^>]*class="[^"]*bookTitle[^"]*"[^>]*>.*?<span[^>]*>(.*?)<\/span>/si', $html, $matches)) {
|
|
$book['title'] = trim(strip_tags($matches[1]));
|
|
} elseif (preg_match('/<a[^>]*class="[^"]*bookTitle[^"]*"[^>]*>(.*?)<\/a>/si', $html, $matches)) {
|
|
$book['title'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract Goodreads URL and ID
|
|
if (preg_match('/<a[^>]*class="[^"]*bookTitle[^"]*"[^>]*href="([^"]+)"/si', $html, $matches)) {
|
|
$book['goodreads_url'] = trim($matches[1]);
|
|
if (preg_match('/\/book\/show\/(\d+)/', $matches[1], $id_matches)) {
|
|
$book['goodreads_id'] = $id_matches[1];
|
|
}
|
|
}
|
|
|
|
// Extract author
|
|
if (preg_match('/<a[^>]*class="[^"]*authorName[^"]*"[^>]*>.*?<span[^>]*itemprop=["\']name["\'][^>]*>(.*?)<\/span>/si', $html, $matches)) {
|
|
$book['author'] = trim(strip_tags($matches[1]));
|
|
} elseif (preg_match('/<a[^>]*class="[^"]*authorName[^"]*"[^>]*>(.*?)<\/a>/si', $html, $matches)) {
|
|
$book['author'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract cover image (class="bookCover" in list, class="bookImage" elsewhere)
|
|
if (preg_match('/<img[^>]*class="[^"]*bookCover[^"]*"[^>]*src="([^"]+)"/si', $html, $matches)) {
|
|
$book['cover_image'] = $matches[1];
|
|
} elseif (preg_match('/<img[^>]*class="[^"]*bookImage[^"]*"[^>]*src="([^"]+)"/si', $html, $matches)) {
|
|
$book['cover_image'] = $matches[1];
|
|
}
|
|
|
|
// Extract rating from minirating span
|
|
if (preg_match('/([\d.]+)\s*avg\s*rating\s*(?:—|—|--)\s*(\d[\d,]*)\s*rating/i', $html, $matches)) {
|
|
$book['rating'] = (float) $matches[1];
|
|
$book['ratings_count'] = absint(str_replace(',', '', $matches[2]));
|
|
} elseif (preg_match('/<span[^>]*class="[^"]*rating[^"]*"[^>]*>\s*(\d+\.?\d*)/si', $html, $matches)) {
|
|
$book['rating'] = (float) $matches[1];
|
|
}
|
|
|
|
// Extract ratings count (fallback)
|
|
if ($book['ratings_count'] === 0 && preg_match('/(\d+[\d,]*)\s*ratings?/si', $html, $matches)) {
|
|
$book['ratings_count'] = absint(str_replace(',', '', $matches[1]));
|
|
}
|
|
|
|
// Extract reviews count
|
|
if (preg_match('/(\d+[\d,]*)\s*reviews?/si', $html, $matches)) {
|
|
$book['reviews_count'] = absint(str_replace(',', '', $matches[1]));
|
|
}
|
|
|
|
// Extract ISBN
|
|
if (preg_match('/isbn(?:13)?[:\s]*(\d{10,13})/si', $html, $matches)) {
|
|
$book['isbn'] = $matches[1];
|
|
}
|
|
|
|
// Extract published date
|
|
if (preg_match('/published[^:]*:\s*([A-Za-z]+\s+\d{1,2},?\s*\d{4})/si', $html, $matches)) {
|
|
$book['published'] = trim($matches[1]);
|
|
}
|
|
|
|
// Upgrade cover image to larger version
|
|
if (!empty($book['cover_image'])) {
|
|
$book['cover_image'] = $this->upgrade_cover_url($book['cover_image']);
|
|
}
|
|
|
|
// Only return if we have a title
|
|
if (empty($book['title'])) {
|
|
return null;
|
|
}
|
|
|
|
return $book;
|
|
}
|
|
|
|
/**
|
|
* Parse book details page
|
|
*/
|
|
private function parse_book_details($html, $goodreads_id) {
|
|
$book = [
|
|
'goodreads_id' => $goodreads_id,
|
|
'title' => '',
|
|
'author' => '',
|
|
'isbn' => '',
|
|
'isbn13' => '',
|
|
'rating' => 0,
|
|
'ratings_count' => 0,
|
|
'reviews_count' => 0,
|
|
'cover_image' => '',
|
|
'description' => '',
|
|
'published' => '',
|
|
'pages' => 0,
|
|
'genre' => '',
|
|
];
|
|
|
|
// Extract title
|
|
if (preg_match('/<h1[^>]*id="bookTitle"[^>]*>(.*?)<\/h1>/si', $html, $matches)) {
|
|
$book['title'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract author
|
|
if (preg_match('/<a[^>]*class="[^"]*authorName[^"]*"[^>]*>(.*?)<\/a>/si', $html, $matches)) {
|
|
$book['author'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract cover image
|
|
if (preg_match('/<img[^>]*id="coverImage"[^>]*src="([^"]+)"/si', $html, $matches)) {
|
|
$book['cover_image'] = $matches[1];
|
|
}
|
|
|
|
// Extract description
|
|
if (preg_match('/<div[^>]*id="description"[^>]*>.*?<span[^>]*>(.*?)<\/span>/si', $html, $matches)) {
|
|
$book['description'] = trim(strip_tags($matches[1]));
|
|
}
|
|
|
|
// Extract ISBN13
|
|
if (preg_match('/isbn13[:\s]*(\d{13})/si', $html, $matches)) {
|
|
$book['isbn13'] = $matches[1];
|
|
}
|
|
|
|
// Extract ISBN
|
|
if (preg_match('/isbn[:\s]*(\d{10})/si', $html, $matches)) {
|
|
$book['isbn'] = $matches[1];
|
|
}
|
|
|
|
// Extract rating
|
|
if (preg_match('/<span[^>]*class="[^"]*RatingStatistics__rating[^"]*"[^>]*>(\d+\.?\d*)/si', $html, $matches)) {
|
|
$book['rating'] = (float) $matches[1];
|
|
}
|
|
|
|
// Extract pages
|
|
if (preg_match('/(\d+)\s*pages?/si', $html, $matches)) {
|
|
$book['pages'] = absint($matches[1]);
|
|
}
|
|
|
|
// Extract published date
|
|
if (preg_match('/published[^:]*:?\s*([A-Za-z]+\s+\d{1,2},?\s*\d{4})/si', $html, $matches)) {
|
|
$book['published'] = trim($matches[1]);
|
|
}
|
|
|
|
return $book;
|
|
}
|
|
|
|
/**
|
|
* Upgrade a Goodreads cover URL to a larger version
|
|
*
|
|
* Goodreads uses _SY75_ for thumbnails. We can change to _SX300_ for larger.
|
|
*/
|
|
private function upgrade_cover_url($url) {
|
|
// Replace thumbnail size with larger version
|
|
$url = preg_replace('/\._SY\d+_/', '._SX300_', $url);
|
|
$url = preg_replace('/\._SX\d+_/', '._SX300_', $url);
|
|
return $url;
|
|
}
|
|
}
|