171 lines
6.1 KiB
PHP
171 lines
6.1 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Installation and Database Setup
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class BD_BookShelfHelper_Install {
|
|
public static function activate() {
|
|
self::create_tables();
|
|
self::create_roles();
|
|
self::setup_scheduled_tasks();
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
public static function deactivate() {
|
|
self::remove_roles();
|
|
wp_clear_scheduled_task('bd_bookshelf_goodreads_refresh');
|
|
wp_clear_scheduled_task('bd_bookshelf_amazon_scrape');
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
public static function create_tables() {
|
|
global $wpdb;
|
|
$charset_collate = $wpdb->get_charset_collate();
|
|
|
|
$tables = array(
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_books (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
slug varchar(100) NOT NULL,
|
|
pen_name_id bigint(20) DEFAULT NULL,
|
|
series_id bigint(20) DEFAULT NULL,
|
|
volume_number int(11) DEFAULT NULL,
|
|
asin varchar(20) DEFAULT NULL,
|
|
isbn varchar(20) DEFAULT NULL,
|
|
publish_date date DEFAULT NULL,
|
|
sort_order int(11) DEFAULT 0,
|
|
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (id),
|
|
UNIQUE KEY slug (slug)
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_book_meta (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
book_id bigint(20) NOT NULL,
|
|
language_code varchar(5) NOT NULL,
|
|
title varchar(255) DEFAULT NULL,
|
|
blurb text,
|
|
preview_excerpt text,
|
|
PRIMARY KEY (id),
|
|
KEY book_id (book_id),
|
|
KEY language_code (language_code)
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_series (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
name varchar(255) NOT NULL,
|
|
slug varchar(100) NOT NULL,
|
|
cover_attachment_id bigint(20) DEFAULT NULL,
|
|
header_attachment_id bigint(20) DEFAULT NULL,
|
|
blurb text,
|
|
display_order int(11) DEFAULT 0,
|
|
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (id),
|
|
UNIQUE KEY slug (slug)
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_pen_names (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
pen_name varchar(255) NOT NULL,
|
|
notes text,
|
|
active tinyint(1) DEFAULT 1,
|
|
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (id)
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_reviews (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
book_id bigint(20) NOT NULL,
|
|
comment_text text,
|
|
synopsis text,
|
|
source_name varchar(255) NOT NULL,
|
|
source_url varchar(500) DEFAULT NULL,
|
|
display_order int(11) DEFAULT 0,
|
|
PRIMARY KEY (id),
|
|
KEY book_id (book_id)
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_ratings (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
book_id bigint(20) NOT NULL,
|
|
source varchar(20) NOT NULL,
|
|
rating decimal(3,2) DEFAULT NULL,
|
|
review_count int(11) DEFAULT NULL,
|
|
last_updated datetime DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (id),
|
|
UNIQUE KEY book_source (book_id, source)
|
|
) $charset_collate;",
|
|
|
|
"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}bd_review_shortlinks (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
book_id bigint(20) NOT NULL,
|
|
site_code varchar(5) NOT NULL,
|
|
slug varchar(20) NOT NULL,
|
|
destination_url varchar(500) DEFAULT NULL,
|
|
status varchar(20) DEFAULT 'pending',
|
|
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
|
activated_at datetime DEFAULT NULL,
|
|
PRIMARY KEY (id),
|
|
UNIQUE KEY slug (slug)
|
|
) $charset_collate;",
|
|
);
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
|
|
|
foreach ($tables as $table) {
|
|
dbDelta($table);
|
|
}
|
|
|
|
self::install_sample_data();
|
|
}
|
|
|
|
public static function install_sample_data() {
|
|
global $wpdb;
|
|
|
|
$pen_names_table = $wpdb->prefix . 'bd_pen_names';
|
|
|
|
$exists = $wpdb->get_var("SELECT COUNT(*) FROM {$pen_names_table}");
|
|
|
|
if ($exists == 0) {
|
|
$wpdb->insert($pen_names_table, array(
|
|
'pen_name' => 'B.D. Murphy',
|
|
'notes' => 'Primary author name',
|
|
'active' => 1,
|
|
'created_at' => current_time('mysql'),
|
|
));
|
|
}
|
|
|
|
update_option('bdm_goodreads_author_id', '30469651');
|
|
}
|
|
|
|
public static function create_roles() {
|
|
add_role('book_manager', __('Book Manager', 'bd-bookshelfhelper'), array(
|
|
'read' => true,
|
|
'edit_books' => true,
|
|
'edit_others_books' => true,
|
|
'publish_books' => true,
|
|
'read_private_posts' => true,
|
|
));
|
|
}
|
|
|
|
public static function remove_roles() {
|
|
remove_role('book_manager');
|
|
}
|
|
|
|
public static function setup_scheduled_tasks() {
|
|
if (!wp_next_scheduled('bd_bookshelf_goodreads_refresh')) {
|
|
wp_schedule_event(time(), 'daily', 'bd_bookshelf_goodreads_refresh');
|
|
}
|
|
|
|
if (!wp_next_scheduled('bd_bookshelf_amazon_scrape')) {
|
|
wp_schedule_event(time(), 'six_hours', 'bd_bookshelf_amazon_scrape');
|
|
}
|
|
}
|
|
}
|