%4$s', esc_attr($class), $user_id, $post_id, esc_html($label) ); } } if (!function_exists('aksrt_render_rewards_modal_for_user')) { function aksrt_render_rewards_modal_for_user(int $user_id, int $post_id = 0): void { static $done = []; $key = $user_id . ':' . $post_id; if ($user_id <= 0 || isset($done[$key]) || !aksrt_rewards_enabled()) { return; } $done[$key] = true; $settings = \AKSRT\Settings\Settings::get_post_page_settings(); $amounts = (array) ($settings['rewards_amounts'] ?? [5, 10, 20, 50, 100]); $recipient = get_userdata($user_id); ?> __('请先登录后赞赏', 'aksrt')], 401); } if (!aksrt_rewards_enabled() || !class_exists('\AKSRT\Credits\Balance') || !\AKSRT\Credits\Balance::enabled()) { wp_send_json_error(['message' => __('赞赏功能当前不可用', 'aksrt')], 400); } $sender_id = get_current_user_id(); $post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0; $recipient_id = isset($_POST['recipient_id']) ? absint($_POST['recipient_id']) : 0; if ($post_id > 0) { $post = get_post($post_id); if (!$post instanceof WP_Post || $post->post_status !== 'publish') { wp_send_json_error(['message' => __('赞赏内容不存在', 'aksrt')], 404); } $recipient_id = (int) $post->post_author; } if ($recipient_id <= 0 || !get_userdata($recipient_id) || $recipient_id === $sender_id) { wp_send_json_error(['message' => __('不能赞赏该用户', 'aksrt')], 400); } $settings = \AKSRT\Settings\Settings::get_post_page_settings(); $amount = isset($_POST['amount']) ? round((float) wp_unslash($_POST['amount']), 2) : 0.0; $minimum = (float) ($settings['rewards_min'] ?? 1); $maximum = (float) ($settings['rewards_max'] ?? 1000); if (!is_finite($amount) || $amount < $minimum || $amount > $maximum) { wp_send_json_error(['message' => sprintf(__('赞赏金额需在 ¥%1$s 至 ¥%2$s 之间', 'aksrt'), number_format_i18n($minimum, 2), number_format_i18n($maximum, 2))], 400); } if (empty($settings['rewards_custom'])) { $allowed_amounts = array_map(static fn($value): float => round((float) $value, 2), (array) ($settings['rewards_amounts'] ?? [])); if (!in_array($amount, $allowed_amounts, true)) { wp_send_json_error(['message' => __('请选择预设的赞赏金额', 'aksrt')], 400); } } $rate_key = 'aksrt_tip_rate_' . $sender_id; if (get_transient($rate_key)) { wp_send_json_error(['message' => __('操作太频繁,请稍后再试', 'aksrt')], 429); } set_transient($rate_key, 1, 3); $result = \AKSRT\Credits\Balance::transfer( $sender_id, $recipient_id, $amount, 'tip_sent', 'tip_received', ['type' => 'tip', 'id' => $post_id], $post_id > 0 ? sprintf(__('赞赏文章 #%d', 'aksrt'), $post_id) : __('用户赞赏', 'aksrt') ); if (is_wp_error($result)) { wp_send_json_error(['message' => $result->get_error_message()], 400); } wp_send_json_success([ 'message' => __('赞赏成功,金额已进入对方余额', 'aksrt'), 'balance' => \AKSRT\Credits\Balance::format((float) $result['sender_balance']), ]); });