Files
Bookshelf/bd-bookshelf-plugin/includes/class-bdm-theme-compatibility.php
bdm 38cdebd0c6 Track Bookshelf WordPress theme, plugins and specs
Initial import of the bdmurphy Bookshelf work: the bd-bookshelf theme,
bd-bookshelf-plugin, bd-bookshelfhelper and bd-simpleemail plugins, plus
the specification documents, mockups and release archives.
2026-09-28 06:24:25 -07:00

215 lines
5.5 KiB
PHP

<?php
/**
* Theme Compatibility Layer
*
* Provides compatibility with BD BookHelper plugin interface
* so the BD Bookshelf theme works with our new plugin.
*
* @package BD_Bookshelf
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* BD_BookShelfHelper class for theme compatibility
* Maps theme expectations to our new plugin methods
*/
class BD_BookShelfHelper {
/**
* 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 = BDM_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 = BDM_Book_Manager::get_books_by_series($series_id);
return count($books);
}
/**
* Get series info
*/
public static function get_series_info($series_id) {
$series = BDM_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 = BDM_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 BDM_Book_Manager::get_book($book_id);
}
// Try to find by title
$post = get_post($post_id);
if ($post) {
$book = BDM_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;
}
}