177 lines
6.9 KiB
PHP
177 lines
6.9 KiB
PHP
<?php
|
|
/**
|
|
* Native WP admin: AKSRT_Admin_Pay_Income.
|
|
*
|
|
* Extracted from AKSRT_Admin_Pay_Menus.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Admin_Pay_Income {
|
|
|
|
public const CAPABILITY = 'manage_options';
|
|
public const PAGE_SLUG = 'aksrt_income';
|
|
|
|
private static $instance = null;
|
|
|
|
public static function get_instance(): self {
|
|
if (null === self::$instance) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function init(): void {
|
|
if (!is_admin()) {
|
|
return;
|
|
}
|
|
add_action('admin_post_aksrt_pay_settle_income', [$this, 'handle_settle_income']);
|
|
}
|
|
|
|
public function render_income_page(): void {
|
|
$this->ensure_cap();
|
|
$this->render_header(__('创作分成', 'aksrt'));
|
|
|
|
if (!class_exists('AKSRT_Revenue')) {
|
|
echo '<div class="notice notice-error"><p>' . esc_html__('分成模块未初始化。', 'aksrt') . '</p></div>';
|
|
$this->render_footer();
|
|
return;
|
|
}
|
|
|
|
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : '';
|
|
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
|
|
$per_page = 20;
|
|
$svc = AKSRT_Revenue::get_instance();
|
|
$stats = $svc->get_stats();
|
|
|
|
echo '<p>' . esc_html(sprintf(
|
|
/* translators: 1: total 2: pending 3: settled */
|
|
__('合计 %1$s · 待结算 %2$s · 已结算 %3$s', 'aksrt'),
|
|
number_format((float) ($stats['total'] ?? 0), 2),
|
|
number_format((float) ($stats['pending'] ?? 0), 2),
|
|
number_format((float) ($stats['settled'] ?? 0), 2)
|
|
)) . '</p>';
|
|
|
|
echo '<form method="get" style="margin:12px 0;">';
|
|
echo '<input type="hidden" name="page" value="aksrt_income" />';
|
|
echo '<select name="status">';
|
|
foreach (['' => __('全部状态', 'aksrt'), 'pending' => __('待结算', 'aksrt'), 'settled' => __('已结算', 'aksrt')] as $key => $label) {
|
|
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
|
|
}
|
|
echo '</select> ';
|
|
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
|
|
echo '</form>';
|
|
|
|
$result = $svc->get_list([
|
|
'status' => $status,
|
|
'limit' => $per_page,
|
|
'offset' => ($page_num - 1) * $per_page,
|
|
]);
|
|
$items = $result['items'] ?? [];
|
|
$total = (int) ($result['total'] ?? 0);
|
|
$pages = max(1, (int) ($result['max_pages'] ?? 1));
|
|
|
|
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
|
|
echo '<th>ID</th><th>' . esc_html__('用户', 'aksrt') . '</th><th>' . esc_html__('订单号', 'aksrt') . '</th><th>' . esc_html__('金额', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
|
|
echo '</tr></thead><tbody>';
|
|
if (!$items) {
|
|
echo '<tr><td colspan="7">' . esc_html__('暂无分成记录', 'aksrt') . '</td></tr>';
|
|
}
|
|
foreach ($items as $row) {
|
|
$id = (int) ($row['id'] ?? 0);
|
|
$user_id = (int) ($row['user_id'] ?? 0);
|
|
$user = $user_id > 0 ? get_userdata($user_id) : null;
|
|
$st = (string) ($row['status'] ?? '');
|
|
echo '<tr>';
|
|
echo '<td>' . esc_html((string) $id) . '</td>';
|
|
echo '<td>' . ($user ? esc_html($user->display_name) : esc_html((string) $user_id)) . '</td>';
|
|
echo '<td><code>' . esc_html((string) ($row['order_number'] ?? '')) . '</code></td>';
|
|
echo '<td>' . esc_html(number_format((float) ($row['amount'] ?? 0), 2)) . '</td>';
|
|
echo '<td>' . esc_html($st) . '</td>';
|
|
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
|
|
echo '<td>';
|
|
if ($st === 'pending') {
|
|
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
|
|
echo '<input type="hidden" name="action" value="aksrt_pay_settle_income" />';
|
|
echo '<input type="hidden" name="revenue_id" value="' . esc_attr((string) $id) . '" />';
|
|
wp_nonce_field('aksrt_pay_settle_income_' . $id);
|
|
submit_button(__('结算', 'aksrt'), 'small', '', false);
|
|
echo '</form>';
|
|
} else {
|
|
echo '—';
|
|
}
|
|
echo '</td></tr>';
|
|
}
|
|
echo '</tbody></table>';
|
|
$this->render_pagination($pages, $page_num);
|
|
$this->render_footer();
|
|
}
|
|
|
|
public function handle_settle_income(): void {
|
|
if (!current_user_can(self::CAPABILITY)) {
|
|
wp_die(esc_html__('权限不足', 'aksrt'));
|
|
}
|
|
$id = isset($_POST['revenue_id']) ? (int) $_POST['revenue_id'] : 0;
|
|
check_admin_referer('aksrt_pay_settle_income_' . $id);
|
|
$ok = $id > 0 && class_exists('AKSRT_Revenue') && AKSRT_Revenue::get_instance()->settle($id);
|
|
wp_safe_redirect($this->page_url('aksrt_income', ['aksrt_pay_msg' => $ok ? 'income_settled' : 'income_failed']));
|
|
exit;
|
|
}
|
|
|
|
|
|
|
|
private function ensure_cap(): void {
|
|
if (!current_user_can(self::CAPABILITY)) {
|
|
wp_die(esc_html__('您没有权限访问此页面。', 'aksrt'));
|
|
}
|
|
}
|
|
|
|
private function render_header(string $title): void {
|
|
echo '<div class="wrap">';
|
|
echo '<h1>' . esc_html($title) . '</h1>';
|
|
$this->render_flash();
|
|
}
|
|
|
|
private function render_footer(): void {
|
|
echo '</div>';
|
|
}
|
|
|
|
private function render_flash(): void {
|
|
if (empty($_GET['aksrt_pay_msg'])) {
|
|
return;
|
|
}
|
|
$code = sanitize_key((string) wp_unslash($_GET['aksrt_pay_msg']));
|
|
$map = [
|
|
'income_settled' => __('分成已结算。', 'aksrt'),
|
|
'income_failed' => __('分成结算失败。', 'aksrt'),
|
|
];
|
|
if (!isset($map[$code])) {
|
|
return;
|
|
}
|
|
$class = str_contains($code, 'failed') ? 'notice-error' : 'notice-success';
|
|
echo '<div class="notice ' . esc_attr($class) . ' is-dismissible"><p>' . esc_html($map[$code]) . '</p></div>';
|
|
}
|
|
|
|
private function page_url(string $slug, array $args = []): string {
|
|
return add_query_arg(array_merge(['page' => $slug], $args), admin_url('admin.php'));
|
|
}
|
|
|
|
private function render_pagination(int $pages, int $page_num): void {
|
|
if ($pages <= 1) {
|
|
return;
|
|
}
|
|
echo '<div class="tablenav"><div class="tablenav-pages">';
|
|
echo paginate_links([
|
|
'base' => add_query_arg('paged', '%#%'),
|
|
'format' => '',
|
|
'prev_text' => '«',
|
|
'next_text' => '»',
|
|
'total' => $pages,
|
|
'current' => $page_num,
|
|
]);
|
|
echo '</div></div>';
|
|
}
|
|
}
|