Files
Bookshelf/Old/bd-murphy-author/inc/widgets.php
2026-10-18 19:01:01 -07:00

103 lines
4.2 KiB
PHP

<?php
// Copyright 2026 Umbra Tools (umbratools.com)
// SPDX-License-Identifier: Apache-2.0
/**
* Widgets
*
* @package BD_Murphy
*/
if (!defined('ABSPATH')) {
exit;
}
class BDM_Book_Shelf_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'bdm_book_shelf',
__('Book Shelf', 'bd-murphy'),
array('description' => __('Displays book covers from BD BookHelper', 'bd-murphy'))
);
}
public function widget($args, $instance) {
echo $args['before_widget'];
if (!empty($instance['title'])) {
echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
}
if (class_exists('BD_BookShelfHelper')) {
$books = BD_BookShelfHelper::get_featured_books(5);
if (!empty($books)) {
echo '<div class="book-shelf-widget">';
foreach ($books as $book) {
$cover_url = BD_BookShelfHelper::get_cover_url($book->id, 'shelf-thumb');
$title = esc_html($book->title);
$url = BD_BookShelfHelper::get_book_url($book->id);
$ratings = BD_BookShelfHelper::get_ratings($book->id);
$retailers = BD_BookShelfHelper::get_retailer_links($book->id);
echo '<div class="book-shelf-item">';
echo '<a href="' . esc_url($url) . '">';
echo '<img src="' . esc_url($cover_url) . '" alt="' . $title . '" class="book-shelf-cover">';
echo '</a>';
echo '<div class="book-shelf-info">';
echo '<h4 class="book-shelf-title"><a href="' . esc_url($url) . '">' . $title . '</a></h4>';
if ($ratings) {
echo '<div class="book-shelf-ratings">';
if (!empty($ratings['amazon'])) {
echo '<span class="rating amazon">Amazon: ' . esc_html($ratings['amazon']) . '</span>';
}
if (!empty($ratings['goodreads'])) {
echo '<span class="rating goodreads">Goodreads: ' . esc_html($ratings['goodreads']) . '</span>';
}
echo '</div>';
}
if ($retailers) {
echo '<div class="book-shelf-retailers">';
foreach ($retailers as $retailer) {
echo '<a href="' . esc_url($retailer['url']) . '" class="retailer-icon retailer-' . esc_attr($retailer['name']) . '" target="_blank" rel="noopener">' . esc_html($retailer['name']) . '</a>';
}
echo '</div>';
}
echo '</div>';
echo '</div>';
}
echo '</div>';
} else {
echo '<p class="no-books">' . __('No books available yet.', 'bd-murphy') . '</p>';
}
} else {
echo '<p class="book-helper-notice">' . __('BD BookHelper plugin is required for this widget.', 'bd-murphy') . '</p>';
}
echo $args['after_widget'];
}
public function form($instance) {
$title = !empty($instance['title']) ? $instance['title'] : __('Book Shelf', 'bd-murphy');
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'bd-murphy'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<?php
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
return $instance;
}
}
function bdm_register_widgets() {
register_widget('BDM_Book_Shelf_Widget');
}
add_action('widgets_init', 'bdm_register_widgets');