|
| 1 | +<?php |
| 2 | + |
| 3 | +if (!class_exists('WP_List_Table')){ |
| 4 | + require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 5 | +} |
| 6 | + |
| 7 | +class SwpmPaymentButtonsListTable extends WP_List_Table { |
| 8 | + |
| 9 | + private $per_page; |
| 10 | + |
| 11 | + function __construct() { |
| 12 | + global $status, $page; |
| 13 | + |
| 14 | + //Set parent defaults |
| 15 | + parent::__construct(array( |
| 16 | + 'singular' => 'payment button', //singular name of the listed records |
| 17 | + 'plural' => 'payment buttons', //plural name of the listed records |
| 18 | + 'ajax' => false //does this table support ajax? |
| 19 | + )); |
| 20 | + |
| 21 | + $this->per_page = 50; |
| 22 | + } |
| 23 | + |
| 24 | + function column_default($item, $column_name) { |
| 25 | + //We need to read the values from our CPT and feed the column value for the given column name manually. |
| 26 | + |
| 27 | + switch ($column_name) { |
| 28 | + case 'title': |
| 29 | + return get_the_title($item['ID']); |
| 30 | + break; |
| 31 | + case 'membership_level': |
| 32 | + return get_post_meta($item['ID'], 'membership_level_id', true); |
| 33 | + break; |
| 34 | + case 'button_type': |
| 35 | + $button_type = get_post_meta($item['ID'], 'button_type', true); |
| 36 | + return $button_type; |
| 37 | + break; |
| 38 | + case 'button_shortcode': |
| 39 | + $level_id = get_post_meta($item['ID'], 'membership_level_id', true); |
| 40 | + if(!SwpmUtils::membership_level_id_exists($level_id)){ |
| 41 | + //This membership level doesn't exist. Show an error instead of the shortcode. |
| 42 | + $shortcode = 'Error! The membership level you specified in this button does not exist. You may have deleted this level. Edit this button and use a valid membership level.'; |
| 43 | + } else { |
| 44 | + $shortcode = '[swpm_payment_button id='.$item['ID'].']'; |
| 45 | + } |
| 46 | + return $shortcode; |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + function column_ID($item) { |
| 52 | + |
| 53 | + $button_type = get_post_meta($item['ID'], 'button_type', true); |
| 54 | + //Build row actions |
| 55 | + $actions = array( |
| 56 | + 'edit' => sprintf('<a href="admin.php?page=simple_wp_membership_payments&tab=edit_button&button_id=%s&button_type=%s">Edit</a>', $item['ID'], $button_type), |
| 57 | + 'delete' => sprintf('<a href="admin.php?page=simple_wp_membership_payments&tab=payment_buttons&action=delete_payment_btn&button_id=%s" onclick="return confirm(\'Are you sure you want to delete this record?\')">Delete</a>', $item['ID']), |
| 58 | + ); |
| 59 | + |
| 60 | + //Return the refid column contents |
| 61 | + return $item['ID'] . $this->row_actions($actions); |
| 62 | + } |
| 63 | + |
| 64 | + function column_cb($item) { |
| 65 | + return sprintf( |
| 66 | + '<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 67 | + /* $1%s */ $this->_args['singular'], //Let's reuse singular label (affiliate) |
| 68 | + /* $2%s */ $item['ID'] //The value of the checkbox should be the record's key/id |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + function get_columns() { |
| 73 | + $columns = array( |
| 74 | + 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text |
| 75 | + 'ID' => SwpmUtils::_('Payment Button ID'), |
| 76 | + 'title' => SwpmUtils::_('Payment Button Title'), |
| 77 | + 'membership_level' => SwpmUtils::_('Membership Level ID'), |
| 78 | + 'button_type' => SwpmUtils::_('Button Type'), |
| 79 | + 'button_shortcode' => SwpmUtils::_('Button Shortcode'), |
| 80 | + ); |
| 81 | + return $columns; |
| 82 | + } |
| 83 | + |
| 84 | + function get_sortable_columns() { |
| 85 | + $sortable_columns = array(); |
| 86 | +// $sortable_columns = array( |
| 87 | +// 'ID' => array('ID', false), //true means its already sorted |
| 88 | +// ); |
| 89 | + return $sortable_columns; |
| 90 | + } |
| 91 | + |
| 92 | + function get_bulk_actions() { |
| 93 | + $actions = array( |
| 94 | + 'delete' => SwpmUtils::_('Delete') |
| 95 | + ); |
| 96 | + return $actions; |
| 97 | + } |
| 98 | + |
| 99 | + function process_bulk_action() { |
| 100 | + //Detect when a bulk action is being triggered... |
| 101 | + if ('delete' === $this->current_action()) { |
| 102 | + $records_to_delete = array_map( 'sanitize_text_field', $_REQUEST['paymentbutton'] ); |
| 103 | + if (empty($records_to_delete)) { |
| 104 | + echo '<div id="message" class="updated fade"><p>Error! You need to select multiple records to perform a bulk action!</p></div>'; |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + foreach ($records_to_delete as $record_id) { |
| 109 | + if(!is_numeric($record_id)){ |
| 110 | + wp_die('Error! ID must be a numeric number.'); |
| 111 | + } |
| 112 | + wp_delete_post( $record_id ); |
| 113 | + } |
| 114 | + echo '<div id="message" class="updated fade"><p>Selected records deleted successfully!</p></div>'; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + function process_delete_action() { |
| 119 | + |
| 120 | + if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'delete_payment_btn') { //Delete link was clicked for a row in list table |
| 121 | + $record_id = sanitize_text_field($_REQUEST['button_id']); |
| 122 | + if(!is_numeric($record_id)){ |
| 123 | + wp_die('Error! ID must be a numeric number.'); |
| 124 | + } |
| 125 | + wp_delete_post( $record_id ); |
| 126 | + $success_msg = '<div id="message" class="updated"><p>'; |
| 127 | + $success_msg .= SwpmUtils::_('The selected entry was deleted!'); |
| 128 | + $success_msg .= '</p></div>'; |
| 129 | + echo $success_msg; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Retrieve the current page number |
| 135 | + */ |
| 136 | + function get_paged() { |
| 137 | + return isset($_GET['paged']) ? absint($_GET['paged']) : 1; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Retrieve the total number of CPT items |
| 142 | + */ |
| 143 | + function get_total_items() { |
| 144 | + $counts = wp_count_posts('swpm_payment_button'); |
| 145 | + $total = 0; |
| 146 | + foreach ($counts as $count) |
| 147 | + $total += $count; |
| 148 | + |
| 149 | + return $total; |
| 150 | + } |
| 151 | + |
| 152 | + function payment_buttons_data() { |
| 153 | + $data = array(); |
| 154 | + $cpt_args = array( |
| 155 | + 'post_type' => 'swpm_payment_button', |
| 156 | + 'post_status' => 'publish', |
| 157 | + 'posts_per_page' => $this->per_page, |
| 158 | + 'paged' => $this->get_paged() |
| 159 | + ); |
| 160 | + |
| 161 | + //TODO - Do search and sort stuff (see example code) |
| 162 | + |
| 163 | + //Retrieve all the CPT items |
| 164 | + $items = get_posts($cpt_args); |
| 165 | + if ($items) { |
| 166 | + foreach ($items as $item) { |
| 167 | + |
| 168 | + $membership_level = get_post_meta($item->ID, 'membership_level_id', true); |
| 169 | + $data[] = array( |
| 170 | + 'ID' => $item->ID, |
| 171 | + 'title' => get_the_title($item->ID), |
| 172 | + 'membership_level' => $membership_level, |
| 173 | + ); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return $data; |
| 178 | + } |
| 179 | + |
| 180 | + function prepare_items() { |
| 181 | + |
| 182 | + // Lets decide how many records per page to show |
| 183 | + $per_page = $this->per_page; |
| 184 | + |
| 185 | + $columns = $this->get_columns(); |
| 186 | + $hidden = array(); |
| 187 | + $sortable = $this->get_sortable_columns(); |
| 188 | + |
| 189 | + $this->_column_headers = array($columns, $hidden, $sortable); |
| 190 | + |
| 191 | + $this->process_delete_action(); |
| 192 | + $this->process_bulk_action(); |
| 193 | + |
| 194 | + // Pagination requirement |
| 195 | + $current_page = $this->get_pagenum(); |
| 196 | + $total_items = $this->get_total_items(); |
| 197 | + |
| 198 | + // Now we add our *sorted* data to the items property, where it can be used by the rest of the class. |
| 199 | + $data = $this->payment_buttons_data(); |
| 200 | + $this->items = $data; |
| 201 | + |
| 202 | + //pagination requirement |
| 203 | + $this->set_pagination_args(array( |
| 204 | + 'total_items' => $total_items, //WE have to calculate the total number of items |
| 205 | + 'per_page' => $per_page, //WE have to determine how many items to show on a page |
| 206 | + 'total_pages' => ceil($total_items / $per_page) //WE have to calculate the total number of pages |
| 207 | + )); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments