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.
296 lines
8.8 KiB
PHP
296 lines
8.8 KiB
PHP
<?php
|
|
/**
|
|
* Retail Sites management class
|
|
*
|
|
* @package BD_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class BDM_Retail_Sites {
|
|
|
|
/**
|
|
* Get all retail sites
|
|
*/
|
|
public static function get_sites($active_only = true) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('retail_sites');
|
|
|
|
$where = $active_only ? 'is_active = 1' : '1=1';
|
|
|
|
$sites = $wpdb->get_results(
|
|
"SELECT * FROM {$table} WHERE {$where} ORDER BY is_custom ASC, name ASC"
|
|
);
|
|
|
|
return $sites;
|
|
}
|
|
|
|
/**
|
|
* Get site by ID
|
|
*/
|
|
public static function get_site($site_id) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('retail_sites');
|
|
|
|
return $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE id = %d",
|
|
$site_id
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create new retail site
|
|
*/
|
|
public static function create_site($data) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('retail_sites');
|
|
|
|
$result = $wpdb->insert($table, [
|
|
'name' => sanitize_text_field($data['name']),
|
|
'url_pattern' => esc_url_raw($data['url_pattern']),
|
|
'icon' => sanitize_text_field($data['icon'] ?? ''),
|
|
'affiliate_param' => sanitize_text_field($data['affiliate_param'] ?? ''),
|
|
'affiliate_code' => sanitize_text_field($data['affiliate_code'] ?? ''),
|
|
'is_custom' => 1,
|
|
'is_active' => 1,
|
|
]);
|
|
|
|
return $result ? $wpdb->insert_id : false;
|
|
}
|
|
|
|
/**
|
|
* Update retail site
|
|
*/
|
|
public static function update_site($site_id, $data) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('retail_sites');
|
|
|
|
$update_data = [];
|
|
|
|
if (isset($data['name'])) {
|
|
$update_data['name'] = sanitize_text_field($data['name']);
|
|
}
|
|
|
|
if (isset($data['url_pattern'])) {
|
|
$update_data['url_pattern'] = esc_url_raw($data['url_pattern']);
|
|
}
|
|
|
|
if (isset($data['icon'])) {
|
|
$update_data['icon'] = sanitize_text_field($data['icon']);
|
|
}
|
|
|
|
if (isset($data['affiliate_param'])) {
|
|
$update_data['affiliate_param'] = sanitize_text_field($data['affiliate_param']);
|
|
}
|
|
|
|
if (isset($data['affiliate_code'])) {
|
|
$update_data['affiliate_code'] = sanitize_text_field($data['affiliate_code']);
|
|
}
|
|
|
|
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' => $site_id],
|
|
null,
|
|
['%d']
|
|
);
|
|
|
|
return $result !== false;
|
|
}
|
|
|
|
/**
|
|
* Delete retail site
|
|
*/
|
|
public static function delete_site($site_id) {
|
|
global $wpdb;
|
|
|
|
// Only allow deleting custom sites
|
|
$site = self::get_site($site_id);
|
|
if ($site && $site->is_custom) {
|
|
$wpdb->delete(BDM_Database::table('retail_links'), ['site_id' => $site_id], ['%d']);
|
|
return $wpdb->delete(BDM_Database::table('retail_sites'), ['id' => $site_id], ['%d']) !== false;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Generate links for a book
|
|
*/
|
|
public static function generate_links_for_book($book_id) {
|
|
global $wpdb;
|
|
|
|
$book = BDM_Book_Manager::get_book($book_id);
|
|
if (!$book) {
|
|
return false;
|
|
}
|
|
|
|
$sites = self::get_sites(true);
|
|
$links_table = BDM_Database::table('retail_links');
|
|
|
|
// Delete existing links
|
|
$wpdb->delete($links_table, ['book_id' => $book_id], ['%d']);
|
|
|
|
// Format to ISBN mapping
|
|
$formats = [
|
|
'ebook' => $book->isbn_ebook,
|
|
'paperback' => $book->isbn_paperback,
|
|
'hardcover' => $book->isbn_hardcover,
|
|
'audio' => $book->isbn_audio,
|
|
];
|
|
|
|
// Use Amazon ASIN for Amazon if available
|
|
$amazon_asin = $book->amazon_asin ?: '';
|
|
|
|
foreach ($formats as $format => $isbn) {
|
|
if (empty($isbn)) {
|
|
continue;
|
|
}
|
|
|
|
foreach ($sites as $site) {
|
|
$site_isbn = $isbn;
|
|
|
|
// Special handling for Amazon - needs ISBN-10 for /dp/ path
|
|
if ($site->name === 'Amazon') {
|
|
// Use ASIN if available (it's already ISBN-10 format)
|
|
if (!empty($amazon_asin)) {
|
|
$site_isbn = $amazon_asin;
|
|
$url = str_replace('{ISBN}', $site_isbn, $site->url_pattern);
|
|
} else {
|
|
// Try to convert ISBN-13 to ISBN-10
|
|
$isbn10 = self::convert_isbn13_to_isbn10($isbn);
|
|
if ($isbn10) {
|
|
$url = str_replace('{ISBN}', $isbn10, $site->url_pattern);
|
|
} else {
|
|
// Use search fallback for ISBNs that can't convert (979 prefix)
|
|
$url = 'https://www.amazon.com/s?k=' . urlencode($isbn) . '&i=stripbooks';
|
|
}
|
|
}
|
|
|
|
// Add affiliate code if available
|
|
if (!empty($site->affiliate_param) && !empty($site->affiliate_code)) {
|
|
$separator = strpos($url, '?') !== false ? '&' : '?';
|
|
$url .= $separator . $site->affiliate_param . '=' . $site->affiliate_code;
|
|
}
|
|
|
|
$wpdb->insert($links_table, [
|
|
'book_id' => $book_id,
|
|
'site_id' => $site->id,
|
|
'format' => $format,
|
|
'url' => $url,
|
|
'is_custom' => 0,
|
|
]);
|
|
continue;
|
|
}
|
|
|
|
$url = str_replace('{ISBN}', $site_isbn, $site->url_pattern);
|
|
|
|
// Add affiliate code if available
|
|
if (!empty($site->affiliate_param) && !empty($site->affiliate_code)) {
|
|
$separator = strpos($url, '?') !== false ? '&' : '?';
|
|
$url .= $separator . $site->affiliate_param . '=' . $site->affiliate_code;
|
|
}
|
|
|
|
$wpdb->insert($links_table, [
|
|
'book_id' => $book_id,
|
|
'site_id' => $site->id,
|
|
'format' => $format,
|
|
'url' => $url,
|
|
'is_custom' => 0,
|
|
]);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Convert ISBN-13 to ISBN-10
|
|
*
|
|
* @param string $isbn13 ISBN-13 to convert.
|
|
* @return string|false ISBN-10 on success, false on failure.
|
|
*/
|
|
private static function convert_isbn13_to_isbn10($isbn13) {
|
|
// Clean the ISBN
|
|
$isbn13 = preg_replace('/[^0-9X]/i', '', $isbn13);
|
|
|
|
// Must be 13 digits
|
|
if (strlen($isbn13) !== 13) {
|
|
return false;
|
|
}
|
|
|
|
// Must start with 978 (Bookland prefix)
|
|
if (substr($isbn13, 0, 3) !== '978') {
|
|
return false;
|
|
}
|
|
|
|
// Take the first 9 digits (drop 978 prefix and last check digit)
|
|
$isbn10 = substr($isbn13, 3, 9);
|
|
|
|
// Calculate check digit
|
|
$sum = 0;
|
|
for ($i = 0; $i < 9; $i++) {
|
|
$sum += (int)$isbn10[$i] * (10 - $i);
|
|
}
|
|
$remainder = $sum % 11;
|
|
$check_digit = (11 - $remainder) % 11;
|
|
|
|
// Convert check digit to ISBN-10 format (10 = X)
|
|
$check_digit = ($check_digit === 10) ? 'X' : (string)$check_digit;
|
|
|
|
return $isbn10 . $check_digit;
|
|
}
|
|
|
|
/**
|
|
* Update custom link
|
|
*/
|
|
public static function update_link($link_id, $url) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('retail_links');
|
|
|
|
return $wpdb->update(
|
|
$table,
|
|
['url' => esc_url_raw($url), 'is_custom' => 1],
|
|
['id' => $link_id],
|
|
['%s', '%d'],
|
|
['%d']
|
|
) !== false;
|
|
}
|
|
|
|
/**
|
|
* Create custom link
|
|
*/
|
|
public static function create_custom_link($book_id, $site_id, $format, $url) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('retail_links');
|
|
|
|
$result = $wpdb->insert($table, [
|
|
'book_id' => $book_id,
|
|
'site_id' => $site_id,
|
|
'format' => $format,
|
|
'url' => esc_url_raw($url),
|
|
'is_custom' => 1,
|
|
]);
|
|
|
|
return $result ? $wpdb->insert_id : false;
|
|
}
|
|
}
|