-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbk_knet_trans_grid.php
368 lines (330 loc) · 16.2 KB
/
cbk_knet_trans_grid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
defined( 'ABSPATH' ) || exit;
if(!class_exists('WP_List_Table')){
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
if(!class_exists("Alnazer_Cbk_KNET_Trans_Grid")){
/**
* Class Alnazer_Cbk_KNET_Trans_Grid
*/
class Alnazer_Cbk_KNET_Trans_Grid extends WP_List_Table
{
public $table;
public $db;
public function __construct($args = array())
{
global $wpdb;
parent::__construct( [
'singular' => __( 'CBK_KNET_List', "cbk_knet" ), //singular name of the listed records
'plural' => __( 'CBK_KNET_List', "cbk_knet" ), //plural name of the listed records
'ajax' => false //should this table support ajax?
]);
$this->db = $wpdb;
$this->table = $this->db->prefix.Alnazer_CBK_KNET_TABLE;
}
private function filter_query(){
$query = "";
if(isset($_REQUEST["order_id"]) && !empty($_REQUEST["order_id"])){
$order_id = sanitize_text_field($_REQUEST["order_id"]);
$query .=" AND `order_id` = $order_id";
}
if(isset($_REQUEST["status"]) && !empty($_REQUEST["status"])){
$status = sanitize_text_field($_REQUEST["status"]);
$query .=" AND `status` = '$status'";
}
if(isset($_REQUEST["result"]) && !empty($_REQUEST["result"])){
$result = sanitize_text_field($_REQUEST["result"]);
$query .=" AND `result` LIKE '%$result%'";
}
if(isset($_REQUEST["amount"]) && !empty($_REQUEST["amount"])){
$amount = sanitize_text_field($_REQUEST["amount"]);
$query .=" AND `amount` = $amount";
}
if(isset($_REQUEST["payment_id"]) && !empty($_REQUEST["payment_id"])){
$payment_id = sanitize_text_field($_REQUEST["payment_id"]);
$query .=" AND `payment_id` = $payment_id";
}
if(isset($_REQUEST["track_id"]) && !empty($_REQUEST["track_id"])){
$track_id = sanitize_text_field($_REQUEST["track_id"]);
$query .=" AND `track_id` = $track_id";
}
if(isset($_REQUEST["tran_id"]) && !empty($_REQUEST["tran_id"])){
$tran_id = sanitize_text_field($_REQUEST["tran_id"]);
$query .=" AND `tran_id` = $tran_id";
}
if(isset($_REQUEST["ref_id"]) && !empty($_REQUEST["ref_id"])){
$ref_id = sanitize_text_field($_REQUEST["ref_id"]);
$query .=" AND `ref_id` = $ref_id";
}
if(isset($_REQUEST["pay_id"]) && !empty($_REQUEST["pay_id"])){
$pay_id = sanitize_text_field($_REQUEST["pay_id"]);
$query .=" AND `pay_id` = '$pay_id'";
}
if(isset($_REQUEST["created_at"]) && !empty($_REQUEST["created_at"])){
$created_at = sanitize_text_field($_REQUEST["created_at"]);
$query .=" AND `created_at` LIKE '%$created_at%'";
}
return $query;
}
public function search_box( $text, $input_id ) {
if ( empty( esc_attr($_REQUEST['s']) ) && ! $this->has_items() ) {
// return;
}
$input_id = $input_id . '-search-input';
?>
<p class="search-box">
<form action="admin.php" method="get">
<input type="hidden" name="page" value="<?php echo esc_attr($_REQUEST['page']); ?>"/>
<?php
if ( ! empty( $_REQUEST['orderby'] ) ) {
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
}
if ( ! empty( $_REQUEST['order'] ) ) {
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
}
if ( ! empty( $_REQUEST['post_mime_type'] ) ) {
echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
}
if ( ! empty( $_REQUEST['detached'] ) ) {
echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
}
?>
<div class="cbk-knet-field">
<label for="order_id"><?php echo __('Order', "cbk_knet") ?></label>
<input type="search" id="order_id" name="order_id" value="<?php echo esc_attr($_REQUEST['order_id'] ?? ""); ?>" placeholder="<?php echo __('Order', "cbk_knet") ?>" />
</div>
<div class="cbk-knet-field">
<label for="status"><?php echo __('Status', "cbk_knet") ?></label>
<select name="status" id="status">
<option value=""><?php echo __('Status', "cbk_knet") ?></option>
<option value="fail" <?php echo (isset($_REQUEST["status"]) && sanitize_text_field($_REQUEST["status"]) =="fail") ? "selected" : "" ?>><?php echo __('Fail', "cbk_knet") ?></option>
<option value="success" <?php echo (isset($_REQUEST["status"]) && sanitize_text_field($_REQUEST["status"]) =="success") ? "selected" : "" ?>><?php echo __('Success', "cbk_knet") ?></option>
</select>
</div>
<div class="cbk-knet-field">
<label for="result"><?php echo __('Result', "cbk_knet") ?></label>
<input type="search" id="result" name="result" value="<?php echo esc_attr($_REQUEST['result'] ?? ""); ?>" placeholder="<?php echo __('Result', "cbk_knet") ?>" />
</div>
<div class="cbk-knet-field">
<label for="amount"><?php echo __('Amount', "cbk_knet") ?></label>
<input type="search" id="amount" name="amount" value="<?php echo esc_attr($_REQUEST['amount'] ?? ""); ?>" placeholder="<?php echo __('amount', "cbk_knet") ?>" />
</div>
<div class="cbk-knet-field">
<label for="payment_id"><?php echo __('Payment id', "cbk_knet") ?></label>
<input type="search" id="payment_id" name="payment_id" value="<?php echo esc_attr($_REQUEST['payment_id'] ?? ""); ?>" placeholder="<?php echo __('Payment id', "cbk_knet") ?>" />
</div>
<div class="cbk-knet-field">
<label for="track_id"><?php echo __('Tracking id', "cbk_knet") ?></label>
<input type="search" id="track_id" name="track_id" value="<?php echo esc_attr($_REQUEST['track_id'] ?? ""); ?>" placeholder="<?php echo __('Tracking id', "cbk_knet") ?>" />
</div>
<div class="cbk-knet-field">
<label for="tran_id"><?php echo __('Transaction id', "cbk_knet") ?></label>
<input type="search" id="tran_id" name="tran_id" value="<?php echo esc_attr($_REQUEST['tran_id'] ?? ""); ?>" placeholder="<?php echo __('Transaction id', "cbk_knet") ?>" />
</div>
<div class="cbk-knet-field">
<label for="ref_id"><?php echo __('Refrance id', "cbk_knet") ?></label>
<input type="search" id="ref_id" name="ref_id" value="<?php echo esc_attr($_REQUEST['ref_id'] ?? ""); ?>" placeholder="<?php echo __('Refrance id', "cbk_knet") ?>" />
</div>
<div class="cbk-knet-field">
<label for="ref_id"><?php echo __('Pay id', "cbk_knet") ?></label>
<input type="search" id="pay_id" name="pay_id" value="<?php echo esc_attr($_REQUEST['pay_id'] ?? ""); ?>" placeholder="<?php echo __('Pay id', "cbk_knet") ?>" />
</div>
<div class="cbk-knet-field">
<label for="created_at"><?php echo __('Created at', "cbk_knet") ?></label>
<input type="date" id="created_at" name="created_at" value="<?php echo esc_attr($_REQUEST['created_at'] ?? ""); ?>" placeholder="<?php echo __('Created at', "cbk_knet") ?>" />
</div>
<div>
<?php submit_button( $text, 'submit', '', false, array( 'id' => 'search-submit' ) ); ?>
<a class="button reset" href="admin.php?page=<?php echo esc_attr($_REQUEST['page'] ?? ""); ?>"><?php echo __('Reset', "cbk_knet") ?></a>
<?php $http_build_query = $_GET; unset($http_build_query["page"]) ?>
<a class="button reset" href="admin.php?page=<?php echo esc_attr($_REQUEST['page'] ?? ""); ?>&cbk_knet_export=excel&<?php echo http_build_query($http_build_query) ?>"><?php echo __('Export excel', "cbk_knet") ?></a>
<a class="button reset" href="admin.php?page=<?php echo esc_attr(REQUEST['page'] ?? ""); ?>&cbk_knet_export=csv&<?php echo http_build_query($http_build_query) ?>"><?php echo __('Export csv', "cbk_knet") ?></a>
</div>
</form>
</p>
<style type="text/css">
.cbk-knet-field{
display: flex;
gap: 3rem;
align-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.cbk-knet-field label{
width: 7rem;
}
</style>
<?php
}
public function no_items() {
_e( 'No Transations avaliable.', "cbk_knet" );
}
public function get_columns()
{
return $columns= array(
'order_id'=>__('Order', "cbk_knet"),
'status'=>__('Status', "cbk_knet"),
'result'=>__('Result', "cbk_knet"),
'amount'=>__('Amount', "cbk_knet"),
'payment_id'=>__('Payment id', "cbk_knet"),
'track_id'=>__('Tracking id', "cbk_knet"),
'tran_id'=>__('Transaction id', "cbk_knet"),
'ref_id'=>__('Refrance id', "cbk_knet"),
'pay_id'=>__('Pay id', "cbk_knet"),
'created_at'=>__('Created at', "cbk_knet"),
);
}
public function get_sortable_columns() {
return $sortable = array(
'order_id'=>'order_id',
'status'=>'status',
'result'=>'result',
'payment_id'=>'payment_id',
'track_id'=>'track_id',
'amount'=>'amount',
'tran_id'=>'tran_id',
'ref_id'=>'ref_id',
'pay_id'=>'pay_id',
'created_at'=>'created_at',
);
}
/**
* Render a column when no column specific method exists.
*
* @param array $item
* @param string $column_name
*
* @return mixed
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'order_id':
return sprintf("<a href='%s' target='_blank'>#%s</a>",get_edit_post_link($item[$column_name]),$item[$column_name]);
case 'status':
return ($item[$column_name] == "fail") ? sprintf("<span style='color:red'>%s</span>",__($item[ $column_name ],"cbk_knet")) : sprintf("<span style='color:green'>%s</span>",__($item[ $column_name ],"cbk_knet"));
case 'result':
return ($item[$column_name] != "Success") ? sprintf("<span style='color:red'>%s</span>",$item[ $column_name ]) : sprintf("<span style='color:green'>%s</span>",$item[ $column_name ]);
default:
return $item["$column_name"]; //Show the whole array for troubleshooting purposes
}
}
/**
* Handles data query and filter, sorting, and pagination.
*/
public function prepare_items() {
$this->_column_headers = $this->get_column_info();
/** Process bulk action */
$this->process_bulk_action();
$per_page = $this->get_items_per_page( 'trans_per_page', 20 );
$current_page = $this->get_pagenum();
$total_items = self::record_count();
$this->set_pagination_args( [
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page //WE have to determine how many items to show on a page
] );
$this->items = self::get_transations( $per_page, $current_page );
}
/**
* Returns the count of records in the database.
*
* @return null|string
*/
public static function record_count() {
$sql = "SELECT COUNT(*) FROM ".(new self)->table." WHERE 1=1 ".(new self)->filter_query();
return (new self)->db->get_var( $sql );
}
public static function get_transations($per_page, $page_number = 1)
{
$sql = "SELECT * FROM ".(new self)->table." WHERE 1=1 ".(new self)->filter_query();
if ( ! empty( $_REQUEST['orderby'] ) ) {
$sql .= ' ORDER BY ' . sanitize_text_field( $_REQUEST['orderby'] );
$sql .= ! empty( $_REQUEST['order'] ) ? ' ' . sanitize_text_field( $_REQUEST['order'] ) : ' ASC';
}else{
$sql .= " ORDER BY `id` DESC ";
}
$sql .= " LIMIT $per_page";
$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
$result = (new self)->db->get_results( $sql, 'ARRAY_A' );
return $result;
}
}
}
if(!class_exists("Alnazer_CBK_KNET_Plugin")){
/**
* Class Alnazer_CBK_KNET_Plugin
*/
class Alnazer_CBK_KNET_Plugin
{
// class instance
static $instance;
// customer WP_List_Table object
public $transations_obj;
// class constructor
public function __construct()
{
add_filter('set-screen-option', [__CLASS__, 'set_screen'], 10, 3);
add_action('admin_menu', [$this, 'plugin_menu']);
}
public static function set_screen( $status, $option, $value ) {
return $value;
}
/** Singleton instance */
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Screen options
*/
public function screen_option() {
$option = 'per_page';
$args = [
'label' => 'Transations count',
'default' => 20,
'option' => 'trans_per_page'
];
add_screen_option( $option, $args );
$this->transations_obj = new Alnazer_Cbk_KNET_Trans_Grid();
}
public function plugin_menu() {
$hook =add_submenu_page(
'woocommerce',
__( '(Al-Tijari) KNET transactions', 'wc-cbk' ),
__( '(Al-Tijari) KNET transactions', 'wc-cbk' ),
'manage_woocommerce',
"cbk-knet-transactions",
[ $this, 'plugin_settings_page' ],
6
);
add_action( "load-$hook", [ $this, 'screen_option' ] );
}
/**
* Plugin settings page
*/
public function plugin_settings_page() {
?>
<div class="wrap">
<h2><?php echo __( '(Al-Tijari) KNET transactions', 'wc-cbk' ) ?></h2>
<div style="display: flex;gap: 3rem;">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<form method="post">
<?php
$this->transations_obj->prepare_items();
$this->transations_obj->display(); ?>
</form>
</div>
</div>
</div>
<div id="post-body" class="metabox-holder columns-2">
<?php $this->transations_obj->search_box(__( 'Filter', 'wc-cbk' ), "cbk_knet_filter") ?>
</div>
</div>
</div>
<?php
}
}
}