[label, short_desc, default_points] * * @return array */ 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} */ 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 */ 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); 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 */ public static function task_list_for_user(int $user_id = 0): array { unset($user_id); // reserved for per-user progress later $cfg = self::settings(); $list = []; foreach (self::catalog() as $key => $meta) { $pts = (int) ($cfg['tasks'][$key] ?? 0); // Hide sign_up from daily task list (one-time); still grant on register. if ($key === 'sign_up') { continue; } if ($pts <= 0) { continue; } $list[] = [ 'key' => $key, 'title' => $meta[0], 'desc' => $meta[1], 'points' => $pts, 'enabled' => true, ]; } return $list; } private static function today_key(): string { return (new DateTimeImmutable('today', wp_timezone()))->format('Y-m-d'); } /** * @return array */ 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 $all * @param array $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; } } }