From 15a883eb54976f908798e5c7bb3257ace7fb5e81 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 16 Jul 2026 16:29:15 +0800 Subject: [PATCH] feat(credits): free points tasks matrix, day cap, and tasks tab MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add AKSRT_Points_Free for catalog/day-max/daily detail, wire the full earn matrix (login, post, like, favorite, follow, comment, forum), expose 积分任务 + 每日详情 tabs, and expand admin free-task settings. --- .../src/schema/sections/users/credits.js | 41 +- inc/bootstrap.php | 2 + .../schema/class-aksrt-admin-schema-users.php | 22 +- inc/classes/class-aksrt-comment-actions.php | 3 + inc/classes/class-aksrt-credits-earn.php | 424 ++++++++++-------- inc/classes/class-aksrt-follow.php | 6 +- inc/classes/class-aksrt-points-free.php | 283 ++++++++++++ inc/classes/class-aksrt-signin.php | 15 +- inc/helpers/aksrt-credits.php | 42 +- inc/helpers/aksrt-frontend-tags.php | 6 + .../user-center-renderers/credits-balance.php | 1 + .../credits-points-tasks.php | 95 ++++ .../user-center-renderers/profile-credits.php | 20 +- 13 files changed, 728 insertions(+), 232 deletions(-) create mode 100644 inc/classes/class-aksrt-points-free.php create mode 100644 inc/helpers/user-center-renderers/credits-points-tasks.php diff --git a/assets/admin/src/schema/sections/users/credits.js b/assets/admin/src/schema/sections/users/credits.js index b9da1ef7..0199f2ca 100644 --- a/assets/admin/src/schema/sections/users/credits.js +++ b/assets/admin/src/schema/sections/users/credits.js @@ -9,12 +9,20 @@ export const creditsSchema = { points_rate: 100, points_name: '积分', points_decimal: 2, - points_earn_comment: 1, - points_earn_post: 5, - points_earn_login: 1, - points_earn_like_received: 1, - points_earn_invite: 10, points_earn_daily_limit: 50, + points_free_sign_up: 10, + points_free_sign_in: 1, + points_free_login: 1, + points_free_post_new: 5, + points_free_post_like: 1, + points_free_post_favorite: 1, + points_free_comment_new: 1, + points_free_comment_like: 1, + points_free_followed: 1, + points_free_bbs_posts_new: 5, + points_free_bbs_reply: 2, + points_free_bbs_essence: 10, + points_earn_invite: 10, points_spend_comment: 0, points_spend_download: 0, points_spend_view_hidden: 0, @@ -50,15 +58,22 @@ export const creditsSchema = { }, { key: 'earn', - title: '积分获取', - icon: 'fas fa-arrow-up', + title: '积分获取 / 任务', fields: [ - { key: 'points_earn_comment', label: '评论奖励', type: 'number', default: 1, min: 0, max: 100 }, - { key: 'points_earn_post', label: '发布文章奖励', type: 'number', default: 5, min: 0, max: 100 }, - { key: 'points_earn_login', label: '每日登录奖励', type: 'number', default: 1, min: 0, max: 100 }, - { key: 'points_earn_like_received', label: '收到点赞奖励', type: 'number', default: 1, min: 0, max: 100 }, - { key: 'points_earn_invite', label: '邀请注册奖励', type: 'number', default: 10, min: 0, max: 1000 }, - { key: 'points_earn_daily_limit', label: '每日获取上限', type: 'number', default: 50, min: 0, max: 9999, help: '0为不限制' }, + { key: 'points_earn_daily_limit', label: '每日免费积分上限', type: 'number', default: 50, min: 0, max: 9999, help: '0为不限制' }, + { key: 'points_free_sign_up', label: '注册奖励', type: 'number', default: 10, min: 0, max: 10000 }, + { key: 'points_free_sign_in', label: '签到展示分值', type: 'number', default: 1, min: 0, max: 1000 }, + { key: 'points_free_login', label: '每日登录', type: 'number', default: 1, min: 0, max: 100 }, + { key: 'points_free_post_new', label: '发布文章', type: 'number', default: 5, min: 0, max: 1000 }, + { key: 'points_free_post_like', label: '文章被赞', type: 'number', default: 1, min: 0, max: 100 }, + { key: 'points_free_post_favorite', label: '文章被收藏', type: 'number', default: 1, min: 0, max: 100 }, + { key: 'points_free_comment_new', label: '发表评论', type: 'number', default: 1, min: 0, max: 100 }, + { key: 'points_free_comment_like', label: '评论被赞', type: 'number', default: 1, min: 0, max: 100 }, + { key: 'points_free_followed', label: '被关注', type: 'number', default: 1, min: 0, max: 100 }, + { key: 'points_free_bbs_posts_new', label: '论坛发帖', type: 'number', default: 5, min: 0, max: 1000 }, + { key: 'points_free_bbs_reply', label: '论坛回复', type: 'number', default: 2, min: 0, max: 1000 }, + { key: 'points_free_bbs_essence', label: '帖子加精', type: 'number', default: 10, min: 0, max: 1000 }, + { key: 'points_earn_invite', label: '邀请注册奖励', type: 'number', default: 10, min: 0, max: 1000, help: '邀请不计入免费日上限' }, ], }, { diff --git a/inc/bootstrap.php b/inc/bootstrap.php index f74a3a50..e5573087 100644 --- a/inc/bootstrap.php +++ b/inc/bootstrap.php @@ -216,6 +216,7 @@ require_once AKSRT_DIR . '/inc/classes/class-aksrt-email-rollback.php'; // 原生用户菜单:积分管理 require_once AKSRT_DIR . '/inc/classes/class-aksrt-user-credits-admin.php'; +require_once AKSRT_DIR . '/inc/classes/class-aksrt-user-balance-admin.php'; // 加载数据表迁移器(CMS / 积分 / 下载 token) require_once AKSRT_DIR . '/inc/classes/db-traits/trait-aksrt-db-migrator-part-2.php'; @@ -227,6 +228,7 @@ require_once AKSRT_DIR . '/inc/classes/class-aksrt-credits.php'; // 站内现金钱包(与积分分离) require_once AKSRT_DIR . '/inc/classes/class-aksrt-balance.php'; require_once AKSRT_DIR . '/inc/classes/class-aksrt-balance-service.php'; +require_once AKSRT_DIR . '/inc/classes/class-aksrt-points-free.php'; require_once AKSRT_DIR . '/inc/classes/class-aksrt-credits-earn.php'; require_once AKSRT_DIR . '/inc/classes/class-aksrt-signin.php'; require_once AKSRT_DIR . '/inc/classes/class-aksrt-invite.php'; diff --git a/inc/classes/admin/schema/class-aksrt-admin-schema-users.php b/inc/classes/admin/schema/class-aksrt-admin-schema-users.php index f2d4ac62..86055c6a 100644 --- a/inc/classes/admin/schema/class-aksrt-admin-schema-users.php +++ b/inc/classes/admin/schema/class-aksrt-admin-schema-users.php @@ -414,13 +414,21 @@ class AKSRT_Admin_Schema_Users { ['key' => 'points_name', 'label' => '积分名称', 'type' => 'input', 'default' => '积分'], ['key' => 'points_decimal', 'label' => '积分小数位数', 'type' => 'number', 'default' => 2, 'min' => 0, 'max' => 4], ]], - ['key' => 'earn', 'title' => '积分获取', 'fields' => [ - ['key' => 'points_earn_comment', 'label' => '评论奖励', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 100], - ['key' => 'points_earn_post', 'label' => '发布文章奖励', 'type' => 'number', 'default' => 5, 'min' => 0, 'max' => 100], - ['key' => 'points_earn_login', 'label' => '每日登录奖励', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 100], - ['key' => 'points_earn_like_received', 'label' => '收到点赞奖励', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 100], - ['key' => 'points_earn_invite', 'label' => '邀请注册奖励', 'type' => 'number', 'default' => 10, 'min' => 0, 'max' => 1000], - ['key' => 'points_earn_daily_limit', 'label' => '每日获取上限', 'type' => 'number', 'default' => 50, 'min' => 0, 'max' => 9999, 'help' => '0为不限制'], + ['key' => 'earn', 'title' => '积分获取 / 任务', 'fields' => [ + ['key' => 'points_earn_daily_limit', 'label' => '每日免费积分上限', 'type' => 'number', 'default' => 50, 'min' => 0, 'max' => 9999, 'help' => '0为不限制;免费任务合计'], + ['key' => 'points_free_sign_up', 'label' => '注册奖励', 'type' => 'number', 'default' => 10, 'min' => 0, 'max' => 10000], + ['key' => 'points_free_sign_in', 'label' => '签到展示分值', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 1000], + ['key' => 'points_free_login', 'label' => '每日登录', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 100], + ['key' => 'points_free_post_new', 'label' => '发布文章', 'type' => 'number', 'default' => 5, 'min' => 0, 'max' => 1000], + ['key' => 'points_free_post_like', 'label' => '文章被赞', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 100], + ['key' => 'points_free_post_favorite', 'label' => '文章被收藏', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 100], + ['key' => 'points_free_comment_new', 'label' => '发表评论', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 100], + ['key' => 'points_free_comment_like', 'label' => '评论被赞', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 100], + ['key' => 'points_free_followed', 'label' => '被关注', 'type' => 'number', 'default' => 1, 'min' => 0, 'max' => 100], + ['key' => 'points_free_bbs_posts_new', 'label' => '论坛发帖', 'type' => 'number', 'default' => 5, 'min' => 0, 'max' => 1000], + ['key' => 'points_free_bbs_reply', 'label' => '论坛回复', 'type' => 'number', 'default' => 2, 'min' => 0, 'max' => 1000], + ['key' => 'points_free_bbs_essence', 'label' => '帖子加精', 'type' => 'number', 'default' => 10, 'min' => 0, 'max' => 1000], + ['key' => 'points_earn_invite', 'label' => '邀请注册奖励', 'type' => 'number', 'default' => 10, 'min' => 0, 'max' => 1000, 'help' => '邀请奖励不走免费日上限'], ]], ['key' => 'spend', 'title' => '积分消费', 'fields' => [ ['key' => 'points_spend_comment', 'label' => '评论消耗', 'type' => 'number', 'default' => 0, 'min' => 0, 'max' => 100], diff --git a/inc/classes/class-aksrt-comment-actions.php b/inc/classes/class-aksrt-comment-actions.php index 41ecb7e8..712188fc 100644 --- a/inc/classes/class-aksrt-comment-actions.php +++ b/inc/classes/class-aksrt-comment-actions.php @@ -526,6 +526,9 @@ class AKSRT_Comment_Actions { if ($target !== $current) { aksrt_set_comment_like_state($comment_id, $target); $count = aksrt_update_comment_counter($comment_id, 'aksrt_comment_likes', $target ? 1 : -1); + if (is_user_logged_in()) { + do_action('aksrt_comment_liked', $comment_id, get_current_user_id(), (bool) $target); + } } else { $count = aksrt_get_comment_likes($comment_id); } diff --git a/inc/classes/class-aksrt-credits-earn.php b/inc/classes/class-aksrt-credits-earn.php index c5c38f1d..877bafe9 100644 --- a/inc/classes/class-aksrt-credits-earn.php +++ b/inc/classes/class-aksrt-credits-earn.php @@ -1,13 +1,6 @@ 'user', 'id' => $user_id]); + } - /** - * 论坛发帖积分奖励 - */ - public function on_forum_topic_created(int $user_id, int $post_id): void { - if (!class_exists('AKSRT_Credits') || !class_exists('AKSRT_Settings')) return; - $cfg = AKSRT_Settings::get_credit_settings(); - if (empty($cfg['enabled'])) return; - $reward = $cfg['forum_topic_reward'] ?? []; - if (empty($reward['enabled'])) return; + /** + * Daily first login free points. + * + * @param string $user_login + * @param WP_User $user + */ + public function on_wp_login($user_login, $user = null): void { + unset($user_login); + $user_id = $user instanceof WP_User ? (int) $user->ID : 0; + if ($user_id <= 0 || !class_exists('AKSRT_Points_Free')) { + return; + } + $today = (new DateTimeImmutable('today', wp_timezone()))->format('Y-m-d'); + $last = (string) get_user_meta($user_id, AKSRT_Points_Free::META_LOGIN, true); + if ($last === $today) { + return; + } + $granted = AKSRT_Points_Free::grant($user_id, 'login', 0, ['type' => 'user', 'id' => $user_id]); + if (!is_wp_error($granted) && $granted >= 0) { + update_user_meta($user_id, AKSRT_Points_Free::META_LOGIN, $today); + } + } - $amount = (int) ($reward['amount'] ?? 0); - if ($amount <= 0) return; + /** + * @param string $new_status + * @param string $old_status + * @param WP_Post $post + */ + public function on_post_status($new_status, $old_status, $post): void { + if (!($post instanceof WP_Post) || $post->post_type !== 'post') { + return; + } + if ($new_status !== 'publish' || $old_status === 'publish') { + return; + } + $user_id = (int) $post->post_author; + if ($user_id <= 0 || !class_exists('AKSRT_Points_Free')) { + return; + } + if (get_post_meta($post->ID, 'aksrt_credit_post_rewarded', true)) { + return; + } + $granted = AKSRT_Points_Free::grant($user_id, 'post_new', 0, ['type' => 'post', 'id' => (int) $post->ID]); + if (!is_wp_error($granted) && $granted > 0) { + update_post_meta($post->ID, 'aksrt_credit_post_rewarded', 1); + } + } - $daily_limit = (int) ($reward['daily_limit'] ?? 5); - $today_count = $this->today_forum_reward_count($user_id, 'topic'); - if ($today_count >= $daily_limit) return; + public function on_comment_inserted(int $comment_id, $comment): void { + $cfg = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_credit_settings() : []; + $reward = is_array($cfg['comment_reward'] ?? null) ? $cfg['comment_reward'] : []; + // Legacy on_approved=true → wait for transition; default true. + $on_approved = !isset($reward['on_approved']) || !empty($reward['on_approved']); + if ($on_approved) { + return; + } + $comment = $comment instanceof WP_Comment ? $comment : get_comment($comment_id); + if (!($comment instanceof WP_Comment) || (int) $comment->comment_approved !== 1) { + return; + } + $this->reward_comment($comment); + } - AKSRT_Credits::add($user_id, $amount, 'forum_topic', ['type' => 'post', 'id' => $post_id]); - } + public function on_comment_transition($new_status, $old_status, $comment): void { + unset($old_status); + if ($new_status !== 'approved' || !($comment instanceof WP_Comment)) { + return; + } + $this->reward_comment($comment); + } - /** - * 今日论坛奖励次数 - */ - private function today_forum_reward_count(int $user_id, string $type = 'topic'): int { - global $wpdb; - $table = AKSRT_DB_Migrator::table('credit_log'); - if (!$table) return 0; + private function reward_comment(WP_Comment $comment): void { + $user_id = (int) $comment->user_id; + if ($user_id <= 0 || !class_exists('AKSRT_Points_Free')) { + return; + } + if (get_comment_meta($comment->comment_ID, 'aksrt_credit_rewarded', true)) { + return; + } + // Skip forum topic replies (handled separately). + $post = get_post((int) $comment->comment_post_ID); + if ($post && $post->post_type === 'aksrt_forum_topic') { + return; + } + $cfg = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_credit_settings() : []; + $reward = is_array($cfg['comment_reward'] ?? null) ? $cfg['comment_reward'] : []; + $min_length = (int) ($reward['min_length'] ?? 0); + if ($min_length > 0 && mb_strlen(wp_strip_all_tags((string) $comment->comment_content)) < $min_length) { + return; + } + $granted = AKSRT_Points_Free::grant($user_id, 'comment_new', 0, [ + 'type' => 'comment', + 'id' => (int) $comment->comment_ID, + ]); + if (!is_wp_error($granted)) { + update_comment_meta($comment->comment_ID, 'aksrt_credit_rewarded', $granted > 0 ? 1 : 'capped'); + } + } - $today = current_time('Y-m-d'); - return (int) $wpdb->get_var($wpdb->prepare( - "SELECT COUNT(*) FROM {$table} WHERE user_id = %d AND reason = %s AND DATE(created_at) = %s", - $user_id, 'forum_' . $type, $today - )); - } + /** + * @param int $post_id + * @param int $actor_id liker + * @param bool $active + */ + public function on_post_liked(int $post_id, int $actor_id, bool $active): void { + if (!$active || $post_id <= 0 || !class_exists('AKSRT_Points_Free')) { + return; + } + $post = get_post($post_id); + if (!$post || $post->post_type !== 'post') { + return; + } + $author = (int) $post->post_author; + if ($author <= 0 || $author === $actor_id) { + return; + } + AKSRT_Points_Free::grant($author, 'post_like', 0, ['type' => 'post', 'id' => $post_id]); + } - /** - * 注册赠送。 - */ - public function on_user_register(int $user_id): void { - if (!class_exists('AKSRT_Credits') || !class_exists('AKSRT_Settings')) return; - $cfg = AKSRT_Settings::get_credit_settings(); - if (empty($cfg['enabled'])) return; - $bonus = (int) ($cfg['register_bonus'] ?? 0); - if ($bonus <= 0) return; + public function on_post_favorited(int $post_id, int $actor_id, bool $active): void { + if (!$active || $post_id <= 0 || !class_exists('AKSRT_Points_Free')) { + return; + } + $post = get_post($post_id); + if (!$post || $post->post_type !== 'post') { + return; + } + $author = (int) $post->post_author; + if ($author <= 0 || $author === $actor_id) { + return; + } + AKSRT_Points_Free::grant($author, 'post_favorite', 0, ['type' => 'post', 'id' => $post_id]); + } - AKSRT_Credits::add($user_id, $bonus, 'register', ['type' => 'user', 'id' => $user_id]); - } + public function on_comment_liked(int $comment_id, int $actor_id, bool $active): void { + if (!$active || $comment_id <= 0 || !class_exists('AKSRT_Points_Free')) { + return; + } + $comment = get_comment($comment_id); + if (!$comment) { + return; + } + $author = (int) $comment->user_id; + if ($author <= 0 || $author === $actor_id) { + return; + } + AKSRT_Points_Free::grant($author, 'comment_like', 0, ['type' => 'comment', 'id' => $comment_id]); + } - /** - * 评论插入(直接发表无需审核场景)。 - */ - public function on_comment_inserted(int $comment_id, $comment): void { - if (!class_exists('AKSRT_Settings')) return; - $cfg = AKSRT_Settings::get_credit_settings(); - $reward = $cfg['comment_reward'] ?? []; - if (empty($cfg['enabled']) || empty($reward['enabled'])) return; + public function on_user_followed(int $follower_id, int $following_id): void { + if ($following_id <= 0 || $follower_id === $following_id || !class_exists('AKSRT_Points_Free')) { + return; + } + AKSRT_Points_Free::grant($following_id, 'followed', 0, ['type' => 'user', 'id' => $follower_id]); + } - // 仅审核通过后发放 → 不在 insert 时处理 - if (!empty($reward['on_approved'])) return; + public function on_forum_topic_created(int $user_id, int $post_id): void { + if ($user_id <= 0 || !class_exists('AKSRT_Points_Free')) { + return; + } + AKSRT_Points_Free::grant($user_id, 'bbs_posts_new', 0, ['type' => 'post', 'id' => $post_id]); + } - $comment = $comment instanceof WP_Comment ? $comment : get_comment($comment_id); - if (!($comment instanceof WP_Comment)) return; + public function on_forum_reply_posted(int $comment_id, $approved, array $commentdata = []): void { + unset($commentdata); + if ((int) $approved !== 1 && $approved !== '1' && $approved !== true) { + // Still load comment and check. + } + $comment = get_comment($comment_id); + if (!$comment || (int) $comment->comment_approved !== 1) { + return; + } + $post = get_post((int) $comment->comment_post_ID); + if (!$post || $post->post_type !== 'aksrt_forum_topic') { + return; + } + $user_id = (int) $comment->user_id; + if ($user_id <= 0 || !class_exists('AKSRT_Points_Free')) { + return; + } + if (get_comment_meta($comment_id, 'aksrt_credit_forum_reply', true)) { + return; + } + $granted = AKSRT_Points_Free::grant($user_id, 'bbs_reply', 0, ['type' => 'comment', 'id' => $comment_id]); + if (!is_wp_error($granted)) { + update_comment_meta($comment_id, 'aksrt_credit_forum_reply', 1); + } + } - // 只奖励已批准的评论 - if ((int) $comment->comment_approved !== 1) return; - - $this->maybe_reward_comment($comment, $reward); - } - - /** - * 评论状态流转。 - */ - public function on_comment_transition($new_status, $old_status, $comment): void { - if (!class_exists('AKSRT_Settings')) return; - $cfg = AKSRT_Settings::get_credit_settings(); - $reward = $cfg['comment_reward'] ?? []; - if (empty($cfg['enabled']) || empty($reward['enabled'])) return; - if (empty($reward['on_approved'])) return; - - if ($new_status !== 'approved') return; - // 跳过已经处理过的:以 commentmeta 标记 - if (!($comment instanceof WP_Comment)) return; - - $this->maybe_reward_comment($comment, $reward); - } - - /** - * 真正的评论奖励逻辑(含字数 / 每日上限 / 去重)。 - */ - private function maybe_reward_comment(WP_Comment $comment, array $reward): void { - $user_id = (int) $comment->user_id; - if ($user_id <= 0) return; - - // 防重复:commentmeta 标记 - if (get_comment_meta($comment->comment_ID, 'aksrt_credit_rewarded', true)) return; - - $min_length = (int) ($reward['min_length'] ?? 0); - if ($min_length > 0 && mb_strlen(strip_tags($comment->comment_content)) < $min_length) return; - - $amount = (int) ($reward['amount'] ?? 0); - if ($amount <= 0) return; - - // 每日上限 - $cap = (int) ($reward['daily_cap'] ?? 0); - if ($cap > 0) { - $today_count = $this->today_comment_reward_count($user_id); - if ($today_count >= $cap) { - update_comment_meta($comment->comment_ID, 'aksrt_credit_rewarded', 'capped'); - return; - } - } - - $result = AKSRT_Credits::add( - $user_id, - $amount, - 'comment', - ['type' => 'comment', 'id' => (int) $comment->comment_ID] - ); - if (!is_wp_error($result)) { - update_comment_meta($comment->comment_ID, 'aksrt_credit_rewarded', 1); - } - } - - /** - * 当日评论奖励次数(查询 credit_log)。 - */ - private function today_comment_reward_count(int $user_id): int { - global $wpdb; - if (!class_exists('AKSRT_DB_Migrator')) return 0; - $table = AKSRT_DB_Migrator::table('credit_log'); - if (!$table) return 0; - - // 站点时区当日 00:00 起 - $tz = wp_timezone(); - $today = (new DateTimeImmutable('today', $tz))->format('Y-m-d H:i:s'); - // 转 UTC(数据库 created_at 用 mysql UTC) - $today_utc = (new DateTimeImmutable('today', $tz))->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s'); - - return (int) $wpdb->get_var($wpdb->prepare( - "SELECT COUNT(*) FROM {$table} WHERE user_id = %d AND reason = 'comment' AND created_at >= %s", - $user_id, - $today_utc - )); - } - - // ================================================================ - // Forum credits (§7.7) - // ================================================================ - - public function on_forum_reply_posted(int $comment_id, $approved, array $commentdata = []): void { - if (!class_exists('AKSRT_Credits') || !class_exists('AKSRT_Settings')) return; - if (!class_exists('AKSRT_Forum')) return; - - $comment = get_comment($comment_id); - if (!$comment) return; - - $post = get_post((int) $comment->comment_post_ID); - if (!$post || $post->post_type !== 'aksrt_forum_topic') return; - - if ((int) $comment->comment_approved !== 1) return; - - $user_id = (int) $comment->user_id; - if ($user_id <= 0) return; - - $forum_cfg = AKSRT_Settings::get_forum_settings(); - $amount = (int) ($forum_cfg['credits']['comment_new'] ?? 2); - if ($amount <= 0) return; - - $daily_max = (int) ($forum_cfg['credits']['daily_max'] ?? 50); - $today_count = $this->today_forum_reward_count($user_id, 'reply'); - if ($today_count >= $daily_max) return; - - AKSRT_Credits::add($user_id, $amount, 'forum_reply', [ - 'type' => 'comment', - 'id' => $comment_id, - ]); - } - - public function on_forum_topic_featured(int $topic_id, int $operator_id): void { - // Essence reward is handled in AKSRT_Forum::reward_essence(). - // This hook is for additional integrations if needed. - } + public function on_forum_topic_featured(int $topic_id, int $operator_id): void { + unset($operator_id); + $post = get_post($topic_id); + if (!$post || $post->post_type !== 'aksrt_forum_topic') { + return; + } + $user_id = (int) $post->post_author; + if ($user_id <= 0 || !class_exists('AKSRT_Points_Free')) { + return; + } + if (get_post_meta($topic_id, 'aksrt_credit_essence_rewarded', true)) { + return; + } + $granted = AKSRT_Points_Free::grant($user_id, 'bbs_essence', 0, ['type' => 'post', 'id' => $topic_id]); + if (!is_wp_error($granted) && $granted > 0) { + update_post_meta($topic_id, 'aksrt_credit_essence_rewarded', 1); + } + } } diff --git a/inc/classes/class-aksrt-follow.php b/inc/classes/class-aksrt-follow.php index 28cbeab0..75d543c8 100644 --- a/inc/classes/class-aksrt-follow.php +++ b/inc/classes/class-aksrt-follow.php @@ -47,7 +47,11 @@ class AKSRT_Follow { 'created_at' => current_time('mysql'), ], ['%d', '%d', '%s']); - return $result !== false; + if ($result === false) { + return false; + } + do_action('aksrt_user_followed', $follower_id, $following_id); + return true; } /** diff --git a/inc/classes/class-aksrt-points-free.php b/inc/classes/class-aksrt-points-free.php new file mode 100644 index 00000000..b4d1bae7 --- /dev/null +++ b/inc/classes/class-aksrt-points-free.php @@ -0,0 +1,283 @@ + [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], + ]; + } + + /** + * 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; + default: + return $default; + } + } +} diff --git a/inc/classes/class-aksrt-signin.php b/inc/classes/class-aksrt-signin.php index 2c8e9612..4ebd0431 100644 --- a/inc/classes/class-aksrt-signin.php +++ b/inc/classes/class-aksrt-signin.php @@ -130,8 +130,19 @@ class AKSRT_Signin { return new WP_Error('aksrt_signin_dup', __('签到失败', 'aksrt'), ['status' => 409]); } - if ($reward > 0 && class_exists('AKSRT_Credits')) { - AKSRT_Credits::add($user_id, $reward, 'signin', ['type' => 'user', 'id' => $user_id]); + if ($reward > 0) { + if (class_exists('AKSRT_Points_Free')) { + $granted = AKSRT_Points_Free::grant_amount($user_id, $reward, 'sign_in', ['type' => 'user', 'id' => $user_id]); + if (is_wp_error($granted)) { + if (class_exists('AKSRT_Credits')) { + AKSRT_Credits::add($user_id, $reward, 'signin', ['type' => 'user', 'id' => $user_id]); + } + } else { + $reward = (int) $granted; + } + } elseif (class_exists('AKSRT_Credits')) { + AKSRT_Credits::add($user_id, $reward, 'signin', ['type' => 'user', 'id' => $user_id]); + } } return new WP_REST_Response([ diff --git a/inc/helpers/aksrt-credits.php b/inc/helpers/aksrt-credits.php index 3de1cc9a..f5919a1f 100644 --- a/inc/helpers/aksrt-credits.php +++ b/inc/helpers/aksrt-credits.php @@ -39,15 +39,39 @@ if (!function_exists('aksrt_credits_label')) { */ function aksrt_credits_label(string $reason): string { $map = [ - 'register' => __('注册赠送', 'aksrt'), - 'signin' => __('每日签到', 'aksrt'), - 'comment' => __('发表评论', 'aksrt'), - 'invite' => __('邀请奖励', 'aksrt'), - 'invitee' => __('被邀请奖励', 'aksrt'), - 'download' => __('下载消费', 'aksrt'), - 'admin' => __('管理员调整', 'aksrt'), - 'refund' => __('退款', 'aksrt'), + 'register' => __('注册赠送', 'aksrt'), + 'signin' => __('每日签到', 'aksrt'), + 'comment' => __('发表评论', 'aksrt'), + 'invite' => __('邀请奖励', 'aksrt'), + 'invitee' => __('被邀请奖励', 'aksrt'), + 'download' => __('下载消费', 'aksrt'), + 'admin' => __('管理员调整', 'aksrt'), + 'refund' => __('退款', 'aksrt'), + 'forum_topic' => __('论坛发帖', 'aksrt'), + 'forum_reply' => __('论坛回复', 'aksrt'), + 'free_sign_up' => __('注册奖励', 'aksrt'), + 'free_sign_in' => __('每日签到', 'aksrt'), + 'free_login' => __('每日登录', 'aksrt'), + 'free_followed'=> __('被关注', 'aksrt'), + 'free_post_new'=> __('发布文章', 'aksrt'), + 'free_post_like'=> __('文章被赞', 'aksrt'), + 'free_post_favorite'=> __('文章被收藏', 'aksrt'), + 'free_comment_new'=> __('发表评论', 'aksrt'), + 'free_comment_like'=> __('评论被赞', 'aksrt'), + 'free_bbs_posts_new'=> __('论坛发帖', 'aksrt'), + 'free_bbs_reply'=> __('论坛回复', 'aksrt'), + 'free_bbs_essence'=> __('帖子加精', 'aksrt'), ]; - return $map[$reason] ?? $reason; + if (isset($map[$reason])) { + return $map[$reason]; + } + if (str_starts_with($reason, 'free_') && class_exists('AKSRT_Points_Free')) { + $key = substr($reason, 5); + $cat = AKSRT_Points_Free::catalog(); + if (isset($cat[$key][0])) { + return (string) $cat[$key][0]; + } + } + return $reason; } } diff --git a/inc/helpers/aksrt-frontend-tags.php b/inc/helpers/aksrt-frontend-tags.php index 6ca0e03b..1a3e94d1 100644 --- a/inc/helpers/aksrt-frontend-tags.php +++ b/inc/helpers/aksrt-frontend-tags.php @@ -775,6 +775,9 @@ if (!function_exists('aksrt_toggle_like_ajax')) : aksrt_set_interaction_cookie(aksrt_get_interaction_cookie_name('like', $post_id), $target); } $count = aksrt_update_post_counter($post_id, '_aksrt_like_count', $target ? 1 : -1); + if (is_user_logged_in()) { + do_action('aksrt_post_liked', $post_id, get_current_user_id(), (bool) $target); + } } else { $count = aksrt_get_post_likes($post_id); } @@ -801,6 +804,9 @@ if (!function_exists('aksrt_toggle_favorite_ajax')) : aksrt_set_interaction_cookie(aksrt_get_interaction_cookie_name('favorite', $post_id), $target); } $count = aksrt_update_post_counter($post_id, '_aksrt_favorite_count', $target ? 1 : -1); + if (is_user_logged_in()) { + do_action('aksrt_post_favorited', $post_id, get_current_user_id(), (bool) $target); + } } else { $count = aksrt_get_post_favorites($post_id); } diff --git a/inc/helpers/user-center-renderers/credits-balance.php b/inc/helpers/user-center-renderers/credits-balance.php index 83628e61..84b9f4dd 100644 --- a/inc/helpers/user-center-renderers/credits-balance.php +++ b/inc/helpers/user-center-renderers/credits-balance.php @@ -50,6 +50,7 @@ $currency = (string) ($currency_name ?? __('积分', 'aksrt'));

+ + + +
+
+
+ +
+ + $val) : ?> +
+
+
+
+
+ + +

+ +
+ +
+ 0 || $day_max > 0) : ?> +
+
+
+
+ +' . esc_html((string) $today) . '' + ); + ?> +
+
+ 0) { + if ($today >= $day_max) { + printf(esc_html__('今日获取的免费积分已达到 %s,将不再获取', 'aksrt'), esc_html((string) $day_max)); + } else { + printf(esc_html__('每日可免费获取 %s 积分,超过后将不再获取', 'aksrt'), esc_html((string) $day_max)); + } + } else { + esc_html_e('免费积分无每日上限', 'aksrt'); + } + ?> +
+
+ +
+
+ + + + +
+
+
+
+
+
+ + + + +
+
+ + +

