306 lines
8.5 KiB
PHP
306 lines
8.5 KiB
PHP
<?php
|
|
/**
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Credits;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class PointsFree {
|
|
|
|
public const META_DETAIL = 'free_points_detail';
|
|
public const META_LOGIN = 'aksrt_free_points_login_date';
|
|
public const META_SIGNIN = 'aksrt_free_points_signin_date';
|
|
public const META_TASKS = 'aksrt_free_points_task_dates';
|
|
|
|
/**
|
|
* Task catalog: key => [label, short_desc, default_points]
|
|
*
|
|
* @return array<string, array{0:string,1:string,2:int}>
|
|
*/
|
|
public static function catalog(): array {
|
|
return [
|
|
'sign_up' => [__('注册奖励', 'aksrt'), __('新用户注册成功后发放', 'aksrt'), 10],
|
|
'sign_in' => [__('每日签到', 'aksrt'), __('完成每日签到', 'aksrt'), 1],
|
|
'login' => [__('每日登录', 'aksrt'), __('每日首次登录', 'aksrt'), 1],
|
|
'followed' => [__('被关注', 'aksrt'), __('被其他用户关注', 'aksrt'), 1],
|
|
'post_new' => [__('发布文章', 'aksrt'), __('发布并通过审核的文章', 'aksrt'), 5],
|
|
'post_like' => [__('文章被赞', 'aksrt'), __('你的文章被点赞', 'aksrt'), 1],
|
|
'post_favorite' => [__('文章被收藏', 'aksrt'), __('你的文章被收藏', 'aksrt'), 1],
|
|
'comment_new' => [__('发表评论', 'aksrt'), __('发表并通过审核的评论', 'aksrt'), 1],
|
|
'comment_like' => [__('评论被赞', 'aksrt'), __('你的评论被点赞', 'aksrt'), 1],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Runtime free-task settings from theme options.
|
|
*
|
|
* @return array{day_max:int,tasks:array<string,int>}
|
|
*/
|
|
public static function settings(): array {
|
|
$all = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
|
|
$tasks = [];
|
|
foreach (self::catalog() as $key => $meta) {
|
|
$flat_key = 'points_free_' . $key;
|
|
$tasks[$key] = max(0, (int) ($all[$flat_key] ?? $meta[2]));
|
|
}
|
|
|
|
$day_max = array_key_exists('points_earn_daily_limit', $all)
|
|
? max(0, (int) $all['points_earn_daily_limit'])
|
|
: 50;
|
|
|
|
return [
|
|
'day_max' => $day_max,
|
|
'tasks' => $tasks,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Points configured for a task (0 = disabled).
|
|
*/
|
|
public static function task_points(string $key): int {
|
|
$cfg = self::settings();
|
|
return max(0, (int) ($cfg['tasks'][$key] ?? 0));
|
|
}
|
|
|
|
/**
|
|
* Whether free points system can run (credits enabled).
|
|
*/
|
|
public static function enabled(): bool {
|
|
return class_exists('\AKSRT\Settings\Settings') && \AKSRT\Settings\Settings::credits_enabled();
|
|
}
|
|
|
|
/**
|
|
* Today's free points total.
|
|
*/
|
|
public static function today_total(int $user_id): int {
|
|
if ($user_id <= 0) {
|
|
return 0;
|
|
}
|
|
$detail = self::detail_map($user_id);
|
|
$today = self::today_key();
|
|
return max(0, (int) ($detail[$today] ?? 0));
|
|
}
|
|
|
|
/**
|
|
* Remaining free points allowed today (PHP_INT_MAX if unlimited).
|
|
*/
|
|
public static function remaining_today(int $user_id): int {
|
|
$day_max = (int) self::settings()['day_max'];
|
|
if ($day_max <= 0) {
|
|
return PHP_INT_MAX;
|
|
}
|
|
return max(0, $day_max - self::today_total($user_id));
|
|
}
|
|
|
|
/**
|
|
* Daily free points detail (newest first).
|
|
*
|
|
* @return array<string,int>
|
|
*/
|
|
public static function date_detail(int $user_id): array {
|
|
$detail = self::detail_map($user_id);
|
|
krsort($detail, SORT_STRING);
|
|
return $detail;
|
|
}
|
|
|
|
/**
|
|
* Grant free points for a task. Respects day_max; clamps amount.
|
|
*
|
|
* @param array{type?:string,id?:int} $ref
|
|
* @return int|\WP_Error granted amount (0 if skipped)
|
|
*/
|
|
public static function grant(int $user_id, string $task_key, int $points = 0, array $ref = []) {
|
|
if (!self::enabled() || $user_id <= 0) {
|
|
return 0;
|
|
}
|
|
$task_key = sanitize_key($task_key);
|
|
if ($task_key === '') {
|
|
return 0;
|
|
}
|
|
if ($points <= 0) {
|
|
$points = self::task_points($task_key);
|
|
}
|
|
$points = max(0, (int) $points);
|
|
if ($points <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
$remaining = self::remaining_today($user_id);
|
|
if ($remaining <= 0) {
|
|
return 0;
|
|
}
|
|
if ($points > $remaining) {
|
|
$points = $remaining;
|
|
}
|
|
|
|
$reason = 'free_' . $task_key;
|
|
$today = self::today_key();
|
|
$operation_key = \AKSRT\Ledger\OperationKey::deterministic('points-free', [$user_id, $task_key, $today]);
|
|
$result = \AKSRT\Ledger\LedgerService::get_instance()->credit(
|
|
$user_id,
|
|
\AKSRT\Ledger\AssetType::POINTS,
|
|
$points,
|
|
$operation_key,
|
|
$reason,
|
|
$ref,
|
|
self::catalog()[$task_key][0] ?? $task_key,
|
|
0,
|
|
['task' => $task_key, 'date' => $today],
|
|
static function () use ($user_id, $points, $task_key) {
|
|
if (self::remaining_today($user_id) < $points) {
|
|
return new \WP_Error('aksrt_points_daily_limit', __('今日积分奖励已达上限', 'aksrt'), ['status' => 409]);
|
|
}
|
|
self::bump_detail($user_id, $points);
|
|
self::mark_task_done_today($user_id, $task_key);
|
|
return true;
|
|
}
|
|
);
|
|
if (is_wp_error($result)) {
|
|
return $result;
|
|
}
|
|
|
|
return $points;
|
|
}
|
|
|
|
/**
|
|
* Grant with explicit amount (e.g. signin streak reward), still under day_max.
|
|
*
|
|
* @return int|\WP_Error
|
|
*/
|
|
public static function grant_amount(int $user_id, int $points, string $task_key, array $ref = []) {
|
|
return self::grant($user_id, $task_key, $points, $ref);
|
|
}
|
|
|
|
/**
|
|
* Task list DTO for user center UI.
|
|
*
|
|
* @return list<array{key:string,title:string,desc:string,points:int,enabled:bool}>
|
|
*/
|
|
public static function task_list_for_user(int $user_id = 0): array {
|
|
$cfg = self::settings();
|
|
$list = [];
|
|
$today = self::today_key();
|
|
$login_done = false;
|
|
$signin_done = false;
|
|
$task_dates = [];
|
|
if ($user_id > 0) {
|
|
$login_done = (string) get_user_meta($user_id, self::META_LOGIN, true) === $today;
|
|
$signin_done = (string) get_user_meta($user_id, self::META_SIGNIN, true) === $today;
|
|
$raw = get_user_meta($user_id, self::META_TASKS, true);
|
|
$task_dates = is_array($raw) ? $raw : [];
|
|
}
|
|
|
|
$passive = ['post_like', 'post_favorite', 'followed', 'comment_like'];
|
|
$active_once = ['post_new', 'comment_new'];
|
|
|
|
foreach (self::catalog() as $key => $meta) {
|
|
$pts = (int) ($cfg['tasks'][$key] ?? 0);
|
|
if ($key === 'sign_up') {
|
|
continue;
|
|
}
|
|
if ($pts <= 0) {
|
|
continue;
|
|
}
|
|
$status = 'todo';
|
|
$progress = null;
|
|
$target = null;
|
|
if ($key === 'login' && $login_done) {
|
|
$status = 'done_today';
|
|
$progress = 1;
|
|
$target = 1;
|
|
} elseif ($key === 'sign_in' && $signin_done) {
|
|
$status = 'done_today';
|
|
$progress = 1;
|
|
$target = 1;
|
|
} elseif (in_array($key, $passive, true)) {
|
|
$status = 'passive';
|
|
} elseif (in_array($key, $active_once, true)) {
|
|
$target = 1;
|
|
$done_day = is_array($task_dates[$key] ?? null) ? (string) ($task_dates[$key]['day'] ?? '') : (string) ($task_dates[$key] ?? '');
|
|
if ($done_day === $today) {
|
|
$status = 'done_today';
|
|
$progress = 1;
|
|
} else {
|
|
$progress = 0;
|
|
}
|
|
}
|
|
$list[] = [
|
|
'key' => $key,
|
|
'title' => $meta[0],
|
|
'desc' => $meta[1],
|
|
'points' => $pts,
|
|
'enabled' => true,
|
|
'status' => $status,
|
|
'progress' => $progress,
|
|
'target' => $target,
|
|
];
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* Mark a free-task as completed today (for progress DTO).
|
|
*/
|
|
public static function mark_task_done_today(int $user_id, string $task_key): void {
|
|
if ($user_id <= 0) {
|
|
return;
|
|
}
|
|
$task_key = sanitize_key($task_key);
|
|
$today = self::today_key();
|
|
if ($task_key === 'login') {
|
|
update_user_meta($user_id, self::META_LOGIN, $today);
|
|
return;
|
|
}
|
|
if ($task_key === 'sign_in') {
|
|
update_user_meta($user_id, self::META_SIGNIN, $today);
|
|
return;
|
|
}
|
|
$raw = get_user_meta($user_id, self::META_TASKS, true);
|
|
$map = is_array($raw) ? $raw : [];
|
|
$map[$task_key] = ['day' => $today, 'ts' => time()];
|
|
if (count($map) > 40) {
|
|
$map = array_slice($map, -40, null, true);
|
|
}
|
|
update_user_meta($user_id, self::META_TASKS, $map);
|
|
}
|
|
|
|
private static function today_key(): string {
|
|
return (new \DateTimeImmutable('today', wp_timezone()))->format('Y-m-d');
|
|
}
|
|
|
|
/**
|
|
* @return array<string,int>
|
|
*/
|
|
private static function detail_map(int $user_id): array {
|
|
$raw = get_user_meta($user_id, self::META_DETAIL, true);
|
|
if (!is_array($raw)) {
|
|
return [];
|
|
}
|
|
$out = [];
|
|
foreach ($raw as $day => $val) {
|
|
$day = is_string($day) ? $day : (string) $day;
|
|
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $day)) {
|
|
continue;
|
|
}
|
|
$out[$day] = max(0, (int) $val);
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
private static function bump_detail(int $user_id, int $points): void {
|
|
if ($points <= 0) {
|
|
return;
|
|
}
|
|
$detail = self::detail_map($user_id);
|
|
$today = self::today_key();
|
|
$detail[$today] = max(0, (int) ($detail[$today] ?? 0)) + $points;
|
|
// Keep ~50 newest days.
|
|
krsort($detail, SORT_STRING);
|
|
$detail = array_slice($detail, 0, 50, true);
|
|
update_user_meta($user_id, self::META_DETAIL, $detail);
|
|
}
|
|
|
|
}
|