get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$data = $this->table_data();
usort($data, [ & $this, 'sort_data']);
$perPage = 30; //每页30个数据
$currentPage = $this->get_pagenum();
$totalItems = count($data);
$this->set_pagination_args([
'total_items' => $totalItems,
'per_page' => $perPage,
]);
$data = array_slice($data, (($currentPage - 1) * $perPage), $perPage);
$this->_column_headers = [
$columns,
$hidden,
$sortable,
];
$this->items = $data;
}
/**
* Override the parent columns method.
* Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns()
{
$columns = [
'point_id' => 'ID',
'user_id' => '用户ID',
'points' => '金币',
'description' => '描述',
'datetime' => '日期&时间',
'status' => '状态',
'actions' => '操作',
];
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns()
{
return [];
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns()
{
return [
'point_id' => [
'point_id',
false,
],
'user_id' => [
'user_id',
false,
],
'points' => [
'points',
false,
],
'description' => [
'description',
false,
],
'datetime' => [
'datetime',
false,
],
'status' => [
'status',
false,
],
];
}
/**
* Get the table data
*
* @return Array
*/
private function __table_data()
{
$data = [];
$data = GDK_Points::get_points(null, null, null, ARRAY_A);
return $data;
}
/**
* Define what data to show on each column of the table
*
* @param Array $item
* Data
* @param String $column_name
* - Current column name
*
* @return Mixed
*/
public function column_default($item, $column_name)
{
switch ($column_name) {
case 'point_id':
case 'user_id':
case 'description':
case 'points':
case 'datetime':
case 'status':
return $item[$column_name];
break;
case 'actions':
$actions = [
'edit' => sprintf('编辑', $_REQUEST['page'], 'edit', $item['point_id']),
'delete' => sprintf('删除', $_REQUEST['page'], 'delete', $item['point_id']),
];
//Return the title contents
return sprintf('%1$s%2$s', $item[$column_name] ?? "",
$this->row_actions($actions, true)
);
break;
default:
return print_r($item, true);
}
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function __sort_data($a, $b)
{
// Set defaults
$orderby = 'point_id';
$order = 'desc';
// If orderby is set, use this as the sort column
if (!empty($_GET['orderby'])) {
$orderby = $_GET['orderby'];
}
// If order is set use this as the order
if (!empty($_GET['order'])) {
$order = $_GET['order'];
}
$result = strnatcmp($a[$orderby], $b[$orderby]);
if ($order === 'asc') {
return $result;
}
return -$result;
}
}
/**
* GDK_Points Admin class
*/
class GDK_Points_Admin
{
public static function init()
{
add_action('admin_notices', [__CLASS__, 'admin_notices']);
add_action('admin_menu', [__CLASS__, 'admin_menu'], 'manage_options');
}
public static function admin_notices()
{
if (!empty(self::$notices)) {
foreach (self::$notices as $notice) {
echo $notice;
}
}
}
/**
* Adds the admin section.
*/
public static function admin_menu()
{
$admin_page = add_menu_page('金币', '金币', 'manage_options', 'points', [__CLASS__, 'points_menu'], 'dashicons-awards');
}
public static function points_menu()
{
$alert = "";
if (isset($_POST['psearch'])) {
$sdata = trim($_POST['psearch']);
if (preg_match('/E20/', $sdata)) { //order id
global $wpdb;
$point_id = $wpdb->get_row("SELECT point_id FROM " . GDK_Points_Database::points_get_table("users") . " WHERE description = '{$sdata}'", ARRAY_A)['point_id'];
$points = GDK_Points::get_point($point_id);
} elseif (filter_var($sdata, FILTER_VALIDATE_EMAIL)) { //email
$user = get_user_by('email', $sdata);
$points = GDK_Points::get_points_by_user($user->ID);
$k[] = '
用户ID:' . $user->ID . ' 总金币为:' . GDK_Points::get_user_total_points($user->ID) . '
';
} else { //userid
$points = GDK_Points::get_points_by_user($sdata);
$k[] = '用户ID:' . $sdata . ' 总金币为:' . GDK_Points::get_user_total_points($sdata) . '
';
}
if (is_array($points)) {
foreach ($points as $point) {
$k[] = '金币:' . $point->points . ' 描述:' . $point->description . ' 日期:' . $point->datetime . '
';
}
} else {
$k[] = '金币:' . $points->points . ' 描述:' . $points->description . ' 日期:' . $points->datetime . '
';
}
$alert = implode(" ", $k);
}
if (isset($_POST['save']) && isset($_POST['action'])) {
if ($_POST['action'] == "edit") {
$point_id = isset($_POST['point_id']) ? intval($_POST['point_id']) : null;
$points = GDK_Points::get_point($point_id);
$data = array();
if (isset($_POST['user_mail'])) {
$data['user_mail'] = $_POST['user_mail'];
}
if (isset($_POST['user_id'])) {
$data['user_id'] = $_POST['user_id'];
}
if (isset($_POST['datetime'])) {
$data['datetime'] = $_POST['datetime'];
}
if (isset($_POST['description'])) {
$data['description'] = $_POST['description'];
}
if (isset($_POST['status'])) {
$data['status'] = $_POST['status'];
}
if (isset($_POST['points'])) {
$data['points'] = $_POST['points'];
}
if ($points) { // 编辑金币
GDK_Points::update_points($point_id, $data);
} else { // 增加金币
if (isset($_POST['user_mail'])) { //如果输入邮箱的话
$usermail = $data['user_mail'];
$user = get_user_by('email', $usermail);
$userid = $user->ID;
$username = $user->display_name;
}
if (isset($_POST['user_id'])) { //如果输入用户ID的话
$user = get_user_by('id', $data['user_id']);
$usermail = $user->user_email;
$userid = $data['user_id'];
$username = $user->display_name;
}
GDK_Points::set_points($_POST['points'], $userid, $data);
$message = '金币金额调整通知
尊敬的' . $username . ',您好!
您的金币金额被管理员调整,请查收!
您的金币详细情况 |
---|
用户名 | ' . $username . ' |
调整金币 | ' . $_POST['points'] . ' |
金币总额 | ' . GDK_Points::get_user_total_points($userid, 'accepted') . ' |
如果您的金币金额有异常,请您在第一时间和我们取得联系哦,联系邮箱:' . get_bloginfo('admin_email') . '
';
$headers = "Content-Type:text/html;charset=UTF-8\n";
wp_mail($usermail, 'Hi,' . $username . ',金币账户金额增加通知!', $message, $headers);
}
}
$alert = "金币已更新";
}
if (isset($_GET["action"])) {
$action = $_GET["action"];
if ($action !== null) {
switch ($action) {
case 'edit':
if (isset($_GET['point_id']) && ($_GET['point_id'] !== null)) {
return self::points_admin_points_edit(intval($_GET['point_id']));
} else {
return self::points_admin_points_edit();
}
break;
case 'delete':
if ($_GET['point_id'] !== null) {
if (current_user_can('administrator')) {
GDK_Points::remove_points($_GET['point_id']);
global $wpdb;
$wcu_sql = "DELETE FROM " . GDK_Points_Database::points_get_table("users") . " WHERE status = 'removed'";
$wpdb->query($wcu_sql);
$alert = "金币已删除";
}
}
break;
}
}
}
if ($alert != "") {
echo '' . $alert . '
';
}
$current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$cancel_url = remove_query_arg('point_id', remove_query_arg('action', $current_url));
$current_url = remove_query_arg('point_id', $current_url);
$current_url = remove_query_arg('action', $current_url);
$exampleListTable = new GDK_Points_List_Table();
$exampleListTable->prepare_items();
?>
金币管理
添加金币
tbody#the-list tr:hover{background:rgba(132,219,162,.61)}';
$exampleListTable->display(); ?>
user_id;
$num_points = $points->points;
$description = $points->description;
$datetime = $points->datetime;
$status = $points->status;
}
} else {
$user_id = "";
$num_points = 0;
$description = "ADD";
$datetime = "";
$status = 'accepted';
}
if (empty($point_id)) {
$pointsclass = 'newpoint';
} else {
$pointsclass = 'editpoint';
}
$output .= '';
$output .= '
';
if (empty($point_id)) {
$output .= '新金币';
} else {
$output .= '编辑金币';
}
$output .= '
';
$output .= '
';
$output .= '
';
echo $output;
}
}