193 lines
6.6 KiB
PHP
193 lines
6.6 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 = '6';
|
|
|
|
public static function activate(): void {
|
|
self::install_schema( get_option( 'sg_db_version' ) );
|
|
if ( false === get_option( 'sg_settings' ) ) {
|
|
add_option( 'sg_settings', Settings::defaults(), '', false );
|
|
}
|
|
Settings::ensure_overlay_token(); // never generated by a public request (see OverlayController)
|
|
}
|
|
|
|
/** Run on plugins_loaded so upgrades apply without re-activation. */
|
|
public static function maybe_upgrade(): void {
|
|
$from = get_option( 'sg_db_version' );
|
|
if ( $from !== self::DB_VERSION ) {
|
|
self::install_schema( $from );
|
|
}
|
|
}
|
|
|
|
/** Schema v6 added sg_donations.effects_applied_at; rows completed before that must not be replayed. */
|
|
public static function needsEffectsBackfill( mixed $from ): bool {
|
|
return false === $from || '' === $from || version_compare( (string) $from, '6', '<' );
|
|
}
|
|
|
|
/**
|
|
* One-time upgrade step: every donation that is ALREADY completed had its side effects applied by the code that
|
|
* completed it (or was lost before this feature existed and is not worth replaying), so stamp it. Without this the
|
|
* reconciler would replay the whole history. Only touches NULL rows, so re-running is harmless. False on DB error.
|
|
*/
|
|
public static function backfill_effects_applied(): bool {
|
|
global $wpdb;
|
|
$n = $wpdb->query( $wpdb->prepare(
|
|
'UPDATE ' . $wpdb->prefix . "sg_donations SET effects_applied_at = %s WHERE status = 'completed' AND effects_applied_at IS NULL",
|
|
gmdate( 'Y-m-d H:i:s' )
|
|
) );
|
|
return false !== $n;
|
|
}
|
|
|
|
/** @param mixed $from Previously installed sg_db_version (false when never installed). */
|
|
private static function install_schema( mixed $from = false ): 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',
|
|
moderation_flags VARCHAR(16) NOT NULL DEFAULT '',
|
|
effects_applied_at DATETIME NULL,
|
|
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,
|
|
frameless TINYINT(1) 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;" );
|
|
|
|
// Backfill BEFORE recording the new version: if it fails, the next load retries instead of replaying history.
|
|
if ( self::needsEffectsBackfill( $from ) && ! self::backfill_effects_applied() ) {
|
|
return;
|
|
}
|
|
|
|
update_option( 'sg_db_version', self::DB_VERSION );
|
|
}
|
|
}
|