refactor(referral): prune dead code and formalize ops list header

- remove unused AuthReader::invitation_mode and ReferralAttribution
  helpers (get_or_create_code / validate_ref); contract test now
  asserts the canonical invit_code_s reader in CreditsRuntime
- drop legacy aksrt_invite table from uninstall cleanup list
- track referral code creation time (aksrt_referral_code_created)
  and surface it in the referral ops REST list
- formalize ops list headers (归属用户/推广码/启用状态/推广用户数/
  有效付费用户数/创建时间) and right-align count columns
This commit is contained in:
kate522 committed 2026-09-19 12:54:22 +08:00
1 parent b375fcc55c
commit ae626ba1c8
5 files changed
+23 -32

No files matched your search

+8 -11
View File
@@ -14,6 +14,8 @@ final class ReferralAttribution {
private const COOKIE = 'aksrt_referral';
private const META_CODE = 'aksrt_referral_code';
private const META_STATUS = 'aksrt_referral_code_status';
/** 推广码生成时间(新生成的码才有;历史码为空,后台列表显示 —)。 */
private const META_CREATED = 'aksrt_referral_code_created';
private const STATUS_ACTIVE = 'active';
private const STATUS_DISABLED = 'disabled';
@@ -258,14 +260,6 @@ final class ReferralAttribution {
];
}
public function get_or_create_code(int $user_id): string {
return (string) ($this->create_link($user_id)['ref'] ?? '');
}
public function validate_ref(string $ref): bool {
return $this->resolve_promoter($ref) > 0;
}
public function resolve(int $user_id = 0): int {
$user_id = $user_id > 0 ? $user_id : get_current_user_id();
if ($user_id <= 0) {
@@ -482,9 +476,10 @@ final class ReferralAttribution {
$where_sql = implode(' AND ', $where);
$joins = $wpdb->prepare(
"LEFT JOIN {$wpdb->usermeta} code ON code.user_id=u.ID AND code.meta_key=%s LEFT JOIN {$wpdb->usermeta} state ON state.user_id=u.ID AND state.meta_key=%s",
"LEFT JOIN {$wpdb->usermeta} code ON code.user_id=u.ID AND code.meta_key=%s LEFT JOIN {$wpdb->usermeta} state ON state.user_id=u.ID AND state.meta_key=%s LEFT JOIN {$wpdb->usermeta} created ON created.user_id=u.ID AND created.meta_key=%s",
self::META_CODE,
self::META_STATUS
self::META_STATUS,
self::META_CREATED
);
$count_sql = "SELECT COUNT(DISTINCT u.ID) FROM {$wpdb->users} u {$joins} WHERE {$where_sql}";
$total = (int) ($params
@@ -495,7 +490,7 @@ final class ReferralAttribution {
$ledger_table = \AKSRT\Core\DBMigrator::table('commission_ledger');
$stats_joins = "LEFT JOIN (SELECT promoter_id, COUNT(*) AS bound_count FROM {$relation_table} WHERE status='bound' GROUP BY promoter_id) relation_stats ON relation_stats.promoter_id=u.ID "
. "LEFT JOIN (SELECT promoter_id, COUNT(DISTINCT source_user_id) AS effective_payers FROM {$ledger_table} WHERE original_amount > 0 GROUP BY promoter_id) payer_stats ON payer_stats.promoter_id=u.ID";
$list_sql = "SELECT DISTINCT u.ID, u.user_login, u.display_name, u.user_email, code.meta_value AS ref, state.meta_value AS ref_status, "
$list_sql = "SELECT DISTINCT u.ID, u.user_login, u.display_name, u.user_email, code.meta_value AS ref, state.meta_value AS ref_status, created.meta_value AS created_at, "
. "COALESCE(relation_stats.bound_count, 0) AS bound_count, COALESCE(payer_stats.effective_payers, 0) AS effective_payers "
. "FROM {$wpdb->users} u {$joins} {$stats_joins} WHERE {$where_sql} ORDER BY u.ID DESC LIMIT %d OFFSET %d";
$list_params = array_merge($params, [$per_page, ($page - 1) * $per_page]);
@@ -514,6 +509,7 @@ final class ReferralAttribution {
],
'ref' => $ref,
'status' => $ref === '' ? 'missing' : ((string) ($row['ref_status'] ?? '') === self::STATUS_DISABLED ? self::STATUS_DISABLED : self::STATUS_ACTIVE),
'created_at' => (string) ($row['created_at'] ?? ''),
'bound_count' => (int) ($row['bound_count'] ?? 0),
'effective_payers' => (int) ($row['effective_payers'] ?? 0),
];
@@ -553,6 +549,7 @@ final class ReferralAttribution {
if (empty($owners)) {
update_user_meta($user_id, self::META_CODE, $code);
update_user_meta($user_id, self::META_STATUS, self::STATUS_ACTIVE);
update_user_meta($user_id, self::META_CREATED, current_time('mysql'));
$updated = $this->code_record($user_id);
if ($updated['code'] === $code && $updated['status'] === self::STATUS_ACTIVE) {
return $code;