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; } /** * The single definition of "abandoned checkout", mirrored by the DELETE below (MaintenancePurgeTest checks both). * Never purgeable: not pending, younger than MAX_AGE_DAYS, a test row (goal_id 0), or ANY row carrying PayPal capture * evidence (a capture id, even from a PENDING/under-review capture: PayPal may still finalize it by webhook). * * @param array|object $row status, goal_id, paypal_capture_id, created_at (UTC 'Y-m-d H:i:s') */ public static function isPurgeable( array|object $row, int $now ): bool { $r = (array) $row; if ( 'pending' !== (string) ( $r['status'] ?? '' ) || (int) ( $r['goal_id'] ?? 0 ) <= 0 ) { return false; } if ( '' !== trim( (string) ( $r['paypal_capture_id'] ?? '' ) ) ) { return false; } $created = strtotime( (string) ( $r['created_at'] ?? '' ) . ' UTC' ); return false !== $created && $created < $now - self::MAX_AGE_DAYS * DAY_IN_SECONDS; } /** @return int Number of rows removed. */ public static function purge_abandoned(): int { global $wpdb; $table = $wpdb->prefix . 'sg_donations'; $cutoff = gmdate( 'Y-m-d H:i:s', time() - self::MAX_AGE_DAYS * DAY_IN_SECONDS ); $n = $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE status = 'pending' AND (paypal_capture_id IS NULL OR paypal_capture_id = '') AND goal_id > 0 AND created_at < %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $cutoff ) ); return (int) $n; } }