refactor: flatten src/AKSRT → src (drop theme-identifier dir)
Move all domain folders from src/AKSRT/{Domain} to src/{Domain}; remap
Composer PSR-4 "AKSRT\\": "src/"; update audit script + README + ADR
paths; remove dead tools/build-psr4-map.php (scanned deleted inc/classes).
This commit is contained in:
1 parent
56f1a8175f
commit
8c5b1555c9
285 files changed
+26
-3600
No files matched your search
@@ -0,0 +1,333 @@
|
||||
<?php
|
||||
/**
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
namespace AKSRT\Credits;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class PointsFree {
|
||||
|
||||
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],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() : [];
|
||||
$credit = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\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\Settings') && \AKSRT\Settings\Settings::credits_enabled() && class_exists('AKSRT\\Credits\\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 = 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'];
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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));
|
||||
default:
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user