Files
wp-donations/includes/Core/Installer.php
T

127 lines
3.8 KiB
PHP
Raw Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Core;
/**
* Creates/upgrades the plugin's tables. Safe to re-run (dbDelta).
*/
final class Installer {
public const DB_VERSION = '2';
public static function activate(): void {
self::install_schema();
if ( false === get_option( 'sg_settings' ) ) {
add_option( 'sg_settings', Settings::defaults(), '', false );
}
}
/** Run on plugins_loaded so upgrades apply without re-activation. */
public static function maybe_upgrade(): void {
if ( get_option( 'sg_db_version' ) !== self::DB_VERSION ) {
self::install_schema();
}
}
private static function install_schema(): void {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$c = $wpdb->get_charset_collate();
$p = $wpdb->prefix . 'sg_';
dbDelta( "CREATE TABLE {$p}goals (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(190) NOT NULL,
target_minor BIGINT UNSIGNED NOT NULL DEFAULT 0,
currency CHAR(3) NOT NULL DEFAULT 'USD',
status VARCHAR(12) NOT NULL DEFAULT 'active',
starts_at DATETIME NULL,
ends_at DATETIME NULL,
counts_fees TINYINT(1) NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL,
PRIMARY KEY (id)
) $c;" );
dbDelta( "CREATE TABLE {$p}donations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
goal_id BIGINT UNSIGNED NOT NULL,
paypal_order_id VARCHAR(64) NOT NULL,
paypal_capture_id VARCHAR(64) NULL,
base_minor BIGINT UNSIGNED NOT NULL,
fee_minor BIGINT UNSIGNED NOT NULL DEFAULT 0,
total_minor BIGINT UNSIGNED NOT NULL,
currency CHAR(3) NOT NULL,
donor_name VARCHAR(100) NOT NULL DEFAULT '',
is_anonymous TINYINT(1) NOT NULL DEFAULT 0,
message VARCHAR(500) NOT NULL DEFAULT '',
status VARCHAR(12) NOT NULL DEFAULT 'pending',
moderation VARCHAR(10) NOT NULL DEFAULT 'approved',
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY capture (paypal_capture_id),
KEY goal_status (goal_id,status),
KEY order_id (paypal_order_id)
) $c;" );
dbDelta( "CREATE TABLE {$p}rules (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
goal_id BIGINT UNSIGNED NOT NULL,
type VARCHAR(16) NOT NULL,
config LONGTEXT NOT NULL,
enabled TINYINT(1) NOT NULL DEFAULT 1,
sort INT NOT NULL DEFAULT 0,
PRIMARY KEY (id),
KEY goal_id (goal_id)
) $c;" );
dbDelta( "CREATE TABLE {$p}rule_events (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
rule_id BIGINT UNSIGNED NOT NULL,
donation_id BIGINT UNSIGNED NOT NULL,
delta BIGINT NOT NULL DEFAULT 0,
reversed TINYINT(1) NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY rule_donation (rule_id,donation_id)
) $c;" );
dbDelta( "CREATE TABLE {$p}state (
goal_id BIGINT UNSIGNED NOT NULL,
skey VARCHAR(64) NOT NULL,
svalue LONGTEXT NOT NULL,
updated_at DATETIME NOT NULL,
PRIMARY KEY (goal_id,skey)
) $c;" );
dbDelta( "CREATE TABLE {$p}actions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
goal_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
title VARCHAR(120) NOT NULL,
description VARCHAR(255) NOT NULL DEFAULT '',
amount_minor BIGINT UNSIGNED NOT NULL,
mode VARCHAR(8) NOT NULL DEFAULT 'exact',
enabled TINYINT(1) NOT NULL DEFAULT 1,
sort INT NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
KEY goal_enabled (goal_id,enabled)
) $c;" );
dbDelta( "CREATE TABLE {$p}action_events (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
donation_id BIGINT UNSIGNED NOT NULL,
action_id BIGINT UNSIGNED NOT NULL,
times INT NOT NULL DEFAULT 1,
title VARCHAR(120) NOT NULL,
status VARCHAR(8) NOT NULL DEFAULT 'queued',
created_at DATETIME NOT NULL,
done_at DATETIME NULL,
PRIMARY KEY (id),
UNIQUE KEY donation_action (donation_id,action_id),
KEY status_id (status,id)
) $c;" );
update_option( 'sg_db_version', self::DB_VERSION );
}
}