Files
wordpress-theme-aksrt/inc/classes/class-aksrt-points-free.php
T
kate522 5949c7a503 feat(identity): close free-task progress, level card, hot threshold
Finish remaining 1:1 parity gaps: free-task done/progress DTO with
daily marks, user-center level/integral card UI, configurable forum
hot recommend threshold, and task progress bars.
2026-07-17 02:05:10 +08:00

385 lines
12 KiB
PHP

<?php
/**
* Free points tasks — Zibll points_free_opt parity.
*
* Unified grant path with daily free-points cap + per-day detail meta.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
class AKSRT_Points_Free {
public const META_DETAIL = 'aksrt_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],
'bbs_posts_new' => [__('论坛发帖', 'aksrt'), __('发布论坛主题', 'aksrt'), 5],
'bbs_reply' => [__('论坛回复', 'aksrt'), __('回复论坛主题', 'aksrt'), 2],
'bbs_essence' => [__('帖子加精', 'aksrt'), __('主题被设为精华', 'aksrt'), 10],
'bbs_adopt' => [__('回复被采纳', 'aksrt'), __('问答帖回复被采纳', 'aksrt'), 20],
'bbs_posts_hot' => [__('热帖奖励', 'aksrt'), __('主题成为热帖', 'aksrt'), 5],
'bbs_comment_hot'=> [__('神评奖励', 'aksrt'), __('回复成为神评', 'aksrt'), 5],
'bbs_score_extra'=> [__('帖子加分', 'aksrt'), __('主题被加分时奖励', 'aksrt'), 3],
];
}
/**
* 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') ? AKSRT_Settings::get_all() : [];
$credit = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_credit_settings() : [];
$tasks = [];
foreach (self::catalog() as $key => $meta) {
$flat_key = 'points_free_' . $key;
if (array_key_exists($flat_key, $all)) {
$tasks[$key] = max(0, (int) $all[$flat_key]);
continue;
}
// Fallback mapping from legacy / parallel settings.
$tasks[$key] = self::legacy_task_points($key, $all, $credit, (int) $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') && AKSRT_Settings::credits_enabled() && class_exists('AKSRT_Credits');
}
/**
* 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;
$result = AKSRT_Credits::add(
$user_id,
$points,
$reason,
$ref,
self::catalog()[$task_key][0] ?? $task_key
);
if (is_wp_error($result)) {
return $result;
}
self::bump_detail($user_id, $points);
self::mark_task_done_today($user_id, $task_key);
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', 'bbs_essence', 'bbs_adopt', 'bbs_posts_hot', 'bbs_comment_hot', 'bbs_score_extra'];
$active_once = ['post_new', 'comment_new', 'bbs_posts_new', 'bbs_reply'];
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);
}
/**
* @param array<string,mixed> $all
* @param array<string,mixed> $credit
*/
private static function legacy_task_points(string $key, array $all, array $credit, int $default): int {
switch ($key) {
case 'sign_up':
return max(0, (int) ($credit['register_bonus'] ?? $default));
case 'sign_in':
$signin = is_array($credit['daily_signin'] ?? null) ? $credit['daily_signin'] : [];
if (isset($signin['enabled']) && empty($signin['enabled'])) {
return 0;
}
return max(0, (int) ($signin['base'] ?? $default));
case 'login':
return max(0, (int) ($all['points_earn_login'] ?? $default));
case 'post_new':
return max(0, (int) ($all['points_earn_post'] ?? $default));
case 'post_like':
return max(0, (int) ($all['points_earn_like_received'] ?? $default));
case 'comment_new':
if (isset($all['points_earn_comment'])) {
return max(0, (int) $all['points_earn_comment']);
}
$cr = is_array($credit['comment_reward'] ?? null) ? $credit['comment_reward'] : [];
if (isset($cr['enabled']) && empty($cr['enabled'])) {
return 0;
}
return max(0, (int) ($cr['amount'] ?? $default));
case 'bbs_posts_new':
if (class_exists('AKSRT_Settings') && method_exists('AKSRT_Settings', 'get_forum_settings')) {
$f = AKSRT_Settings::get_forum_settings();
return max(0, (int) ($f['credits']['post_new'] ?? $f['credits']['topic_new'] ?? $all['bbs_credits_post_new'] ?? $default));
}
return max(0, (int) ($all['bbs_credits_post_new'] ?? $default));
case 'bbs_reply':
if (class_exists('AKSRT_Settings') && method_exists('AKSRT_Settings', 'get_forum_settings')) {
$f = AKSRT_Settings::get_forum_settings();
return max(0, (int) ($f['credits']['comment_new'] ?? $default));
}
return $default;
case 'bbs_essence':
if (class_exists('AKSRT_Settings') && method_exists('AKSRT_Settings', 'get_forum_settings')) {
$f = AKSRT_Settings::get_forum_settings();
return max(0, (int) ($f['credits']['essence'] ?? $default));
}
return $default;
case 'bbs_adopt':
if (class_exists('AKSRT_Settings') && method_exists('AKSRT_Settings', 'get_forum_settings')) {
$f = AKSRT_Settings::get_forum_settings();
return max(0, (int) ($f['credits']['adopted'] ?? $default));
}
return $default;
case 'bbs_posts_hot':
if (class_exists('AKSRT_Settings') && method_exists('AKSRT_Settings', 'get_forum_settings')) {
$f = AKSRT_Settings::get_forum_settings();
return max(0, (int) ($f['credits']['posts_hot'] ?? $default));
}
return $default;
case 'bbs_comment_hot':
if (class_exists('AKSRT_Settings') && method_exists('AKSRT_Settings', 'get_forum_settings')) {
$f = AKSRT_Settings::get_forum_settings();
return max(0, (int) ($f['credits']['comment_hot'] ?? $f['credits']['comment_like'] ?? $default));
}
return $default;
case 'bbs_score_extra':
if (class_exists('AKSRT_Settings') && method_exists('AKSRT_Settings', 'get_forum_settings')) {
$f = AKSRT_Settings::get_forum_settings();
return max(0, (int) ($f['credits']['score_extra'] ?? $default));
}
return $default;
default:
return $default;
}
}
}