268 lines
9.4 KiB
PHP
268 lines
9.4 KiB
PHP
<?php
|
|
/**
|
|
* Settings credits/invite runtime helper.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Settings_Credits_Runtime {
|
|
|
|
/**
|
|
* 积分配置(合并默认值)。
|
|
* @return array
|
|
*/
|
|
public static function get_credit_settings(): array {
|
|
$defaults = [
|
|
'enabled' => true,
|
|
'currency_name' => '',
|
|
|
|
'register_bonus' => 10,
|
|
|
|
'daily_signin' => [
|
|
'enabled' => true,
|
|
'base' => 1,
|
|
'streak' => [
|
|
3 => 2,
|
|
7 => 5,
|
|
30 => 20,
|
|
],
|
|
],
|
|
|
|
'comment_reward' => [
|
|
'enabled' => true,
|
|
'amount' => 1,
|
|
'daily_cap' => 5,
|
|
'min_length' => 10,
|
|
'on_approved' => true,
|
|
],
|
|
|
|
'invite_reward' => [
|
|
'enabled' => true,
|
|
'inviter_amount' => 5,
|
|
'invitee_amount' => 2,
|
|
'require_verify' => true,
|
|
'ip_daily_cap' => 3,
|
|
'register_field' => 'optional',
|
|
'reject_invalid' => true,
|
|
],
|
|
|
|
'download_default_cost' => 0,
|
|
];
|
|
$saved = get_option('aksrt_credit_settings', []);
|
|
if (!is_array($saved)) {
|
|
$saved = [];
|
|
}
|
|
return array_replace_recursive($defaults, $saved);
|
|
}
|
|
|
|
/**
|
|
* Normalized runtime settings for daily sign-in rewards.
|
|
*
|
|
* @return array{enabled:bool,base:int,streak:array<int,int>,currency_name:string}
|
|
*/
|
|
public static function get_signin_reward_settings(): array {
|
|
$config = self::get_credit_settings();
|
|
$signin = is_array($config['daily_signin'] ?? null) ? $config['daily_signin'] : [];
|
|
$streak = [];
|
|
|
|
foreach ((array) ($signin['streak'] ?? []) as $day => $bonus) {
|
|
$day = (int) $day;
|
|
$bonus = (int) $bonus;
|
|
if ($day <= 0 || $bonus < 0) {
|
|
continue;
|
|
}
|
|
$streak[$day] = $bonus;
|
|
}
|
|
|
|
ksort($streak, SORT_NUMERIC);
|
|
|
|
return [
|
|
'enabled' => !empty($config['enabled']) && !empty($signin['enabled']),
|
|
'base' => max(0, (int) ($signin['base'] ?? 0)),
|
|
'streak' => $streak,
|
|
'currency_name' => self::get_currency_name(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Normalized runtime settings for invite rewards and registration gating.
|
|
*
|
|
* @return array{
|
|
* enabled:bool,
|
|
* inviter_amount:int,
|
|
* invitee_amount:int,
|
|
* require_verify:bool,
|
|
* ip_daily_cap:int,
|
|
* register_field:string,
|
|
* reject_invalid:bool,
|
|
* registration_enabled:bool
|
|
* }
|
|
*/
|
|
public static function get_invite_reward_settings(): array {
|
|
$config = self::get_credit_settings();
|
|
$invite = is_array($config['invite_reward'] ?? null) ? $config['invite_reward'] : [];
|
|
$all = AKSRT_Settings::get_all();
|
|
$register_field = (string) ($invite['register_field'] ?? 'optional');
|
|
|
|
if (!in_array($register_field, ['hidden', 'optional', 'required'], true)) {
|
|
$register_field = 'optional';
|
|
}
|
|
|
|
$inviter_amount = array_key_exists('invit_reward_points', $all)
|
|
? max(0, (int) $all['invit_reward_points'])
|
|
: max(0, (int) ($invite['inviter_amount'] ?? 0));
|
|
$invitee_enabled = array_key_exists('invit_invitee_reward', $all)
|
|
? !empty($all['invit_invitee_reward'])
|
|
: max(0, (int) ($invite['invitee_amount'] ?? 0)) > 0;
|
|
$invitee_amount = $invitee_enabled
|
|
? max(0, (int) ($invite['invitee_amount'] ?? $inviter_amount))
|
|
: 0;
|
|
|
|
return [
|
|
'enabled' => !empty($config['enabled']) && !empty($invite['enabled']),
|
|
'inviter_amount' => $inviter_amount,
|
|
'invitee_amount' => $invitee_amount,
|
|
'require_verify' => !empty($invite['require_verify']),
|
|
'ip_daily_cap' => max(0, (int) ($invite['ip_daily_cap'] ?? 0)),
|
|
'register_field' => $register_field,
|
|
'reject_invalid' => !empty($invite['reject_invalid']),
|
|
'registration_enabled' => self::invite_registration_enabled(),
|
|
'invit_reward_points' => $inviter_amount,
|
|
'invit_reward_level' => max(0, (int) ($all['invit_reward_level'] ?? 0)),
|
|
'invit_reward_vip_days' => max(0, (int) ($all['invit_reward_vip_days'] ?? 0)),
|
|
'invit_invitee_reward' => $invitee_enabled,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Canonical invite registration config derived from users.invite fields.
|
|
*
|
|
* @return array{
|
|
* enabled:bool,
|
|
* mode:string,
|
|
* register_field:string,
|
|
* reject_invalid:bool,
|
|
* code_length:int,
|
|
* open_desc:string,
|
|
* must_desc:string
|
|
* }
|
|
*/
|
|
public static function get_invite_registration_config(): array {
|
|
$all = AKSRT_Settings::get_all();
|
|
$credit_config = self::get_credit_settings();
|
|
|
|
$enabled = array_key_exists('invit_code_s', $all)
|
|
? !empty($all['invit_code_s'])
|
|
: (!isset($all['invite_registration_enabled']) || !empty($all['invite_registration_enabled']));
|
|
|
|
$mode = isset($all['invit_code_mode']) ? (string) $all['invit_code_mode'] : '';
|
|
if (!in_array($mode, ['open', 'must', 'close'], true)) {
|
|
$legacy_mode = (string) ($credit_config['invite_reward']['register_field'] ?? 'optional');
|
|
if ($legacy_mode === 'required') {
|
|
$mode = 'must';
|
|
} elseif ($legacy_mode === 'hidden') {
|
|
$mode = 'close';
|
|
} else {
|
|
$mode = 'open';
|
|
}
|
|
}
|
|
|
|
$register_field = 'optional';
|
|
if (!$enabled || $mode === 'close') {
|
|
$register_field = 'hidden';
|
|
} elseif ($mode === 'must') {
|
|
$register_field = 'required';
|
|
}
|
|
|
|
$open_desc = trim((string) ($all['invit_code_open_desc'] ?? ''));
|
|
if ($open_desc === '') {
|
|
$open_desc = __('邀请码为选填,没有邀请码也可以注册', 'aksrt');
|
|
}
|
|
|
|
$must_desc = trim((string) ($all['invit_code_must_desc'] ?? ''));
|
|
if ($must_desc === '') {
|
|
$must_desc = __('请输入邀请码', 'aksrt');
|
|
}
|
|
|
|
$code_length = isset($all['invit_code_length']) ? (int) $all['invit_code_length'] : 0;
|
|
if ($code_length <= 0) {
|
|
$code_length = (int) ($all['invite_code_length'] ?? 6);
|
|
}
|
|
$code_length = max(4, min(12, $code_length));
|
|
|
|
return [
|
|
'enabled' => $enabled && $mode !== 'close',
|
|
'mode' => $mode,
|
|
'register_field' => $register_field,
|
|
'reject_invalid' => !empty($credit_config['invite_reward']['reject_invalid']),
|
|
'code_length' => $code_length,
|
|
'open_desc' => $open_desc,
|
|
'must_desc' => $must_desc,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 积分功能是否启用(开关 + cms_features.download_credit 联合判定)。
|
|
*/
|
|
public static function credits_enabled(): bool {
|
|
$config = self::get_credit_settings();
|
|
return !empty($config['enabled']);
|
|
}
|
|
|
|
/**
|
|
* 自定义货币名称,为空时回退"积分"。
|
|
*/
|
|
public static function get_currency_name(): string {
|
|
$config = self::get_credit_settings();
|
|
$name = trim((string) ($config['currency_name'] ?? ''));
|
|
return $name !== '' ? $name : __('积分', 'aksrt');
|
|
}
|
|
|
|
/**
|
|
* 邀请系统是否启用(=积分系统启用 && invite_reward.enabled)。
|
|
* 用于 UI 入口(个人中心邀请 Tab)的展示门禁。
|
|
*/
|
|
public static function invite_enabled(): bool {
|
|
$config = self::get_credit_settings();
|
|
return !empty($config['enabled']) && !empty($config['invite_reward']['enabled']);
|
|
}
|
|
|
|
/**
|
|
* 邀请注册功能总开关(独立于积分系统)。
|
|
* 控制邀请链接、注册页邀请码字段、用户中心邀请卡片的显示。
|
|
* 存储在 aksrt_basic_settings['invite_registration_enabled']。
|
|
*/
|
|
public static function invite_registration_enabled(): bool {
|
|
return !empty(self::get_invite_registration_config()['enabled']);
|
|
}
|
|
|
|
/**
|
|
* 注册页邀请码字段模式:'hidden' | 'optional' | 'required'
|
|
*/
|
|
public static function invite_register_field_mode(): string {
|
|
$mode = (string) (self::get_invite_registration_config()['register_field'] ?? 'hidden');
|
|
return in_array($mode, ['hidden', 'optional', 'required'], true) ? $mode : 'hidden';
|
|
}
|
|
|
|
/**
|
|
* 必填模式下邀请码无效是否拒绝注册。
|
|
*/
|
|
public static function invite_reject_invalid(): bool {
|
|
return !empty(self::get_invite_registration_config()['reject_invalid']);
|
|
}
|
|
|
|
public static function invite_code_length(): int {
|
|
return max(4, min(12, (int) (self::get_invite_registration_config()['code_length'] ?? 6)));
|
|
}
|
|
|
|
/**
|
|
* 资源下载默认积分价(当资源未单独设价时回退使用)。
|
|
*/
|
|
public static function download_default_cost(): int {
|
|
$config = self::get_credit_settings();
|
|
return max(0, (int) ($config['download_default_cost'] ?? 0));
|
|
}
|
|
}
|