Easy Digital Downloads
  • Package
  • Class
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Classes

  • EDD_Discount_Codes_Table

Functions

  • edd_activate_discount
  • edd_add_discount
  • edd_deactivate_discount
  • edd_delete_discount
  • edd_discounts_contextual_help
  • edd_discounts_page
  • edd_edit_discount
  1 <?php
  2 /**
  3  * Discount Codes Table Class
  4  *
  5  * @package     EDD
  6  * @subpackage  Admin/Discounts
  7  * @copyright   Copyright (c) 2013, Pippin Williamson
  8  * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
  9  * @since       1.4
 10  */
 11 
 12 
 13 // Exit if accessed directly
 14 if ( ! defined( 'ABSPATH' ) ) exit;
 15 
 16 // Load WP_List_Table if not loaded
 17 if ( ! class_exists( 'WP_List_Table' ) ) {
 18     require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
 19 }
 20 
 21 /**
 22  * EDD_Discount_Codes_Table Class
 23  *
 24  * Renders the Discount Codes table on the Discount Codes page
 25  *
 26  * @since 1.4
 27  * @author Sunny Ratilal
 28  */
 29 class EDD_Discount_Codes_Table extends WP_List_Table {
 30     /**
 31      * Number of results to show per page
 32      *
 33      * @var string
 34      * @since 1.4
 35      */
 36     public $per_page = 30;
 37 
 38     /**
 39      *
 40      * Total number of discounts
 41      * @var string
 42      * @since 1.4
 43      */
 44     public $total_count;
 45 
 46     /**
 47      * Active number of discounts
 48      *
 49      * @var string
 50      * @since 1.4
 51      */
 52     public $active_count;
 53 
 54     /**
 55      * Inactive number of discounts
 56      *
 57      * @var string
 58      * @since 1.4
 59      */
 60     public $inactive_count;
 61 
 62     /**
 63      * Get things started
 64      *
 65      * @access public
 66      * @since 1.4
 67      * @uses EDD_Discount_Codes_Table::get_discount_code_counts()
 68      * @see WP_List_Table::__construct()
 69      * @return void
 70      */
 71     public function __construct() {
 72         global $status, $page;
 73 
 74         parent::__construct( array(
 75             'singular'  => edd_get_label_singular(),    // Singular name of the listed records
 76             'plural'    => edd_get_label_plural(),      // Plural name of the listed records
 77             'ajax'      => false                        // Does this table support ajax?
 78         ) );
 79 
 80         $this->get_discount_code_counts();
 81     }
 82 
 83     /**
 84      * Show the search field
 85      *
 86      * @access public
 87      * @since 1.4
 88      *
 89      * @param string $text Label for the search box
 90      * @param string $input_id ID of the search box
 91      *
 92      * @return svoid
 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      * Retrieve the view types
115      *
116      * @access public
117      * @since 1.4
118      * @return array $views All the views available
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    = '&nbsp;<span class="count">(' . $this->total_count    . ')</span>';
125         $active_count   = '&nbsp;<span class="count">(' . $this->active_count . ')</span>';
126         $inactive_count = '&nbsp;<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      * Retrieve the table columns
139      *
140      * @access public
141      * @since 1.4
142      * @return array $columns Array of all the list table columns
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      * Retrieve the table's sortable columns
163      *
164      * @access public
165      * @since 1.4
166      * @return array Array of all the sortable columns
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      * This function renders most of the columns in the list table.
177      *
178      * @access public
179      * @since 1.4
180      *
181      * @param array $item Contains all the data of the discount code
182      * @param string $column_name The name of the column
183      *
184      * @return string Column Name
185      */
186     function column_default( $item, $column_name ) {
187         switch( $column_name ){
188             default:
189                 return $item[ $column_name ];
190         }
191     }
192 
193     /**
194      * Render the Name Column
195      *
196      * @access public
197      * @since 1.4
198      * @param array $item Contains all the data of the discount code
199      * @return string Data shown in the Name column
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      * Render the checkbox column
222      *
223      * @access public
224      * @since 1.4
225      * @param array $item Contains all the data for the checkbox column
226      * @return string Displays a checkbox
227      */
228     function column_cb( $item ) {
229         return sprintf(
230             '<input type="checkbox" name="%1$s[]" value="%2$s" />',
231             /*$1%s*/ $this->_args['singular'],
232             /*$2%s*/ $item['ID']
233         );
234     }
235 
236     /**
237      * Retrieve the bulk actions
238      *
239      * @access public
240      * @since 1.4
241      * @return array $actions Array of the bulk actions
242      */
243     public function get_bulk_actions() {
244         $actions = array(
245             'delete' => __( 'Delete', 'edd' )
246         );
247 
248         return $actions;
249     }
250 
251     /**
252      * Process the bulk actions
253      *
254      * @access public
255      * @since 1.4
256      * @return void
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      * Retrieve the discount code counts
274      *
275      * @access public
276      * @since 1.4
277      * @return void
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      * Retrieve all the data for all the discount codes
288      *
289      * @access public
290      * @since 1.4
291      * @return array $discount_codes_data Array of all the data for the discount codes
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      * Setup the final data for the table
367      *
368      * @access public
369      * @since 1.4
370      * @uses EDD_Discount_Codes_Table::get_columns()
371      * @uses EDD_Discount_Codes_Table::get_sortable_columns()
372      * @uses EDD_Discount_Codes_Table::process_bulk_action()
373      * @uses EDD_Discount_Codes_Table::discount_codes_data()
374      * @uses WP_List_Table::get_pagenum()
375      * @uses WP_List_Table::set_pagination_args()
376      * @return void
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 }
Easy Digital Downloads API documentation generated by ApiGen 2.8.0