Scaffold plugin: architecture contract, schema, settings, fee math

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 08:42:30 -08:00
commit 939193f258
12 files changed
+2081

No files matched your search

+99
View File
@@ -0,0 +1,99 @@
<?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 = '1';
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;" );
update_option( 'sg_db_version', self::DB_VERSION );
}
}