180 lines
4.6 KiB
PHP
180 lines
4.6 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Authors Management class
|
|
*
|
|
* @package BD_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class BDM_Authors {
|
|
|
|
/**
|
|
* Get all authors
|
|
*/
|
|
public static function get_authors($args = []) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('authors');
|
|
|
|
$defaults = [
|
|
'number' => 20,
|
|
'offset' => 0,
|
|
'orderby' => 'display_name',
|
|
'order' => 'ASC',
|
|
'search' => '',
|
|
];
|
|
|
|
$args = wp_parse_args($args, $defaults);
|
|
|
|
$where = '1=1';
|
|
$params = [];
|
|
|
|
if (!empty($args['search'])) {
|
|
$where .= ' AND display_name LIKE %s';
|
|
$params[] = '%' . $wpdb->esc_like($args['search']) . '%';
|
|
}
|
|
|
|
$order = strtoupper($args['order']) === 'DESC' ? 'DESC' : 'ASC';
|
|
|
|
$query = "SELECT * FROM {$table} WHERE {$where} ORDER BY {$args['orderby']} {$order} LIMIT %d OFFSET %d";
|
|
$params[] = $args['number'];
|
|
$params[] = $args['offset'];
|
|
|
|
if (!empty($params)) {
|
|
$query = $wpdb->prepare($query, ...$params);
|
|
}
|
|
|
|
return $wpdb->get_results($query);
|
|
}
|
|
|
|
/**
|
|
* Get author by ID
|
|
*/
|
|
public static function get_author($author_id) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('authors');
|
|
|
|
return $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE id = %d",
|
|
$author_id
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create author
|
|
*/
|
|
public static function create_author($data) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('authors');
|
|
|
|
$result = $wpdb->insert($table, [
|
|
'display_name' => sanitize_text_field($data['display_name']),
|
|
'email' => sanitize_email($data['email'] ?? ''),
|
|
'website' => esc_url_raw($data['website'] ?? ''),
|
|
'bio' => wp_kses_post($data['bio'] ?? ''),
|
|
'goodreads_author_id' => sanitize_text_field($data['goodreads_author_id'] ?? ''),
|
|
'photo_url' => esc_url_raw($data['photo_url'] ?? ''),
|
|
]);
|
|
|
|
return $result ? $wpdb->insert_id : false;
|
|
}
|
|
|
|
/**
|
|
* Update author
|
|
*/
|
|
public static function update_author($author_id, $data) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('authors');
|
|
|
|
$update_data = [];
|
|
|
|
if (isset($data['display_name'])) {
|
|
$update_data['display_name'] = sanitize_text_field($data['display_name']);
|
|
}
|
|
|
|
if (isset($data['email'])) {
|
|
$update_data['email'] = sanitize_email($data['email']);
|
|
}
|
|
|
|
if (isset($data['website'])) {
|
|
$update_data['website'] = esc_url_raw($data['website']);
|
|
}
|
|
|
|
if (isset($data['bio'])) {
|
|
$update_data['bio'] = wp_kses_post($data['bio']);
|
|
}
|
|
|
|
if (isset($data['goodreads_author_id'])) {
|
|
$update_data['goodreads_author_id'] = sanitize_text_field($data['goodreads_author_id']);
|
|
}
|
|
|
|
if (isset($data['photo_url'])) {
|
|
$update_data['photo_url'] = esc_url_raw($data['photo_url']);
|
|
}
|
|
|
|
if (empty($update_data)) {
|
|
return false;
|
|
}
|
|
|
|
$result = $wpdb->update(
|
|
$table,
|
|
$update_data,
|
|
['id' => $author_id],
|
|
null,
|
|
['%d']
|
|
);
|
|
|
|
return $result !== false;
|
|
}
|
|
|
|
/**
|
|
* Delete author
|
|
*/
|
|
public static function delete_author($author_id) {
|
|
global $wpdb;
|
|
|
|
// Remove from book_authors
|
|
$wpdb->delete(BDM_Database::table('book_authors'), ['author_id' => $author_id], ['%d']);
|
|
|
|
// Delete author
|
|
$table = BDM_Database::table('authors');
|
|
return $wpdb->delete($table, ['id' => $author_id], ['%d']) !== false;
|
|
}
|
|
|
|
/**
|
|
* Count authors
|
|
*/
|
|
public static function count_authors($search = '') {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('authors');
|
|
|
|
$where = '1=1';
|
|
$params = [];
|
|
|
|
if (!empty($search)) {
|
|
$where .= ' AND display_name LIKE %s';
|
|
$params[] = '%' . $wpdb->esc_like($search) . '%';
|
|
}
|
|
|
|
$query = "SELECT COUNT(*) FROM {$table} WHERE {$where}";
|
|
|
|
if (!empty($params)) {
|
|
$query = $wpdb->prepare($query, ...$params);
|
|
}
|
|
|
|
return $wpdb->get_var($query);
|
|
}
|
|
}
|