get_columns(); $sortable = $this->get_sortable_columns(); $data = GDK_Points::get_points(null, null, null, ARRAY_A); 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, $sortable, ]; $this->items = $data; } /** * Override the parent columns method. * Defines the columns to use in your listing table. * * @return array */ public function get_columns() { return [ 'point_id' => 'ID', 'user_id' => '用户ID', 'points' => '金币', 'description' => '描述', 'datetime' => '日期&时间', 'status' => '状态', 'actions' => '操作', ]; } /** * 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, ], ]; } /** * 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. * * @param mixed $a * @param mixed $b * * @return mixed */ private function sort_data($a, $b) { // Set defaults $orderby = 'point_id'; $order = 'desc'; //desc asc // 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 ('asc' === $order) { 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 (preg_match('/(D|d)/', $sdata)) {// description $data = substr($sdata, 1); global $wpdb; $points = $wpdb->get_results('SELECT * FROM '.GDK_Points_Database::points_get_table('users')." WHERE description = '{$data}'"); //var_dump($points); $k[] = '
文章ID:'.$data.'    ||   文章名为:'.get_post($data)->post_title.'

'; } 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) { $userid = $point->user_id; $user_name = get_user_by('id', $userid)->display_name; $k[] = '
用户ID:'.$userid.'   ||   金币:'.$point->points.'    ||   描述:'.$point->description.'    ||   日期:'.$point->datetime.'   ||   用户名:'.$user_name.'
'; } } else { $k[] = '
用户ID:'.$point->user_id.'  金币:'.$points->points.'   描述:'.$points->description.'   日期:'.$points->datetime.'
'; } $alert = implode(' ', $k); } if (isset($_POST['save'], $_POST['action'])) { if ('edit' == $_POST['action']) { $point_id = isset($_POST['point_id']) ? intval($_POST['point_id']) : null; $points = GDK_Points::get_point($point_id); $data = []; 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); $mail_title = $username.'您好,金币增加通知'; $mail_cotent = '

您的金币金额被管理员调整,请查收!

如果您的金币金额有异常,请您在第一时间和我们取得联系哦,联系邮箱:'.get_bloginfo('admin_email').'

'; $message = gdk_mail_temp($mail_title, $mail_cotent, home_url(), get_bloginfo('name')); $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 (null !== $action) { switch ($action) { case 'edit': if (isset($_GET['point_id']) && (null !== $_GET['point_id'])) { return self::points_admin_points_edit(intval($_GET['point_id'])); } return self::points_admin_points_edit(); break; case 'delete': if (null !== $_GET['point_id']) { 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 .= '
'; if ($point_id) { $output .= sprintf('', intval($point_id)); } $output .= ''; $output .= '

'; $output .= ''; $output .= '

'; $output .= '

'; $output .= ''; $output .= '

'; $output .= '

'; $output .= ''; $output .= '

'; $output .= '

'; $output .= ''; $output .= '

'; $output .= '

'; $output .= ''; $output .= '

'; $status_descriptions = [ 'accepted' => '正常', 'pending' => '待审', 'rejected' => '驳回', ]; $output .= '

'; $output .= ''; $output .= '

'; $output .= wp_nonce_field('save', 'points-nonce', true, false); $output .= sprintf('', '保存'); $output .= ' '; $output .= sprintf('%s', $cancel_url, $saved ? '返回' : '取消'); $output .= '
'; $output .= '
'; $output .= '
'; echo $output; } }