perf(revenue): server-render the promotion link and stop settling on reads
Opening the 推广收益 tab was slow because every read path ran settlement and two COUNT aggregates before the browser could show the link. - profile-revenue.php: the link input now carries a server-rendered value from ReferralAttribution::create_link(), so 复制链接 works on first paint instead of after a REST round trip - CommissionLedger::overview() no longer calls lazy_settle_due(); the lazy-settlement hook and its static flag are gone. Settlement runs only on the existing hourly aksrt_affiliate_settle_due cron, which uses a larger batch (100 vs 20) and would otherwise re-run up to 20 locked SELECT..FOR UPDATE + write cycles inside a GET request - ReferralAttribution::rest_me() drops the lazy_settle_due() call and the stats() result; stats() itself is deleted — its two COUNTs (one of them COUNT(DISTINCT source_user_id)) had no consumer left once the 推广人数 row was removed - credits.js: reads the link from the already-rendered input and only fetches /revenue/me for poster config and commission ratio. A failed fetch no longer blocks copying — it silently skips the poster enhancement - tests: the lazy-settlement contract is inverted to assert the read path stays read-only and cron stays wired; new assertions pin the server-rendered value and the no-COUNT REST payload Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -103,14 +103,6 @@ final class CommissionLedger {
|
||||
foreach ((array) $rows as $row) if ($this->settle_one((int)$row['id'])) $count++;
|
||||
return $count;
|
||||
}
|
||||
/** 懒结算兜底:收益相关入口顺带结算到期佣金,弥补 wp-cron 在低流量站点的时效缺口。 */
|
||||
private static bool $lazy_settled = false;
|
||||
public function lazy_settle_due(): void {
|
||||
if (self::$lazy_settled) return;
|
||||
self::$lazy_settled = true;
|
||||
if (!\AKSRT\Settings\Settings::affiliate_enabled()) return;
|
||||
$this->settle_due(20);
|
||||
}
|
||||
private function settle_one(int $id, string $operation_key = '') {
|
||||
global $wpdb; $table = $this->table('commission_ledger');
|
||||
$row = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$table} WHERE id=%d", $id), ARRAY_A);
|
||||
@@ -241,7 +233,6 @@ final class CommissionLedger {
|
||||
return $negative ? -$minor : $minor;
|
||||
}
|
||||
public function overview(int $user_id): array {
|
||||
$this->lazy_settle_due();
|
||||
global $wpdb; $table=$this->table('commission_ledger');
|
||||
$row=$wpdb->get_row($wpdb->prepare("SELECT COALESCE(SUM(original_amount+adjustment_total),0) total, COALESCE(SUM(CASE WHEN status='pending' THEN original_amount+adjustment_total ELSE 0 END),0) pending, COALESCE(SUM(CASE WHEN status='settled' THEN original_amount+adjustment_total ELSE 0 END),0) settled FROM {$table} WHERE promoter_id=%d",$user_id),ARRAY_A) ?: [];
|
||||
$withdrawn = '0.00';
|
||||
|
||||
@@ -291,24 +291,6 @@ final class ReferralAttribution {
|
||||
return $bound > 0 && $bound !== $user_id ? $bound : 0;
|
||||
}
|
||||
|
||||
public function stats(int $promoter_id): array {
|
||||
global $wpdb;
|
||||
$table = \AKSRT\Core\DBMigrator::table('affiliate_relation');
|
||||
if ($promoter_id <= 0 || $table === '') {
|
||||
return ['promoters' => 0, 'effective_payers' => 0];
|
||||
}
|
||||
$promoters = (int) $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM {$table} WHERE promoter_id = %d AND status = 'bound'",
|
||||
$promoter_id
|
||||
));
|
||||
$ledger = \AKSRT\Core\DBMigrator::table('commission_ledger');
|
||||
$payers = $ledger !== '' ? (int) $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT COUNT(DISTINCT source_user_id) FROM {$ledger} WHERE promoter_id = %d AND original_amount > 0",
|
||||
$promoter_id
|
||||
)) : 0;
|
||||
return ['promoters' => $promoters, 'effective_payers' => $payers];
|
||||
}
|
||||
|
||||
/** @return array{items:array<int,array<string,mixed>>,total:int,page:int,per_page:int,max_pages:int} */
|
||||
public function list_referred_users(int $promoter_id, int $page = 1, int $per_page = 10): array {
|
||||
global $wpdb;
|
||||
@@ -361,10 +343,8 @@ final class ReferralAttribution {
|
||||
}
|
||||
|
||||
public function rest_me(): \WP_REST_Response {
|
||||
\AKSRT\Payment\CommissionLedger::get_instance()->lazy_settle_due();
|
||||
$user_id = get_current_user_id();
|
||||
$link = $this->create_link($user_id);
|
||||
$stats = $this->stats($user_id);
|
||||
$affiliate = \AKSRT\Settings\Settings::get_affiliate_settings();
|
||||
$registration = $this->registration_settings();
|
||||
$reward = $this->reward_settings();
|
||||
@@ -386,7 +366,6 @@ final class ReferralAttribution {
|
||||
'window_days' => (int) ($this->shared_settings()['window_days'] ?? 30),
|
||||
'settlement_days' => (int) ($affiliate['settlement_days'] ?? 7),
|
||||
],
|
||||
'stats' => $stats,
|
||||
], 200);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user