/app/helper/listtable/class-ms-helper-listtable-billing.php

  1. <?php 
  2. /** 
  3. * Membership List Table 
  4. * 
  5. * @since 1.0.0 
  6. */ 
  7. class MS_Helper_ListTable_Billing extends MS_Helper_ListTable { 
  8.  
  9. protected $id = 'billing'; 
  10.  
  11. public function __construct() { 
  12. parent::__construct( 
  13. array( 
  14. 'singular' => 'billing',  
  15. 'plural' => 'billings',  
  16. 'ajax' => false,  
  17. ); 
  18.  
  19. public function get_columns() { 
  20. $currency = MS_Plugin::instance()->settings->currency; 
  21.  
  22. $columns = apply_filters( 
  23. 'ms_helper_listtable_billing_columns',  
  24. array( 
  25. 'cb' => '<input type="checkbox" />',  
  26. 'invoice' => __( 'Invoice #', 'membership2' ),  
  27. 'user' => __( 'User', 'membership2' ),  
  28. 'membership' => __( 'Membership', 'membership2' ),  
  29. 'status' => __( 'Status', 'membership2' ),  
  30. 'total' => __( 'Total', 'membership2' ),  
  31. 'due_date' => __( 'Due date', 'membership2' ),  
  32. 'gateway_id' => __( 'Gateway', 'membership2' ),  
  33. ); 
  34.  
  35. $columns = apply_filters( 
  36. 'ms_helper_listtable_billing_get_columns',  
  37. $columns,  
  38. $currency 
  39. ); 
  40.  
  41. return $columns; 
  42.  
  43. public function column_cb( $item ) { 
  44. return sprintf( 
  45. '<input type="checkbox" name="invoice_id[]" value="%1$s" />',  
  46. esc_attr( $item->id ) 
  47. ); 
  48.  
  49. public function get_hidden_columns() { 
  50. return apply_filters( 
  51. 'ms_helper_listtable_billing_hidden_columns',  
  52. array() 
  53. ); 
  54.  
  55. public function get_sortable_columns() { 
  56. return apply_filters( 
  57. 'ms_helper_listtable_billing_sortable_columns',  
  58. array( 
  59. 'invoice' => array( 'ID', false ),  
  60. 'user' => array( 'author', false ),  
  61. 'membership' => array( 'ms_membership_ids', false ),  
  62. 'status' => array( 'status', false ),  
  63. 'amount' => array( 'amount', false ),  
  64. 'total' => array( 'total', false ),  
  65. 'due_date' => array( 'due_date', false ),  
  66. 'gateway_id' => array( 'gateway_id', false ),  
  67. ); 
  68.  
  69. public function prepare_items() { 
  70. $this->_column_headers = array( 
  71. $this->get_columns(),  
  72. $this->get_hidden_columns(),  
  73. $this->get_sortable_columns(),  
  74. ); 
  75.  
  76. if ( ! isset( $_REQUEST['status'] ) ) { 
  77. $_GET['status'] = 'default'; 
  78. $_REQUEST['status'] = 'default'; 
  79.  
  80. $args = $this->get_query_args(); 
  81.  
  82. $total_items = MS_Model_Invoice::get_invoice_count( $args ); 
  83.  
  84. $this->items = apply_filters( 
  85. 'ms_helper_listtable_billing_items',  
  86. MS_Model_Invoice::get_invoices( $args ) 
  87. ); 
  88.  
  89. $per_page = $this->get_items_per_page( 
  90. 'invoice_per_page',  
  91. self::DEFAULT_PAGE_SIZE 
  92. ); 
  93. $this->set_pagination_args( 
  94. array( 
  95. 'total_items' => $total_items,  
  96. 'per_page' => $per_page,  
  97. ); 
  98.  
  99. private function get_query_args() { 
  100. $defaults = MS_Model_Invoice::get_query_args(); 
  101. lib3()->array->equip_request( 's' ); 
  102.  
  103. $per_page = $this->get_items_per_page( 
  104. 'invoice_per_page',  
  105. self::DEFAULT_PAGE_SIZE 
  106. ); 
  107. $current_page = $this->get_pagenum(); 
  108.  
  109. $args = array( 
  110. 'posts_per_page' => $per_page,  
  111. 'offset' => ( $current_page - 1 ) * $per_page,  
  112. ); 
  113.  
  114. // Filter by search-term 
  115. $search_filter = $_REQUEST['s']; 
  116. if ( ! empty( $search_filter ) ) { 
  117. $this->search_string = $search_filter; 
  118.  
  119. $args = wp_parse_args( $args, $defaults ); 
  120.  
  121. return $args; 
  122.  
  123. /** 
  124. * Returns the row-class to be used for the specified table item. 
  125. * 
  126. * @param object $item The current item. 
  127. * @return string Class to be added to the table row. 
  128. */ 
  129. protected function single_row_class( $item ) { 
  130. return 'invoice-' . $item->status; 
  131.  
  132. public function column_invoice( $item ) { 
  133. $actions = array(); 
  134.  
  135. // Prepare the item actions. 
  136. $actions['view'] = sprintf( 
  137. '<a href="%s">%s</a>',  
  138. get_permalink( $item->id ),  
  139. __( 'View', 'membership2' ) 
  140. ); 
  141.  
  142. if ( MS_Gateway_Manual::ID == $item->gateway_id && ! $item->is_paid() ) { 
  143. $action_url = MS_Controller_Plugin::get_admin_url( 
  144. 'billing',  
  145. array( 
  146. 'action' => MS_Controller_Billing::ACTION_PAY_IT,  
  147. '_wpnonce' => wp_create_nonce( MS_Controller_Billing::ACTION_PAY_IT ),  
  148. 'invoice_id' => $item->id,  
  149. ); 
  150.  
  151. $actions['pay_it'] = sprintf( 
  152. '<a href="%s">%s</a>',  
  153. $action_url,  
  154. __( 'Mark as paid', 'membership2' ) 
  155. ); 
  156.  
  157. $edit_url = MS_Controller_Plugin::get_admin_url( 
  158. 'billing',  
  159. array( 
  160. 'action' => MS_Controller_Billing::ACTION_EDIT,  
  161. 'invoice_id' => $item->id,  
  162. ); 
  163.  
  164. return sprintf( 
  165. '<a href="%3$s"><b>%1$s</b></a> %2$s',  
  166. $item->get_invoice_number(),  
  167. $this->row_actions( $actions ),  
  168. $edit_url 
  169. ); 
  170.  
  171. public function column_user( $item, $column_name ) { 
  172. $member = MS_Factory::load( 'MS_Model_Member', $item->user_id ); 
  173.  
  174. $html = sprintf( 
  175. '<a href="%s">%s</a>',  
  176. MS_Controller_Plugin::get_admin_url( 
  177. 'add-member',  
  178. array( 'user_id' => $item->user_id ) 
  179. ),  
  180. $member->username 
  181. ); 
  182.  
  183. return $html; 
  184.  
  185. public function column_membership( $item, $column_name ) { 
  186. $membership = MS_Factory::load( 'MS_Model_Membership', $item->membership_id ); 
  187.  
  188. return $membership->get_name_tag(); 
  189.  
  190. public function column_status( $item, $column_name ) { 
  191. $icon = ''; 
  192.  
  193. switch ( $item->status ) { 
  194. case MS_Model_Invoice::STATUS_NEW: 
  195. $icon = '<i class="wpmui-fa wpmui-fa-circle-o"></i>'; 
  196. break; 
  197.  
  198. case MS_Model_Invoice::STATUS_PAID: 
  199. $icon = '<i class="wpmui-fa wpmui-fa-check-circle"></i>'; 
  200. break; 
  201.  
  202. case MS_Model_Invoice::STATUS_PENDING: 
  203. case MS_Model_Invoice::STATUS_BILLED: 
  204. $icon = '<i class="wpmui-fa wpmui-fa-clock-o"></i>'; 
  205. break; 
  206.  
  207. case MS_Model_Invoice::STATUS_DENIED: 
  208. $icon = '<i class="wpmui-fa wpmui-fa-times-circle"></i>'; 
  209. break; 
  210.  
  211. case MS_Model_Invoice::STATUS_ARCHIVED: 
  212. $icon = '<i class="wpmui-fa wpmui-fa-times-circle-o"></i>'; 
  213. break; 
  214.  
  215. default: 
  216. $icon = $item->status_text(); 
  217. break; 
  218.  
  219. return sprintf( 
  220. '<span class="payment-status payment-status-%1$s" title="%3$s">%2$s</span>',  
  221. $item->status,  
  222. $icon,  
  223. $item->status_text() 
  224. ); 
  225.  
  226. public function column_amount( $item, $column_name ) { 
  227. $html = MS_Helper_Billing::format_price( $item->amount ); 
  228. return $html; 
  229.  
  230. public function column_total( $item, $column_name ) { 
  231. if ( $item->total ) { 
  232. $currency = $item->currency; 
  233. $value = MS_Helper_Billing::format_price( $item->total ); 
  234.  
  235. $html = sprintf( 
  236. '<b>%1$s</b> <small>%2$s</small>',  
  237. $value,  
  238. $currency 
  239. ); 
  240. } else { 
  241. $html = __( 'Free', 'membership2' ); 
  242.  
  243. return $html; 
  244.  
  245. public function column_due_date( $item, $column_name ) { 
  246. $due_now = false; 
  247. $is_paid = $item->is_paid(); 
  248.  
  249. if ( ! $is_paid ) { 
  250. $diff = MS_Helper_Period::subtract_dates( 
  251. $item->due_date,  
  252. MS_Helper_Period::current_date(),  
  253. null,  
  254. true 
  255. ); 
  256. $due_now = ($diff < 0); 
  257.  
  258. $due_date = MS_Helper_Period::format_date( $item->due_date ); 
  259.  
  260. if ( $due_now ) { 
  261. $html = sprintf( 
  262. '<span class="due-now" title="%2$s">%1$s</span>',  
  263. $due_date,  
  264. __( 'Payment is overdue', 'membership2' ) 
  265. ); 
  266. } elseif ( $item->pay_date ) { 
  267. $pay_date = MS_Helper_Period::format_date( $item->pay_date, 'M j, Y' ); 
  268. $html = sprintf( 
  269. '<span class="is-paid" title="%2$s">%1$s</span>',  
  270. $due_date,  
  271. sprintf( 
  272. __( 'Paid: %s', 'membership2' ),  
  273. $pay_date 
  274. ); 
  275. } else { 
  276. $html = sprintf( 
  277. '<span>%1$s</span>',  
  278. $due_date 
  279. ); 
  280.  
  281. return $html; 
  282.  
  283. public function column_gateway_id( $item, $column_name ) { 
  284. $html = MS_Model_Gateway::get_name( $item->gateway_id ); 
  285. return $html; 
  286.  
  287. public function column_default( $item, $column_name ) { 
  288. $html = ''; 
  289.  
  290. if ( property_exists( $item, $column_name ) ) { 
  291. $html = $item->column_name; 
  292.  
  293. return $html; 
  294.  
  295. public function get_bulk_actions() { 
  296. $bulk_actions = array( 
  297. 'archive' => __( 'Remove', 'membership2' ),  
  298. ); 
  299.  
  300. return apply_filters( 
  301. 'ms_helper_listtable_billing_bulk_actions',  
  302. $bulk_actions,  
  303. $this 
  304. ); 
  305.  
  306. public function get_views() { 
  307. $all_status = array( 
  308. MS_Model_Invoice::STATUS_PAID => __( 'Paid', 'membership2' ),  
  309. MS_Model_Invoice::STATUS_NEW => __( 'Draft', 'membership2' ),  
  310. MS_Model_Invoice::STATUS_DENIED => __( 'Denied', 'membership2' ),  
  311. ); 
  312.  
  313. $views = array(); 
  314. $orig_status = ''; 
  315.  
  316. if ( isset( $_REQUEST['status'] ) ) { 
  317. $orig_status = $_REQUEST['status']; 
  318. $_REQUEST['status'] = 'default'; 
  319. $args = $this->get_query_args(); 
  320. $_REQUEST['status'] = $orig_status; 
  321. $base_url = esc_url_raw( remove_query_arg( array( 'status', 'msg' ) ) ); 
  322. $count = MS_Model_Invoice::get_invoice_count( $args ); 
  323. $views['all'] = array( 
  324. 'url' => $base_url,  
  325. 'label' => __( 'Default', 'membership2' ),  
  326. 'count' => $count,  
  327. ); 
  328.  
  329. $url = esc_url_raw( add_query_arg( 'status', 'open', $base_url ) ); 
  330. $args = $this->get_query_args(); 
  331. $args['meta_query']['status']['value'] = array( 
  332. MS_Model_Invoice::STATUS_BILLED,  
  333. MS_Model_Invoice::STATUS_PENDING,  
  334. ); 
  335. $args['meta_query']['status']['compare'] = 'IN'; 
  336. $count = MS_Model_Invoice::get_invoice_count( $args ); 
  337. $views['open'] = array( 
  338. 'url' => $url,  
  339. 'label' => __( 'Billed or Pending', 'membership2' ),  
  340. 'count' => $count,  
  341. ); 
  342.  
  343. foreach ( $all_status as $status => $desc ) { 
  344. $args = $this->get_query_args(); 
  345. $args['meta_query']['status']['value'] = $status; 
  346. $count = MS_Model_Invoice::get_invoice_count( $args ); 
  347.  
  348. if ( $count ) { 
  349. $status_url = esc_url_raw( 
  350. add_query_arg( 
  351. array( 'status' => $status ),  
  352. remove_query_arg( array( 'msg' ) ) 
  353. ); 
  354. } else { 
  355. $status_url = false; 
  356. $desc .= ' (0)'; 
  357. if ( MS_Model_Invoice::STATUS_DENIED != $status ) { 
  358. $desc .= ' |'; 
  359. $count = false; 
  360.  
  361. $views[ $status ] = array( 
  362. 'url' => $status_url,  
  363. 'label' => $desc,  
  364. 'count' => $count,  
  365. ); 
  366.  
  367. return apply_filters( 'ms_helper_listtable_billing_views', $views ); 
  368.  
.