147 lines
3.4 KiB
PHP
147 lines
3.4 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Series Management class
|
|
*
|
|
* @package BD_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class BDM_Series {
|
|
|
|
/**
|
|
* Get all series
|
|
*/
|
|
public static function get_series($args = []) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('series');
|
|
|
|
$defaults = [
|
|
'number' => 20,
|
|
'offset' => 0,
|
|
'orderby' => 'name',
|
|
'order' => 'ASC',
|
|
];
|
|
|
|
$args = wp_parse_args($args, $defaults);
|
|
|
|
$order = strtoupper($args['order']) === 'DESC' ? 'DESC' : 'ASC';
|
|
|
|
$query = $wpdb->prepare(
|
|
"SELECT * FROM {$table} ORDER BY {$args['orderby']} {$order} LIMIT %d OFFSET %d",
|
|
$args['number'],
|
|
$args['offset']
|
|
);
|
|
|
|
return $wpdb->get_results($query);
|
|
}
|
|
|
|
/**
|
|
* Get series by ID
|
|
*/
|
|
public static function get_series_by_id($series_id) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('series');
|
|
|
|
return $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE id = %d",
|
|
$series_id
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create series
|
|
*/
|
|
public static function create_series($data) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('series');
|
|
|
|
$result = $wpdb->insert($table, [
|
|
'name' => sanitize_text_field($data['name']),
|
|
'description' => wp_kses_post($data['description'] ?? ''),
|
|
'cover_image' => esc_url_raw($data['cover_image'] ?? ''),
|
|
]);
|
|
|
|
return $result ? $wpdb->insert_id : false;
|
|
}
|
|
|
|
/**
|
|
* Update series
|
|
*/
|
|
public static function update_series($series_id, $data) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('series');
|
|
|
|
$update_data = [];
|
|
|
|
if (isset($data['name'])) {
|
|
$update_data['name'] = sanitize_text_field($data['name']);
|
|
}
|
|
|
|
if (isset($data['description'])) {
|
|
$update_data['description'] = wp_kses_post($data['description']);
|
|
}
|
|
|
|
if (isset($data['cover_image'])) {
|
|
$update_data['cover_image'] = esc_url_raw($data['cover_image']);
|
|
}
|
|
|
|
if (empty($update_data)) {
|
|
return false;
|
|
}
|
|
|
|
$result = $wpdb->update(
|
|
$table,
|
|
$update_data,
|
|
['id' => $series_id],
|
|
null,
|
|
['%d']
|
|
);
|
|
|
|
return $result !== false;
|
|
}
|
|
|
|
/**
|
|
* Delete series
|
|
*/
|
|
public static function delete_series($series_id) {
|
|
global $wpdb;
|
|
|
|
// Unlink books from series
|
|
$books_table = BDM_Database::table('books');
|
|
$wpdb->update(
|
|
$books_table,
|
|
['series_id' => 0],
|
|
['series_id' => $series_id],
|
|
['%d'],
|
|
['%d']
|
|
);
|
|
|
|
// Delete series
|
|
$table = BDM_Database::table('series');
|
|
return $wpdb->delete($table, ['id' => $series_id], ['%d']) !== false;
|
|
}
|
|
|
|
/**
|
|
* Count series
|
|
*/
|
|
public static function count_series() {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('series');
|
|
|
|
return $wpdb->get_var("SELECT COUNT(*) FROM {$table}");
|
|
}
|
|
}
|