Files
Umbra-Bookshelf/bd-bookshelf-plugin/includes/class-bdm-shortcodes.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

336 lines
9.7 KiB
PHP

<?php
/**
* Shortcodes Manager class
*
* @package BD_Bookshelf
*/
if (!defined('ABSPATH')) {
exit;
}
class BDM_Shortcodes {
/**
* Constructor
*/
public function __construct() {
// Register shortcodes
add_shortcode('sell_ebook', [$this, 'shortcode_sell_ebook']);
add_shortcode('sell_pb', [$this, 'shortcode_sell_pb']);
add_shortcode('sell_hc', [$this, 'shortcode_sell_hc']);
add_shortcode('sell_audio', [$this, 'shortcode_sell_audio']);
add_shortcode('rating', [$this, 'shortcode_rating']);
// Register redirect handler
add_action('template_redirect', [$this, 'handle_redirect']);
}
/**
* Shortcode: [sell_ebook id="123"]
*/
public function shortcode_sell_ebook($atts) {
return $this->render_sell_grid($atts, 'ebook');
}
/**
* Shortcode: [sell_pb id="123"]
*/
public function shortcode_sell_pb($atts) {
return $this->render_sell_grid($atts, 'paperback');
}
/**
* Shortcode: [sell_hc id="123"]
*/
public function shortcode_sell_hc($atts) {
return $this->render_sell_grid($atts, 'hardcover');
}
/**
* Shortcode: [sell_audio id="123"]
*/
public function shortcode_sell_audio($atts) {
return $this->render_sell_grid($atts, 'audio');
}
/**
* Render sell grid for a format
*/
private function render_sell_grid($atts, $format) {
$atts = shortcode_atts([
'id' => 0,
'class' => '',
'columns' => 'auto',
'show_icons' => 'true',
'show_names' => 'true',
'icon_style' => 'simple',
], $atts);
$book_id = absint($atts['id']);
if (!$book_id) {
return '';
}
$links = BDM_Book_Manager::get_book_retail_links($book_id, $format);
if (empty($links)) {
return '';
}
$class = sanitize_html_class('bd-bookshelf-sell-grid bd-bookshelf-format-' . $format . ' ' . $atts['class']);
$columns = in_array($atts['columns'], ['2', '3', '4']) ? absint($atts['columns']) : '';
$show_icons = $atts['show_icons'] === 'true';
$show_names = $atts['show_names'] === 'true';
$output = '<div class="' . esc_attr($class) . '"';
if ($columns) {
$output .= ' style="grid-template-columns: repeat(' . $columns . ', 1fr);"';
}
$output .= '>';
foreach ($links as $link) {
$output .= '<a href="' . esc_url($link->url) . '" class="bd-bookshelf-sell-item" target="_blank" rel="nofollow noopener">';
if ($show_icons) {
$output .= $this->render_site_icon($link->site_icon, $link->site_name, $atts['icon_style']);
}
if ($show_names) {
$output .= '<span class="bd-bookshelf-site-name">' . esc_html($link->site_name) . '</span>';
}
$output .= '</a>';
}
$output .= '</div>';
return $output;
}
/**
* Render site icon
*/
private function render_site_icon($icon, $name, $style = 'simple') {
if ($style === 'uploaded' && preg_match('/^https?:\/\//', $icon)) {
return '<img src="' . esc_url($icon) . '" alt="' . esc_attr($name) . '" class="bd-bookshelf-site-icon">';
}
// Simple letter icon
$letter = strtoupper(substr($name, 0, 1));
return '<span class="bd-bookshelf-site-icon bd-bookshelf-icon-simple">' . esc_html($letter) . '</span>';
}
/**
* Shortcode: [rating title="Book Title" format="both"]
*/
public function shortcode_rating($atts) {
$atts = shortcode_atts([
'title' => '',
'format' => 'both',
], $atts);
if (empty($atts['title'])) {
return '';
}
$book = BDM_Book_Manager::get_book_by_title(sanitize_text_field($atts['title']));
if (!$book) {
return '';
}
$rating = BDM_Book_Manager::get_rating($book->id);
$format = in_array($atts['format'], ['stars', 'numeric', 'both']) ? $atts['format'] : 'both';
$output = '<span class="bd-bookshelf-rating">';
if ($format === 'stars' || $format === 'both') {
$output .= $this->render_stars($rating['goodreads']);
}
if ($format === 'numeric' || $format === 'both') {
$output .= '<span class="bd-bookshelf-rating-number">' . number_format($rating['goodreads'], 1) . '/5</span>';
}
$output .= '</span>';
return $output;
}
/**
* Render star rating with Unicode stars
*/
private function render_stars($rating) {
$full_stars = floor($rating);
$half_star = ($rating - $full_stars) >= 0.5;
$empty_stars = 5 - $full_stars - ($half_star ? 1 : 0);
$output = '<span class="bd-bookshelf-stars">';
for ($i = 0; $i < $full_stars; $i++) {
$output .= '<span class="bd-bookshelf-star full">★</span>';
}
if ($half_star) {
$output .= '<span class="bd-bookshelf-star half">★</span>';
}
for ($i = 0; $i < $empty_stars; $i++) {
$output .= '<span class="bd-bookshelf-star empty">☆</span>';
}
$output .= '</span>';
return $output;
}
/**
* Handle shortcode redirects
*/
public function handle_redirect() {
global $wpdb;
if (is_admin()) {
return;
}
$slug = get_query_var('bd_bookshelf_redirect');
if (empty($slug)) {
return;
}
$table = BDM_Database::table('shortcodes');
$shortcode = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$table} WHERE slug = %s AND is_active = 1",
$slug
)
);
if (!$shortcode) {
return;
}
// Track click
$this->track_click($shortcode->id);
// Redirect
$type = in_array($shortcode->redirect_type, ['301', '302', '307']) ? $shortcode->redirect_type : '301';
wp_redirect($shortcode->target_url, absint($type));
exit;
}
/**
* Track click for shortcode
*/
private function track_click($shortcode_id) {
global $wpdb;
$table = BDM_Database::table('shortcode_clicks');
$wpdb->insert($table, [
'shortcode_id' => $shortcode_id,
'ip_address' => $this->get_client_ip(),
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? sanitize_text_field($_SERVER['HTTP_USER_AGENT']) : '',
'referer' => isset($_SERVER['HTTP_REFERER']) ? esc_url_raw($_SERVER['HTTP_REFERER']) : '',
]);
// Increment click count
$wpdb->query(
$wpdb->prepare(
"UPDATE " . BDM_Database::table('shortcodes') . " SET click_count = click_count + 1 WHERE id = %d",
$shortcode_id
)
);
}
/**
* Get client IP address
*/
private function get_client_ip() {
$ip = '';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (!empty($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return sanitize_text_field($ip);
}
/**
* Create custom shortcode
*/
public static function create_shortcode($slug, $target_url, $redirect_type = '301', $description = '') {
global $wpdb;
$table = BDM_Database::table('shortcodes');
$result = $wpdb->insert($table, [
'slug' => sanitize_title($slug),
'target_url' => esc_url_raw($target_url),
'redirect_type' => in_array($redirect_type, ['301', '302', '307']) ? $redirect_type : '301',
'description' => sanitize_text_field($description),
'is_active' => 1,
]);
return $result ? $wpdb->insert_id : false;
}
/**
* Update custom shortcode
*/
public static function update_shortcode($shortcode_id, $data) {
global $wpdb;
$table = BDM_Database::table('shortcodes');
$update_data = [];
if (isset($data['slug'])) {
$update_data['slug'] = sanitize_title($data['slug']);
}
if (isset($data['target_url'])) {
$update_data['target_url'] = esc_url_raw($data['target_url']);
}
if (isset($data['redirect_type'])) {
$update_data['redirect_type'] = in_array($data['redirect_type'], ['301', '302', '307']) ? $data['redirect_type'] : '301';
}
if (isset($data['description'])) {
$update_data['description'] = sanitize_text_field($data['description']);
}
if (isset($data['is_active'])) {
$update_data['is_active'] = absint($data['is_active']);
}
if (empty($update_data)) {
return false;
}
$result = $wpdb->update(
$table,
$update_data,
['id' => $shortcode_id],
null,
['%d']
);
return $result !== false;
}
/**
* Delete custom shortcode
*/
public static function delete_shortcode($shortcode_id) {
global $wpdb;
$wpdb->delete(BDM_Database::table('shortcode_clicks'), ['shortcode_id' => $shortcode_id], ['%d']);
return $wpdb->delete(BDM_Database::table('shortcodes'), ['id' => $shortcode_id], ['%d']) !== false;
}
}