Files
wp-donations/includes/Core/Installer.php
T
shadowdaoandClaude Sonnet 5.5 4c379a0245
Test and Release / test (push) Successful in 1m21s
Test and Release / release (push) Successful in 4s
Tiered goals: ordered milestones with unlocks, tier-aware goal bar, TIER UNLOCKED alerts, next-unlock line
- Tiers\TierPolicy (pure), TierRepository (atomic reached marking), Tiers\Recorder hooked before close-at-target
- Admin tiers screen; goal target derived from the final tier
- Overlay goal widget shows Tier N of M and fills within the tier; alert pill; donate form 'Next unlock'
- Installer DB_VERSION 4 (sg_goal_tiers)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 13:09:18 -08:00

163 lines
5.1 KiB
PHP

<?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 = '4';
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,
close_at_target 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;" );
dbDelta( "CREATE TABLE {$p}alert_defs (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
goal_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
trigger_type VARCHAR(8) NOT NULL DEFAULT 'amount',
min_minor BIGINT UNSIGNED NOT NULL DEFAULT 0,
action_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
title VARCHAR(120) NOT NULL DEFAULT '',
visual_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
visual_type VARCHAR(6) NOT NULL DEFAULT '',
sound_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
volume TINYINT UNSIGNED NOT NULL DEFAULT 80,
duration_ms INT UNSIGNED NOT NULL DEFAULT 6000,
message VARCHAR(255) NOT NULL DEFAULT '',
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}goal_tiers (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
goal_id BIGINT UNSIGNED NOT NULL,
target_minor BIGINT UNSIGNED NOT NULL,
title VARCHAR(120) NOT NULL,
description VARCHAR(255) NOT NULL DEFAULT '',
reached_at DATETIME NULL,
reached_donation_id BIGINT UNSIGNED NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY goal_target (goal_id,target_minor),
KEY reached_donation (reached_donation_id)
) $c;" );
update_option( 'sg_db_version', self::DB_VERSION );
}
}