+ +
+ diff --git a/inc/helpers/user-center-renderers/profile-credits.php b/inc/helpers/user-center-renderers/profile-credits.php index 410355fd..2b9d59a6 100644 --- a/inc/helpers/user-center-renderers/profile-credits.php +++ b/inc/helpers/user-center-renderers/profile-credits.php @@ -48,9 +48,10 @@ if ($show_credits) { // Tabbed ledgers (Zibll record tabs). if ($show_wallet || $show_credits) : $active_record = isset($_GET['asset_tab']) ? sanitize_key(wp_unslash($_GET['asset_tab'])) : ''; - if ($active_record === '' || !in_array($active_record, ['balance', 'points'], true)) { + if ($active_record === '' || !in_array($active_record, ['balance', 'points', 'points_free', 'points_date'], true)) { $active_record = $show_wallet ? 'balance' : 'points'; } + $show_tasks = $show_credits && class_exists('AKSRT_Points_Free') && AKSRT_Points_Free::enabled(); ?>
    @@ -64,6 +65,11 @@ if ($show_wallet || $show_credits) : + +
  • + +
  • +
@@ -100,6 +106,10 @@ if ($show_wallet || $show_credits) :
+ + + +
@@ -123,6 +133,14 @@ if ($show_wallet || $show_credits) : + +
> + +
+
> + +
+