Route forum essence/adopt/hot/score rewards through free-points day cap, notify users on withdraw complete/reject, surface 海报分享 on single share grid, and extend admin free-task keys for the remaining matrix.
248 lines
8.1 KiB
PHP
248 lines
8.1 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT Credits Earn — free-task matrix hooks via AKSRT_Points_Free.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Credits_Earn {
|
|
|
|
private static $instance = null;
|
|
|
|
private function __construct() {}
|
|
|
|
public static function get_instance(): self {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function init(): void {
|
|
add_action('user_register', [$this, 'on_user_register'], 10, 1);
|
|
add_action('wp_login', [$this, 'on_wp_login'], 20, 2);
|
|
|
|
add_action('wp_insert_comment', [$this, 'on_comment_inserted'], 10, 2);
|
|
add_action('transition_comment_status', [$this, 'on_comment_transition'], 10, 3);
|
|
add_action('transition_post_status', [$this, 'on_post_status'], 10, 3);
|
|
|
|
add_action('aksrt_post_liked', [$this, 'on_post_liked'], 10, 3);
|
|
add_action('aksrt_post_favorited', [$this, 'on_post_favorited'], 10, 3);
|
|
add_action('aksrt_comment_liked', [$this, 'on_comment_liked'], 10, 3);
|
|
add_action('aksrt_user_followed', [$this, 'on_user_followed'], 10, 2);
|
|
|
|
add_action('aksrt_forum_topic_created', [$this, 'on_forum_topic_created'], 10, 2);
|
|
add_action('comment_post', [$this, 'on_forum_reply_posted'], 20, 3);
|
|
add_action('aksrt_forum_topic_featured', [$this, 'on_forum_topic_featured'], 10, 2);
|
|
}
|
|
|
|
public function on_user_register(int $user_id): void {
|
|
if (!class_exists('AKSRT_Points_Free')) {
|
|
return;
|
|
}
|
|
AKSRT_Points_Free::grant($user_id, 'sign_up', 0, ['type' => 'user', 'id' => $user_id]);
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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 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');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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_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]);
|
|
}
|
|
|
|
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_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]);
|
|
}
|
|
|
|
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]);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Essence reward is owned by AKSRT_Forum::reward_essence() (free-day-cap).
|
|
* Keep hook for external integrations only — do not double-grant.
|
|
*
|
|
* @param int $topic_id
|
|
* @param int $author_or_operator Author id from forum (2nd arg) or operator.
|
|
*/
|
|
public function on_forum_topic_featured(int $topic_id, int $author_or_operator = 0): void {
|
|
unset($topic_id, $author_or_operator);
|
|
}
|
|
}
|