1 <?php
2 3 4 5 6 7 8 9 10
11
12
13
14 if ( ! defined( 'ABSPATH' ) ) exit;
15
16
17 if ( ! class_exists( 'WP_List_Table' ) ) {
18 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
19 }
20
21 22 23 24 25 26 27 28
29 class EDD_Discount_Codes_Table extends WP_List_Table {
30 31 32 33 34 35
36 public $per_page = 30;
37
38 39 40 41 42 43
44 public $total_count;
45
46 47 48 49 50 51
52 public $active_count;
53
54 55 56 57 58 59
60 public $inactive_count;
61
62 63 64 65 66 67 68 69 70
71 public function __construct() {
72 global $status, $page;
73
74 parent::__construct( array(
75 'singular' => edd_get_label_singular(),
76 'plural' => edd_get_label_plural(),
77 'ajax' => false
78 ) );
79
80 $this->get_discount_code_counts();
81 }
82
83 84 85 86 87 88 89 90 91 92 93
94 public function search_box( $text, $input_id ) {
95 if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
96 return;
97
98 $input_id = $input_id . '-search-input';
99
100 if ( ! empty( $_REQUEST['orderby'] ) )
101 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
102 if ( ! empty( $_REQUEST['order'] ) )
103 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
104 ?>
105 <p class="search-box">
106 <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
107 <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
108 <?php submit_button( $text, 'button', false, false, array('ID' => 'search-submit') ); ?>
109 </p>
110 <?php
111 }
112
113 114 115 116 117 118 119
120 public function get_views() {
121 $base = admin_url('edit.php?post_type=download&page=edd-discounts');
122
123 $current = isset( $_GET['status'] ) ? $_GET['status'] : '';
124 $total_count = ' <span class="count">(' . $this->total_count . ')</span>';
125 $active_count = ' <span class="count">(' . $this->active_count . ')</span>';
126 $inactive_count = ' <span class="count">(' . $this->inactive_count . ')</span>';
127
128 $views = array(
129 'all' => sprintf( '<a href="%s"%s>%s</a>', remove_query_arg( 'status', $base ), $current === 'all' || $current == '' ? ' class="current"' : '', __('All', 'edd') . $total_count ),
130 'active' => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( 'status', 'active', $base ), $current === 'active' ? ' class="current"' : '', __('Active', 'edd') . $active_count ),
131 'inactive' => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( 'status', 'inactive', $base ), $current === 'inactive' ? ' class="current"' : '', __('Inactive', 'edd') . $inactive_count ),
132 );
133
134 return $views;
135 }
136
137 138 139 140 141 142 143
144 public function get_columns() {
145 $columns = array(
146 'cb' => '<input type="checkbox" />',
147 'ID' => __( 'ID', 'edd' ),
148 'name' => __( 'Name', 'edd' ),
149 'code' => __( 'Code', 'edd' ),
150 'amount' => __( 'Amount', 'edd' ),
151 'uses' => __( 'Uses', 'edd' ),
152 'max_uses' => __( 'Max Uses', 'edd' ),
153 'start_date'=> __( 'Start Date', 'edd' ),
154 'expiration'=> __( 'Expiration', 'edd' ),
155 'status' => __( 'Status', 'edd' ),
156 );
157
158 return $columns;
159 }
160
161 162 163 164 165 166 167
168 public function get_sortable_columns() {
169 return array(
170 'ID' => array( 'ID', true ),
171 'name' => array( 'name', false )
172 );
173 }
174
175 176 177 178 179 180 181 182 183 184 185
186 function column_default( $item, $column_name ) {
187 switch( $column_name ){
188 default:
189 return $item[ $column_name ];
190 }
191 }
192
193 194 195 196 197 198 199 200
201 function column_name( $item ) {
202 $discount = get_post( $item['ID'] );
203 $base = admin_url( 'edit.php?post_type=download&page=edd-discounts&edd-action=edit_discount&discount=' . $item['ID'] );
204 $row_actions = array();
205
206 $row_actions['edit'] = '<a href="' . add_query_arg( array( 'edd-action' => 'edit_discount', 'discount' => $discount->ID ) ) . '">' . __( 'Edit', 'edd' ) . '</a>';
207
208 if( strtolower( $item['status'] ) == 'active' )
209 $row_actions['deactivate'] = '<a href="' . add_query_arg( array( 'edd-action' => 'deactivate_discount', 'discount' => $discount->ID ) ) . '">' . __( 'Deactive', 'edd' ) . '</a>';
210 else
211 $row_actions['activate'] = '<a href="' . add_query_arg( array( 'edd-action' => 'activate_discount', 'discount' => $discount->ID ) ) . '">' . __( 'Activate', 'edd' ) . '</a>';
212
213 $row_actions['delete'] = '<a href="' . wp_nonce_url( add_query_arg( array( 'edd-action' => 'delete_discount', 'discount' => $discount->ID ) ), 'edd_discount_nonce' ) . '">' . __( 'Delete', 'edd' ) . '</a>';
214
215 $row_actions = apply_filters( 'edd_discount_row_actions', $row_actions, $discount );
216
217 return $item['name'] . $this->row_actions( $row_actions );
218 }
219
220 221 222 223 224 225 226 227
228 function column_cb( $item ) {
229 return sprintf(
230 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
231 $this->_args['singular'],
232 $item['ID']
233 );
234 }
235
236 237 238 239 240 241 242
243 public function get_bulk_actions() {
244 $actions = array(
245 'delete' => __( 'Delete', 'edd' )
246 );
247
248 return $actions;
249 }
250
251 252 253 254 255 256 257
258 public function process_bulk_action() {
259 $ids = isset( $_GET['download'] ) ? $_GET['download'] : false;
260
261 if ( ! is_array( $ids ) )
262 $ids = array( $ids );
263
264 foreach ( $ids as $id ) {
265 if ( 'delete' === $this->current_action() ) {
266 edd_remove_discount( $id );
267 }
268 }
269
270 }
271
272 273 274 275 276 277 278
279 public function get_discount_code_counts() {
280 $discount_code_count = wp_count_posts( 'edd_discount' );
281 $this->active_count = $discount_code_count->active;
282 $this->inactive_count = $discount_code_count->inactive;
283 $this->total_count = $discount_code_count->active + $discount_code_count->inactive;
284 }
285
286 287 288 289 290 291 292
293 public function discount_codes_data() {
294 $discount_codes_data = array();
295
296 if ( isset( $_GET['paged'] ) ) $page = $_GET['paged']; else $page = 1;
297
298 $per_page = $this->per_page;
299
300 $mode = edd_is_test_mode() ? 'test' : 'live';
301
302 $orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : 'ID';
303 $order = isset( $_GET['order'] ) ? $_GET['order'] : 'DESC';
304 $order_inverse = $order == 'DESC' ? 'ASC' : 'DESC';
305 $status = isset( $_GET['status'] ) ? $_GET['status'] : array( 'active', 'inactive' );
306 $meta_key = isset( $_GET['meta_key'] ) ? $_GET['meta_key'] : null;
307 $search = isset( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : null;
308 $order_class = strtolower( $order_inverse );
309
310 $discounts = edd_get_discounts( array(
311 'posts_per_page' => $per_page,
312 'page' => isset( $_GET['paged'] ) ? $_GET['paged'] : null,
313 'orderby' => $orderby,
314 'order' => $order,
315 'post_status' => $status,
316 'meta_key' => $meta_key,
317 's' => $search
318 ) );
319
320 if ( $discounts ) {
321 foreach ( $discounts as $discount ) {
322 if ( edd_get_discount_max_uses( $discount->ID ) ) {
323 $uses = edd_get_discount_uses( $discount->ID ) . '/' . edd_get_discount_max_uses( $discount->ID );
324 } else {
325 $uses = edd_get_discount_uses( $discount->ID );
326 }
327
328 if ( edd_get_discount_max_uses( $discount->ID ) ) {
329 $max_uses = edd_get_discount_max_uses( $discount->ID ) ? edd_get_discount_max_uses( $discount->ID ) : __( 'unlimited', 'edd' );
330 } else {
331 $max_uses = __( 'Unlimited', 'edd' );
332 }
333
334 $start_date = edd_get_discount_start_date( $discount->ID );
335
336 if ( ! empty( $start_date ) ) {
337 $discount_start_date = date_i18n( get_option( 'date_format' ), strtotime( $start_date ) );
338 } else {
339 $discount_start_date = __( 'No start date', 'edd' );
340 }
341
342 if ( edd_get_discount_expiration( $discount->ID ) ) {
343 $expiration = edd_is_discount_expired( $discount->ID ) ? __( 'Expired', 'edd' ) : date_i18n( get_option( 'date_format' ), strtotime( edd_get_discount_expiration( $discount->ID ) ) );
344 } else {
345 $expiration = __( 'No expiration', 'edd' );
346 }
347
348 $discount_codes_data[] = array(
349 'ID' => $discount->ID,
350 'name' => get_the_title( $discount->ID ),
351 'code' => edd_get_discount_code( $discount->ID ),
352 'amount' => edd_format_discount_rate( edd_get_discount_type( $discount->ID ), edd_get_discount_amount( $discount->ID ) ),
353 'uses' => $uses,
354 'max_uses' => $max_uses,
355 'start_date' => $discount_start_date,
356 'expiration' => $expiration,
357 'status' => ucwords( $discount->post_status ),
358 );
359 }
360 }
361
362 return $discount_codes_data;
363 }
364
365 366 367 368 369 370 371 372 373 374 375 376 377
378 public function prepare_items() {
379 $per_page = $this->per_page;
380
381 $columns = $this->get_columns();
382
383 $hidden = array();
384
385 $sortable = $this->get_sortable_columns();
386
387 $this->_column_headers = array( $columns, $hidden, $sortable );
388
389 $this->process_bulk_action();
390
391 $data = $this->discount_codes_data();
392
393 $current_page = $this->get_pagenum();
394
395 $status = isset( $_GET['status'] ) ? $_GET['status'] : 'any';
396
397 switch( $status ) {
398 case 'active':
399 $total_items = $this->active_count;
400 break;
401 case 'inactive':
402 $total_items = $this->inactive_count;
403 break;
404 case 'any':
405 $total_items = $this->total_count;
406 break;
407 }
408
409 $this->items = $data;
410
411 $this->set_pagination_args( array(
412 'total_items' => $total_items,
413 'per_page' => $per_page,
414 'total_pages' => ceil( $total_items / $per_page )
415 )
416 );
417 }
418 }