| Server IP : 43.141.50.122 / Your IP : 113.219.202.44 Web Server : Apache System : Linux VM-8-5-opencloudos 6.6.34-9.oc9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 19 19:35:45 CST 2024 x86_64 User : www ( 1000) PHP Version : 8.1.27 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /www/wwwroot/www.ucppt.com/wp-content/plugins/wpcom-member-pro/includes/ |
Upload File : |
<?php
namespace WPCOM\Member;
defined('ABSPATH') || exit;
if( ! class_exists( '\WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Card_Items extends \WP_List_Table {
protected $product_id;
function __construct($product_id) {
$this->product_id = $product_id;
parent::__construct([
'singular' => 'card_item',
'plural' => 'card_items',
'screen' => 'card_items'
]);
}
function get_columns() {
return [
'cb' => '<input type="checkbox" />',
'code' => '卡密',
'status' => '状态',
'order_id' => '订单ID',
'sold_at' => '售出时间',
'expire_at' => '过期时间',
'created_at' => '创建时间'
];
}
function prepare_items() {
global $wpdb;
$this->process_bulk_action();
$table = $wpdb->prefix . 'wpcom_card_items';
$where = $wpdb->prepare('WHERE product_id = %d', $this->product_id);
$search = isset($_REQUEST['s']) ? sanitize_text_field($_REQUEST['s']) : '';
if ($search) {
$where .= $wpdb->prepare(" AND code LIKE %s", "%$search%");
}
$status = isset($_GET['status_filter']) ? $_GET['status_filter'] : null;
if ($status !== null && $status !== 'all' && $status !== '') {
$where .= $wpdb->prepare(" AND status = %d", intval($status));
}
$total_items = $wpdb->get_var("SELECT COUNT(*) FROM $table $where");
$per_page = 20;
$paged = max(1, intval($_GET['paged'] ?? 1));
$offset = ($paged - 1) * $per_page;
$orderby = $_GET['orderby'] ?? 'ID';
$order = $_GET['order'] ?? 'DESC';
$items = $wpdb->get_results("SELECT * FROM $table $where ORDER BY $orderby $order LIMIT $offset, $per_page", ARRAY_A);
$this->items = $items ? $items : [];
$this->set_pagination_args([
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil($total_items / $per_page)
]);
}
function get_bulk_actions() {
return [
'delete' => '批量删除'
];
}
function process_bulk_action() {
global $wpdb;
if ( current_user_can( 'manage_options' ) && 'delete' === $this->current_action() ) {
$table = $wpdb->prefix . 'wpcom_card_items';
$nonce = esc_attr( $_REQUEST['_wpnonce'] );
if ( wp_verify_nonce( $nonce, 'bulk-card_items' ) ) {
$ids = isset($_REQUEST['check']) ? $_REQUEST['check'] : [];
if(!empty($ids)) {
$ids = implode( ',', array_map( 'absint', $ids ) );
$wpdb->query("DELETE FROM $table WHERE ID IN($ids)");
}
$redirect = remove_query_arg(['action', 'action2', 'check', '_wpnonce']);
if (!$redirect || strpos($redirect, admin_url()) !== 0) {
$redirect = admin_url('admin.php?page=wpcom-cards-items&product_id=' . $this->product_id);
}
} else if(isset($_GET['id']) && $_GET['id'] && wp_verify_nonce( $nonce, 'delete_card_item_'.$_GET['id'] )) {
$wpdb->delete($table, ['ID' => intval($_GET['id'])]);
$redirect = remove_query_arg(['action', 'id', '_wpnonce']);
if (!$redirect || strpos($redirect, admin_url()) !== 0) {
$redirect = admin_url('admin.php?page=wpcom-cards-items&product_id=' . $this->product_id);
}
}
if(isset($redirect) && $redirect) {
// 更新库存
Cards::update_stock($this->product_id);
echo '<script>window.location.href="' . $redirect . '";</script>';
exit;
}
}
}
function column_cb($item) {
return sprintf('<input type="checkbox" name="check[]" value="%s" />', esc_attr($item['ID']));
}
function column_code($item) {
return esc_html($item['code']);
}
function column_status($item) {
$status = intval($item['status']);
if ($status == 0) {
return '<span style="color:green;">未售出</span>';
} elseif ($status == 1) {
return '<span style="color:#ff8900;">已售出</span>';
} else {
return '<span style="color:#999;">已作废</span>';
}
}
function column_order_id($item) {
if ($item['order_id']) {
$order = Order::get_order($item['order_id']);
if ($order && isset($order->number)) {
$edit_url = admin_url('admin.php?page=wpcom-edit-order&id=' . intval($item['order_id']));
return '<a href="' . esc_url($edit_url) . '" target="_blank">' . esc_html($order->number) . '</a>';
}
}
return '-';
}
function column_sold_at($item) {
return $item['sold_at'] && $item['sold_at'] != '0000-00-00 00:00:00' ? esc_html($item['sold_at']) : '-';
}
function column_expire_at($item) {
return $item['expire_at'] && $item['expire_at'] != '0000-00-00 00:00:00' ? esc_html($item['expire_at']) : '-';
}
function column_created_at($item) {
return $item['created_at'] && $item['created_at'] != '0000-00-00 00:00:00' ? esc_html($item['created_at']) : '-';
}
function column_default($item, $column_name) {
return isset($item[$column_name]) ? esc_html($item[$column_name]) : '';
}
protected function get_default_primary_column_name() {
return 'code';
}
function no_items() {
echo '<div style="text-align:center;padding: 1em;">暂无卡密。</div>';
}
protected function handle_row_actions($item, $column_name, $primary) {
if ($column_name !== $primary) return '';
$edit_url = admin_url('admin.php?page=wpcom-cards-items&product_id=' . $this->product_id . '&action=edit&id=' . $item['ID']);
$delete_url = wp_nonce_url(
admin_url('admin.php?page=wpcom-cards-items&product_id=' . $this->product_id . '&action=delete&id=' . $item['ID']),
'delete_card_item_' . $item['ID']
);
$actions = [
'edit' => '<a href="' . esc_url($edit_url) . '">编辑</a>',
'delete' => '<a href="' . esc_url($delete_url) . '" onclick="return confirm(\'确定要删除该卡密吗?\')">删除</a>',
];
return $this->row_actions($actions);
}
function get_views() {
global $wpdb;
$table = $wpdb->prefix . 'wpcom_card_items';
$base_url = admin_url('admin.php?page=wpcom-cards-items&product_id=' . $this->product_id);
// 统计数量
$where = $wpdb->prepare('WHERE product_id = %d', $this->product_id);
$total = (int)$wpdb->get_var("SELECT COUNT(*) FROM $table $where");
$unsold = (int)$wpdb->get_var("SELECT COUNT(*) FROM $table $where AND status = 0");
$sold = (int)$wpdb->get_var("SELECT COUNT(*) FROM $table $where AND status = 1");
$invalid = (int)$wpdb->get_var("SELECT COUNT(*) FROM $table $where AND status = 2");
$current = isset($_GET['status_filter']) ? intval($_GET['status_filter']) : 'all';
$views = [
'all' => '<a href="' . $base_url . '"' . ($current === 'all' ? ' class="current"' : '') . '>全部 <span class="count">(' . $total . ')</span></a>',
'unsold' => '<a href="' . $base_url . '&status_filter=0"' . ($current === 0 ? ' class="current"' : '') . '>未售出 <span class="count">(' . $unsold . ')</span></a>',
'sold' => '<a href="' . $base_url . '&status_filter=1"' . ($current === 1 ? ' class="current"' : '') . '>已售出 <span class="count">(' . $sold . ')</span></a>',
'invalid' => '<a href="' . $base_url . '&status_filter=2"' . ($current === 2 ? ' class="current"' : '') . '>已作废 <span class="count">(' . $invalid . ')</span></a>',
];
return $views;
}
}