2020-02-02 04:13:58 +01:00
< ? php
/**
2021-10-10 15:48:53 +02:00
* GDK_Points Table class .
2020-02-02 04:13:58 +01:00
*/
// WP_List_Table is not loaded automatically so we need to load it in our application
2020-03-15 14:29:06 +01:00
if ( ! class_exists ( 'WP_List_Table' )) {
2021-10-10 15:48:53 +02:00
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ;
2020-02-02 04:13:58 +01:00
}
2020-03-15 14:29:06 +01:00
class GDK_Points_List_Table extends WP_List_Table
{
/**
2021-10-10 15:48:53 +02:00
* Prepare the items for the table to process .
2020-03-15 14:29:06 +01:00
*/
public function prepare_items ()
{
2021-10-10 15:48:53 +02:00
$columns = $this -> get_columns ();
2020-03-15 14:29:06 +01:00
$sortable = $this -> get_sortable_columns ();
2021-10-10 15:48:53 +02:00
$data = GDK_Points :: get_points ( null , null , null , ARRAY_A );
usort ( $data , [
2020-04-01 09:52:24 +02:00
& $this ,
2021-10-10 15:48:53 +02:00
'sort_data' ,
]);
$perPage = 30 ; //每页30个数据
2020-03-15 14:29:06 +01:00
$currentPage = $this -> get_pagenum ();
2021-10-10 15:48:53 +02:00
$totalItems = count ( $data );
2020-03-15 14:29:06 +01:00
$this -> set_pagination_args ([
'total_items' => $totalItems ,
2021-10-10 15:48:53 +02:00
'per_page' => $perPage ,
2020-03-15 14:29:06 +01:00
]);
$data = array_slice ( $data , (( $currentPage - 1 ) * $perPage ), $perPage );
$this -> _column_headers = [
$columns ,
$sortable ,
];
$this -> items = $data ;
}
/**
* Override the parent columns method .
2021-10-10 15:48:53 +02:00
* Defines the columns to use in your listing table .
2020-03-15 14:29:06 +01:00
*
2021-10-10 15:48:53 +02:00
* @ return array
2020-03-15 14:29:06 +01:00
*/
public function get_columns ()
{
2021-10-10 15:48:53 +02:00
return [
'point_id' => 'ID' ,
'user_id' => '用户ID' ,
'points' => '金币' ,
2020-03-15 14:29:06 +01:00
'description' => '描述' ,
2021-10-10 15:48:53 +02:00
'datetime' => '日期&时间' ,
'status' => '状态' ,
'actions' => '操作' ,
2020-03-15 14:29:06 +01:00
];
}
/**
2021-10-10 15:48:53 +02:00
* Define the sortable columns .
2020-03-15 14:29:06 +01:00
*
2021-10-10 15:48:53 +02:00
* @ return array
2020-03-15 14:29:06 +01:00
*/
public function get_sortable_columns ()
{
return [
2021-10-10 15:48:53 +02:00
'point_id' => [
2020-03-15 14:29:06 +01:00
'point_id' ,
false ,
],
2021-10-10 15:48:53 +02:00
'user_id' => [
2020-03-15 14:29:06 +01:00
'user_id' ,
false ,
],
2021-10-10 15:48:53 +02:00
'points' => [
2020-03-15 14:29:06 +01:00
'points' ,
false ,
],
'description' => [
'description' ,
false ,
],
2021-10-10 15:48:53 +02:00
'datetime' => [
2020-03-15 14:29:06 +01:00
'datetime' ,
false ,
],
2021-10-10 15:48:53 +02:00
'status' => [
2020-03-15 14:29:06 +01:00
'status' ,
false ,
],
];
}
/**
2021-10-10 15:48:53 +02:00
* Define what data to show on each column of the table .
2020-03-15 14:29:06 +01:00
*
2021-10-10 15:48:53 +02:00
* @ param array $item
* Data
* @ param string $column_name
* - Current column name
2020-03-15 14:29:06 +01:00
*
2021-10-10 15:48:53 +02:00
* @ return mixed
2020-03-15 14:29:06 +01:00
*/
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 ];
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
break ;
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
case 'actions' :
$actions = [
2021-10-10 15:48:53 +02:00
'edit' => sprintf ( '<a href="?page=%s&action=%s&point_id=%s">编辑</a>' , $_REQUEST [ 'page' ], 'edit' , $item [ 'point_id' ]),
2020-03-15 14:29:06 +01:00
'delete' => sprintf ( '<a href="?page=%s&action=%s&point_id=%s">删除</a>' , $_REQUEST [ 'page' ], 'delete' , $item [ 'point_id' ]),
];
//Return the title contents
2021-10-10 15:48:53 +02:00
return sprintf (
'%1$s%2$s' ,
$item [ $column_name ] ? ? '' ,
2020-03-15 14:29:06 +01:00
$this -> row_actions ( $actions , true )
);
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
break ;
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
default :
return print_r ( $item , true );
}
}
/**
2021-10-10 15:48:53 +02:00
* Allows you to sort the data by the variables set in the $_GET .
2020-03-15 14:29:06 +01:00
*
2021-10-10 15:48:53 +02:00
* @ param mixed $a
* @ param mixed $b
*
* @ return mixed
2020-03-15 14:29:06 +01:00
*/
2020-04-01 09:52:24 +02:00
private function sort_data ( $a , $b )
2020-03-15 14:29:06 +01:00
{
// Set defaults
$orderby = 'point_id' ;
2021-10-10 15:48:53 +02:00
$order = 'desc' ; //desc asc
2020-03-15 14:29:06 +01:00
// 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 ]);
2021-10-10 15:48:53 +02:00
if ( 'asc' === $order ) {
2020-03-15 14:29:06 +01:00
return $result ;
}
return - $result ;
}
2020-02-02 04:13:58 +01:00
}
/**
2021-10-10 15:48:53 +02:00
* GDK_Points Admin class .
2020-02-02 04:13:58 +01:00
*/
2020-03-15 14:29:06 +01:00
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 )) {
2021-10-21 12:43:29 +02:00
foreach (( array ) self :: $notices as $notice ) {
2020-03-15 14:29:06 +01:00
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 ()
{
2021-10-10 15:48:53 +02:00
$alert = '' ;
2020-03-15 14:29:06 +01:00
if ( isset ( $_POST [ 'psearch' ])) {
$sdata = trim ( $_POST [ 'psearch' ]);
2021-10-10 15:48:53 +02:00
if ( preg_match ( '/E20/' , $sdata )) {
2020-04-01 07:28:27 +02:00
//order id
2020-03-15 14:29:06 +01:00
global $wpdb ;
2021-10-10 15:48:53 +02:00
$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 [] = '<div style="margin-bottom:10px;">文章ID: ' . $data . ' || 文章名为:' . get_post ( $data ) -> post_title . '</div><hr />' ;
} elseif ( filter_var ( $sdata , FILTER_VALIDATE_EMAIL )) {
2020-04-01 07:28:27 +02:00
//email
2021-10-10 15:48:53 +02:00
$user = get_user_by ( 'email' , $sdata );
2020-03-15 14:29:06 +01:00
$points = GDK_Points :: get_points_by_user ( $user -> ID );
2021-10-10 15:48:53 +02:00
$k [] = '<div style="margin-bottom:10px;">用户ID: ' . $user -> ID . ' || 总金币为:' . GDK_Points :: get_user_total_points ( $user -> ID ) . '</div>' ;
2020-04-01 07:28:27 +02:00
} else {
//userid
2020-03-15 14:29:06 +01:00
$points = GDK_Points :: get_points_by_user ( $sdata );
2021-10-10 15:48:53 +02:00
$k [] = '<div style="margin-bottom:10px;">用户ID: ' . $sdata . ' || 总金币为:' . GDK_Points :: get_user_total_points ( $sdata ) . '</div>' ;
2020-03-15 14:29:06 +01:00
}
if ( is_array ( $points )) {
2021-10-21 12:43:29 +02:00
foreach (( array ) $points as $point ) {
2021-10-10 15:48:53 +02:00
$userid = $point -> user_id ;
$user_name = get_user_by ( 'id' , $userid ) -> display_name ;
$k [] = '<div style="margin-bottom:5px;">用户ID: ' . $userid . ' || 金币:' . $point -> points . ' || 描述:' . $point -> description . ' || 日期:' . $point -> datetime . ' || 用户名:' . $user_name . '</div>' ;
2020-03-15 14:29:06 +01:00
}
} else {
2021-10-10 15:48:53 +02:00
$k [] = '<div style="margin-bottom:5px;">用户ID: ' . $point -> user_id . ' 金币:' . $points -> points . ' 描述:' . $points -> description . ' 日期:' . $points -> datetime . '</div>' ;
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
$alert = implode ( ' ' , $k );
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
if ( isset ( $_POST [ 'save' ], $_POST [ 'action' ])) {
if ( 'edit' == $_POST [ 'action' ]) {
2020-03-15 14:29:06 +01:00
$point_id = isset ( $_POST [ 'point_id' ]) ? intval ( $_POST [ 'point_id' ]) : null ;
2021-10-10 15:48:53 +02:00
$points = GDK_Points :: get_point ( $point_id );
$data = [];
2020-03-15 14:29:06 +01:00
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' ];
}
2020-04-01 07:28:27 +02:00
if ( $points ) {
// 编辑金币
2020-03-15 14:29:06 +01:00
GDK_Points :: update_points ( $point_id , $data );
2020-04-01 07:28:27 +02:00
} else {
// 增加金币
2020-03-15 14:29:06 +01:00
if ( isset ( $_POST [ 'user_mail' ])) { //如果输入邮箱的话
$usermail = $data [ 'user_mail' ];
2021-10-10 15:48:53 +02:00
$user = get_user_by ( 'email' , $usermail );
$userid = $user -> ID ;
2020-03-15 14:29:06 +01:00
$username = $user -> display_name ;
}
2020-04-01 07:28:27 +02:00
if ( isset ( $_POST [ 'user_id' ])) {
//如果输入用户ID的话
2021-10-10 15:48:53 +02:00
$user = get_user_by ( 'id' , $data [ 'user_id' ]);
2020-03-15 14:29:06 +01:00
$usermail = $user -> user_email ;
2021-10-10 15:48:53 +02:00
$userid = $data [ 'user_id' ];
2020-03-15 14:29:06 +01:00
$username = $user -> display_name ;
}
GDK_Points :: set_points ( $_POST [ 'points' ], $userid , $data );
2021-10-10 15:48:53 +02:00
$mail_title = $username . '您好,金币增加通知' ;
2020-04-01 09:52:24 +02:00
$mail_cotent = ' < p > 您的金币金额被管理员调整,请查收! </ p >
< ul >
2021-10-10 15:48:53 +02:00
< li > 用户名: '.$username.' </ li >
< li > 增加金币: '.$_POST[' points '].' </ li >
< li > 金币总额: '.GDK_Points::get_user_total_points($userid, ' accepted ').' </ li >
2020-04-01 09:52:24 +02:00
</ ul >
2021-10-10 15:48:53 +02:00
< p > 如果您的金币金额有异常,请您在第一时间和我们取得联系哦,联系邮箱: '.get_bloginfo(' admin_email ').' </ p > ' ;
$message = gdk_mail_temp ( $mail_title , $mail_cotent , home_url (), get_bloginfo ( 'name' ));
2020-03-15 14:29:06 +01:00
$headers = " Content-Type:text/html;charset=UTF-8 \n " ;
2021-10-10 15:48:53 +02:00
wp_mail ( $usermail , 'Hi,' . $username . ',金币账户金额变动通知!' , $message , $headers );
2020-03-15 14:29:06 +01:00
}
}
2021-10-10 15:48:53 +02:00
$alert = '金币已更新' ;
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
if ( isset ( $_GET [ 'action' ])) {
$action = $_GET [ 'action' ];
if ( null !== $action ) {
2020-03-15 14:29:06 +01:00
switch ( $action ) {
case 'edit' :
2021-10-10 15:48:53 +02:00
if ( isset ( $_GET [ 'point_id' ]) && ( null !== $_GET [ 'point_id' ])) {
2020-03-15 14:29:06 +01:00
return self :: points_admin_points_edit ( intval ( $_GET [ 'point_id' ]));
}
2021-10-10 15:48:53 +02:00
return self :: points_admin_points_edit ();
2020-03-15 14:29:06 +01:00
break ;
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
case 'delete' :
2021-10-10 15:48:53 +02:00
if ( null !== $_GET [ 'point_id' ]) {
2020-03-15 14:29:06 +01:00
if ( current_user_can ( 'administrator' )) {
GDK_Points :: remove_points ( $_GET [ 'point_id' ]);
global $wpdb ;
2021-10-10 15:48:53 +02:00
$wcu_sql = 'DELETE FROM ' . GDK_Points_Database :: points_get_table ( 'users' ) . " WHERE status = 'removed' " ;
2020-03-15 14:29:06 +01:00
$wpdb -> query ( $wcu_sql );
2021-10-10 15:48:53 +02:00
$alert = '金币已删除' ;
2020-03-15 14:29:06 +01:00
}
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
break ;
}
}
}
2021-10-10 15:48:53 +02:00
if ( '' != $alert ) {
echo '<div style="background-color: #ffffe0;border: 1px solid #993;padding: 1em;margin-right: 1em;">' . $alert . '</div>' ;
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
$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 );
2020-03-15 14:29:06 +01:00
$exampleListTable = new GDK_Points_List_Table ();
2021-10-10 15:48:53 +02:00
$exampleListTable -> prepare_items (); ?>
< div class = " wrap " >
< h2 > 金币管理 </ h2 >
< span class = " manage add " >
< a class = " add button "
href = " <?php echo esc_url(add_query_arg('action', 'edit', $current_url )); ?> "
title = " 点击手动添加金币 " > 添加金币 </ a >
</ span >
< form method = " POST " style = " float:right; " >
< input size = " 40 " placeholder = " 搜索用户ID/用户邮箱/订单号/D文章ID " type = " search " name = " psearch " value = " " />
</ form >
< ? php echo '<style type="text/css">tbody#the-list tr:hover{background:rgba(132,219,162,.61)}</style>' ;
2020-03-15 14:29:06 +01:00
$exampleListTable -> display (); ?>
2021-10-10 15:48:53 +02:00
</ div >
< ? php
}
2020-03-15 14:29:06 +01:00
public static function points_admin_points_edit ( $point_id = null )
{
global $wpdb ;
$output = '' ;
if ( ! current_user_can ( 'administrator' )) {
wp_die ( 'Access denied.' );
}
2021-10-10 15:48:53 +02:00
$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 ));
2020-03-15 14:29:06 +01:00
$current_url = remove_query_arg ( 'point_id' , $current_url );
$current_url = remove_query_arg ( 'action' , $current_url );
$saved = false ; // temporal
2021-10-10 15:48:53 +02:00
if ( null !== $point_id ) {
2020-03-15 14:29:06 +01:00
$points = GDK_Points :: get_point ( $point_id );
2021-10-10 15:48:53 +02:00
if ( null !== $points ) {
$user_id = $points -> user_id ;
$num_points = $points -> points ;
2020-03-15 14:29:06 +01:00
$description = $points -> description ;
2021-10-10 15:48:53 +02:00
$datetime = $points -> datetime ;
$status = $points -> status ;
2020-03-15 14:29:06 +01:00
}
} else {
2021-10-10 15:48:53 +02:00
$user_id = '' ;
$num_points = 0 ;
$description = 'ADD' ;
$datetime = '' ;
$status = 'accepted' ;
2020-03-15 14:29:06 +01:00
}
if ( empty ( $point_id )) {
$pointsclass = 'newpoint' ;
} else {
$pointsclass = 'editpoint' ;
}
2021-10-10 15:48:53 +02:00
$output .= '<div class="points ' . $pointsclass . '">' ;
2020-03-15 14:29:06 +01:00
$output .= '<h2>' ;
if ( empty ( $point_id )) {
$output .= '新金币' ;
} else {
$output .= '编辑金币' ;
}
$output .= '</h2>' ;
2021-10-10 15:48:53 +02:00
$output .= '<form id="points" action="' . $current_url . '" method="post">' ;
2020-03-15 14:29:06 +01:00
$output .= '<div>' ;
if ( $point_id ) {
$output .= sprintf ( '<input type="hidden" name="point_id" value="%d" />' , intval ( $point_id ));
}
$output .= '<input type="hidden" name="action" value="edit" />' ;
$output .= '<p class="usermail">' ;
$output .= '<label>' ;
$output .= '<span class="title">用户邮箱</span>' ;
$output .= ' ' ;
$output .= sprintf ( '<input type="text" name="user_mail" value="%s" />' , $user_mail );
$output .= ' ' ;
$output .= '<span class="description">用户在网站的注册邮箱</span>' ;
$output .= '</label>' ;
$output .= '</p>' ;
$output .= '<p class="userid">' ;
$output .= '<label>' ;
$output .= '<span class="title">用户ID</span>' ;
$output .= ' ' ;
$output .= sprintf ( '<input type="text" name="user_id" value="%s" />' , $user_id );
$output .= ' ' ;
$output .= '<span class="description">输入用户ID, 与用户邮箱勿冲突</span>' ;
$output .= '</label>' ;
$output .= '</p>' ;
$output .= '<p>' ;
$output .= '<label>' ;
$output .= '<span class="title">日期&时间</span>' ;
$output .= ' ' ;
$output .= sprintf ( '<input type="text" name="datetime" value="%s" id="datetimepicker" />' , esc_attr ( $datetime ));
$output .= ' ' ;
$output .= '<span class="description">格式 : YYYY-MM-DD HH:MM:SS【可忽略, 自动生成】</span>' ;
$output .= '</label>' ;
$output .= '</p>' ;
$output .= '<p>' ;
$output .= '<label>' ;
$output .= '<span class="title">描述</span>' ;
$output .= '<br>' ;
$output .= '<textarea name="description">' ;
$output .= stripslashes ( $description );
$output .= '</textarea>' ;
$output .= '</label>' ;
$output .= '</p>' ;
$output .= '<p>' ;
$output .= '<label>' ;
$output .= '<span class="title">金币</span>' ;
$output .= ' ' ;
$output .= sprintf ( '<input type="text" name="points" value="%s" />' , esc_attr ( $num_points ));
$output .= '</label>' ;
$output .= '</p>' ;
2021-10-10 15:48:53 +02:00
$status_descriptions = [
2020-03-15 14:29:06 +01:00
'accepted' => '正常' ,
2021-10-10 15:48:53 +02:00
'pending' => '待审' ,
2020-03-15 14:29:06 +01:00
'rejected' => '驳回' ,
2021-10-10 15:48:53 +02:00
];
2020-03-15 14:29:06 +01:00
$output .= '<p>' ;
$output .= '<label>' ;
$output .= '<span class="title">状态</span>' ;
$output .= ' ' ;
$output .= '<select name="status">' ;
2021-10-21 12:43:29 +02:00
foreach (( array ) $status_descriptions as $key => $label ) {
2020-03-15 14:29:06 +01:00
$selected = $key == $status ? ' selected="selected" ' : '' ;
2021-10-10 15:48:53 +02:00
$output .= '<option ' . $selected . ' value="' . esc_attr ( $key ) . '">' . $label . '</option>' ;
2020-03-15 14:29:06 +01:00
}
$output .= '</select>' ;
$output .= '</label>' ;
$output .= '</p>' ;
$output .= wp_nonce_field ( 'save' , 'points-nonce' , true , false );
$output .= sprintf ( '<input class="button" type="submit" name="save" value="%s"/>' , '保存' );
$output .= ' ' ;
$output .= sprintf ( '<a class="cancel" href="%s">%s</a>' , $cancel_url , $saved ? '返回' : '取消' );
$output .= '</div>' ;
$output .= '</form>' ;
$output .= '</div>' ;
echo $output ;
}
2021-10-10 15:48:53 +02:00
}