/app/controller/class-ms-controller-billing.php

  1. <?php 
  2. /** 
  3. * This file defines the MS_Controller_Billing class. 
  4. * 
  5. * @copyright Incsub (http://incsub.com/) 
  6. * 
  7. * @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0) 
  8. * 
  9. * This program is free software; you can redistribute it and/or modify 
  10. * it under the terms of the GNU General Public License, version 2, as 
  11. * published by the Free Software Foundation. 
  12. * 
  13. * This program is distributed in the hope that it will be useful,  
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  16. * GNU General Public License for more details. 
  17. * 
  18. * You should have received a copy of the GNU General Public License 
  19. * along with this program; if not, write to the Free Software 
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,  
  21. * MA 02110-1301 USA 
  22. * 
  23. */ 
  24.  
  25. /** 
  26. * Controller to manage billing and invoices. 
  27. * 
  28. * @since 1.0.0 
  29. * 
  30. * @package Membership2 
  31. * @subpackage Controller 
  32. */ 
  33. class MS_Controller_Billing extends MS_Controller { 
  34.  
  35. /** 
  36. * Prepare the Billing manager. 
  37. * 
  38. * @since 1.0.0 
  39. */ 
  40. public function __construct() { 
  41. parent::__construct(); 
  42.  
  43. /** 
  44. * Initialize the admin-side functions. 
  45. * 
  46. * @since 2.0.0 
  47. */ 
  48. public function admin_init() { 
  49. $hook = MS_Controller_Plugin::admin_page_hook( 'billing' ); 
  50.  
  51. $this->run_action( 'load-' . $hook, 'admin_billing_manager' ); 
  52. $this->run_action( 'admin_print_scripts-' . $hook, 'enqueue_scripts' ); 
  53. $this->run_action( 'admin_print_styles-' . $hook, 'enqueue_styles' ); 
  54.  
  55. /** 
  56. * Show admin notices. 
  57. * 
  58. * @since 1.0.0 
  59. * 
  60. */ 
  61. public function print_admin_message() { 
  62. add_action( 'admin_notices', array( 'MS_Helper_Billing', 'print_admin_message' ) ); 
  63.  
  64. /** 
  65. * Manages billing actions. 
  66. * 
  67. * Verifies GET and POST requests to manage billing. 
  68. * 
  69. * @since 1.0.0 
  70. */ 
  71. public function admin_billing_manager() { 
  72. $this->print_admin_message(); 
  73. $msg = 0; 
  74. $redirect = false; 
  75.  
  76. if ( ! $this->is_admin_user() ) { 
  77. return; 
  78.  
  79. $fields = array( 'user_id', 'membership_id' ); 
  80.  
  81. if ( self::validate_required( $fields ) 
  82. && $this->verify_nonce() 
  83. ) { 
  84. // Save billing add/edit 
  85. $msg = $this->save_invoice( $_POST ); 
  86.  
  87. $redirect = esc_url_raw( 
  88. add_query_arg( 
  89. array( 'msg' => $msg ),  
  90. remove_query_arg( array( 'invoice_id') ) 
  91. ); 
  92. } elseif ( self::validate_required( array( 'invoice_id' ) ) 
  93. && $this->verify_nonce( 'bulk' ) 
  94. ) { 
  95. // Execute bulk actions. 
  96. $action = $_POST['action'] != -1 ? $_POST['action'] : $_POST['action2']; 
  97. $msg = $this->billing_do_action( $action, $_POST['invoice_id'] ); 
  98. $redirect = esc_url_raw( add_query_arg( array( 'msg' => $msg ) ) ); 
  99.  
  100. if ( $redirect ) { 
  101. wp_safe_redirect( $redirect ); 
  102. exit; 
  103.  
  104. /** 
  105. * Sets up the 'Billing' navigation and list page. 
  106. * 
  107. * @since 1.0.0 
  108. */ 
  109. public function admin_billing() { 
  110. $this->print_admin_message(); 
  111.  
  112. // Action view page request 
  113. $isset = array( 'action', 'invoice_id' ); 
  114. if ( self::validate_required( $isset, 'GET', false ) && 'edit' == $_GET['action'] ) { 
  115. $invoice_id = ! empty( $_GET['invoice_id'] ) ? $_GET['invoice_id'] : 0; 
  116. $data['invoice'] = MS_Factory::load( 'MS_Model_Invoice', $_GET['invoice_id'] ); 
  117. $data['action'] = $_GET['action']; 
  118. $data['users'] = MS_Model_Member::get_usernames( null, MS_Model_Member::SEARCH_ALL_USERS ); 
  119. $data['memberships'] = MS_Model_Membership::get_membership_names( null ); 
  120. $view = MS_Factory::create( 'MS_View_Billing_Edit' ); 
  121. $view->data = apply_filters( 'ms_view_billing_edit_data', $data ); 
  122. $view->render(); 
  123. } else { 
  124. $view = MS_Factory::create( 'MS_View_Billing_List' ); 
  125. $view->render(); 
  126.  
  127. /** 
  128. * Perform actions for each invoice. 
  129. * 
  130. * @since 1.0.0 
  131. * @param string $action The action to perform on selected invoices. 
  132. * @param int[] $invoice_ids The list of invoices ids to process. 
  133. */ 
  134. public function billing_do_action( $action, $invoice_ids ) { 
  135. $msg = MS_Helper_Billing::BILLING_MSG_NOT_UPDATED; 
  136.  
  137. if ( $this->is_admin_user() && is_array( $invoice_ids ) ) { 
  138. foreach ( $invoice_ids as $invoice_id ) { 
  139. switch ( $action ) { 
  140. case 'delete': 
  141. $invoice = MS_Factory::load( 'MS_Model_Invoice', $invoice_id ); 
  142. $invoice->delete(); 
  143. $msg = MS_Helper_Billing::BILLING_MSG_DELETED; 
  144. break; 
  145.  
  146. default: 
  147. do_action( 'ms_controller_billing_do_action_' . $action, $invoice_ids ); 
  148. break; 
  149.  
  150. return apply_filters( 
  151. 'ms_controller_billing_billing_do_action',  
  152. $msg,  
  153. $action,  
  154. $invoice_ids,  
  155. $this 
  156. ); 
  157.  
  158. /** 
  159. * Save invoices using the invoices model. 
  160. * 
  161. * @since 1.0.0 
  162. * 
  163. * @param mixed $fields Transaction fields 
  164. */ 
  165. private function save_invoice( $fields ) { 
  166. $msg = MS_Helper_Billing::BILLING_MSG_NOT_UPDATED; 
  167.  
  168. if ( $this->is_admin_user() 
  169. && is_array( $fields ) 
  170. && ! empty( $fields['user_id'] ) 
  171. && ! empty( $fields['membership_id'] ) 
  172. ) { 
  173. $member = MS_Factory::load( 'MS_Model_Member', $fields['user_id'] ); 
  174. $membership_id = $fields['membership_id']; 
  175. $gateway_id = 'admin'; 
  176.  
  177. $subscription = MS_Model_Relationship::get_subscription( $member->id, $membership_id ); 
  178. if ( empty( $subscription ) ) { 
  179. $subscription = MS_Model_Relationship::create_ms_relationship( 
  180. $membership_id,  
  181. $member->id,  
  182. $gateway_id 
  183. ); 
  184. } else { 
  185. $subscription->gateway_id = $gateway_id; 
  186. $subscription->save(); 
  187.  
  188. $invoice = MS_Factory::load( 'MS_Model_Invoice', $fields['invoice_id'] ); 
  189. if ( ! $invoice->is_valid() ) { 
  190. $invoice = $subscription->get_current_invoice(); 
  191. $msg = MS_Helper_Billing::BILLING_MSG_ADDED; 
  192. } else { 
  193. $msg = MS_Helper_Billing::BILLING_MSG_UPDATED; 
  194.  
  195. foreach ( $fields as $field => $value ) { 
  196. $invoice->$field = $value; 
  197.  
  198. $invoice->save(); 
  199.  
  200. if ( ! empty( $fields['execute'] ) ) { 
  201. $invoice->changed(); 
  202.  
  203. return apply_filters( 
  204. 'ms_controller_billing_save_invoice',  
  205. $msg,  
  206. $fields,  
  207. $this 
  208. ); 
  209.  
  210. /** 
  211. * Load Billing specific styles. 
  212. * 
  213. * @since 1.0.0 
  214. */ 
  215. public function enqueue_styles() { 
  216. if ( empty( $_GET['action'] ) ) { 
  217. $action = ''; 
  218. } else { 
  219. $action = $_GET['action']; 
  220.  
  221. if ( 'edit' == $action ) { 
  222. lib2()->ui->add( 'jquery-ui' ); 
  223.  
  224. /** 
  225. * Load Billing specific scripts. 
  226. * 
  227. * @since 1.0.0 
  228. */ 
  229. public function enqueue_scripts() { 
  230. if ( isset( $_GET['action'] ) && 'edit' == $_GET['action'] ) { 
  231. wp_enqueue_script( 'jquery-ui-datepicker' ); 
  232. wp_enqueue_script( 'jquery-validate' ); 
  233.  
  234. $data = array( 
  235. 'ms_init' => array( 'view_billing_edit' ),  
  236. ); 
  237.  
  238. lib2()->ui->data( 'ms_data', $data ); 
  239. wp_enqueue_script( 'ms-admin' ); 
  240.  
.