class.swpm-payment-buttons-list-table.php 7.8 KB

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