Hold donations with flagged donor names for moderation; reconcile lost side effects (schema v6)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
473fe192cd
commit
353f7ca5be
16 files changed
+681
-41
No files matched your search
@@ -22,7 +22,7 @@ final class DonationRepository {
|
||||
|
||||
/** @return int|null new id, or null on failure (e.g. duplicate capture id). */
|
||||
public function insert( array $d ): ?int {
|
||||
$allowed = array( 'goal_id', 'paypal_order_id', 'paypal_capture_id', 'base_minor', 'fee_minor', 'total_minor', 'currency', 'donor_name', 'is_anonymous', 'message', 'status', 'moderation' );
|
||||
$allowed = array( 'goal_id', 'paypal_order_id', 'paypal_capture_id', 'base_minor', 'fee_minor', 'total_minor', 'currency', 'donor_name', 'is_anonymous', 'message', 'status', 'moderation', 'moderation_flags' );
|
||||
$row = array_intersect_key( $d, array_flip( $allowed ) );
|
||||
$row += array( 'status' => 'pending', 'moderation' => 'approved', 'fee_minor' => 0 );
|
||||
$row['created_at'] = gmdate( 'Y-m-d H:i:s' );
|
||||
|
||||
@@ -8,10 +8,10 @@ namespace StreamGoals\Core;
|
||||
*/
|
||||
final class Installer {
|
||||
|
||||
public const DB_VERSION = '5';
|
||||
public const DB_VERSION = '6';
|
||||
|
||||
public static function activate(): void {
|
||||
self::install_schema();
|
||||
self::install_schema( get_option( 'sg_db_version' ) );
|
||||
if ( false === get_option( 'sg_settings' ) ) {
|
||||
add_option( 'sg_settings', Settings::defaults(), '', false );
|
||||
}
|
||||
@@ -20,12 +20,33 @@ final class Installer {
|
||||
|
||||
/** 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();
|
||||
$from = get_option( 'sg_db_version' );
|
||||
if ( $from !== self::DB_VERSION ) {
|
||||
self::install_schema( $from );
|
||||
}
|
||||
}
|
||||
|
||||
private static function install_schema(): void {
|
||||
/** 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();
|
||||
@@ -59,6 +80,8 @@ final class Installer {
|
||||
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),
|
||||
@@ -159,6 +182,11 @@ final class Installer {
|
||||
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 );
|
||||
}
|
||||
}
|
||||
@@ -14,15 +14,87 @@ final class Maintenance {
|
||||
public const HOOK = 'sg_purge_abandoned';
|
||||
public const MAX_AGE_DAYS = 7;
|
||||
|
||||
/** Reconcile "completed but side effects lost" donations (see reconcile_effects()). */
|
||||
public const RECONCILE_HOOK = 'sg_reconcile_effects';
|
||||
public const RECONCILE_SCHEDULE = 'sg_five_minutes';
|
||||
/** A donation must be this old before it is replayed, so a request still applying its effects is left alone. */
|
||||
public const RECONCILE_MIN_AGE = 120;
|
||||
/** Older donations are not retried (a row that can never succeed must not be replayed forever). */
|
||||
public const RECONCILE_MAX_AGE_DAYS = 30;
|
||||
public const RECONCILE_BATCH = 25;
|
||||
|
||||
public static function register(): void {
|
||||
add_action( self::HOOK, array( self::class, 'purge_abandoned' ) );
|
||||
if ( ! wp_next_scheduled( self::HOOK ) ) {
|
||||
wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::HOOK );
|
||||
}
|
||||
add_filter( 'cron_schedules', array( self::class, 'cron_schedules' ) );
|
||||
add_action( self::RECONCILE_HOOK, array( self::class, 'reconcile_effects' ) );
|
||||
if ( ! wp_next_scheduled( self::RECONCILE_HOOK ) ) {
|
||||
wp_schedule_event( time() + 300, self::RECONCILE_SCHEDULE, self::RECONCILE_HOOK );
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array $schedules @return array */
|
||||
public static function cron_schedules( $schedules ): array {
|
||||
$schedules = is_array( $schedules ) ? $schedules : array();
|
||||
$schedules[ self::RECONCILE_SCHEDULE ] = array( 'interval' => 300, 'display' => 'Every five minutes (StreamGoals)' );
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
public static function unschedule(): void {
|
||||
wp_clear_scheduled_hook( self::HOOK );
|
||||
wp_clear_scheduled_hook( self::RECONCILE_HOOK );
|
||||
}
|
||||
|
||||
/**
|
||||
* Replays the side effects (rules/timer/counter, rewards, tiers, end-at-target) of completed donations whose request
|
||||
* died before DonationStore::notify_applied() could stamp effects_applied_at. Safe by construction: every effect is
|
||||
* idempotent per (rule, donation) / (donation, action) / tier, and the donation row itself is never touched here.
|
||||
* Single-flight through a MySQL named lock so overlapping cron runs do not pile up.
|
||||
*
|
||||
* @return int Donations whose effects were confirmed applied by this run.
|
||||
*/
|
||||
public static function reconcile_effects(): int {
|
||||
global $wpdb;
|
||||
$locked = $wpdb && 1 === (int) $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, 0)', 'sg_reconcile_effects' ) );
|
||||
if ( ! $locked ) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
return self::reconcile(
|
||||
time(),
|
||||
array( \StreamGoals\PayPal\DonationStore::class, 'unapplied_completed_ids' ),
|
||||
array( \StreamGoals\PayPal\DonationStore::class, 'notify_applied' )
|
||||
);
|
||||
} catch ( \Throwable $e ) {
|
||||
error_log( 'StreamGoals: reconcile failed: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
|
||||
return 0;
|
||||
} finally {
|
||||
$wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', 'sg_reconcile_effects' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure orchestration (unit-tested).
|
||||
*
|
||||
* @param callable $find fn(string $notAfter, string $notBefore, int $limit): int[]
|
||||
* @param callable $apply fn(int $donationId): bool true once the donation is stamped as applied
|
||||
*/
|
||||
public static function reconcile( int $now, callable $find, callable $apply ): int {
|
||||
$notAfter = gmdate( 'Y-m-d H:i:s', $now - self::RECONCILE_MIN_AGE );
|
||||
$notBefore = gmdate( 'Y-m-d H:i:s', $now - self::RECONCILE_MAX_AGE_DAYS * 86400 );
|
||||
$done = 0;
|
||||
foreach ( (array) $find( $notAfter, $notBefore, self::RECONCILE_BATCH ) as $id ) {
|
||||
try {
|
||||
if ( $apply( (int) $id ) ) {
|
||||
++$done;
|
||||
}
|
||||
} catch ( \Throwable $e ) {
|
||||
error_log( 'StreamGoals: reconcile of donation ' . (int) $id . ' failed: ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
|
||||
}
|
||||
}
|
||||
return $done;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,8 +58,23 @@ final class ProfanityFilter {
|
||||
return $this->enabled && array() !== $this->words;
|
||||
}
|
||||
|
||||
/**
|
||||
* True when at least one word of $text is listed, i.e. filter() would change it. Same rules as filter()
|
||||
* (whole words, leetspeak, case-insensitive). Used to hold a donor name for moderation. Never throws.
|
||||
*/
|
||||
public function matches( string $text ): bool {
|
||||
$hit = false;
|
||||
$this->scan( $text, $hit );
|
||||
return $hit;
|
||||
}
|
||||
|
||||
/** Mask every listed word; everything else is returned byte-for-byte (after UTF-8 scrubbing). */
|
||||
public function filter( string $text ): string {
|
||||
$hit = false;
|
||||
return $this->scan( $text, $hit );
|
||||
}
|
||||
|
||||
private function scan( string $text, bool &$hit ): string {
|
||||
if ( ! $this->isEnabled() || '' === $text ) {
|
||||
return $text;
|
||||
}
|
||||
@@ -70,13 +85,14 @@ final class ProfanityFilter {
|
||||
$text = mb_scrub( $text, 'UTF-8' );
|
||||
$out = preg_replace_callback(
|
||||
'/[\p{L}\p{M}\p{N}@$!]+/u',
|
||||
function ( array $m ): string {
|
||||
function ( array $m ) use ( &$hit ): string {
|
||||
$tok = $m[0];
|
||||
// "!" is only a leet char inside a word; trailing/leading punctuation is not part of it.
|
||||
$core = trim( $tok, '!' );
|
||||
if ( '' === $core || ! $this->isListed( $core ) ) {
|
||||
return $tok;
|
||||
}
|
||||
$hit = true;
|
||||
$lead = (int) strspn( $tok, '!' );
|
||||
$trail = strlen( $tok ) - strlen( rtrim( $tok, '!' ) );
|
||||
return str_repeat( '!', $lead ) . str_repeat( '*', mb_strlen( $core ) ) . str_repeat( '!', $trail );
|
||||
|
||||
@@ -82,6 +82,7 @@ final class Uninstaller {
|
||||
public static function run_site( object $wpdb ): void {
|
||||
$delete = self::wants_data_deleted( get_option( 'sg_settings', array() ) );
|
||||
wp_clear_scheduled_hook( self::CRON_HOOK );
|
||||
wp_clear_scheduled_hook( Maintenance::RECONCILE_HOOK );
|
||||
self::delete_dynamic_transients( $wpdb );
|
||||
if ( $delete ) {
|
||||
self::drop_tables( $wpdb );
|
||||
|
||||
Reference in new issue
Block a user